KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > lang > model > util > ElementFilter


1 /*
2  * @(#)ElementFilter.java 1.6 06/08/02
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.lang.model.util;
9
10 import java.lang.Iterable JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.EnumSet JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.LinkedHashSet JavaDoc;
18 import java.util.NoSuchElementException JavaDoc;
19
20 import javax.lang.model.element.*;
21 import javax.lang.model.type.*;
22
23
24 /**
25  * Filters for selecting just the elements of interest from a
26  * collection of elements. The returned sets and lists are new
27  * collections and do use the argument as a backing store. The
28  * methods in this class do not make any attempts to guard against
29  * concurrent modifications of the arguments. The returned sets and
30  * lists are mutable but unsafe for concurrent access. A returned set
31  * has the same iteration order as the argument set to a method.
32  *
33  * <p>If iterables and sets containing {@code null} are passed as
34  * arguments to methods in this class, a {@code NullPointerException}
35  * will be thrown.
36  *
37  * <p>Note that a <i>static import</i> statement can make the text of
38  * calls to the methods in this class more concise; for example:
39  *
40  * <blockquote><pre>
41  * import static javax.lang.model.util.ElementFilter.*;
42  * ...
43  * {@code List<VariableElement>} fs = fieldsIn(someClass.getEnclosedElements());
44  * </pre></blockquote>
45  *
46  * @author Joseph D. Darcy
47  * @author Scott Seligman
48  * @author Peter von der Ah&eacute;
49  * @author Martin Buchholz
50  * @version 1.6 06/08/02
51  * @since 1.6
52  */

53 public class ElementFilter {
54     private ElementFilter() {} // Do not instantiate.
55

56     private static Set JavaDoc<ElementKind> CONSTRUCTOR_KIND =
57     Collections.unmodifiableSet(EnumSet.of(ElementKind.CONSTRUCTOR));
58
59     private static Set JavaDoc<ElementKind> FIELD_KINDS =
60     Collections.unmodifiableSet(EnumSet.of(ElementKind.FIELD,
61                            ElementKind.ENUM_CONSTANT));
62     private static Set JavaDoc<ElementKind> METHOD_KIND =
63     Collections.unmodifiableSet(EnumSet.of(ElementKind.METHOD));
64
65     private static Set JavaDoc<ElementKind> PACKAGE_KIND =
66     Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE));
67
68     private static Set JavaDoc<ElementKind> TYPE_KINDS =
69     Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS,
70                            ElementKind.ENUM,
71                            ElementKind.INTERFACE,
72                            ElementKind.ANNOTATION_TYPE));
73     /**
74      * Returns a list of fields in {@code elements}.
75      * @return a list of fields in {@code elements}
76      * @param elements the elements to filter
77      */

78     public static List JavaDoc<VariableElement>
79         fieldsIn(Iterable JavaDoc<? extends Element> elements) {
80     return listFilter(elements, FIELD_KINDS, VariableElement.class);
81     }
82
83     /**
84      * Returns a set of fields in {@code elements}.
85      * @return a set of fields in {@code elements}
86      * @param elements the elements to filter
87      */

88     public static Set JavaDoc<VariableElement>
89         fieldsIn(Set JavaDoc<? extends Element> elements) {
90     return setFilter(elements, FIELD_KINDS, VariableElement.class);
91     }
92
93     /**
94      * Returns a list of constructors in {@code elements}.
95      * @return a list of constructors in {@code elements}
96      * @param elements the elements to filter
97      */

98     public static List JavaDoc<ExecutableElement>
99         constructorsIn(Iterable JavaDoc<? extends Element> elements) {
100     return listFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class);
101     }
102
103     /**
104      * Returns a set of constructors in {@code elements}.
105      * @return a set of constructors in {@code elements}
106      * @param elements the elements to filter
107      */

108     public static Set JavaDoc<ExecutableElement>
109         constructorsIn(Set JavaDoc<? extends Element> elements) {
110     return setFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class);
111     }
112
113     /**
114      * Returns a list of methods in {@code elements}.
115      * @return a list of methods in {@code elements}
116      * @param elements the elements to filter
117      */

118     public static List JavaDoc<ExecutableElement>
119         methodsIn(Iterable JavaDoc<? extends Element> elements) {
120     return listFilter(elements, METHOD_KIND, ExecutableElement.class);
121     }
122
123     /**
124      * Returns a set of methods in {@code elements}.
125      * @return a set of methods in {@code elements}
126      * @param elements the elements to filter
127      */

128     public static Set JavaDoc<ExecutableElement>
129         methodsIn(Set JavaDoc<? extends Element> elements) {
130     return setFilter(elements, METHOD_KIND, ExecutableElement.class);
131     }
132
133     /**
134      * Returns a list of types in {@code elements}.
135      * @return a list of types in {@code elements}
136      * @param elements the elements to filter
137      */

138     public static List JavaDoc<TypeElement>
139         typesIn(Iterable JavaDoc<? extends Element> elements) {
140     return listFilter(elements, TYPE_KINDS, TypeElement.class);
141     }
142
143     /**
144      * Returns a set of types in {@code elements}.
145      * @return a set of types in {@code elements}
146      * @param elements the elements to filter
147      */

148     public static Set JavaDoc<TypeElement>
149         typesIn(Set JavaDoc<? extends Element> elements) {
150     return setFilter(elements, TYPE_KINDS, TypeElement.class);
151     }
152
153     /**
154      * Returns a list of packages in {@code elements}.
155      * @return a list of packages in {@code elements}
156      * @param elements the elements to filter
157      */

158     public static List JavaDoc<PackageElement>
159         packagesIn(Iterable JavaDoc<? extends Element> elements) {
160     return listFilter(elements, PACKAGE_KIND, PackageElement.class);
161     }
162
163     /**
164      * Returns a set of packages in {@code elements}.
165      * @return a set of packages in {@code elements}
166      * @param elements the elements to filter
167      */

168     public static Set JavaDoc<PackageElement>
169         packagesIn(Set JavaDoc<? extends Element> elements) {
170     return setFilter(elements, PACKAGE_KIND, PackageElement.class);
171     }
172
173     // Assumes targetKinds and E are sensible.
174
private static <E extends Element> List JavaDoc<E> listFilter(Iterable JavaDoc<? extends Element> elements,
175                               Set JavaDoc<ElementKind> targetKinds,
176                               Class JavaDoc<E> clazz) {
177     List JavaDoc<E> list = new ArrayList JavaDoc<E>();
178     for (Element e : elements) {
179         if (targetKinds.contains(e.getKind()))
180         list.add(clazz.cast(e));
181     }
182     return list;
183     }
184
185     // Assumes targetKinds and E are sensible.
186
private static <E extends Element> Set JavaDoc<E> setFilter(Set JavaDoc<? extends Element> elements,
187                             Set JavaDoc<ElementKind> targetKinds,
188                             Class JavaDoc<E> clazz) {
189     // Return set preserving iteration order of input set.
190
Set JavaDoc<E> set = new LinkedHashSet JavaDoc<E>();
191     for (Element e : elements) {
192         if (targetKinds.contains(e.getKind()))
193         set.add(clazz.cast(e));
194     }
195     return set;
196     }
197 }
198
Popular Tags