28 |
|
class Implementation implements MapImplementation { |
29 |
|
public Class<?> klazz() { return ConcurrentHashMap.class; } |
30 |
|
public Map emptyMap() { return new ConcurrentHashMap(); } |
31 |
– |
public Object makeKey(int i) { return i; } |
32 |
– |
public Object makeValue(int i) { return i; } |
31 |
|
public boolean isConcurrent() { return true; } |
32 |
|
public boolean permitsNullKeys() { return false; } |
33 |
|
public boolean permitsNullValues() { return false; } |
54 |
|
return map; |
55 |
|
} |
56 |
|
|
59 |
– |
/** Re-implement Integer.compare for old java versions */ |
60 |
– |
static int compare(int x, int y) { |
61 |
– |
return (x < y) ? -1 : (x > y) ? 1 : 0; |
62 |
– |
} |
63 |
– |
|
57 |
|
// classes for testing Comparable fallbacks |
58 |
|
static class BI implements Comparable<BI> { |
59 |
|
private final int value; |
60 |
|
BI(int value) { this.value = value; } |
61 |
|
public int compareTo(BI other) { |
62 |
< |
return compare(value, other.value); |
62 |
> |
return Integer.compare(value, other.value); |
63 |
|
} |
64 |
|
public boolean equals(Object x) { |
65 |
|
return (x instanceof BI) && ((BI)x).value == value; |
93 |
|
break; |
94 |
|
} |
95 |
|
if (r == 0) |
96 |
< |
r = compare(size(), other.size()); |
96 |
> |
r = Integer.compare(size(), other.size()); |
97 |
|
return r; |
98 |
|
} |
99 |
|
private static final long serialVersionUID = 0; |
121 |
|
*/ |
122 |
|
public void testComparableFamily() { |
123 |
|
int size = 500; // makes measured test run time -> 60ms |
124 |
< |
ConcurrentHashMap<BI, Boolean> m = |
132 |
< |
new ConcurrentHashMap<BI, Boolean>(); |
124 |
> |
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>(); |
125 |
|
for (int i = 0; i < size; i++) { |
126 |
|
assertNull(m.put(new CI(i), true)); |
127 |
|
} |
137 |
|
*/ |
138 |
|
public void testGenericComparable() { |
139 |
|
int size = 120; // makes measured test run time -> 60ms |
140 |
< |
ConcurrentHashMap<Object, Boolean> m = |
149 |
< |
new ConcurrentHashMap<Object, Boolean>(); |
140 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
141 |
|
for (int i = 0; i < size; i++) { |
142 |
|
BI bi = new BI(i); |
143 |
|
BS bs = new BS(String.valueOf(i)); |
144 |
< |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
145 |
< |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
144 |
> |
LexicographicList<BI> bis = new LexicographicList<>(bi); |
145 |
> |
LexicographicList<BS> bss = new LexicographicList<>(bs); |
146 |
|
assertNull(m.putIfAbsent(bis, true)); |
147 |
|
assertTrue(m.containsKey(bis)); |
148 |
|
if (m.putIfAbsent(bss, true) == null) |
161 |
|
*/ |
162 |
|
public void testGenericComparable2() { |
163 |
|
int size = 500; // makes measured test run time -> 60ms |
164 |
< |
ConcurrentHashMap<Object, Boolean> m = |
174 |
< |
new ConcurrentHashMap<Object, Boolean>(); |
164 |
> |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
165 |
|
for (int i = 0; i < size; i++) { |
166 |
|
m.put(Collections.singletonList(new BI(i)), true); |
167 |
|
} |
168 |
|
|
169 |
|
for (int i = 0; i < size; i++) { |
170 |
< |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
170 |
> |
LexicographicList<BI> bis = new LexicographicList<>(new BI(i)); |
171 |
|
assertTrue(m.containsKey(bis)); |
172 |
|
} |
173 |
|
} |
178 |
|
*/ |
179 |
|
public void testMixedComparable() { |
180 |
|
int size = 1200; // makes measured test run time -> 35ms |
181 |
< |
ConcurrentHashMap<Object, Object> map = |
192 |
< |
new ConcurrentHashMap<Object, Object>(); |
181 |
> |
ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>(); |
182 |
|
Random rng = new Random(); |
183 |
|
for (int i = 0; i < size; i++) { |
184 |
|
Object x; |
836 |
|
assertEquals(mapSize, map.size()); |
837 |
|
} |
838 |
|
|
839 |
+ |
public void testReentrantComputeIfAbsent() { |
840 |
+ |
ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(16); |
841 |
+ |
try { |
842 |
+ |
for (int i = 0; i < 100; i++) { // force a resize |
843 |
+ |
map.computeIfAbsent(i, key -> findValue(map, key)); |
844 |
+ |
} |
845 |
+ |
fail("recursive computeIfAbsent should throw IllegalStateException"); |
846 |
+ |
} catch (IllegalStateException success) {} |
847 |
+ |
} |
848 |
+ |
|
849 |
+ |
private Integer findValue(ConcurrentHashMap<Integer, Integer> map, |
850 |
+ |
Integer key) { |
851 |
+ |
return (key % 5 == 0) ? key : |
852 |
+ |
map.computeIfAbsent(key + 1, k -> findValue(map, k)); |
853 |
+ |
} |
854 |
+ |
|
855 |
|
} |