1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.onemind.commons.java.datastructure;
22
23 import java.util.*;
24 /***
25 * Represents a set of classes. User can use isSubSetOf() to detect whether a given class is subclass of a class in the class set
26 * @author TiongHiang Lee (thlee@onemindsoft.org)
27 * @version $Id: ClassSet.java,v 1.3 2004/08/26 12:33:16 thlee Exp $ $Name: $
28 */
29 public class ClassSet
30 {
31
32 /*** the classes * */
33 private HashSet _classes = new HashSet();
34
35 /***
36 * {@inheritDoc}
37 */
38 public ClassSet()
39 {
40 super();
41 }
42
43 /***
44 * {@inheritDoc}
45 */
46 public ClassSet(Collection c)
47 {
48 addAll(c);
49 }
50
51 /***
52 * Add all in the classes to the ClassSet
53 * @param classes the collection containing the classes
54 */
55 public void addAll(Collection classes)
56 {
57 Iterator it = classes.iterator();
58 while (it.hasNext())
59 {
60 Object o = it.next();
61 if (o instanceof Class)
62 {
63 add((Class) o);
64 } else
65 {
66 throw new IllegalArgumentException(o + " is not a subclass of class");
67 }
68 }
69 }
70
71 /***
72 * Add the class
73 * @param c the class
74 */
75 public void add(Class c)
76 {
77 _classes.add(c);
78 }
79
80 /***
81 * Check whether the class is subclass of one of the class in the class set
82 * @param c the class
83 * @return true if is subclass
84 */
85 public boolean isSubclassOfClasses(Class c)
86 {
87 Class current = c;
88 while ((current != null) && (current != Object.class))
89 {
90 if (_classes.contains(c))
91 {
92 return true;
93 } else
94 {
95 current = current.getSuperclass();
96 }
97 }
98 Class[] interfaces = c.getInterfaces();
99 for (int i = 0; i < interfaces.length; i++)
100 {
101 if (_classes.contains(interfaces[i]))
102 {
103 return true;
104 }
105 }
106 return false;
107 }
108
109 /***
110 * Get the classes
111 * @return the classes
112 */
113 public Set getClasses()
114 {
115 return _classes;
116 }
117 }