KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > attributes > AttributeUtil


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.attributes;
17
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.WeakHashMap JavaDoc;
30
31 /**
32  * Commonly used convenience functions.
33  */

34 public class AttributeUtil {
35     
36     /**
37      * Filters a Collection of <code>Class</code> objects. The returned collection
38      * only contains those classes that have an attribute of the specified type.
39      * That is, for each Class clazz in the
40      * returned collection,
41      * <code>clazz != null && Attributes.hasAttributeType (clazz, attributeClass)</code>
42      * is true.
43      *
44      * @param classes Collection of Classes to filter. May contain <code>null</code> references,
45      * but may not itself be <code>null</code>.
46      * @param attributeClass the class to filter based on.
47      */

48     public static Collection JavaDoc getClassesWithAttributeType (Collection JavaDoc classes, Class JavaDoc attributeClass) {
49         if (classes == null) {
50             throw new NullPointerException JavaDoc ("classes");
51         }
52         
53         if (attributeClass == null) {
54             throw new NullPointerException JavaDoc ("attributeClass");
55         }
56         
57         ArrayList JavaDoc result = new ArrayList JavaDoc ();
58         Iterator JavaDoc iter = classes.iterator ();
59         while (iter.hasNext ()) {
60             Class JavaDoc clazz = (Class JavaDoc) iter.next ();
61             if (clazz != null) {
62                 if (Attributes.hasAttributeType (clazz, attributeClass)) {
63                     result.add (clazz);
64                 }
65             }
66         }
67         
68         return result;
69     }
70     
71     /**
72      * Filters a collection of objects. The returned collection
73      * only contains those objects whose class have an attribute
74      * of the specified type. That is, for each Object o in the
75      * returned collection,
76      * <code>o != null && Attributes.hasAttributeType (o.getClass (), attributeClass)</code>
77      * is true.
78      *
79      * @param objects Collection of objects to filter. May contain <code>null</code> references,
80      * but may not itself be <code>null</code>.
81      * @param attributeClass the class to filter based on.
82      */

83     public static Collection JavaDoc getObjectsWithAttributeType (Collection JavaDoc objects, Class JavaDoc attributeClass) {
84         if (objects == null) {
85             throw new NullPointerException JavaDoc ("objects");
86         }
87         
88         if (attributeClass == null) {
89             throw new NullPointerException JavaDoc ("attributeClass");
90         }
91         
92         ArrayList JavaDoc result = new ArrayList JavaDoc ();
93         Iterator JavaDoc iter = objects.iterator ();
94         while (iter.hasNext ()) {
95             Object JavaDoc object = iter.next ();
96             if (object != null) {
97                 Class JavaDoc clazz = object.getClass ();
98                 if (Attributes.hasAttributeType (clazz, attributeClass)) {
99                     result.add (object);
100                 }
101             }
102         }
103         
104         return result;
105     }
106 }
Popular Tags