56 |
|
return map; |
57 |
|
} |
58 |
|
|
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 |
– |
|
59 |
|
// classes for testing Comparable fallbacks |
60 |
|
static class BI implements Comparable<BI> { |
61 |
|
private final int value; |
62 |
|
BI(int value) { this.value = value; } |
63 |
|
public int compareTo(BI other) { |
64 |
< |
return compare(value, other.value); |
64 |
> |
return Integer.compare(value, other.value); |
65 |
|
} |
66 |
|
public boolean equals(Object x) { |
67 |
|
return (x instanceof BI) && ((BI)x).value == value; |
95 |
|
break; |
96 |
|
} |
97 |
|
if (r == 0) |
98 |
< |
r = compare(size(), other.size()); |
98 |
> |
r = Integer.compare(size(), other.size()); |
99 |
|
return r; |
100 |
|
} |
101 |
|
private static final long serialVersionUID = 0; |
838 |
|
assertEquals(mapSize, map.size()); |
839 |
|
} |
840 |
|
|
841 |
+ |
|
842 |
+ |
|
843 |
+ |
public void testReentrantComputeIfAbsent() { |
844 |
+ |
ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(16); |
845 |
+ |
try { |
846 |
+ |
for (int i = 0; i < 100; i++) { // force a resize |
847 |
+ |
map.computeIfAbsent(i, key -> findValue(map, key)); |
848 |
+ |
} |
849 |
+ |
fail("recursive computeIfAbsent"); |
850 |
+ |
} catch (IllegalStateException ex) { |
851 |
+ |
} |
852 |
+ |
} |
853 |
+ |
|
854 |
+ |
private Integer findValue(ConcurrentHashMap<Integer, Integer> map, |
855 |
+ |
Integer key) { |
856 |
+ |
return (key % 5 == 0) ? key : |
857 |
+ |
map.computeIfAbsent(key + 1, k -> findValue(map, k)); |
858 |
+ |
} |
859 |
+ |
|
860 |
|
} |