by-class generated instanceof checker
[fedora-idea.git] / platform-api / src / com / intellij / patterns / ElementPatternCondition.java
blobb9c73a5bb104466afab20bcf16cc703e22d0d50a
1 /*
2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
4 */
5 package com.intellij.patterns;
7 import com.intellij.util.SmartList;
8 import com.intellij.util.ProcessingContext;
9 import org.jetbrains.annotations.Nullable;
11 import java.util.List;
13 /**
14 * @author peter
16 public class ElementPatternCondition<T> {
17 private final InitialPatternCondition<T> myInitialCondition;
18 private final List<PatternCondition<? super T>> myConditions = new SmartList<PatternCondition<? super T>>();
20 public ElementPatternCondition(final InitialPatternCondition<T> startCondition) {
21 myInitialCondition = startCondition;
24 public boolean accepts(@Nullable Object o, final ProcessingContext context) {
25 if (!myInitialCondition.accepts(o, context)) return false;
26 for (final PatternCondition<? super T> condition : myConditions) {
27 if (!condition.accepts((T)o, context)) return false;
29 return true;
32 public final String toString() {
33 StringBuilder builder = new StringBuilder();
34 append(builder, "");
35 return builder.toString();
38 public void append(StringBuilder builder, String indent) {
39 myInitialCondition.append(builder, indent);
40 final int conditionSize = myConditions.size();
42 for (int i = 0; i < conditionSize; ++i) { // for each is slower
43 final PatternCondition<? super T> condition = myConditions.get(i);
44 condition.append(builder.append(".\n").append(indent), indent);
48 public List<PatternCondition<? super T>> getConditions() {
49 return myConditions;
52 public InitialPatternCondition<T> getInitialCondition() {
53 return myInitialCondition;
56 public ElementPatternCondition<T> append(PatternCondition<? super T> condition) {
57 final ElementPatternCondition<T> copy = new ElementPatternCondition<T>(myInitialCondition);
58 copy.myConditions.addAll(myConditions);
59 copy.myConditions.add(condition);
60 return copy;