KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > util > TypeMap


1 package net.sourceforge.pmd.util;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 /**
7  * A specialized map that stores classes by both their full and short names.
8  *
9  * @author Brian Remedios
10  */

11 public class TypeMap {
12
13     private Map JavaDoc typesByName;
14     
15     /**
16      * Constructor for TypeMap.
17      * @param initialSize int
18      */

19     public TypeMap(int initialSize) {
20         typesByName = new HashMap JavaDoc(initialSize);
21     }
22
23     /**
24      * Constructor for TypeMap that takes in an initial set of types.
25      *
26      * @param types Class[]
27      */

28     public TypeMap(Class JavaDoc[] types) {
29         this(types.length);
30         add(types);
31     }
32     
33     /**
34      * Adds a type to the receiver and stores it keyed by both its full
35      * and short names.
36      *
37      * @param type Class
38      */

39     public void add(Class JavaDoc type) {
40         typesByName.put(type.getName(), type);
41         typesByName.put(ClassUtil.withoutPackageName(type.getName()), type);
42     }
43     
44     /**
45      * Returns whether the type is known to the receiver.
46      *
47      * @param type Class
48      * @return boolean
49      */

50     public boolean contains(Class JavaDoc type) {
51         return typesByName.containsValue(type);
52     }
53     
54     /**
55      * Returns whether the typeName is known to the receiver.
56      *
57      * @param typeName String
58      * @return boolean
59      */

60     public boolean contains(String JavaDoc typeName) {
61         return typesByName.containsKey(typeName);
62     }
63     
64     /**
65      * Returns the type for the typeName specified.
66      *
67      * @param typeName String
68      * @return Class
69      */

70     public Class JavaDoc typeFor(String JavaDoc typeName) {
71         return (Class JavaDoc)typesByName.get(typeName);
72     }
73     
74     /**
75      * Adds an array of types to the receiver at once.
76      *
77      * @param types Class[]
78      */

79     public void add(Class JavaDoc[] types) {
80         for (int i=0; i<types.length; i++) {
81             add(types[i]);
82         }
83     }
84 }
85
Popular Tags