View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
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 }