KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > etc > TypeFilteredPropertyPredicate


1 package jfun.yan.etc;
2
3 import java.beans.IntrospectionException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import jfun.util.beans.BeanType;
11
12 /**
13  * A PropertyPredicate implementation that filters
14  * property by property name and the declaring class
15  * of the property setter method.
16  * <p>
17  * This class is not thread safe.
18  * </p>
19  * @author Ben Yu
20  * Jan 18, 2006 9:52:15 PM
21  */

22 public class TypeFilteredPropertyPredicate implements PropertyPredicate {
23   private final HashMap JavaDoc filter_types = new HashMap JavaDoc();
24   public boolean isProperty(Class JavaDoc type, Object JavaDoc key, Class JavaDoc property_type) {
25     final Object JavaDoc t = filter_types.get(key);
26     if(t==null) return false;
27     if(t instanceof Class JavaDoc){
28       return ((Class JavaDoc)t).isAssignableFrom(type);
29     }
30     else{
31       final ArrayList JavaDoc types = (ArrayList JavaDoc)t;
32       final int sz = types.size();
33       for(int i=0; i<sz; i++){
34         final Class JavaDoc filter_type = (Class JavaDoc)types.get(i);
35         if(filter_type.isAssignableFrom(type)){
36           return true;
37         }
38       }
39       return false;
40     }
41   }
42   /**
43    * Add property setters of an entire class to the filter list.
44    * @param type the type.
45    * @return this object.
46    */

47   public TypeFilteredPropertyPredicate addType(Class JavaDoc type){
48     try{
49       final BeanType btype = BeanType.instance(type);
50       final Set JavaDoc names = btype.getPropertyNames();
51       for(Iterator JavaDoc it=names.iterator();it.hasNext();){
52         final String JavaDoc key = (String JavaDoc)it.next();
53         final Method JavaDoc writer = btype.getWriter(key);
54         if(writer!=null){
55           addProperty(writer.getDeclaringClass(), key);
56         }
57       }
58     }
59     catch(IntrospectionException JavaDoc e){}
60     return this;
61   }
62   /**
63    * Add a single property of a type to the filter list.
64    * @param type the declaring class.
65    * @param key the property key.
66    * @return this object.
67    */

68   public TypeFilteredPropertyPredicate addProperty(Class JavaDoc type, String JavaDoc key){
69     final Object JavaDoc slot = filter_types.get(key);
70     if(slot==null){
71       filter_types.put(key, type);
72     }
73     else{
74       final ArrayList JavaDoc list = toArrayList(slot);
75       list.add(type);
76       filter_types.put(key, list);
77     }
78     return this;
79   }
80   private static ArrayList JavaDoc toArrayList(Object JavaDoc slot){
81     if(slot instanceof Class JavaDoc){
82       final ArrayList JavaDoc list = new ArrayList JavaDoc(4);
83       list.add(slot);
84       return list;
85     }
86     else{
87       return (ArrayList JavaDoc)slot;
88     }
89   }
90 }
91
Popular Tags