19 |
|
} |
20 |
|
|
21 |
|
/** |
22 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
22 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
23 |
|
*/ |
24 |
|
private static ConcurrentHashMap map5() { |
25 |
|
ConcurrentHashMap map = new ConcurrentHashMap(5); |
34 |
|
return map; |
35 |
|
} |
36 |
|
|
37 |
+ |
/** Re-implement Integer.compare for old java versions */ |
38 |
+ |
static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } |
39 |
+ |
|
40 |
+ |
// classes for testing Comparable fallbacks |
41 |
+ |
static class BI implements Comparable<BI> { |
42 |
+ |
private final int value; |
43 |
+ |
BI(int value) { this.value = value; } |
44 |
+ |
public int compareTo(BI other) { |
45 |
+ |
return compare(value, other.value); |
46 |
+ |
} |
47 |
+ |
public boolean equals(Object x) { |
48 |
+ |
return (x instanceof BI) && ((BI)x).value == value; |
49 |
+ |
} |
50 |
+ |
public int hashCode() { return 42; } |
51 |
+ |
} |
52 |
+ |
static class CI extends BI { CI(int value) { super(value); } } |
53 |
+ |
static class DI extends BI { DI(int value) { super(value); } } |
54 |
+ |
|
55 |
+ |
static class BS implements Comparable<BS> { |
56 |
+ |
private final String value; |
57 |
+ |
BS(String value) { this.value = value; } |
58 |
+ |
public int compareTo(BS other) { |
59 |
+ |
return value.compareTo(other.value); |
60 |
+ |
} |
61 |
+ |
public boolean equals(Object x) { |
62 |
+ |
return (x instanceof BS) && value.equals(((BS)x).value); |
63 |
+ |
} |
64 |
+ |
public int hashCode() { return 42; } |
65 |
+ |
} |
66 |
+ |
|
67 |
+ |
static class LexicographicList<E extends Comparable<E>> extends ArrayList<E> |
68 |
+ |
implements Comparable<LexicographicList<E>> { |
69 |
+ |
LexicographicList(Collection<E> c) { super(c); } |
70 |
+ |
LexicographicList(E e) { super(Collections.singleton(e)); } |
71 |
+ |
public int compareTo(LexicographicList<E> other) { |
72 |
+ |
int common = Math.min(size(), other.size()); |
73 |
+ |
int r = 0; |
74 |
+ |
for (int i = 0; i < common; i++) { |
75 |
+ |
if ((r = get(i).compareTo(other.get(i))) != 0) |
76 |
+ |
break; |
77 |
+ |
} |
78 |
+ |
if (r == 0) |
79 |
+ |
r = compare(size(), other.size()); |
80 |
+ |
return r; |
81 |
+ |
} |
82 |
+ |
private static final long serialVersionUID = 0; |
83 |
+ |
} |
84 |
+ |
|
85 |
+ |
/** |
86 |
+ |
* Inserted elements that are subclasses of the same Comparable |
87 |
+ |
* class are found. |
88 |
+ |
*/ |
89 |
+ |
public void testComparableFamily() { |
90 |
+ |
ConcurrentHashMap<BI, Boolean> m = |
91 |
+ |
new ConcurrentHashMap<BI, Boolean>(); |
92 |
+ |
for (int i = 0; i < 1000; i++) { |
93 |
+ |
assertTrue(m.put(new CI(i), true) == null); |
94 |
+ |
} |
95 |
+ |
for (int i = 0; i < 1000; i++) { |
96 |
+ |
assertTrue(m.containsKey(new CI(i))); |
97 |
+ |
assertTrue(m.containsKey(new DI(i))); |
98 |
+ |
} |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
/** |
102 |
+ |
* Elements of classes with erased generic type parameters based |
103 |
+ |
* on Comparable can be inserted and found. |
104 |
+ |
*/ |
105 |
+ |
public void testGenericComparable() { |
106 |
+ |
ConcurrentHashMap<Object, Boolean> m = |
107 |
+ |
new ConcurrentHashMap<Object, Boolean>(); |
108 |
+ |
for (int i = 0; i < 1000; i++) { |
109 |
+ |
BI bi = new BI(i); |
110 |
+ |
BS bs = new BS(String.valueOf(i)); |
111 |
+ |
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
112 |
+ |
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
113 |
+ |
assertTrue(m.putIfAbsent(bis, true) == null); |
114 |
+ |
assertTrue(m.containsKey(bis)); |
115 |
+ |
if (m.putIfAbsent(bss, true) == null) |
116 |
+ |
assertTrue(m.containsKey(bss)); |
117 |
+ |
assertTrue(m.containsKey(bis)); |
118 |
+ |
} |
119 |
+ |
for (int i = 0; i < 1000; i++) { |
120 |
+ |
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
121 |
+ |
} |
122 |
+ |
} |
123 |
+ |
|
124 |
+ |
/** |
125 |
+ |
* Elements of non-comparable classes equal to those of classes |
126 |
+ |
* with erased generic type parameters based on Comparable can be |
127 |
+ |
* inserted and found. |
128 |
+ |
*/ |
129 |
+ |
public void testGenericComparable2() { |
130 |
+ |
ConcurrentHashMap<Object, Boolean> m = |
131 |
+ |
new ConcurrentHashMap<Object, Boolean>(); |
132 |
+ |
for (int i = 0; i < 1000; i++) { |
133 |
+ |
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
134 |
+ |
} |
135 |
+ |
|
136 |
+ |
for (int i = 0; i < 1000; i++) { |
137 |
+ |
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
138 |
+ |
assertTrue(m.containsKey(bis)); |
139 |
+ |
} |
140 |
+ |
} |
141 |
+ |
|
142 |
|
/** |
143 |
|
* clear removes all pairs |
144 |
|
*/ |
657 |
|
Map x = map5(); |
658 |
|
Map y = serialClone(x); |
659 |
|
|
660 |
< |
assertTrue(x != y); |
660 |
> |
assertNotSame(x, y); |
661 |
|
assertEquals(x.size(), y.size()); |
662 |
|
assertEquals(x, y); |
663 |
|
assertEquals(y, x); |
674 |
|
map.put(new Integer(i), new Integer(i)); |
675 |
|
assertFalse(map.isEmpty()); |
676 |
|
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
677 |
< |
|
678 |
< |
// assert that entry1 is not 16 |
679 |
< |
assertTrue("entry is 16, test not valid", |
680 |
< |
!entry1.getKey().equals(new Integer(16))); |
681 |
< |
|
682 |
< |
// remove 16 (a different key) from map |
683 |
< |
// which just happens to cause entry1 to be cloned in map |
684 |
< |
map.remove(new Integer(16)); |
685 |
< |
entry1.setValue("XYZ"); |
581 |
< |
assertTrue(map.containsValue("XYZ")); // fails |
677 |
> |
// Unless it happens to be first (in which case remainder of |
678 |
> |
// test is skipped), remove a possibly-colliding key from map |
679 |
> |
// which, under some implementations, may cause entry1 to be |
680 |
> |
// cloned in map |
681 |
> |
if (!entry1.getKey().equals(new Integer(16))) { |
682 |
> |
map.remove(new Integer(16)); |
683 |
> |
entry1.setValue("XYZ"); |
684 |
> |
assertTrue(map.containsValue("XYZ")); // fails if write-through broken |
685 |
> |
} |
686 |
|
} |
687 |
|
|
688 |
|
} |