2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
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
;
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;
32 public final String
toString() {
33 StringBuilder builder
= new StringBuilder();
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() {
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
);