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 |
|
*/ |
798 |
|
assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2); |
799 |
|
} |
800 |
|
|
801 |
< |
/* |
801 |
> |
/** |
802 |
|
* Mapped reduceKeysSequentially accumulates mapped keys |
803 |
|
*/ |
804 |
|
public void testMapReduceKeysSequentially() { |
808 |
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
809 |
|
} |
810 |
|
|
811 |
< |
/* |
811 |
> |
/** |
812 |
|
* Mapped reduceValuesSequentially accumulates mapped values |
813 |
|
*/ |
814 |
|
public void testMapReduceValuesSequentially() { |
829 |
|
assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); |
830 |
|
} |
831 |
|
|
832 |
< |
/* |
832 |
> |
/** |
833 |
|
* Mapped reduceKeysInParallel, accumulates mapped keys |
834 |
|
*/ |
835 |
|
public void testMapReduceKeysInParallel() { |
839 |
|
assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); |
840 |
|
} |
841 |
|
|
842 |
< |
/* |
842 |
> |
/** |
843 |
|
* Mapped reduceValuesInParallel accumulates mapped values |
844 |
|
*/ |
845 |
|
public void testMapReduceValuesInParallel() { |
861 |
|
} |
862 |
|
|
863 |
|
|
864 |
< |
/* |
864 |
> |
/** |
865 |
|
* reduceKeysToLongSequentially accumulates mapped keys |
866 |
|
*/ |
867 |
|
public void testReduceKeysToLongSequentially() { |
870 |
|
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
871 |
|
} |
872 |
|
|
873 |
< |
/* |
873 |
> |
/** |
874 |
|
* reduceKeysToIntSequentially accumulates mapped keys |
875 |
|
*/ |
876 |
|
public void testReduceKeysToIntSequentially() { |
879 |
|
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
880 |
|
} |
881 |
|
|
882 |
< |
/* |
882 |
> |
/** |
883 |
|
* reduceKeysToDoubleSequentially accumulates mapped keys |
884 |
|
*/ |
885 |
|
public void testReduceKeysToDoubleSequentially() { |
888 |
|
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
889 |
|
} |
890 |
|
|
891 |
< |
/* |
891 |
> |
/** |
892 |
|
* reduceValuesToLongSequentially accumulates mapped values |
893 |
|
*/ |
894 |
|
public void testReduceValuesToLongSequentially() { |
897 |
|
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
898 |
|
} |
899 |
|
|
900 |
< |
/* |
900 |
> |
/** |
901 |
|
* reduceValuesToIntSequentially accumulates mapped values |
902 |
|
*/ |
903 |
|
public void testReduceValuesToIntSequentially() { |
906 |
|
assertEquals(ir, SIZE * (SIZE - 1)); |
907 |
|
} |
908 |
|
|
909 |
< |
/* |
909 |
> |
/** |
910 |
|
* reduceValuesToDoubleSequentially accumulates mapped values |
911 |
|
*/ |
912 |
|
public void testReduceValuesToDoubleSequentially() { |
915 |
|
assertEquals(dr, (double)SIZE * (SIZE - 1)); |
916 |
|
} |
917 |
|
|
918 |
< |
/* |
918 |
> |
/** |
919 |
|
* reduceKeysToLongInParallel accumulates mapped keys |
920 |
|
*/ |
921 |
|
public void testReduceKeysToLongInParallel() { |
924 |
|
assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); |
925 |
|
} |
926 |
|
|
927 |
< |
/* |
927 |
> |
/** |
928 |
|
* reduceKeysToIntInParallel accumulates mapped keys |
929 |
|
*/ |
930 |
|
public void testReduceKeysToIntInParallel() { |
933 |
|
assertEquals(ir, SIZE * (SIZE - 1) / 2); |
934 |
|
} |
935 |
|
|
936 |
< |
/* |
936 |
> |
/** |
937 |
|
* reduceKeysToDoubleInParallel accumulates mapped values |
938 |
|
*/ |
939 |
|
public void testReduceKeysToDoubleInParallel() { |
942 |
|
assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); |
943 |
|
} |
944 |
|
|
945 |
< |
/* |
945 |
> |
/** |
946 |
|
* reduceValuesToLongInParallel accumulates mapped values |
947 |
|
*/ |
948 |
|
public void testReduceValuesToLongInParallel() { |
951 |
|
assertEquals(lr, (long)SIZE * (SIZE - 1)); |
952 |
|
} |
953 |
|
|
954 |
< |
/* |
954 |
> |
/** |
955 |
|
* reduceValuesToIntInParallel accumulates mapped values |
956 |
|
*/ |
957 |
|
public void testReduceValuesToIntInParallel() { |
960 |
|
assertEquals(ir, SIZE * (SIZE - 1)); |
961 |
|
} |
962 |
|
|
963 |
< |
/* |
963 |
> |
/** |
964 |
|
* reduceValuesToDoubleInParallel accumulates mapped values |
965 |
|
*/ |
966 |
|
public void testReduceValuesToDoubleInParallel() { |