1 | package edu.stanford.rt.credential; |
---|
2 | |
---|
3 | import java.util.*; |
---|
4 | |
---|
5 | /** |
---|
6 | * @author Ninghui Li, Sandra Qiu<br> |
---|
7 | * |
---|
8 | * OrderedMap maintains a map from key to values and the ordering |
---|
9 | * of the keys. |
---|
10 | * |
---|
11 | */ |
---|
12 | public class OrderedMap |
---|
13 | { |
---|
14 | /** key type */ |
---|
15 | private Class keyClass; |
---|
16 | /** value type */ |
---|
17 | private Class valueClass; |
---|
18 | /** map for the key-value pairs */ |
---|
19 | private HashMap dataMap; |
---|
20 | /** also stores the keys as they are added to the list, |
---|
21 | * so that we can remember the ordering of the keys. |
---|
22 | */ |
---|
23 | private ArrayList keyList; |
---|
24 | /** whether this map is editable */ |
---|
25 | private boolean editable; |
---|
26 | /** defualt map capacity */ |
---|
27 | private static final int DEFAULT_SIZE = 10; |
---|
28 | |
---|
29 | /** |
---|
30 | * Constructor for OrderedMap. |
---|
31 | * |
---|
32 | * Constructs a OrderedMap object with size default to 10. |
---|
33 | * Both key and value are of Object type. |
---|
34 | */ |
---|
35 | public OrderedMap() |
---|
36 | { |
---|
37 | this(DEFAULT_SIZE, Object.class, Object.class); |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Constructor for OrderedMap. |
---|
42 | * Constructs a OrderedMap object with given size. |
---|
43 | * Both key and value are of Object type. |
---|
44 | */ |
---|
45 | public OrderedMap(int size) |
---|
46 | { |
---|
47 | this(size, Object.class, Object.class); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Constructor for OrderedMap. |
---|
52 | * Constructs a OrderedMap object with size default to 10. |
---|
53 | * The key and value type are specified by the given keyClass and |
---|
54 | * valueClass, respectively. |
---|
55 | */ |
---|
56 | public OrderedMap(Class keyClass, Class valueClass) |
---|
57 | { |
---|
58 | this(DEFAULT_SIZE, keyClass, valueClass); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Constructor for OrderedMap. |
---|
63 | * Constructs a OrderedMap object with given size. |
---|
64 | * The key and value type are specified by the given keyClass and |
---|
65 | * valueClass, respectively. |
---|
66 | */ |
---|
67 | public OrderedMap(int size, Class keyClass, Class valueClass) |
---|
68 | { |
---|
69 | this.keyClass = keyClass; |
---|
70 | this.valueClass = valueClass; |
---|
71 | this.dataMap = new HashMap(size); |
---|
72 | this.keyList = new ArrayList(size); |
---|
73 | this.editable = true; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Method constainsKey. |
---|
78 | * Returns true if there is a mapping for the given key. |
---|
79 | * @param key |
---|
80 | * @return boolean |
---|
81 | */ |
---|
82 | public synchronized boolean constainsKey(Object key) |
---|
83 | { |
---|
84 | return dataMap.containsKey(key); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Method put. |
---|
89 | * Add a new key-value mapping entry to the map. |
---|
90 | * Exception is thrown if the map is not editable, the key is null, |
---|
91 | * or the given key or value's type does not match the specified |
---|
92 | * key type or value type. Exception will also be thrown if trying |
---|
93 | * to add duplicated key-value pair. |
---|
94 | * @param key |
---|
95 | * @param value |
---|
96 | * @throws DomainSpecException |
---|
97 | */ |
---|
98 | public synchronized void put(Object key, Object value) |
---|
99 | throws DomainSpecException |
---|
100 | { |
---|
101 | if (!editable) |
---|
102 | throw new DomainSpecException("Adding to unmodifiable map"); |
---|
103 | |
---|
104 | if (key == null) |
---|
105 | throw new DomainSpecException("Wrong key value: null."); |
---|
106 | |
---|
107 | if (!keyClass.isInstance(key)) |
---|
108 | throw new DomainSpecException("Wrong key type"); |
---|
109 | |
---|
110 | if (value != null && !valueClass.isInstance(value)) |
---|
111 | throw new DomainSpecException("Wrong value type"); |
---|
112 | |
---|
113 | myPut(key, value); |
---|
114 | } |
---|
115 | |
---|
116 | private void myPut(Object key, Object value) |
---|
117 | throws DomainSpecException |
---|
118 | { |
---|
119 | //RTUtil.debugInfo("myPut() > key = " + key.toString()); |
---|
120 | |
---|
121 | if (dataMap.get(key) != null) |
---|
122 | { |
---|
123 | //RTUtil.debugInfo("myPut() > " + dataMap.get(key).toString()); |
---|
124 | throw new DomainSpecException("Duplicate mapping"); |
---|
125 | } |
---|
126 | dataMap.put(key, value); |
---|
127 | keyList.add(key); |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | Copies all the mapping entries to this map, perserving the |
---|
132 | original ordering of the entries. |
---|
133 | |
---|
134 | Exception is thrown when the key type or value type of the given |
---|
135 | map does not match that of this map, or when duplicated entries |
---|
136 | are detected. |
---|
137 | */ |
---|
138 | public synchronized void putAll(OrderedMap map) |
---|
139 | throws DomainSpecException |
---|
140 | { |
---|
141 | if (!this.keyClass.equals(map.getKeyClass()) |
---|
142 | || !this.valueClass.equals(map.getValueClass())) |
---|
143 | { |
---|
144 | throw new DomainSpecException("Illegal key/value type."); |
---|
145 | } |
---|
146 | Iterator it = map.keyIterator(); |
---|
147 | while (it.hasNext()) |
---|
148 | { |
---|
149 | Object key = it.next(); |
---|
150 | Object value = map.get(key); |
---|
151 | myPut(key, value); |
---|
152 | } |
---|
153 | // Do not call setUneditable mothod here! |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | marks this map as uneditable. |
---|
158 | */ |
---|
159 | public synchronized void setUneditable() |
---|
160 | { |
---|
161 | editable = false; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | Returns true if the map is editable. |
---|
166 | */ |
---|
167 | public boolean isEditable() |
---|
168 | { |
---|
169 | return editable; |
---|
170 | } |
---|
171 | |
---|
172 | /** Returns the size of this map */ |
---|
173 | public synchronized int size() |
---|
174 | { |
---|
175 | return keyList.size(); |
---|
176 | } |
---|
177 | |
---|
178 | /** Returns iterator to the list of keys in the map.*/ |
---|
179 | public synchronized Iterator keyIterator() |
---|
180 | { |
---|
181 | return keyList.iterator(); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | Returns the value for the given key and returns null if there is |
---|
186 | no mapping for the given key . |
---|
187 | |
---|
188 | Exception is thrown when the type of given key does not match the |
---|
189 | specified key type of this map. |
---|
190 | */ |
---|
191 | public Object get(Object key) throws DomainSpecException |
---|
192 | { |
---|
193 | if (!keyClass.isInstance(key)) |
---|
194 | throw new DomainSpecException("Wrong key type"); |
---|
195 | |
---|
196 | return dataMap.get(key); |
---|
197 | } |
---|
198 | |
---|
199 | /** Returns an unmodifiable view of all the keys. */ |
---|
200 | public synchronized List keyList() |
---|
201 | { |
---|
202 | // returns a unmodifiable version of the keys |
---|
203 | return Collections.unmodifiableList(keyList); |
---|
204 | } |
---|
205 | |
---|
206 | /** Returns an unmodifiable view of this map. */ |
---|
207 | public synchronized Map valueMap() |
---|
208 | { |
---|
209 | return Collections.unmodifiableMap(dataMap); |
---|
210 | } |
---|
211 | |
---|
212 | /** Returns the key type. */ |
---|
213 | public Class getKeyClass() |
---|
214 | { |
---|
215 | return keyClass; |
---|
216 | } |
---|
217 | |
---|
218 | /** Returns the value type */ |
---|
219 | public Class getValueClass() |
---|
220 | { |
---|
221 | return valueClass; |
---|
222 | } |
---|
223 | |
---|
224 | } |
---|