KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > query > CompositeFilter


1 package spoon.support.query;
2
3 import spoon.reflect.declaration.CtElement;
4 import spoon.reflect.visitor.Filter;
5
6 /**
7  * This class defines a composite filter, which can compose several filters
8  * together by using {@link spoon.support.query.FilteringOperator}.
9  *
10  * @author Renaud Pawlak
11  */

12 public class CompositeFilter<T extends CtElement> implements Filter<T> {
13
14     /**
15      * Defines the matching using {@link spoon.support.query.FilteringOperator}.
16      */

17     public boolean matches(T element) {
18         switch (operator) {
19             case INTERSECTION:
20                 for (Filter<T> f : filters) {
21                     if (!f.matches(element))
22                         return false;
23                 }
24                 return true;
25             case UNION:
26                 for (Filter<T> f : filters) {
27                     if (f.matches(element))
28                         return true;
29                 }
30                 return false;
31             case SUBSTRACTION:
32                 if (filters.length == 0)
33                     return false;
34                 if (!filters[0].matches(element))
35                     return false;
36                 for (int i = 1; i < filters.length; i++) {
37                     if (filters[i].matches(element))
38                         return false;
39                 }
40                 return true;
41             default:
42                 return false;
43         }
44     }
45
46     Filter<T>[] filters;
47
48     FilteringOperator operator;
49
50     /**
51      * Creates a new composite filter.
52      *
53      * @param operator the operator used to compose the filters together
54      * @param filters the filters to be composed
55      */

56     public CompositeFilter(FilteringOperator operator, Filter<T>... filters) {
57         this.filters = filters;
58         this.operator = operator;
59     }
60
61     @SuppressWarnings JavaDoc("unchecked")
62     public Class JavaDoc<T> getType() {
63         return (Class JavaDoc<T>) CtElement.class;
64     }
65
66 }
67
Popular Tags