author | dl |
Fri, 29 Jan 2016 11:49:37 -0800 | |
changeset 35396 | c58a744d0bb4 |
parent 32991 | b27c76b82713 |
child 45006 | 1c15ce5f636c |
permissions | -rw-r--r-- |
12859 | 1 |
/* |
16042
0bf6469a1cfb
8008785: IdentityHashMap.values().toArray(V[]) broken by JDK-8008167
mduigou
parents:
13817
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
12859 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
26 |
* @bug 8010122 8004518 8024331 8024688 |
16867 | 27 |
* @summary Test Map default methods |
12859 | 28 |
* @author Mike Duigou |
16867 | 29 |
* @run testng Defaults |
12859 | 30 |
*/ |
16867 | 31 |
import java.util.AbstractMap; |
32 |
import java.util.AbstractSet; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Arrays; |
|
35 |
import java.util.Collection; |
|
36 |
import java.util.Collections; |
|
37 |
import java.util.EnumMap; |
|
38 |
import java.util.HashMap; |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
39 |
import java.util.Hashtable; |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
40 |
import java.util.HashSet; |
16867 | 41 |
import java.util.IdentityHashMap; |
42 |
import java.util.Iterator; |
|
43 |
import java.util.LinkedHashMap; |
|
44 |
import java.util.Map; |
|
45 |
import java.util.TreeMap; |
|
46 |
import java.util.Set; |
|
47 |
import java.util.WeakHashMap; |
|
48 |
import java.util.concurrent.ConcurrentMap; |
|
12859 | 49 |
import java.util.concurrent.ConcurrentHashMap; |
50 |
import java.util.concurrent.ConcurrentSkipListMap; |
|
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
51 |
import java.util.concurrent.atomic.AtomicBoolean; |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
52 |
import java.util.function.BiFunction; |
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
53 |
import java.util.function.Function; |
16867 | 54 |
import java.util.function.Supplier; |
12859 | 55 |
|
16867 | 56 |
import org.testng.annotations.Test; |
57 |
import org.testng.annotations.DataProvider; |
|
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
58 |
import static java.util.Objects.requireNonNull; |
16867 | 59 |
import static org.testng.Assert.fail; |
60 |
import static org.testng.Assert.assertEquals; |
|
61 |
import static org.testng.Assert.assertTrue; |
|
62 |
import static org.testng.Assert.assertFalse; |
|
63 |
import static org.testng.Assert.assertNull; |
|
64 |
import static org.testng.Assert.assertSame; |
|
65 |
||
66 |
public class Defaults { |
|
67 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
68 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=all keys=withNull values=withNull") |
16867 | 69 |
public void testGetOrDefaultNulls(String description, Map<IntegerEnum, String> map) { |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
70 |
assertTrue(map.containsKey(null), description + ": null key absent"); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
71 |
assertNull(map.get(null), description + ": value not null"); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
72 |
assertSame(map.get(null), map.getOrDefault(null, EXTRA_VALUE), description + ": values should match"); |
16867 | 73 |
} |
74 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
75 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=all keys=all values=all") |
16867 | 76 |
public void testGetOrDefault(String description, Map<IntegerEnum, String> map) { |
77 |
assertTrue(map.containsKey(KEYS[1]), "expected key missing"); |
|
78 |
assertSame(map.get(KEYS[1]), map.getOrDefault(KEYS[1], EXTRA_VALUE), "values should match"); |
|
79 |
assertFalse(map.containsKey(EXTRA_KEY), "expected absent key"); |
|
80 |
assertSame(map.getOrDefault(EXTRA_KEY, EXTRA_VALUE), EXTRA_VALUE, "value not returned as default"); |
|
81 |
assertNull(map.getOrDefault(EXTRA_KEY, null), "null not returned as default"); |
|
82 |
} |
|
12859 | 83 |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
84 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
16867 | 85 |
public void testPutIfAbsentNulls(String description, Map<IntegerEnum, String> map) { |
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
86 |
// null -> null |
16867 | 87 |
assertTrue(map.containsKey(null), "null key absent"); |
88 |
assertNull(map.get(null), "value not null"); |
|
89 |
assertNull(map.putIfAbsent(null, EXTRA_VALUE), "previous not null"); |
|
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
90 |
// null -> EXTRA_VALUE |
16867 | 91 |
assertTrue(map.containsKey(null), "null key absent"); |
92 |
assertSame(map.get(null), EXTRA_VALUE, "unexpected value"); |
|
93 |
assertSame(map.putIfAbsent(null, null), EXTRA_VALUE, "previous not expected value"); |
|
94 |
assertTrue(map.containsKey(null), "null key absent"); |
|
95 |
assertSame(map.get(null), EXTRA_VALUE, "unexpected value"); |
|
96 |
assertSame(map.remove(null), EXTRA_VALUE, "removed unexpected value"); |
|
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
97 |
// null -> <absent> |
16867 | 98 |
|
99 |
assertFalse(map.containsKey(null), description + ": key present after remove"); |
|
100 |
assertNull(map.putIfAbsent(null, null), "previous not null"); |
|
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
101 |
// null -> null |
16867 | 102 |
assertTrue(map.containsKey(null), "null key absent"); |
103 |
assertNull(map.get(null), "value not null"); |
|
104 |
assertNull(map.putIfAbsent(null, EXTRA_VALUE), "previous not null"); |
|
105 |
assertSame(map.get(null), EXTRA_VALUE, "value not expected"); |
|
106 |
} |
|
107 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
108 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
16867 | 109 |
public void testPutIfAbsent(String description, Map<IntegerEnum, String> map) { |
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
110 |
// 1 -> 1 |
16867 | 111 |
assertTrue(map.containsKey(KEYS[1])); |
112 |
Object expected = map.get(KEYS[1]); |
|
113 |
assertTrue(null == expected || expected == VALUES[1]); |
|
114 |
assertSame(map.putIfAbsent(KEYS[1], EXTRA_VALUE), expected); |
|
115 |
assertSame(map.get(KEYS[1]), expected); |
|
116 |
||
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
117 |
// EXTRA_KEY -> <absent> |
16867 | 118 |
assertFalse(map.containsKey(EXTRA_KEY)); |
119 |
assertSame(map.putIfAbsent(EXTRA_KEY, EXTRA_VALUE), null); |
|
120 |
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); |
|
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
121 |
assertSame(map.putIfAbsent(EXTRA_KEY, VALUES[2]), EXTRA_VALUE); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
122 |
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); |
16867 | 123 |
} |
13805
29e474ceeddc
7189926: Reduce test size for default run. Add additional run enabling alternative hashing.
mduigou
parents:
13025
diff
changeset
|
124 |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
125 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=all keys=all values=all") |
16867 | 126 |
public void testForEach(String description, Map<IntegerEnum, String> map) { |
127 |
IntegerEnum[] EACH_KEY = new IntegerEnum[map.size()]; |
|
128 |
||
129 |
map.forEach((k, v) -> { |
|
130 |
int idx = (null == k) ? 0 : k.ordinal(); // substitute for index. |
|
131 |
assertNull(EACH_KEY[idx]); |
|
132 |
EACH_KEY[idx] = (idx == 0) ? KEYS[0] : k; // substitute for comparison. |
|
133 |
assertSame(v, map.get(k)); |
|
134 |
}); |
|
135 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
136 |
assertEquals(KEYS, EACH_KEY, description); |
16867 | 137 |
} |
138 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
139 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
140 |
public static void testReplaceAll(String description, Map<IntegerEnum, String> map) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
141 |
IntegerEnum[] EACH_KEY = new IntegerEnum[map.size()]; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
142 |
Set<String> EACH_REPLACE = new HashSet<>(map.size()); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
143 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
144 |
map.replaceAll((k,v) -> { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
145 |
int idx = (null == k) ? 0 : k.ordinal(); // substitute for index. |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
146 |
assertNull(EACH_KEY[idx]); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
147 |
EACH_KEY[idx] = (idx == 0) ? KEYS[0] : k; // substitute for comparison. |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
148 |
assertSame(v, map.get(k)); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
149 |
String replacement = v + " replaced"; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
150 |
EACH_REPLACE.add(replacement); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
151 |
return replacement; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
152 |
}); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
153 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
154 |
assertEquals(KEYS, EACH_KEY, description); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
155 |
assertEquals(map.values().size(), EACH_REPLACE.size(), description + EACH_REPLACE); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
156 |
assertTrue(EACH_REPLACE.containsAll(map.values()), description + " : " + EACH_REPLACE + " != " + map.values()); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
157 |
assertTrue(map.values().containsAll(EACH_REPLACE), description + " : " + EACH_REPLACE + " != " + map.values()); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
158 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
159 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
160 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull") |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
161 |
public static void testReplaceAllNoNullReplacement(String description, Map<IntegerEnum, String> map) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
162 |
assertThrows( |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
163 |
() -> { map.replaceAll(null); }, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
164 |
NullPointerException.class, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
165 |
description); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
166 |
assertThrows( |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
167 |
() -> { map.replaceAll((k,v) -> null); }, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
168 |
NullPointerException.class, |
19855 | 169 |
description + " should not allow replacement with null value"); |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
170 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
171 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
172 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
16867 | 173 |
public static void testRemoveNulls(String description, Map<IntegerEnum, String> map) { |
174 |
assertTrue(map.containsKey(null), "null key absent"); |
|
175 |
assertNull(map.get(null), "value not null"); |
|
176 |
assertFalse(map.remove(null, EXTRA_VALUE), description); |
|
177 |
assertTrue(map.containsKey(null)); |
|
178 |
assertNull(map.get(null)); |
|
179 |
assertTrue(map.remove(null, null)); |
|
180 |
assertFalse(map.containsKey(null)); |
|
181 |
assertNull(map.get(null)); |
|
182 |
assertFalse(map.remove(null, null)); |
|
183 |
} |
|
12859 | 184 |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
185 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
16867 | 186 |
public static void testRemove(String description, Map<IntegerEnum, String> map) { |
187 |
assertTrue(map.containsKey(KEYS[1])); |
|
188 |
Object expected = map.get(KEYS[1]); |
|
189 |
assertTrue(null == expected || expected == VALUES[1]); |
|
190 |
assertFalse(map.remove(KEYS[1], EXTRA_VALUE), description); |
|
191 |
assertSame(map.get(KEYS[1]), expected); |
|
192 |
assertTrue(map.remove(KEYS[1], expected)); |
|
193 |
assertNull(map.get(KEYS[1])); |
|
194 |
assertFalse(map.remove(KEYS[1], expected)); |
|
195 |
||
196 |
assertFalse(map.containsKey(EXTRA_KEY)); |
|
197 |
assertFalse(map.remove(EXTRA_KEY, EXTRA_VALUE)); |
|
198 |
} |
|
199 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
200 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
16867 | 201 |
public void testReplaceKVNulls(String description, Map<IntegerEnum, String> map) { |
202 |
assertTrue(map.containsKey(null), "null key absent"); |
|
203 |
assertNull(map.get(null), "value not null"); |
|
204 |
assertSame(map.replace(null, EXTRA_VALUE), null); |
|
205 |
assertSame(map.get(null), EXTRA_VALUE); |
|
206 |
} |
|
207 |
||
19855 | 208 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull") |
209 |
public void testReplaceKVNoNulls(String description, Map<IntegerEnum, String> map) { |
|
210 |
assertTrue(map.containsKey(FIRST_KEY), "expected key missing"); |
|
211 |
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value"); |
|
212 |
assertThrows( () -> {map.replace(FIRST_KEY, null);}, NullPointerException.class, description + ": should throw NPE"); |
|
213 |
assertSame(map.replace(FIRST_KEY, EXTRA_VALUE), FIRST_VALUE, description + ": replaced wrong value"); |
|
214 |
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value"); |
|
215 |
} |
|
216 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
217 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
16867 | 218 |
public void testReplaceKV(String description, Map<IntegerEnum, String> map) { |
219 |
assertTrue(map.containsKey(KEYS[1])); |
|
220 |
Object expected = map.get(KEYS[1]); |
|
221 |
assertTrue(null == expected || expected == VALUES[1]); |
|
222 |
assertSame(map.replace(KEYS[1], EXTRA_VALUE), expected); |
|
223 |
assertSame(map.get(KEYS[1]), EXTRA_VALUE); |
|
224 |
||
225 |
assertFalse(map.containsKey(EXTRA_KEY)); |
|
226 |
assertNull(map.replace(EXTRA_KEY, EXTRA_VALUE)); |
|
227 |
assertFalse(map.containsKey(EXTRA_KEY)); |
|
228 |
assertNull(map.get(EXTRA_KEY)); |
|
229 |
assertNull(map.put(EXTRA_KEY, EXTRA_VALUE)); |
|
230 |
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); |
|
231 |
assertSame(map.replace(EXTRA_KEY, (String)expected), EXTRA_VALUE); |
|
232 |
assertSame(map.get(EXTRA_KEY), expected); |
|
12859 | 233 |
} |
234 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
235 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
16867 | 236 |
public void testReplaceKVVNulls(String description, Map<IntegerEnum, String> map) { |
237 |
assertTrue(map.containsKey(null), "null key absent"); |
|
238 |
assertNull(map.get(null), "value not null"); |
|
239 |
assertFalse(map.replace(null, EXTRA_VALUE, EXTRA_VALUE)); |
|
240 |
assertNull(map.get(null)); |
|
241 |
assertTrue(map.replace(null, null, EXTRA_VALUE)); |
|
242 |
assertSame(map.get(null), EXTRA_VALUE); |
|
243 |
assertTrue(map.replace(null, EXTRA_VALUE, EXTRA_VALUE)); |
|
244 |
assertSame(map.get(null), EXTRA_VALUE); |
|
245 |
} |
|
246 |
||
19855 | 247 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull") |
248 |
public void testReplaceKVVNoNulls(String description, Map<IntegerEnum, String> map) { |
|
249 |
assertTrue(map.containsKey(FIRST_KEY), "expected key missing"); |
|
250 |
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value"); |
|
251 |
assertThrows( () -> {map.replace(FIRST_KEY, FIRST_VALUE, null);}, NullPointerException.class, description + ": should throw NPE"); |
|
252 |
assertThrows( () -> {if (!map.replace(FIRST_KEY, null, EXTRA_VALUE)) throw new NullPointerException("default returns false rather than throwing");}, NullPointerException.class, description + ": should throw NPE"); |
|
253 |
assertTrue(map.replace(FIRST_KEY, FIRST_VALUE, EXTRA_VALUE), description + ": replaced wrong value"); |
|
254 |
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value"); |
|
255 |
} |
|
256 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
257 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
16867 | 258 |
public void testReplaceKVV(String description, Map<IntegerEnum, String> map) { |
259 |
assertTrue(map.containsKey(KEYS[1])); |
|
260 |
Object expected = map.get(KEYS[1]); |
|
261 |
assertTrue(null == expected || expected == VALUES[1]); |
|
262 |
assertFalse(map.replace(KEYS[1], EXTRA_VALUE, EXTRA_VALUE)); |
|
263 |
assertSame(map.get(KEYS[1]), expected); |
|
264 |
assertTrue(map.replace(KEYS[1], (String)expected, EXTRA_VALUE)); |
|
265 |
assertSame(map.get(KEYS[1]), EXTRA_VALUE); |
|
266 |
assertTrue(map.replace(KEYS[1], EXTRA_VALUE, EXTRA_VALUE)); |
|
267 |
assertSame(map.get(KEYS[1]), EXTRA_VALUE); |
|
12859 | 268 |
|
16867 | 269 |
assertFalse(map.containsKey(EXTRA_KEY)); |
270 |
assertFalse(map.replace(EXTRA_KEY, EXTRA_VALUE, EXTRA_VALUE)); |
|
271 |
assertFalse(map.containsKey(EXTRA_KEY)); |
|
272 |
assertNull(map.get(EXTRA_KEY)); |
|
273 |
assertNull(map.put(EXTRA_KEY, EXTRA_VALUE)); |
|
274 |
assertTrue(map.containsKey(EXTRA_KEY)); |
|
275 |
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); |
|
276 |
assertTrue(map.replace(EXTRA_KEY, EXTRA_VALUE, EXTRA_VALUE)); |
|
277 |
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); |
|
278 |
} |
|
279 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
280 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
16867 | 281 |
public void testComputeIfAbsentNulls(String description, Map<IntegerEnum, String> map) { |
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
282 |
// null -> null |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
283 |
assertTrue(map.containsKey(null), "null key absent"); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
284 |
assertNull(map.get(null), "value not null"); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
285 |
assertSame(map.computeIfAbsent(null, (k) -> null), null, "not expected result"); |
16867 | 286 |
assertTrue(map.containsKey(null), "null key absent"); |
287 |
assertNull(map.get(null), "value not null"); |
|
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
288 |
assertSame(map.computeIfAbsent(null, (k) -> EXTRA_VALUE), EXTRA_VALUE, "not mapped to result"); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
289 |
// null -> EXTRA_VALUE |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
290 |
assertTrue(map.containsKey(null), "null key absent"); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
291 |
assertSame(map.get(null), EXTRA_VALUE, "not expected value"); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
292 |
assertSame(map.remove(null), EXTRA_VALUE, "removed unexpected value"); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
293 |
// null -> <absent> |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
294 |
assertFalse(map.containsKey(null), "null key present"); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
295 |
assertSame(map.computeIfAbsent(null, (k) -> EXTRA_VALUE), EXTRA_VALUE, "not mapped to result"); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
296 |
// null -> EXTRA_VALUE |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
297 |
assertTrue(map.containsKey(null), "null key absent"); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
298 |
assertSame(map.get(null), EXTRA_VALUE, "not expected value"); |
16867 | 299 |
} |
300 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
301 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
16867 | 302 |
public void testComputeIfAbsent(String description, Map<IntegerEnum, String> map) { |
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
303 |
// 1 -> 1 |
16867 | 304 |
assertTrue(map.containsKey(KEYS[1])); |
305 |
Object expected = map.get(KEYS[1]); |
|
306 |
assertTrue(null == expected || expected == VALUES[1], description + String.valueOf(expected)); |
|
307 |
expected = (null == expected) ? EXTRA_VALUE : expected; |
|
308 |
assertSame(map.computeIfAbsent(KEYS[1], (k) -> EXTRA_VALUE), expected, description); |
|
309 |
assertSame(map.get(KEYS[1]), expected, description); |
|
310 |
||
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
311 |
// EXTRA_KEY -> <absent> |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
312 |
assertFalse(map.containsKey(EXTRA_KEY)); |
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
313 |
assertNull(map.computeIfAbsent(EXTRA_KEY, (k) -> null)); |
16867 | 314 |
assertFalse(map.containsKey(EXTRA_KEY)); |
315 |
assertSame(map.computeIfAbsent(EXTRA_KEY, (k) -> EXTRA_VALUE), EXTRA_VALUE); |
|
22054
503bc3781dfe
8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents:
21352
diff
changeset
|
316 |
// EXTRA_KEY -> EXTRA_VALUE |
16867 | 317 |
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); |
318 |
} |
|
12859 | 319 |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
320 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
321 |
public void testComputeIfAbsentNullFunction(String description, Map<IntegerEnum, String> map) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
322 |
assertThrows( () -> { map.computeIfAbsent(KEYS[1], null);}, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
323 |
NullPointerException.class, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
324 |
"Should throw NPE"); |
20189
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
325 |
} |
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
326 |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
327 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
16867 | 328 |
public void testComputeIfPresentNulls(String description, Map<IntegerEnum, String> map) { |
18532
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
329 |
assertTrue(map.containsKey(null), description + ": null key absent"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
330 |
assertNull(map.get(null), description + ": value not null"); |
16867 | 331 |
assertSame(map.computeIfPresent(null, (k, v) -> { |
18532
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
332 |
fail(description + ": null value is not deemed present"); |
16867 | 333 |
return EXTRA_VALUE; |
334 |
}), null, description); |
|
335 |
assertTrue(map.containsKey(null)); |
|
18532
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
336 |
assertNull(map.get(null), description); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
337 |
assertNull(map.remove(EXTRA_KEY), description + ": unexpected mapping"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
338 |
assertNull(map.put(EXTRA_KEY, null), description + ": unexpected value"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
339 |
assertSame(map.computeIfPresent(EXTRA_KEY, (k, v) -> { |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
340 |
fail(description + ": null value is not deemed present"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
341 |
return EXTRA_VALUE; |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
342 |
}), null, description); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
343 |
assertNull(map.get(EXTRA_KEY), description + ": null mapping gone"); |
16867 | 344 |
} |
345 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
346 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
16867 | 347 |
public void testComputeIfPresent(String description, Map<IntegerEnum, String> map) { |
348 |
assertTrue(map.containsKey(KEYS[1])); |
|
349 |
Object value = map.get(KEYS[1]); |
|
350 |
assertTrue(null == value || value == VALUES[1], description + String.valueOf(value)); |
|
351 |
Object expected = (null == value) ? null : EXTRA_VALUE; |
|
352 |
assertSame(map.computeIfPresent(KEYS[1], (k, v) -> { |
|
353 |
assertSame(v, value); |
|
354 |
return EXTRA_VALUE; |
|
355 |
}), expected, description); |
|
356 |
assertSame(map.get(KEYS[1]), expected, description); |
|
357 |
||
358 |
assertFalse(map.containsKey(EXTRA_KEY)); |
|
359 |
assertSame(map.computeIfPresent(EXTRA_KEY, (k, v) -> { |
|
360 |
fail(); |
|
361 |
return EXTRA_VALUE; |
|
362 |
}), null); |
|
363 |
assertFalse(map.containsKey(EXTRA_KEY)); |
|
364 |
assertSame(map.get(EXTRA_KEY), null); |
|
365 |
} |
|
366 |
||
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
367 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
368 |
public void testComputeIfPresentNullFunction(String description, Map<IntegerEnum, String> map) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
369 |
assertThrows( () -> { map.computeIfPresent(KEYS[1], null);}, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
370 |
NullPointerException.class, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
371 |
"Should throw NPE"); |
20189
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
372 |
} |
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
373 |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
374 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull") |
16867 | 375 |
public void testComputeNulls(String description, Map<IntegerEnum, String> map) { |
376 |
assertTrue(map.containsKey(null), "null key absent"); |
|
377 |
assertNull(map.get(null), "value not null"); |
|
378 |
assertSame(map.compute(null, (k, v) -> { |
|
18532
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
379 |
assertNull(k); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
380 |
assertNull(v); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
381 |
return null; |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
382 |
}), null, description); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
383 |
assertFalse(map.containsKey(null), description + ": null key present."); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
384 |
assertSame(map.compute(null, (k, v) -> { |
16867 | 385 |
assertSame(k, null); |
386 |
assertNull(v); |
|
387 |
return EXTRA_VALUE; |
|
388 |
}), EXTRA_VALUE, description); |
|
389 |
assertTrue(map.containsKey(null)); |
|
390 |
assertSame(map.get(null), EXTRA_VALUE, description); |
|
18532
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
391 |
assertSame(map.remove(null), EXTRA_VALUE, description + ": removed value not expected"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
392 |
// no mapping before and after |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
393 |
assertFalse(map.containsKey(null), description + ": null key present"); |
16867 | 394 |
assertSame(map.compute(null, (k, v) -> { |
18532
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
395 |
assertNull(k); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
396 |
assertNull(v); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
397 |
return null; |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
398 |
}), null, description + ": expected null result" ); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
399 |
assertFalse(map.containsKey(null), description + ": null key present"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
400 |
// compute with map not containing value |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
401 |
assertNull(map.remove(EXTRA_KEY), description + ": unexpected mapping"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
402 |
assertFalse(map.containsKey(EXTRA_KEY), description + ": key present"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
403 |
assertSame(map.compute(EXTRA_KEY, (k, v) -> { |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
404 |
assertSame(k, EXTRA_KEY); |
16867 | 405 |
assertNull(v); |
406 |
return null; |
|
407 |
}), null, description); |
|
18532
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
408 |
assertFalse(map.containsKey(EXTRA_KEY), description + ": null key present"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
409 |
// ensure removal. |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
410 |
assertNull(map.put(EXTRA_KEY, EXTRA_VALUE)); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
411 |
assertSame(map.compute(EXTRA_KEY, (k, v) -> { |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
412 |
assertSame(k, EXTRA_KEY); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
413 |
assertSame(v, EXTRA_VALUE); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
414 |
return null; |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
415 |
}), null, description + ": null resulted expected"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
416 |
assertFalse(map.containsKey(EXTRA_KEY), description + ": null key present"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
417 |
// compute with map containing null value |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
418 |
assertNull(map.put(EXTRA_KEY, null), description + ": unexpected value"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
419 |
assertSame(map.compute(EXTRA_KEY, (k, v) -> { |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
420 |
assertSame(k, EXTRA_KEY); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
421 |
assertNull(v); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
422 |
return null; |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
423 |
}), null, description); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
424 |
assertFalse(map.containsKey(EXTRA_KEY), description + ": null key present"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
425 |
assertNull(map.put(EXTRA_KEY, null), description + ": unexpected value"); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
426 |
assertSame(map.compute(EXTRA_KEY, (k, v) -> { |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
427 |
assertSame(k, EXTRA_KEY); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
428 |
assertNull(v); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
429 |
return EXTRA_VALUE; |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
430 |
}), EXTRA_VALUE, description); |
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
431 |
assertTrue(map.containsKey(EXTRA_KEY), "null key present"); |
16867 | 432 |
} |
433 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
434 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
16867 | 435 |
public void testCompute(String description, Map<IntegerEnum, String> map) { |
436 |
assertTrue(map.containsKey(KEYS[1])); |
|
437 |
Object value = map.get(KEYS[1]); |
|
438 |
assertTrue(null == value || value == VALUES[1], description + String.valueOf(value)); |
|
439 |
assertSame(map.compute(KEYS[1], (k, v) -> { |
|
440 |
assertSame(k, KEYS[1]); |
|
441 |
assertSame(v, value); |
|
442 |
return EXTRA_VALUE; |
|
443 |
}), EXTRA_VALUE, description); |
|
444 |
assertSame(map.get(KEYS[1]), EXTRA_VALUE, description); |
|
445 |
assertNull(map.compute(KEYS[1], (k, v) -> { |
|
446 |
assertSame(v, EXTRA_VALUE); |
|
447 |
return null; |
|
448 |
}), description); |
|
449 |
assertFalse(map.containsKey(KEYS[1])); |
|
450 |
||
451 |
assertFalse(map.containsKey(EXTRA_KEY)); |
|
452 |
assertSame(map.compute(EXTRA_KEY, (k, v) -> { |
|
453 |
assertNull(v); |
|
454 |
return EXTRA_VALUE; |
|
455 |
}), EXTRA_VALUE); |
|
456 |
assertTrue(map.containsKey(EXTRA_KEY)); |
|
457 |
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); |
|
12859 | 458 |
} |
459 |
||
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
460 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
461 |
public void testComputeNullFunction(String description, Map<IntegerEnum, String> map) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
462 |
assertThrows( () -> { map.compute(KEYS[1], null);}, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
463 |
NullPointerException.class, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
464 |
"Should throw NPE"); |
20189
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
465 |
} |
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
466 |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
467 |
@Test(dataProvider = "MergeCases") |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
468 |
private void testMerge(String description, Map<IntegerEnum, String> map, Merging.Value oldValue, Merging.Value newValue, Merging.Merger merger, Merging.Value put, Merging.Value result) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
469 |
// add and check initial conditions. |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
470 |
switch (oldValue) { |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
471 |
case ABSENT : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
472 |
map.remove(EXTRA_KEY); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
473 |
assertFalse(map.containsKey(EXTRA_KEY), "key not absent"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
474 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
475 |
case NULL : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
476 |
map.put(EXTRA_KEY, null); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
477 |
assertTrue(map.containsKey(EXTRA_KEY), "key absent"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
478 |
assertNull(map.get(EXTRA_KEY), "wrong value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
479 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
480 |
case OLDVALUE : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
481 |
map.put(EXTRA_KEY, VALUES[1]); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
482 |
assertTrue(map.containsKey(EXTRA_KEY), "key absent"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
483 |
assertSame(map.get(EXTRA_KEY), VALUES[1], "wrong value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
484 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
485 |
default: |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
486 |
fail("unexpected old value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
487 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
488 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
489 |
String returned = map.merge(EXTRA_KEY, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
490 |
newValue == Merging.Value.NULL ? (String) null : VALUES[2], |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
491 |
merger |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
492 |
); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
493 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
494 |
// check result |
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
495 |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
496 |
switch (result) { |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
497 |
case NULL : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
498 |
assertNull(returned, "wrong value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
499 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
500 |
case NEWVALUE : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
501 |
assertSame(returned, VALUES[2], "wrong value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
502 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
503 |
case RESULT : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
504 |
assertSame(returned, VALUES[3], "wrong value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
505 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
506 |
default: |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
507 |
fail("unexpected new value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
508 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
509 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
510 |
// check map |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
511 |
switch (put) { |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
512 |
case ABSENT : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
513 |
assertFalse(map.containsKey(EXTRA_KEY), "key not absent"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
514 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
515 |
case NULL : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
516 |
assertTrue(map.containsKey(EXTRA_KEY), "key absent"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
517 |
assertNull(map.get(EXTRA_KEY), "wrong value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
518 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
519 |
case NEWVALUE : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
520 |
assertTrue(map.containsKey(EXTRA_KEY), "key absent"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
521 |
assertSame(map.get(EXTRA_KEY), VALUES[2], "wrong value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
522 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
523 |
case RESULT : |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
524 |
assertTrue(map.containsKey(EXTRA_KEY), "key absent"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
525 |
assertSame(map.get(EXTRA_KEY), VALUES[3], "wrong value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
526 |
break; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
527 |
default: |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
528 |
fail("unexpected new value"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
529 |
} |
16867 | 530 |
} |
531 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
532 |
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all") |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
533 |
public void testMergeNullMerger(String description, Map<IntegerEnum, String> map) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
534 |
assertThrows( () -> { map.merge(KEYS[1], VALUES[1], null);}, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
535 |
NullPointerException.class, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
536 |
"Should throw NPE"); |
16867 | 537 |
} |
538 |
||
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
539 |
/** A function that flipflops between running two other functions. */ |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
540 |
static <T,U,V> BiFunction<T,U,V> twoStep(AtomicBoolean b, |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
541 |
BiFunction<T,U,V> first, |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
542 |
BiFunction<T,U,V> second) { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
543 |
return (t, u) -> { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
544 |
boolean bb = b.get(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
545 |
try { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
546 |
return (b.get() ? first : second).apply(t, u); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
547 |
} finally { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
548 |
b.set(!bb); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
549 |
}}; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
550 |
} |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
551 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
552 |
/** |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
553 |
* Simulates races by modifying the map within the mapping function. |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
554 |
*/ |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
555 |
@Test |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
556 |
public void testConcurrentMap_computeIfAbsent_racy() { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
557 |
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
558 |
final Long two = 2L; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
559 |
Function<Long,Long> f, g; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
560 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
561 |
// race not detected if function returns null |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
562 |
f = (k) -> { map.put(two, 42L); return null; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
563 |
assertNull(map.computeIfAbsent(two, f)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
564 |
assertEquals(42L, (long)map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
565 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
566 |
map.clear(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
567 |
f = (k) -> { map.put(two, 42L); return 86L; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
568 |
assertEquals(42L, (long)map.computeIfAbsent(two, f)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
569 |
assertEquals(42L, (long)map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
570 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
571 |
// mapping function ignored if value already exists |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
572 |
map.put(two, 99L); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
573 |
assertEquals(99L, (long)map.computeIfAbsent(two, f)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
574 |
assertEquals(99L, (long)map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
575 |
} |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
576 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
577 |
/** |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
578 |
* Simulates races by modifying the map within the remapping function. |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
579 |
*/ |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
580 |
@Test |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
581 |
public void testConcurrentMap_computeIfPresent_racy() { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
582 |
final AtomicBoolean b = new AtomicBoolean(true); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
583 |
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
584 |
final Long two = 2L; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
585 |
BiFunction<Long,Long,Long> f, g; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
586 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
587 |
for (Long val : new Long[] { null, 86L }) { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
588 |
map.clear(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
589 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
590 |
// Function not invoked if no mapping exists |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
591 |
f = (k, v) -> { map.put(two, 42L); return val; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
592 |
assertNull(map.computeIfPresent(two, f)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
593 |
assertNull(map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
594 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
595 |
map.put(two, 42L); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
596 |
f = (k, v) -> { map.put(two, 86L); return val; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
597 |
g = (k, v) -> { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
598 |
assertSame(two, k); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
599 |
assertEquals(86L, (long)v); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
600 |
return null; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
601 |
}; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
602 |
assertNull(map.computeIfPresent(two, twoStep(b, f, g))); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
603 |
assertFalse(map.containsKey(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
604 |
assertTrue(b.get()); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
605 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
606 |
map.put(two, 42L); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
607 |
f = (k, v) -> { map.put(two, 86L); return val; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
608 |
g = (k, v) -> { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
609 |
assertSame(two, k); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
610 |
assertEquals(86L, (long)v); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
611 |
return 99L; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
612 |
}; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
613 |
assertEquals(99L, (long)map.computeIfPresent(two, twoStep(b, f, g))); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
614 |
assertTrue(map.containsKey(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
615 |
assertTrue(b.get()); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
616 |
} |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
617 |
} |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
618 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
619 |
@Test |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
620 |
public void testConcurrentMap_compute_simple() { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
621 |
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
622 |
BiFunction<Long,Long,Long> fun = (k, v) -> ((v == null) ? 0L : k + v); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
623 |
assertEquals(Long.valueOf(0L), map.compute(3L, fun)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
624 |
assertEquals(Long.valueOf(3L), map.compute(3L, fun)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
625 |
assertEquals(Long.valueOf(6L), map.compute(3L, fun)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
626 |
assertNull(map.compute(3L, (k, v) -> null)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
627 |
assertTrue(map.isEmpty()); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
628 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
629 |
assertEquals(Long.valueOf(0L), map.compute(new Long(3L), fun)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
630 |
assertEquals(Long.valueOf(3L), map.compute(new Long(3L), fun)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
631 |
assertEquals(Long.valueOf(6L), map.compute(new Long(3L), fun)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
632 |
assertNull(map.compute(3L, (k, v) -> null)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
633 |
assertTrue(map.isEmpty()); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
634 |
} |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
635 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
636 |
/** |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
637 |
* Simulates races by modifying the map within the remapping function. |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
638 |
*/ |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
639 |
@Test |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
640 |
public void testConcurrentMap_compute_racy() { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
641 |
final AtomicBoolean b = new AtomicBoolean(true); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
642 |
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
643 |
final Long two = 2L; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
644 |
BiFunction<Long,Long,Long> f, g; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
645 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
646 |
// null -> null is a no-op; race not detected |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
647 |
f = (k, v) -> { map.put(two, 42L); return null; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
648 |
assertNull(map.compute(two, f)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
649 |
assertEquals(42L, (long)map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
650 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
651 |
for (Long val : new Long[] { null, 86L }) { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
652 |
map.clear(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
653 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
654 |
f = (k, v) -> { map.put(two, 42L); return 86L; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
655 |
g = (k, v) -> { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
656 |
assertSame(two, k); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
657 |
assertEquals(42L, (long)v); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
658 |
return k + v; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
659 |
}; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
660 |
assertEquals(44L, (long)map.compute(two, twoStep(b, f, g))); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
661 |
assertEquals(44L, (long)map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
662 |
assertTrue(b.get()); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
663 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
664 |
f = (k, v) -> { map.remove(two); return val; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
665 |
g = (k, v) -> { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
666 |
assertSame(two, k); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
667 |
assertNull(v); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
668 |
return 44L; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
669 |
}; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
670 |
assertEquals(44L, (long)map.compute(two, twoStep(b, f, g))); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
671 |
assertEquals(44L, (long)map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
672 |
assertTrue(map.containsKey(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
673 |
assertTrue(b.get()); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
674 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
675 |
f = (k, v) -> { map.remove(two); return val; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
676 |
g = (k, v) -> { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
677 |
assertSame(two, k); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
678 |
assertNull(v); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
679 |
return null; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
680 |
}; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
681 |
assertNull(map.compute(two, twoStep(b, f, g))); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
682 |
assertNull(map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
683 |
assertFalse(map.containsKey(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
684 |
assertTrue(b.get()); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
685 |
} |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
686 |
} |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
687 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
688 |
/** |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
689 |
* Simulates races by modifying the map within the remapping function. |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
690 |
*/ |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
691 |
@Test |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
692 |
public void testConcurrentMap_merge_racy() { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
693 |
final AtomicBoolean b = new AtomicBoolean(true); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
694 |
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
695 |
final Long two = 2L; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
696 |
BiFunction<Long,Long,Long> f, g; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
697 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
698 |
for (Long val : new Long[] { null, 86L }) { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
699 |
map.clear(); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
700 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
701 |
f = (v, w) -> { throw new AssertionError(); }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
702 |
assertEquals(99L, (long)map.merge(two, 99L, f)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
703 |
assertEquals(99L, (long)map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
704 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
705 |
f = (v, w) -> { map.put(two, 42L); return val; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
706 |
g = (v, w) -> { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
707 |
assertEquals(42L, (long)v); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
708 |
assertEquals(3L, (long)w); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
709 |
return v + w; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
710 |
}; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
711 |
assertEquals(45L, (long)map.merge(two, 3L, twoStep(b, f, g))); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
712 |
assertEquals(45L, (long)map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
713 |
assertTrue(b.get()); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
714 |
|
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
715 |
f = (v, w) -> { map.remove(two); return val; }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
716 |
g = (k, v) -> { throw new AssertionError(); }; |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
717 |
assertEquals(55L, (long)map.merge(two, 55L, twoStep(b, f, g))); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
718 |
assertEquals(55L, (long)map.get(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
719 |
assertTrue(map.containsKey(two)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
720 |
assertFalse(b.get()); b.set(true); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
721 |
} |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
722 |
} |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
723 |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
724 |
public enum IntegerEnum { |
16867 | 725 |
|
726 |
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, |
|
727 |
e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, |
|
728 |
e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, |
|
729 |
e30, e31, e32, e33, e34, e35, e36, e37, e38, e39, |
|
730 |
e40, e41, e42, e43, e44, e45, e46, e47, e48, e49, |
|
731 |
e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, |
|
732 |
e60, e61, e62, e63, e64, e65, e66, e67, e68, e69, |
|
733 |
e70, e71, e72, e73, e74, e75, e76, e77, e78, e79, |
|
734 |
e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, |
|
735 |
e90, e91, e92, e93, e94, e95, e96, e97, e98, e99, |
|
736 |
EXTRA_KEY; |
|
737 |
public static final int SIZE = values().length; |
|
738 |
}; |
|
739 |
private static final int TEST_SIZE = IntegerEnum.SIZE - 1; |
|
740 |
/** |
|
741 |
* Realized keys ensure that there is always a hard ref to all test objects. |
|
742 |
*/ |
|
743 |
private static final IntegerEnum[] KEYS = new IntegerEnum[TEST_SIZE]; |
|
744 |
/** |
|
745 |
* Realized values ensure that there is always a hard ref to all test |
|
746 |
* objects. |
|
747 |
*/ |
|
748 |
private static final String[] VALUES = new String[TEST_SIZE]; |
|
749 |
||
750 |
static { |
|
751 |
IntegerEnum[] keys = IntegerEnum.values(); |
|
16042
0bf6469a1cfb
8008785: IdentityHashMap.values().toArray(V[]) broken by JDK-8008167
mduigou
parents:
13817
diff
changeset
|
752 |
for (int each = 0; each < TEST_SIZE; each++) { |
16867 | 753 |
KEYS[each] = keys[each]; |
754 |
VALUES[each] = String.valueOf(each); |
|
12859 | 755 |
} |
16867 | 756 |
} |
19855 | 757 |
|
758 |
private static final IntegerEnum FIRST_KEY = KEYS[0]; |
|
759 |
private static final String FIRST_VALUE = VALUES[0]; |
|
16867 | 760 |
private static final IntegerEnum EXTRA_KEY = IntegerEnum.EXTRA_KEY; |
761 |
private static final String EXTRA_VALUE = String.valueOf(TEST_SIZE); |
|
12859 | 762 |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
763 |
@DataProvider(name = "Map<IntegerEnum,String> rw=all keys=all values=all", parallel = true) |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
764 |
public static Iterator<Object[]> allMapProvider() { |
16867 | 765 |
return makeAllMaps().iterator(); |
766 |
} |
|
767 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
768 |
@DataProvider(name = "Map<IntegerEnum,String> rw=all keys=withNull values=withNull", parallel = true) |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
769 |
public static Iterator<Object[]> allMapWithNullsProvider() { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
770 |
return makeAllMapsWithNulls().iterator(); |
16867 | 771 |
} |
772 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
773 |
@DataProvider(name = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull", parallel = true) |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
774 |
public static Iterator<Object[]> rwNonNullMapProvider() { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
775 |
return makeRWNoNullsMaps().iterator(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
776 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
777 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
778 |
@DataProvider(name = "Map<IntegerEnum,String> rw=true keys=nonNull values=all", parallel = true) |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
779 |
public static Iterator<Object[]> rwNonNullKeysMapProvider() { |
16867 | 780 |
return makeRWMapsNoNulls().iterator(); |
781 |
} |
|
782 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
783 |
@DataProvider(name = "Map<IntegerEnum,String> rw=true keys=all values=all", parallel = true) |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
784 |
public static Iterator<Object[]> rwMapProvider() { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
785 |
return makeAllRWMaps().iterator(); |
16867 | 786 |
} |
787 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
788 |
@DataProvider(name = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull", parallel = true) |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
789 |
public static Iterator<Object[]> rwNullsMapProvider() { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
790 |
return makeAllRWMapsWithNulls().iterator(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
791 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
792 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
793 |
private static Collection<Object[]> makeAllRWMapsWithNulls() { |
16867 | 794 |
Collection<Object[]> all = new ArrayList<>(); |
795 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
796 |
all.addAll(makeRWMaps(true, true)); |
16867 | 797 |
|
798 |
return all; |
|
799 |
} |
|
800 |
||
801 |
private static Collection<Object[]> makeRWMapsNoNulls() { |
|
802 |
Collection<Object[]> all = new ArrayList<>(); |
|
803 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
804 |
all.addAll(makeRWNoNullKeysMaps(false)); |
16867 | 805 |
all.addAll(makeRWNoNullsMaps()); |
806 |
||
807 |
return all; |
|
808 |
} |
|
809 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
810 |
private static Collection<Object[]> makeAllROMaps() { |
16867 | 811 |
Collection<Object[]> all = new ArrayList<>(); |
812 |
||
813 |
all.addAll(makeROMaps(false)); |
|
814 |
all.addAll(makeROMaps(true)); |
|
12859 | 815 |
|
16867 | 816 |
return all; |
817 |
} |
|
818 |
||
819 |
private static Collection<Object[]> makeAllRWMaps() { |
|
820 |
Collection<Object[]> all = new ArrayList<>(); |
|
821 |
||
822 |
all.addAll(makeRWNoNullsMaps()); |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
823 |
all.addAll(makeRWMaps(false,true)); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
824 |
all.addAll(makeRWMaps(true,true)); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
825 |
all.addAll(makeRWNoNullKeysMaps(true)); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
826 |
return all; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
827 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
828 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
829 |
private static Collection<Object[]> makeAllMaps() { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
830 |
Collection<Object[]> all = new ArrayList<>(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
831 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
832 |
all.addAll(makeAllROMaps()); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
833 |
all.addAll(makeAllRWMaps()); |
16867 | 834 |
|
835 |
return all; |
|
836 |
} |
|
837 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
838 |
private static Collection<Object[]> makeAllMapsWithNulls() { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
839 |
Collection<Object[]> all = new ArrayList<>(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
840 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
841 |
all.addAll(makeROMaps(true)); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
842 |
all.addAll(makeRWMaps(true,true)); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
843 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
844 |
return all; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
845 |
} |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
846 |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
847 |
/** |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
848 |
* @param nullKeys include null keys |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
849 |
* @param nullValues include null values |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
850 |
* @return |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
851 |
*/ |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
852 |
private static Collection<Object[]> makeRWMaps(boolean nullKeys, boolean nullValues) { |
16867 | 853 |
return Arrays.asList( |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
854 |
new Object[]{"HashMap", makeMap(HashMap::new, nullKeys, nullValues)}, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
855 |
new Object[]{"IdentityHashMap", makeMap(IdentityHashMap::new, nullKeys, nullValues)}, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
856 |
new Object[]{"LinkedHashMap", makeMap(LinkedHashMap::new, nullKeys, nullValues)}, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
857 |
new Object[]{"WeakHashMap", makeMap(WeakHashMap::new, nullKeys, nullValues)}, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
858 |
new Object[]{"Collections.checkedMap(HashMap)", Collections.checkedMap(makeMap(HashMap::new, nullKeys, nullValues), IntegerEnum.class, String.class)}, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
859 |
new Object[]{"Collections.synchronizedMap(HashMap)", Collections.synchronizedMap(makeMap(HashMap::new, nullKeys, nullValues))}, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
860 |
new Object[]{"ExtendsAbstractMap", makeMap(ExtendsAbstractMap::new, nullKeys, nullValues)}); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
861 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
862 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
863 |
/** |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
864 |
* @param nulls include null values |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
865 |
* @return |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
866 |
*/ |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
867 |
private static Collection<Object[]> makeRWNoNullKeysMaps(boolean nulls) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
868 |
return Arrays.asList( |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
869 |
// null key hostile |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
870 |
new Object[]{"EnumMap", makeMap(() -> new EnumMap(IntegerEnum.class), false, nulls)}, |
19855 | 871 |
new Object[]{"TreeMap", makeMap(TreeMap::new, false, nulls)}, |
872 |
new Object[]{"ExtendsAbstractMap(TreeMap)", makeMap(() -> {return new ExtendsAbstractMap(new TreeMap());}, false, nulls)}, |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
873 |
new Object[]{"Collections.synchronizedMap(EnumMap)", Collections.synchronizedMap(makeMap(() -> new EnumMap(IntegerEnum.class), false, nulls))} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
874 |
); |
16867 | 875 |
} |
876 |
||
877 |
private static Collection<Object[]> makeRWNoNullsMaps() { |
|
878 |
return Arrays.asList( |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
879 |
// null key and value hostile |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
880 |
new Object[]{"Hashtable", makeMap(Hashtable::new, false, false)}, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
881 |
new Object[]{"ConcurrentHashMap", makeMap(ConcurrentHashMap::new, false, false)}, |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
882 |
new Object[]{"ConcurrentSkipListMap", makeMap(ConcurrentSkipListMap::new, false, false)}, |
19855 | 883 |
new Object[]{"Collections.synchronizedMap(ConcurrentHashMap)", Collections.synchronizedMap(makeMap(ConcurrentHashMap::new, false, false))}, |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
884 |
new Object[]{"Collections.checkedMap(ConcurrentHashMap)", Collections.checkedMap(makeMap(ConcurrentHashMap::new, false, false), IntegerEnum.class, String.class)}, |
19855 | 885 |
new Object[]{"ExtendsAbstractMap(ConcurrentHashMap)", makeMap(() -> {return new ExtendsAbstractMap(new ConcurrentHashMap());}, false, false)}, |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
886 |
new Object[]{"ImplementsConcurrentMap", makeMap(ImplementsConcurrentMap::new, false, false)} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
887 |
); |
16867 | 888 |
} |
889 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
890 |
/** |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
891 |
* @param nulls include nulls |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
892 |
* @return |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
893 |
*/ |
16867 | 894 |
private static Collection<Object[]> makeROMaps(boolean nulls) { |
895 |
return Arrays.asList(new Object[][]{ |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
896 |
new Object[]{"Collections.unmodifiableMap(HashMap)", Collections.unmodifiableMap(makeMap(HashMap::new, nulls, nulls))} |
16867 | 897 |
}); |
898 |
} |
|
899 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
900 |
/** |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
901 |
* @param supplier a supplier of mutable map instances. |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
902 |
* |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
903 |
* @param nullKeys include null keys |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
904 |
* @param nullValues include null values |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
905 |
* @return |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
906 |
*/ |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
907 |
private static Map<IntegerEnum, String> makeMap(Supplier<Map<IntegerEnum, String>> supplier, boolean nullKeys, boolean nullValues) { |
16867 | 908 |
Map<IntegerEnum, String> result = supplier.get(); |
909 |
||
910 |
for (int each = 0; each < TEST_SIZE; each++) { |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
911 |
IntegerEnum key = nullKeys ? (each == 0) ? null : KEYS[each] : KEYS[each]; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
912 |
String value = nullValues ? (each == 0) ? null : VALUES[each] : VALUES[each]; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
913 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
914 |
result.put(key, value); |
12859 | 915 |
} |
916 |
||
16867 | 917 |
return result; |
918 |
} |
|
919 |
||
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
920 |
static class Merging { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
921 |
public enum Value { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
922 |
ABSENT, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
923 |
NULL, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
924 |
OLDVALUE, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
925 |
NEWVALUE, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
926 |
RESULT |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
927 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
928 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
929 |
public enum Merger implements BiFunction<String,String,String> { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
930 |
UNUSED { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
931 |
public String apply(String oldValue, String newValue) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
932 |
fail("should not be called"); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
933 |
return null; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
934 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
935 |
}, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
936 |
NULL { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
937 |
public String apply(String oldValue, String newValue) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
938 |
return null; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
939 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
940 |
}, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
941 |
RESULT { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
942 |
public String apply(String oldValue, String newValue) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
943 |
return VALUES[3]; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
944 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
945 |
}, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
946 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
947 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
948 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
949 |
@DataProvider(name = "MergeCases", parallel = true) |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
950 |
public Iterator<Object[]> mergeCasesProvider() { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
951 |
Collection<Object[]> cases = new ArrayList<>(); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
952 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
953 |
cases.addAll(makeMergeTestCases()); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
954 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
955 |
return cases.iterator(); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
956 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
957 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
958 |
static Collection<Object[]> makeMergeTestCases() { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
959 |
Collection<Object[]> cases = new ArrayList<>(); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
960 |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
961 |
for (Object[] mapParams : makeAllRWMaps() ) { |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
962 |
cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.ABSENT, Merging.Value.NEWVALUE, Merging.Merger.UNUSED, Merging.Value.NEWVALUE, Merging.Value.NEWVALUE }); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
963 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
964 |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
965 |
for (Object[] mapParams : makeAllRWMaps() ) { |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
966 |
cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.OLDVALUE, Merging.Value.NEWVALUE, Merging.Merger.NULL, Merging.Value.ABSENT, Merging.Value.NULL }); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
967 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
968 |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
969 |
for (Object[] mapParams : makeAllRWMaps() ) { |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
970 |
cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.OLDVALUE, Merging.Value.NEWVALUE, Merging.Merger.RESULT, Merging.Value.RESULT, Merging.Value.RESULT }); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
971 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
972 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
973 |
return cases; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
974 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
20189
diff
changeset
|
975 |
|
16867 | 976 |
public interface Thrower<T extends Throwable> { |
977 |
||
978 |
public void run() throws T; |
|
979 |
} |
|
12859 | 980 |
|
16867 | 981 |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) { |
982 |
assertThrows(thrower, throwable, null); |
|
983 |
} |
|
984 |
||
985 |
public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) { |
|
19855 | 986 |
Throwable thrown; |
16867 | 987 |
try { |
988 |
thrower.run(); |
|
19855 | 989 |
thrown = null; |
16867 | 990 |
} catch (Throwable caught) { |
19855 | 991 |
thrown = caught; |
12859 | 992 |
} |
993 |
||
19855 | 994 |
assertInstance(thrown, throwable, |
995 |
((null != message) ? message : "") + |
|
996 |
" Failed to throw " + throwable.getCanonicalName()); |
|
16867 | 997 |
} |
998 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
999 |
public static <T extends Throwable> void assertThrows(Class<T> throwable, String message, Thrower<T>... throwers) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
1000 |
for (Thrower<T> thrower : throwers) { |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
1001 |
assertThrows(thrower, throwable, message); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
1002 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
1003 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
1004 |
|
19855 | 1005 |
public static void assertInstance(Object actual, Class<?> expected) { |
16867 | 1006 |
assertInstance(expected.isInstance(actual), null); |
1007 |
} |
|
1008 |
||
19855 | 1009 |
public static void assertInstance(Object actual, Class<?> expected, String message) { |
16867 | 1010 |
assertTrue(expected.isInstance(actual), message); |
1011 |
} |
|
1012 |
||
1013 |
/** |
|
1014 |
* A simple mutable map implementation that provides only default |
|
1015 |
* implementations of all methods. ie. none of the Map interface default |
|
1016 |
* methods have overridden implementations. |
|
1017 |
* |
|
1018 |
* @param <K> Type of keys |
|
1019 |
* @param <V> Type of values |
|
1020 |
*/ |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
1021 |
public static class ExtendsAbstractMap<M extends Map<K,V>, K, V> extends AbstractMap<K,V> { |
16867 | 1022 |
|
1023 |
protected final M map; |
|
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
1024 |
|
16867 | 1025 |
public ExtendsAbstractMap() { this( (M) new HashMap<K,V>()); } |
1026 |
||
1027 |
protected ExtendsAbstractMap(M map) { this.map = map; } |
|
1028 |
||
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1029 |
@Override public Set<Map.Entry<K,V>> entrySet() { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
1030 |
return new AbstractSet<Map.Entry<K,V>>() { |
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1031 |
@Override public int size() { |
16867 | 1032 |
return map.size(); |
1033 |
} |
|
1034 |
||
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1035 |
@Override public Iterator<Map.Entry<K,V>> iterator() { |
16867 | 1036 |
final Iterator<Map.Entry<K,V>> source = map.entrySet().iterator(); |
1037 |
return new Iterator<Map.Entry<K,V>>() { |
|
1038 |
public boolean hasNext() { return source.hasNext(); } |
|
1039 |
public Map.Entry<K,V> next() { return source.next(); } |
|
1040 |
public void remove() { source.remove(); } |
|
1041 |
}; |
|
1042 |
} |
|
1043 |
||
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1044 |
@Override public boolean add(Map.Entry<K,V> e) { |
16867 | 1045 |
return map.entrySet().add(e); |
1046 |
} |
|
1047 |
}; |
|
1048 |
} |
|
1049 |
||
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1050 |
@Override public V put(K key, V value) { |
16867 | 1051 |
return map.put(key, value); |
13025
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
1052 |
} |
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
1053 |
} |
e070f58ad775
7175758: Improve unit test of Map iterators and Iterator.remove()
mduigou
parents:
12859
diff
changeset
|
1054 |
|
16867 | 1055 |
/** |
1056 |
* A simple mutable concurrent map implementation that provides only default |
|
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1057 |
* implementations of all methods, i.e. none of the ConcurrentMap interface |
16867 | 1058 |
* default methods have overridden implementations. |
1059 |
* |
|
1060 |
* @param <K> Type of keys |
|
1061 |
* @param <V> Type of values |
|
1062 |
*/ |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
22055
diff
changeset
|
1063 |
public static class ImplementsConcurrentMap<K,V> extends ExtendsAbstractMap<ConcurrentMap<K,V>, K, V> implements ConcurrentMap<K,V> { |
16867 | 1064 |
public ImplementsConcurrentMap() { super(new ConcurrentHashMap<K,V>()); } |
12859 | 1065 |
|
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1066 |
// ConcurrentMap reabstracts these methods. |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1067 |
// |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1068 |
// Unlike ConcurrentHashMap, we have zero tolerance for null values. |
12859 | 1069 |
|
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1070 |
@Override public V replace(K k, V v) { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1071 |
return map.replace(requireNonNull(k), requireNonNull(v)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1072 |
} |
12859 | 1073 |
|
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1074 |
@Override public boolean replace(K k, V v, V vv) { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1075 |
return map.replace(requireNonNull(k), |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1076 |
requireNonNull(v), |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1077 |
requireNonNull(vv)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1078 |
} |
12859 | 1079 |
|
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1080 |
@Override public boolean remove(Object k, Object v) { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1081 |
return map.remove(requireNonNull(k), requireNonNull(v)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1082 |
} |
12859 | 1083 |
|
35396
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1084 |
@Override public V putIfAbsent(K k, V v) { |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1085 |
return map.putIfAbsent(requireNonNull(k), requireNonNull(v)); |
c58a744d0bb4
8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents:
32991
diff
changeset
|
1086 |
} |
12859 | 1087 |
} |
1088 |
} |