34 |
|
return map; |
35 |
|
} |
36 |
|
|
37 |
+ |
// classes for testing Comparable fallbacks |
38 |
+ |
static class BI implements Comparable<BI> { |
39 |
+ |
private final int value; |
40 |
+ |
BI(int value) { this.value = value; } |
41 |
+ |
public int compareTo(BI other) { |
42 |
+ |
return Integer.compare(value, other.value); |
43 |
+ |
} |
44 |
+ |
public boolean equals(Object x) { |
45 |
+ |
return (x instanceof BI) && ((BI)x).value == value; |
46 |
+ |
} |
47 |
+ |
public int hashCode() { return 42; } |
48 |
+ |
} |
49 |
+ |
static class CI extends BI { CI(int value) { super(value); } } |
50 |
+ |
static class DI extends BI { DI(int value) { super(value); } } |
51 |
+ |
|
52 |
+ |
static class BS implements Comparable<BS> { |
53 |
+ |
private final String value; |
54 |
+ |
BS(String value) { this.value = value; } |
55 |
+ |
public int compareTo(BS other) { |
56 |
+ |
return value.compareTo(other.value); |
57 |
+ |
} |
58 |
+ |
public boolean equals(Object x) { |
59 |
+ |
return (x instanceof BS) && value.equals(((BS)x).value); |
60 |
+ |
} |
61 |
+ |
public int hashCode() { return 42; } |
62 |
+ |
} |
63 |
+ |
|
64 |
+ |
static class LexicographicList<E extends Comparable<E>> extends ArrayList<E> |
65 |
+ |
implements Comparable<LexicographicList<E>> { |
66 |
+ |
LexicographicList(Collection<E> c) { super(c); } |
67 |
+ |
LexicographicList(E e) { super(Collections.singleton(e)); } |
68 |
+ |
public int compareTo(LexicographicList<E> other) { |
69 |
+ |
int common = Math.min(size(), other.size()); |
70 |
+ |
int r = 0; |
71 |
+ |
for (int i = 0; i < common; i++) { |
72 |
+ |
if ((r = get(i).compareTo(other.get(i))) != 0) |
73 |
+ |
break; |
74 |
+ |
} |
75 |
+ |
if (r == 0) |
76 |
+ |
r = Integer.compare(size(), other.size()); |
77 |
+ |
return r; |
78 |
+ |
} |
79 |
+ |
private static final long serialVersionUID = 0; |
80 |
+ |
} |
81 |
+ |
|
82 |
+ |
/** |
83 |
+ |
* Inserted elements that are subclasses of the same Comparable |
84 |
+ |
* class are found. |
85 |
+ |
*/ |
86 |
+ |
public void testComparableFamily() { |
87 |
+ |
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>(); |
88 |
+ |
for (int i = 0; i < 1000; i++) { |
89 |
+ |
assertTrue(m.put(new CI(i), true) == null); |
90 |
+ |
} |
91 |
+ |
for (int i = 0; i < 1000; i++) { |
92 |
+ |
assertTrue(m.containsKey(new CI(i))); |
93 |
+ |
assertTrue(m.containsKey(new DI(i))); |
94 |
+ |
} |
95 |
+ |
} |
96 |
+ |
|
97 |
+ |
/** |
98 |
+ |
* Elements of classes with erased generic type parameters based |
99 |
+ |
* on Comparable can be inserted and found. |
100 |
+ |
*/ |
101 |
+ |
public void testGenericComparable() { |
102 |
+ |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
103 |
+ |
for (int i = 0; i < 1000; i++) { |
104 |
+ |
BI bi = new BI(i); |
105 |
+ |
BS bs = new BS(String.valueOf(i)); |
106 |
+ |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
107 |
+ |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
108 |
+ |
assertTrue(m.putIfAbsent(bis, true) == null); |
109 |
+ |
assertTrue(m.containsKey(bis)); |
110 |
+ |
if (m.putIfAbsent(bss, true) == null) |
111 |
+ |
assertTrue(m.containsKey(bss)); |
112 |
+ |
assertTrue(m.containsKey(bis)); |
113 |
+ |
} |
114 |
+ |
for (int i = 0; i < 1000; i++) { |
115 |
+ |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
116 |
+ |
} |
117 |
+ |
} |
118 |
+ |
|
119 |
+ |
/** |
120 |
+ |
* Elements of non-comparable classes equal to those of classes |
121 |
+ |
* with erased generic type parameters based on Comparable can be |
122 |
+ |
* inserted and found. |
123 |
+ |
*/ |
124 |
+ |
public void testGenericComparable2() { |
125 |
+ |
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
126 |
+ |
for (int i = 0; i < 1000; i++) { |
127 |
+ |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
128 |
+ |
} |
129 |
+ |
|
130 |
+ |
for (int i = 0; i < 1000; i++) { |
131 |
+ |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
132 |
+ |
assertTrue(m.containsKey(bis)); |
133 |
+ |
} |
134 |
+ |
} |
135 |
+ |
|
136 |
|
/** |
137 |
|
* getOrDefault returns value if present, else default |
138 |
|
*/ |
167 |
|
map.computeIfAbsent(six, (x) -> null); |
168 |
|
assertFalse(map.containsKey(six)); |
169 |
|
} |
170 |
< |
|
170 |
> |
|
171 |
|
/** |
172 |
|
* computeIfPresent does not replace if the key is already present |
173 |
|
*/ |
479 |
|
elements[i] = i; |
480 |
|
Collections.shuffle(Arrays.asList(elements)); |
481 |
|
Collection<Integer> full = populatedSet(elements); |
482 |
< |
|
482 |
> |
|
483 |
|
assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray()))); |
484 |
|
assertTrue(full.containsAll(Arrays.asList(full.toArray()))); |
485 |
|
assertSame(Object[].class, full.toArray().getClass()); |
541 |
|
|
542 |
|
static final int SIZE = 10000; |
543 |
|
static ConcurrentHashMap<Long, Long> longMap; |
544 |
< |
|
544 |
> |
|
545 |
|
static ConcurrentHashMap<Long, Long> longMap() { |
546 |
|
if (longMap == null) { |
547 |
|
longMap = new ConcurrentHashMap<Long, Long>(SIZE); |
555 |
|
static class AddKeys implements BiFunction<Map.Entry<Long,Long>, Map.Entry<Long,Long>, Map.Entry<Long,Long>> { |
556 |
|
public Map.Entry<Long,Long> apply(Map.Entry<Long,Long> x, Map.Entry<Long,Long> y) { |
557 |
|
return new AbstractMap.SimpleEntry<Long,Long> |
558 |
< |
(Long.valueOf(x.getKey().longValue() + y.getKey().longValue()), |
558 |
> |
(Long.valueOf(x.getKey().longValue() + y.getKey().longValue()), |
559 |
|
Long.valueOf(1L)); |
560 |
|
} |
561 |
|
} |
825 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
826 |
|
Long r = m.reduceSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), |
827 |
|
(Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); |
828 |
< |
|
828 |
> |
|
829 |
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
830 |
|
} |
831 |
|
|
876 |
|
public void testReduceKeysToIntSequentially() { |
877 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
878 |
|
int ir = m.reduceKeysToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); |
879 |
< |
assertEquals(ir, (int)SIZE * (SIZE - 1) / 2); |
879 |
> |
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
880 |
|
} |
881 |
|
|
882 |
|
/* |
903 |
|
public void testReduceValuesToIntSequentially() { |
904 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
905 |
|
int ir = m.reduceValuesToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); |
906 |
< |
assertEquals(ir, (int)SIZE * (SIZE - 1)); |
906 |
> |
assertEquals(ir, SIZE * (SIZE - 1)); |
907 |
|
} |
908 |
|
|
909 |
|
/* |
930 |
|
public void testReduceKeysToIntInParallel() { |
931 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
932 |
|
int ir = m.reduceKeysToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); |
933 |
< |
assertEquals(ir, (int)SIZE * (SIZE - 1) / 2); |
933 |
> |
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
934 |
|
} |
935 |
|
|
936 |
|
/* |
957 |
|
public void testReduceValuesToIntInParallel() { |
958 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
959 |
|
int ir = m.reduceValuesToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); |
960 |
< |
assertEquals(ir, (int)SIZE * (SIZE - 1)); |
960 |
> |
assertEquals(ir, SIZE * (SIZE - 1)); |
961 |
|
} |
962 |
|
|
963 |
|
/* |
976 |
|
public void testSearchKeysSequentially() { |
977 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
978 |
|
Long r; |
979 |
< |
r = m.searchKeysSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
979 |
> |
r = m.searchKeysSequentially((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
980 |
|
assertEquals((long)r, (long)(SIZE/2)); |
981 |
< |
r = m.searchKeysSequentially((Long x) -> x.longValue() < 0L? x : null); |
981 |
> |
r = m.searchKeysSequentially((Long x) -> x.longValue() < 0L ? x : null); |
982 |
|
assertNull(r); |
983 |
|
} |
984 |
|
|
991 |
|
Long r; |
992 |
|
r = m.searchValuesSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
993 |
|
assertEquals((long)r, (long)(SIZE/2)); |
994 |
< |
r = m.searchValuesSequentially((Long x) -> x.longValue() < 0L? x : null); |
994 |
> |
r = m.searchValuesSequentially((Long x) -> x.longValue() < 0L ? x : null); |
995 |
|
assertNull(r); |
996 |
|
} |
997 |
|
|
1002 |
|
public void testSearchSequentially() { |
1003 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1004 |
|
Long r; |
1005 |
< |
r = m.searchSequentially((Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null); |
1005 |
> |
r = m.searchSequentially((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1006 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1007 |
< |
r = m.searchSequentially((Long x, Long y) -> x.longValue() < 0L? x : null); |
1007 |
> |
r = m.searchSequentially((Long x, Long y) -> x.longValue() < 0L ? x : null); |
1008 |
|
assertNull(r); |
1009 |
|
} |
1010 |
|
|
1015 |
|
public void testSearchEntriesSequentially() { |
1016 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1017 |
|
Long r; |
1018 |
< |
r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null); |
1018 |
> |
r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); |
1019 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1020 |
< |
r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L? e.getKey() : null); |
1020 |
> |
r = m.searchEntriesSequentially((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
1021 |
|
assertNull(r); |
1022 |
|
} |
1023 |
|
|
1028 |
|
public void testSearchKeysInParallel() { |
1029 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1030 |
|
Long r; |
1031 |
< |
r = m.searchKeysInParallel((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
1031 |
> |
r = m.searchKeysInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1032 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1033 |
< |
r = m.searchKeysInParallel((Long x) -> x.longValue() < 0L? x : null); |
1033 |
> |
r = m.searchKeysInParallel((Long x) -> x.longValue() < 0L ? x : null); |
1034 |
|
assertNull(r); |
1035 |
|
} |
1036 |
|
|
1041 |
|
public void testSearchValuesInParallel() { |
1042 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1043 |
|
Long r; |
1044 |
< |
r = m.searchValuesInParallel((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); |
1044 |
> |
r = m.searchValuesInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1045 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1046 |
< |
r = m.searchValuesInParallel((Long x) -> x.longValue() < 0L? x : null); |
1046 |
> |
r = m.searchValuesInParallel((Long x) -> x.longValue() < 0L ? x : null); |
1047 |
|
assertNull(r); |
1048 |
|
} |
1049 |
|
|
1054 |
|
public void testSearchInParallel() { |
1055 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1056 |
|
Long r; |
1057 |
< |
r = m.searchInParallel((Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null); |
1057 |
> |
r = m.searchInParallel((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); |
1058 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1059 |
< |
r = m.searchInParallel((Long x, Long y) -> x.longValue() < 0L? x : null); |
1059 |
> |
r = m.searchInParallel((Long x, Long y) -> x.longValue() < 0L ? x : null); |
1060 |
|
assertNull(r); |
1061 |
|
} |
1062 |
|
|
1067 |
|
public void testSearchEntriesInParallel() { |
1068 |
|
ConcurrentHashMap<Long, Long> m = longMap(); |
1069 |
|
Long r; |
1070 |
< |
r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null); |
1070 |
> |
r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); |
1071 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1072 |
< |
r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L? e.getKey() : null); |
1072 |
> |
r = m.searchEntriesInParallel((Map.Entry<Long,Long> e) -> e.getKey().longValue() < 0L ? e.getKey() : null); |
1073 |
|
assertNull(r); |
1074 |
|
} |
1075 |
|
|
1093 |
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
1094 |
|
adder.reset(); |
1095 |
|
ConcurrentHashMap.ForkJoinTasks.forEachEntry |
1096 |
< |
(m, |
1096 |
> |
(m, |
1097 |
|
(Map.Entry<Long,Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())).invoke(); |
1098 |
|
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); |
1099 |
|
adder.reset(); |
1142 |
|
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
1143 |
|
ir = ConcurrentHashMap.ForkJoinTasks.reduceKeysToInt |
1144 |
|
(m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); |
1145 |
< |
assertEquals(ir, (int)SIZE * (SIZE - 1) / 2); |
1145 |
> |
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
1146 |
|
dr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToDouble |
1147 |
|
(m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); |
1148 |
|
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
1155 |
|
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
1156 |
|
ir = ConcurrentHashMap.ForkJoinTasks.reduceValuesToInt |
1157 |
|
(m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); |
1158 |
< |
assertEquals(ir, (int)SIZE * (SIZE - 1)); |
1158 |
> |
assertEquals(ir, SIZE * (SIZE - 1)); |
1159 |
|
dr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToDouble |
1160 |
|
(m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); |
1161 |
|
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
1171 |
|
r = ConcurrentHashMap.ForkJoinTasks.searchEntries |
1172 |
|
(m, (Map.Entry<Long,Long> e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null).invoke(); |
1173 |
|
assertEquals((long)r, (long)(SIZE/2)); |
1174 |
< |
} |
1174 |
> |
} |
1175 |
|
} |