1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.foundation;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* A pure java implementation of NSDictionary that implements Map for greater
* java interoperability.
*
* @author michael@mpowers.net
* @author cgruber@israfil.net
* @author $Author: cgruber $
* @version $Revision: 893 $
*/
public class NSDictionary<K, V> extends HashMap<K, V> implements NSKeyValueCoding {
private static final long serialVersionUID = -5043903788052328633L;
public static final NSDictionary<?, ?> EmptyDictionary = new NSDictionary<>();
/**
* Default constructor produces an empty dictionary.
*/
public NSDictionary() {
super();
}
/**
* Constructor produces an empty dictionary with an initial capacity.
*/
public NSDictionary(int initialCapacity) {
super(initialCapacity);
}
/**
* Produces a dictionary that contains one key referencing one value.
*/
public NSDictionary(K key, V value) {
super();
put(key, value);
}
/**
* Produces a dictionary containing the specified keys and values. An
* IllegalArgumentException is thrown if the arrays are not of the same length.
*/
public NSDictionary(V[] objects, K[] keys) {
super();
if (keys.length != objects.length) {
throw new IllegalArgumentException("Array lengths do not match.");
}
for (int i = 0; i < keys.length; i++) {
put(keys[i], objects[i]);
}
}
/**
* Produces a dictionary that is a copy of the specified map (or dictionary).
*/
public NSDictionary(Map<K, V> aMap) {
super(aMap);
}
/**
* Returns a count of the key-value pairs in this dictionary.
*/
public int count() {
return size();
}
/**
* Returns an NSArray containing all keys in this dictionary.
*/
public NSArray<K> allKeys() {
return new NSArray<>(keySet());
}
/**
* Returns an NSArray containing all keys that reference the specified value.
*/
public NSArray<K> allKeysForObject(V value) {
NSMutableArray<K> result = new NSMutableArray<>();
Map.Entry<K, V> entry;
Iterator<Map.Entry<K, V>> it = entrySet().iterator();
while (it.hasNext()) {
entry = (Map.Entry<K, V>) it.next();
// handle null values
if ((value == null) && (entry.getValue() == null) || (value.equals(entry.getValue()))) {
// if match, add to result set
result.addObject(entry.getKey());
}
}
return result;
}
/**
* Returns an NSArray containing all values in this dictionary.
*/
public NSArray<V> allValues() {
return new NSArray<>(values());
}
/**
* Returns whether the specified dictionary has the same or equivalent key-value
* pairs as this dictionary.
*/
public boolean isEqualToDictionary(NSDictionary<K, V> aDictionary) {
return equals(aDictionary);
}
/**
* Returns an array of objects for the specified array of keys. If a key isn't
* found, the marker parameter will be placed in the corresponding index(es) in
* the returned array.
*/
public NSArray<V> objectsForKeys(NSArray<K> anArray, V aMarker) {
NSMutableArray<V> result = new NSMutableArray<>();
if (anArray == null)
return result;
V value;
Enumeration<K> enumeration = anArray.objectEnumerator();
while (enumeration.hasMoreElements()) {
value = objectForKey(enumeration.nextElement());
if (value == null) {
value = aMarker;
}
result.addObject(value);
}
return result;
}
/**
* Returns an enumeration over the keys in this dictionary.
*/
public java.util.Enumeration<K> keyEnumerator() {
return new java.util.Enumeration<>() {
Iterator<K> it = NSDictionary.this.keySet().iterator();
public boolean hasMoreElements() {
return it.hasNext();
}
public K nextElement() {
return it.next();
}
};
}
/**
* Returns an enumeration over the values in this dictionary.
*/
public java.util.Enumeration<V> objectEnumerator() {
return new java.util.Enumeration<>() {
Iterator<V> it = NSDictionary.this.values().iterator();
public boolean hasMoreElements() {
return it.hasNext();
}
public V nextElement() {
return it.next();
}
};
}
/**
* Returns the value for the specified key, or null if the key is not found.
*/
public V objectForKey(K aKey) {
return get(aKey);
}
// interface NSKeyValueCoding
@SuppressWarnings("unchecked")
@Override
public Object valueForKey(String aKey) { // System.out.println( "valueForKey: " + aKey + "->" + this );
Object result = objectForKey((K) aKey);
if (result == null)
result = NSKeyValueCodingSupport.valueForKey(this, aKey);
return result;
}
@SuppressWarnings("unchecked")
@Override
public void takeValueForKey(Object aValue, String aKey) { // System.out.println( "takeValueForKey: " + aKey + " : "
// + aValue + "->" + this );
put((K)aKey, (V)aValue); // FIXME: technically cheating since this is a read-only class
}
@SuppressWarnings("unchecked")
@Override
public Object storedValueForKey(String aKey) {
Object result = objectForKey((K) aKey);
if (result == null)
result = NSKeyValueCodingSupport.storedValueForKey(this, aKey);
return result;
}
@SuppressWarnings("unchecked")
@Override
public void takeStoredValueForKey(Object aValue, String aKey) {
put((K)aKey, (V)aValue); // FIXME: technically cheating since this is a read-only class
}
@Override
public Object handleQueryWithUnboundKey(String aKey) {
return NSKeyValueCodingSupport.handleQueryWithUnboundKey(this, aKey);
}
@Override
public void handleTakeValueForUnboundKey(Object aValue, String aKey) {
NSKeyValueCodingSupport.handleTakeValueForUnboundKey(this, aValue, aKey);
}
@Override
public void unableToSetNullForKey(String aKey) {
NSKeyValueCodingSupport.unableToSetNullForKey(this, aKey);
}
public Object validateTakeValueForKeyPath(Object aValue, String aKey) {
throw new RuntimeException("Not implemented yet.");
}
@SuppressWarnings("unchecked")
@Override
public String toString() {
StringBuffer buf = new StringBuffer();
Enumeration<K> enumeration = keyEnumerator();
buf.append(NSPropertyListSerialization.TOKEN_BEGIN[NSPropertyListSerialization.PLIST_DICTIONARY]);
while (enumeration.hasMoreElements()) {
if (buf.length() == 1)
buf.append(' ');
Object k = enumeration.nextElement();
buf.append(NSPropertyListSerialization.stringForPropertyList(k));
buf.append(" = ");
k = objectForKey((K) k);
buf.append(NSPropertyListSerialization.stringForPropertyList(k));
buf.append("; ");
}
buf.append(NSPropertyListSerialization.TOKEN_END[NSPropertyListSerialization.PLIST_DICTIONARY]);
return buf.toString();
}
}
/*
* $Log$ Revision 1.2 2006/02/16 13:15:00 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.9 2005/05/11 15:21:53 cgruber Change enum to enumeration, since
* enum is now a keyword as of Java 5.0
*
* A few other comments in the code.
*
* Revision 1.8 2003/08/05 00:50:14 chochos use NSPropertyListSerialization to
* get the tokens to enclose the string description
*
* Revision 1.7 2003/08/04 20:26:10 chochos use NSPropertyListSerialization
* inside toString()
*
* Revision 1.6 2003/08/04 18:49:38 chochos NSDictionary(Object[], Object[]) was
* taking the parameters in the wrong order; for compatibility with Apple's
* NSDictionary, objects comes first, then keys.
*
* Revision 1.5 2003/08/04 18:26:19 chochos fixed opening '{'
*
* Revision 1.4 2003/01/28 22:11:30 mpowers Now implements NSKeyValueCoding.
*
* Revision 1.3 2002/06/30 17:58:06 mpowers Add a capacity constructor and
* static empty dictionary: thanks cgruber.
*
* Revision 1.2 2001/02/23 23:43:41 mpowers Removed ill-advised this.
*
* Revision 1.1.1.1 2000/12/21 15:47:31 mpowers Contributing wotonomy.
*
* Revision 1.3 2000/12/20 16:25:38 michael Added log to all files.
*
*
*/
|