View Javadoc

1   /***
2    * ClassMap.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2003 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.util;
27  
28  import java.util.Arrays;
29  import java.util.Hashtable;
30  import java.util.Vector;
31  
32  /***
33   * This class enables to associate a Class with an Object.
34   * 
35   * It is particularly useful when managing configurations as, when looking up an
36   * eventual association, the class's interfaces and superclass are recursively
37   * considered to provide a result.
38   * 
39   * The look up occurs in the following order: parent class then interfaces, then
40   * parent class's parent class and parent class interfaces and so on.
41   * 
42   * @author $Author: dbregeon $
43   * @version $Revision: 1.1 $
44   */
45  public class ClassMap {
46  	/***
47  	 * Keys are classes, Values are Objects instances.
48  	 */
49  	private final Hashtable values = new Hashtable();
50  
51  	/***
52  	 * 
53  	 * @return
54  	 */
55  	public Class[] getKeys() {
56  		return (Class[]) this.values.keySet().toArray(new Class[0]);
57  	}
58  
59  	/***
60  	 * 
61  	 * @param clazz
62  	 * @return
63  	 */
64  	public Object get(Class clazz) {
65  		Object result = null;
66  		if (clazz != null) {
67  			Vector classes = new Vector();
68  			Class current = null;
69  			classes.add(clazz);
70  			while ((result == null) && (classes.size() > 0)) {
71  				current = (Class) classes.remove(0);
72  				result = this.values.get(current);
73  				if (result == null) {
74  					if (current.getSuperclass() != null) {
75  						classes.add(current.getSuperclass());
76  					}
77  					if ((current.getInterfaces() != null)
78  							&& (current.getInterfaces().length > 0)) {
79  						classes.addAll(Arrays.asList(current.getInterfaces()));
80  					}
81  				}
82  			}
83  		}
84  		return result;
85  	}
86  	
87  	/***
88  	 * 
89  	 * @param clazz
90  	 * @param o
91  	 */
92      public void put(Class clazz, Object o) {
93          if ((clazz != null) && (o != null)) {
94                  this.values.put(clazz, o);
95          }
96      }
97      
98      /***
99       * 
100      * @param clazz
101      * @return
102      */
103     public Object remove(Class clazz) {
104     		Object result = null;
105     		if (clazz != null) {
106     			result = this.values.remove(clazz);
107     		}
108     		return result;
109     }
110 
111 }