-
Notifications
You must be signed in to change notification settings - Fork 17
/
ConcurrentOrderedMap.java
451 lines (395 loc) · 13.4 KB
/
ConcurrentOrderedMap.java
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**
* $Id: ConcurrentOrderedMap.java 2 2008-10-01 10:04:26Z azeckoski $
* $URL: http://reflectutils.googlecode.com/svn/trunk/src/main/java/org/azeckoski/reflectutils/map/ConcurrentOrderedMap.java $
* ConcurrentOrderedMap.java - genericdao - May 5, 2008 2:16:35 PM - azeckoski
**************************************************************************
* Copyright (c) 2008 Aaron Zeckoski
* Licensed under the Apache License, Version 2
*
* A copy of the Apache License, Version 2 has been included in this
* distribution and is available at: http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Aaron Zeckoski (azeckoski@gmail.com) (aaronz@vt.edu) (aaron@caret.cam.ac.uk)
*/
package org.azeckoski.reflectutils.map;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
/**
* A map which keeps track of the order the entries are added
* and allows retrieval of entries in the order they were entered as well,
* this is safe for multi-threaded access,
* this is backed by a {@link ConcurrentHashMap} and {@link Vector}
*
* @author Aaron Zeckoski (azeckoski @ gmail.com)
*/
public class ConcurrentOrderedMap<K, V> extends ConcurrentHashMap<K, V> implements OrderedMap<K, V> {
public static final long serialVersionUID = 1l;
private Vector<K> list = new Vector<K>(5,5);
public ConcurrentOrderedMap() {
this(10);
}
public ConcurrentOrderedMap(int initialCapacity) {
super(initialCapacity);
list = new Vector<K>(initialCapacity);
}
public ConcurrentOrderedMap(Map<K, V> map) {
this(map.size());
for (Entry<K, V> entry : map.entrySet()) {
this.put(entry.getKey(), entry.getValue());
}
}
private String name = "entity";
public String getName() {
return name;
}
/**
* @param name the name to use when encoding this map of entities
*/
public void setName(String name) {
this.name = name;
}
/**
* @return a list of all the keys in this map in the order they were entered
*/
public List<K> getKeys() {
return new ArrayList<K>(list);
}
/* (non-Javadoc)
* @see org.azeckoski.reflectutils.map.OrderedMap#getValues()
*/
public List<V> getValues() {
return new ArrayList<V>( values() );
}
/**
* @return a list of all the entries in this map in the order they were created
*/
public List<Entry<K, V>> getEntries() {
ArrayList<Entry<K, V>> entries = new ArrayList<Entry<K,V>>();
for (K key : list) {
Entry<K, V> entry = new SimpleEntry<K,V>(key, this.get(key));
entries.add(entry);
}
return entries;
}
/**
* Get an entry based on the position it is in the map (based on the order entries were created)
* @param position the position in the map (must be less that the size)
* @return the entry at that position
* @throws IllegalArgumentException if the position is greater than the map size
*/
public Entry<K, V> getEntry(int position) {
if (position >= list.size()) {
throw new IllegalArgumentException("Value is too large for the map size: " + list.size());
}
K key = list.get(position);
Entry<K, V> entry = new SimpleEntry<K,V>(key, this.get(key));
return entry;
}
@Override
public V get(Object key) {
V value = super.get(key);
return fixNullOut(value);
}
@Override
public boolean containsKey(Object key) {
return super.get(key) != null;
}
@Override
public V put(K key, V value) {
value = fixNullIn(value);
V v = super.put(key, value);
if (v != null) {
// displaced
list.remove(key);
}
list.add(key);
return v;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
@Override
public V putIfAbsent(K key, V value) {
if (! containsKey(key))
return put(key, value);
else
return get(key);
}
@Override
public V remove(Object key) {
V v = super.remove(key);
if (v != null) {
list.remove(key);
}
return v;
}
@Override
public boolean remove(Object key, Object value) {
value = fixNullIn(value);
boolean removed = super.remove(key, value);
if (removed) {
list.remove(key);
}
return removed;
}
@Override
public void clear() {
super.clear();
list.clear();
}
@Override
public Enumeration<K> keys() {
return new KeyIterator();
}
@Override
public Enumeration<V> elements() {
return new ValueIterator();
}
transient Set<K> keySet;
transient Set<Map.Entry<K,V>> entrySet;
transient Collection<V> values;
@Override
public Collection<V> values() {
Collection<V> vs = values;
return (vs != null) ? vs : (values = new Values());
}
@SuppressWarnings("unchecked")
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es = entrySet;
return (es != null) ? es : (entrySet = (Set<Map.Entry<K,V>>) (Set) new EntrySet());
}
// Iterator support
abstract class CoreIterator {
int currentPos = -1;
Entry<K, V> lastReturned = null;
private List<Entry<K, V>> entries = ConcurrentOrderedMap.this.getEntries();
public boolean hasMore() {
if ((currentPos + 1) < entries.size()) {
return true;
}
return false;
}
public Entry<K, V> getNext() {
currentPos++;
try {
lastReturned = entries.get(currentPos);
} catch (RuntimeException e) {
throw new NoSuchElementException("There are no more items available to get, the last one was reached");
}
return lastReturned;
}
public void removeCurrent() {
if (currentPos < 0) {
throw new IllegalArgumentException("Have not called next yet, cannot remove from this iterator");
}
entries.remove(currentPos);
ConcurrentOrderedMap.this.remove(lastReturned.getKey());
}
// shared methods
public boolean hasNext() { return hasMore(); }
public boolean hasMoreElements() { return hasMore(); }
public void remove() { removeCurrent(); }
}
final class KeyIterator extends CoreIterator implements Iterator<K>, Enumeration<K> {
public K next() { return super.getNext().getKey(); }
public K nextElement() { return next(); }
}
final class ValueIterator extends CoreIterator implements Iterator<V>, Enumeration<V> {
public V next() { return super.getNext().getValue(); }
public V nextElement() { return next(); }
}
final class EntryIterator extends CoreIterator implements Iterator<Entry<K,V>>, Enumeration<Entry<K,V>> {
public Entry<K,V> next() { return super.getNext(); }
public Entry<K,V> nextElement() { return next(); }
}
// All below copied from CHM
final class KeySet extends AbstractSet<K> {
public Iterator<K> iterator() {
return new KeyIterator();
}
public int size() {
return ConcurrentOrderedMap.this.size();
}
public boolean contains(Object o) {
return ConcurrentOrderedMap.this.containsKey(o);
}
public boolean remove(Object o) {
return ConcurrentOrderedMap.this.remove(o) != null;
}
public void clear() {
ConcurrentOrderedMap.this.clear();
}
public Object[] toArray() {
Collection<K> c = new ArrayList<K>();
for (Iterator<K> i = iterator(); i.hasNext(); )
c.add(i.next());
return c.toArray();
}
public <T> T[] toArray(T[] a) {
Collection<K> c = new ArrayList<K>();
for (Iterator<K> i = iterator(); i.hasNext(); )
c.add(i.next());
return c.toArray(a);
}
}
final class Values extends AbstractCollection<V> {
public Iterator<V> iterator() {
return new ValueIterator();
}
public int size() {
return ConcurrentOrderedMap.this.size();
}
public boolean contains(Object o) {
return ConcurrentOrderedMap.this.containsValue(o);
}
public void clear() {
ConcurrentOrderedMap.this.clear();
}
public Object[] toArray() {
Collection<V> c = new ArrayList<V>();
for (Iterator<V> i = iterator(); i.hasNext(); )
c.add(i.next());
return c.toArray();
}
public <T> T[] toArray(T[] a) {
Collection<V> c = new ArrayList<V>();
for (Iterator<V> i = iterator(); i.hasNext(); )
c.add(i.next());
return c.toArray(a);
}
}
@SuppressWarnings("unchecked")
final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public Iterator<Map.Entry<K,V>> iterator() {
return new EntryIterator();
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K,V> e = (Map.Entry<K,V>)o;
V v = ConcurrentOrderedMap.this.get(e.getKey());
return v != null && v.equals(e.getValue());
}
public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K,V> e = (Map.Entry<K,V>)o;
return ConcurrentOrderedMap.this.remove(e.getKey(), e.getValue());
}
public int size() {
return ConcurrentOrderedMap.this.size();
}
public void clear() {
ConcurrentOrderedMap.this.clear();
}
public Object[] toArray() {
// Since we don't ordinarily have distinct Entry objects, we
// must pack elements using exportable SimpleEntry
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size());
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); )
c.add(new SimpleEntry<K,V>(i.next()));
return c.toArray();
}
public <T> T[] toArray(T[] a) {
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size());
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); )
c.add(new SimpleEntry<K,V>(i.next()));
return c.toArray(a);
}
}
/**
* This duplicates java.util.AbstractMap.SimpleEntry until this class
* is made accessible.
*/
static final class SimpleEntry<K,V> implements Entry<K,V> {
K key;
V value;
public SimpleEntry(K key, V value) {
this.key = key;
this.value = value;
}
public SimpleEntry(Entry<K,V> e) {
this.key = e.getKey();
this.value = e.getValue();
}
public K getKey() {
return key;
}
public V getValue() {
return fixNullOut(value);
}
public V setValue(V value) {
V oldValue = fixNullOut(this.value);
this.value = value;
return oldValue;
}
@SuppressWarnings("unchecked")
public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry)o;
return eq(key, e.getKey())
&& eq(value, e.getValue());
}
public int hashCode() {
return ((key == null) ? 0 : key.hashCode()) ^
((value == null) ? 0 : value.hashCode());
}
public String toString() {
return key + "=" + fixNullOut(value);
}
static boolean eq(Object o1, Object o2) {
return (o1 == null ? o2 == null : o1.equals(o2));
}
}
/**
* Fixes up the null values so that the map will happily store it,
* has to be applied to every value going into the map
*/
@SuppressWarnings("unchecked")
protected static <T> T fixNullIn(T value) {
if (value == null) {
return (T) new Null();
} else {
if (value instanceof Null) {
return null;
}
}
return value;
}
/**
* Fixes up the stored Null objects so they are returned as nulls,
* has to be applied to every value going out of the map
*/
protected static <T> T fixNullOut(T value) {
if (value != null) {
if (value instanceof Null) {
return null;
}
}
return value;
}
public static class Null {
@Override
public boolean equals(Object obj) {
return true;
}
@Override
public int hashCode() {
return 0;
}
}
}