01: package net.sourceforge.pmd.util;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: /**
07: * A specialized map that stores classes by both their full and short names.
08: *
09: * @author Brian Remedios
10: */
11: public class TypeMap {
12:
13: private Map<String, Class> typesByName;
14:
15: /**
16: * Constructor for TypeMap.
17: * @param initialSize int
18: */
19: public TypeMap(int initialSize) {
20: typesByName = new HashMap<String, Class>(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... 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 type) {
40: typesByName.put(type.getName(), type);
41: typesByName.put(ClassUtil.withoutPackageName(type.getName()),
42: type);
43: }
44:
45: /**
46: * Returns whether the type is known to the receiver.
47: *
48: * @param type Class
49: * @return boolean
50: */
51: public boolean contains(Class type) {
52: return typesByName.containsValue(type);
53: }
54:
55: /**
56: * Returns whether the typeName is known to the receiver.
57: *
58: * @param typeName String
59: * @return boolean
60: */
61: public boolean contains(String typeName) {
62: return typesByName.containsKey(typeName);
63: }
64:
65: /**
66: * Returns the type for the typeName specified.
67: *
68: * @param typeName String
69: * @return Class
70: */
71: public Class typeFor(String typeName) {
72: return typesByName.get(typeName);
73: }
74:
75: /**
76: * Adds an array of types to the receiver at once.
77: *
78: * @param types Class[]
79: */
80: public void add(Class... types) {
81: for (int i = 0; i < types.length; i++) {
82: add(types[i]);
83: }
84: }
85: }
|