OpenJDK / lambda / lambda / jdk
changeset 7738:c8d40b7e6de3
bug fixes and unit test for Map Defaults
author | mduigou |
---|---|
date | Wed, 20 Mar 2013 20:32:27 -0700 |
parents | 3d6ae2796f62 |
children | 07dfd7108c33 |
files | src/share/classes/java/util/HashMap.java src/share/classes/java/util/Map.java test/java/util/Map/Defaults.java |
diffstat | 3 files changed, 549 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/java/util/HashMap.java Mon Mar 25 18:54:34 2013 +0100 +++ b/src/share/classes/java/util/HashMap.java Wed Mar 20 20:32:27 2013 -0700 @@ -608,6 +608,9 @@ while (e != null) { Entry<K,V> next = e.next; if (e.hash == hash && Objects.equals(e.key, key)) { + if(!Objects.equals(e.value, value)) { + return false; + } modCount++; size--; if (prev == e) @@ -668,7 +671,7 @@ for(; e != null; e = e.next) { if (e.hash == hash && Objects.equals(e.key, key)) { V oldValue = e.value; - return oldValue == null ? mappingFunction.apply(key) : oldValue; + return oldValue == null ? (e.value = mappingFunction.apply(key)) : oldValue; } } @@ -731,7 +734,7 @@ if (e.hash == hash && Objects.equals(e.key, key)) { V oldValue = e.value; V newValue = remappingFunction.apply(key, oldValue); - if (oldValue != null) { + if (newValue != oldValue) { modCount++; if (newValue == null) { size--; @@ -740,8 +743,7 @@ else prev.next = next; e.recordRemoval(this); - } - else { + } else { e.value = newValue; e.recordAccess(this); } @@ -773,25 +775,20 @@ Entry<K,V> next = e.next; if (e.hash == hash && Objects.equals(e.key, key)) { V oldValue = e.value; - if (oldValue != null) { - V newValue = remappingFunction.apply(oldValue, value); - modCount++; - if (newValue == null) { - size--; - if (prev == e) - table[i] = next; - else - prev.next = next; - e.recordRemoval(this); - } - else { - e.value = newValue; - e.recordAccess(this); - } - return newValue; + V newValue = remappingFunction.apply(oldValue, value); + modCount++; + if (newValue == null) { + size--; + if (prev == e) + table[i] = next; + else + prev.next = next; + e.recordRemoval(this); + } else { + e.value = newValue; + e.recordAccess(this); } - else - return value; + return newValue; } prev = e; e = next;
--- a/src/share/classes/java/util/Map.java Mon Mar 25 18:54:34 2013 +0100 +++ b/src/share/classes/java/util/Map.java Wed Mar 20 20:32:27 2013 -0700 @@ -613,6 +613,7 @@ * if the implementation supports null values.) * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * @throws NullPointerException if the specified key or value is null, @@ -651,6 +652,7 @@ * @return <tt>true</tt> if the value was removed * @throws UnsupportedOperationException if the <tt>remove</tt> operation * is not supported by this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws ClassCastException if the key or value is of an inappropriate * type for this map * (<a href="Collection.html#optional-restrictions">optional</a>) @@ -693,6 +695,7 @@ * @return <tt>true</tt> if the value was replaced * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws ClassCastException if the class of a specified key or value * prevents it from being stored in this map * @throws NullPointerException if a specified key or value is null, @@ -737,8 +740,10 @@ * if the implementation supports null values.) * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values * @throws IllegalArgumentException if some property of the specified key @@ -784,7 +789,7 @@ * if (map.get(key) == null) { * V newValue = mappingFunction.apply(key); * if (newValue != null) - * map.putIfAbsent(key, newValue); + * map.put(key, newValue); * }}</pre> * * @param key key with which the specified value is to be associated @@ -796,15 +801,17 @@ * mappingFunction is null * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @since 1.8 */ default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { V v, newValue; return ((v = get(key)) == null && (newValue = mappingFunction.apply(key)) != null && - (v = putIfAbsent(key, newValue)) == null) ? newValue : v; + (v = put(key, newValue)) == null) ? newValue : v; } /** @@ -854,8 +861,10 @@ * remappingFunction is null * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @since 1.8 */ default V computeIfPresent(K key, @@ -923,8 +932,10 @@ * remappingFunction is null * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @since 1.8 */ default V compute(K key, @@ -944,7 +955,7 @@ } else { if (newValue != null) { - if ((oldValue = putIfAbsent(key, newValue)) == null) + if ((oldValue = put(key, newValue)) == null) return newValue; } else @@ -1004,6 +1015,12 @@ * @param value the value to use if absent * @param remappingFunction the function to recompute a value if present * @return the new value associated with the specified key, or null if none + * @throws UnsupportedOperationException if the <tt>put</tt> operation + * is not supported by this map + * (<a href="Collection.html#optional-restrictions">optional</a>) + * @throws ClassCastException if the class of the specified key or value + * prevents it from being stored in this map + * (<a href="Collection.html#optional-restrictions">optional</a>) * @throws NullPointerException if the specified key is null and * this map does not support null keys, or the * remappingFunction is null @@ -1026,7 +1043,7 @@ } else { if (value != null) { - if ((oldValue = putIfAbsent(key, value)) == null) + if ((oldValue = put(key, value)) == null) return value; } else
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/util/Map/Defaults.java Wed Mar 20 20:32:27 2013 -0700 @@ -0,0 +1,509 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8010122 + * @summary Test Map default methods + * @author Mike Duigou + * @run testng Defaults + */ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.TreeMap; +import java.util.WeakHashMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.function.Supplier; + +import org.testng.annotations.Test; +import org.testng.annotations.DataProvider; +import static org.testng.Assert.fail; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertSame; + +public class Defaults { + + @Test(dataProvider = "Nulls Map<IntegerEnum,String>") + public void testGetOrDefaultNulls(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(null), "null key absent"); + assertNull(map.get(null), "value not null"); + assertSame(map.get(null), map.getOrDefault(null, EXTRA_VALUE), "values should match"); + } + + @Test(dataProvider = "Map<IntegerEnum,String>") + public void testGetOrDefault(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(KEYS[1]), "expected key missing"); + assertSame(map.get(KEYS[1]), map.getOrDefault(KEYS[1], EXTRA_VALUE), "values should match"); + assertFalse(map.containsKey(EXTRA_KEY), "expected absent key"); + assertSame(map.getOrDefault(EXTRA_KEY, EXTRA_VALUE), EXTRA_VALUE, "value not returned as default"); + assertNull(map.getOrDefault(EXTRA_KEY, null), "null not returned as default"); + } + + @Test(dataProvider = "R/W Nulls Map<IntegerEnum,String>") + public void testPutIfAbsentNulls(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(null), "null key absent"); + assertNull(map.get(null), "value not null"); + assertNull(map.putIfAbsent(null, EXTRA_VALUE), "previous not null"); + assertNull(map.get(null), "value not null"); + assertNull(map.remove(null), "removed not null"); + assertFalse(map.containsKey(null), "key present after remove"); + assertNull(map.putIfAbsent(null, EXTRA_VALUE), "previous not null"); + assertSame(map.get(null), EXTRA_VALUE, "value not expected"); + } + + @Test(dataProvider = "R/W Map<IntegerEnum,String>") + public void testPutIfAbsent(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(KEYS[1])); + Object expected = map.get(KEYS[1]); + assertTrue(null == expected || expected == VALUES[1]); + assertSame(map.putIfAbsent(KEYS[1], EXTRA_VALUE), expected); + assertSame(map.get(KEYS[1]), expected); + + assertFalse(map.containsKey(EXTRA_KEY)); + assertSame(map.putIfAbsent(EXTRA_KEY, EXTRA_VALUE), null); + assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); + } + + @Test(dataProvider = "Nulls Map<IntegerEnum,String>") + public void testForEach(String description, Map<IntegerEnum, String> map) { + IntegerEnum[] EACH_KEY = new IntegerEnum[map.size()]; + + map.forEach((k, v) -> { + int idx = (null == k) ? 0 : k.ordinal(); // substitute for index. + assertNull(EACH_KEY[idx]); + EACH_KEY[idx] = (idx == 0) ? KEYS[0] : k; // substitute for comparison. + assertSame(v, map.get(k)); + }); + + assertEquals(KEYS, EACH_KEY); + } + + @Test(dataProvider = "R/W Nulls Map<IntegerEnum,String>") + public static void testRemoveNulls(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(null), "null key absent"); + assertNull(map.get(null), "value not null"); + assertFalse(map.remove(null, EXTRA_VALUE), description); + assertTrue(map.containsKey(null)); + assertNull(map.get(null)); + assertTrue(map.remove(null, null)); + assertFalse(map.containsKey(null)); + assertNull(map.get(null)); + assertFalse(map.remove(null, null)); + } + + @Test(dataProvider = "R/W Map<IntegerEnum,String>") + public static void testRemove(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(KEYS[1])); + Object expected = map.get(KEYS[1]); + assertTrue(null == expected || expected == VALUES[1]); + assertFalse(map.remove(KEYS[1], EXTRA_VALUE), description); + assertSame(map.get(KEYS[1]), expected); + assertTrue(map.remove(KEYS[1], expected)); + assertNull(map.get(KEYS[1])); + assertFalse(map.remove(KEYS[1], expected)); + + assertFalse(map.containsKey(EXTRA_KEY)); + assertFalse(map.remove(EXTRA_KEY, EXTRA_VALUE)); + } + + @Test(dataProvider = "R/W Nulls Map<IntegerEnum,String>") + public void testReplaceKVNulls(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(null), "null key absent"); + assertNull(map.get(null), "value not null"); + assertSame(map.replace(null, EXTRA_VALUE), null); + assertSame(map.get(null), EXTRA_VALUE); + } + + @Test(dataProvider = "R/W Map<IntegerEnum,String>") + public void testReplaceKV(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(KEYS[1])); + Object expected = map.get(KEYS[1]); + assertTrue(null == expected || expected == VALUES[1]); + assertSame(map.replace(KEYS[1], EXTRA_VALUE), expected); + assertSame(map.get(KEYS[1]), EXTRA_VALUE); + + assertFalse(map.containsKey(EXTRA_KEY)); + assertNull(map.replace(EXTRA_KEY, EXTRA_VALUE)); + assertFalse(map.containsKey(EXTRA_KEY)); + assertNull(map.get(EXTRA_KEY)); + assertNull(map.put(EXTRA_KEY, EXTRA_VALUE)); + assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); + assertSame(map.replace(EXTRA_KEY, (String)expected), EXTRA_VALUE); + assertSame(map.get(EXTRA_KEY), expected); + } + + @Test(dataProvider = "R/W Nulls Map<IntegerEnum,String>") + public void testReplaceKVVNulls(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(null), "null key absent"); + assertNull(map.get(null), "value not null"); + assertFalse(map.replace(null, EXTRA_VALUE, EXTRA_VALUE)); + assertNull(map.get(null)); + assertTrue(map.replace(null, null, EXTRA_VALUE)); + assertSame(map.get(null), EXTRA_VALUE); + assertTrue(map.replace(null, EXTRA_VALUE, EXTRA_VALUE)); + assertSame(map.get(null), EXTRA_VALUE); + } + + @Test(dataProvider = "R/W Map<IntegerEnum,String>") + public void testReplaceKVV(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(KEYS[1])); + Object expected = map.get(KEYS[1]); + assertTrue(null == expected || expected == VALUES[1]); + assertFalse(map.replace(KEYS[1], EXTRA_VALUE, EXTRA_VALUE)); + assertSame(map.get(KEYS[1]), expected); + assertTrue(map.replace(KEYS[1], (String)expected, EXTRA_VALUE)); + assertSame(map.get(KEYS[1]), EXTRA_VALUE); + assertTrue(map.replace(KEYS[1], EXTRA_VALUE, EXTRA_VALUE)); + assertSame(map.get(KEYS[1]), EXTRA_VALUE); + + assertFalse(map.containsKey(EXTRA_KEY)); + assertFalse(map.replace(EXTRA_KEY, EXTRA_VALUE, EXTRA_VALUE)); + assertFalse(map.containsKey(EXTRA_KEY)); + assertNull(map.get(EXTRA_KEY)); + assertNull(map.put(EXTRA_KEY, EXTRA_VALUE)); + assertTrue(map.containsKey(EXTRA_KEY)); + assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); + assertTrue(map.replace(EXTRA_KEY, EXTRA_VALUE, EXTRA_VALUE)); + assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); + } + + @Test(dataProvider = "R/W Nulls Map<IntegerEnum,String>") + public void testComputeIfAbsentNulls(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(null), "null key absent"); + assertNull(map.get(null), "value not null"); + assertSame(map.computeIfAbsent(null, (k) -> EXTRA_VALUE), EXTRA_VALUE, description); + assertSame(map.get(null), EXTRA_VALUE, description); + } + + @Test(dataProvider = "R/W Map<IntegerEnum,String>") + public void testComputeIfAbsent(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(KEYS[1])); + Object expected = map.get(KEYS[1]); + assertTrue(null == expected || expected == VALUES[1], description + String.valueOf(expected)); + expected = (null == expected) ? EXTRA_VALUE : expected; + assertSame(map.computeIfAbsent(KEYS[1], (k) -> EXTRA_VALUE), expected, description); + assertSame(map.get(KEYS[1]), expected, description); + + assertFalse(map.containsKey(EXTRA_KEY)); + assertSame(map.computeIfAbsent(EXTRA_KEY, (k) -> EXTRA_VALUE), EXTRA_VALUE); + assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); + } + + @Test(dataProvider = "R/W Nulls Map<IntegerEnum,String>") + public void testComputeIfPresentNulls(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(null)); + assertNull(map.get(null)); + assertSame(map.computeIfPresent(null, (k, v) -> { + fail(); + return EXTRA_VALUE; + }), null, description); + assertTrue(map.containsKey(null)); + assertSame(map.get(null), null, description); + } + + @Test(dataProvider = "R/W Map<IntegerEnum,String>") + public void testComputeIfPresent(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(KEYS[1])); + Object value = map.get(KEYS[1]); + assertTrue(null == value || value == VALUES[1], description + String.valueOf(value)); + Object expected = (null == value) ? null : EXTRA_VALUE; + assertSame(map.computeIfPresent(KEYS[1], (k, v) -> { + assertSame(v, value); + return EXTRA_VALUE; + }), expected, description); + assertSame(map.get(KEYS[1]), expected, description); + + assertFalse(map.containsKey(EXTRA_KEY)); + assertSame(map.computeIfPresent(EXTRA_KEY, (k, v) -> { + fail(); + return EXTRA_VALUE; + }), null); + assertFalse(map.containsKey(EXTRA_KEY)); + assertSame(map.get(EXTRA_KEY), null); + } + + @Test(dataProvider = "R/W Nulls Map<IntegerEnum,String>") + public void testComputeNulls(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(null), "null key absent"); + assertNull(map.get(null), "value not null"); + assertSame(map.compute(null, (k, v) -> { + assertSame(k, null); + assertNull(v); + return EXTRA_VALUE; + }), EXTRA_VALUE, description); + assertTrue(map.containsKey(null)); + assertSame(map.get(null), EXTRA_VALUE, description); + } + + @Test(dataProvider = "R/W Map<IntegerEnum,String>") + public void testCompute(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(KEYS[1])); + Object value = map.get(KEYS[1]); + assertTrue(null == value || value == VALUES[1], description + String.valueOf(value)); + assertSame(map.compute(KEYS[1], (k, v) -> { + assertSame(k, KEYS[1]); + assertSame(v, value); + return EXTRA_VALUE; + }), EXTRA_VALUE, description); + assertSame(map.get(KEYS[1]), EXTRA_VALUE, description); + assertNull(map.compute(KEYS[1], (k, v) -> { + assertSame(v, EXTRA_VALUE); + return null; + }), description); + assertFalse(map.containsKey(KEYS[1])); + + assertFalse(map.containsKey(EXTRA_KEY)); + assertSame(map.compute(EXTRA_KEY, (k, v) -> { + assertNull(v); + return EXTRA_VALUE; + }), EXTRA_VALUE); + assertTrue(map.containsKey(EXTRA_KEY)); + assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); + } + + + @Test(dataProvider = "R/W Nulls Map<IntegerEnum,String>") + public void testMergeNulls(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(null), "null key absent"); + assertNull(map.get(null), "value not null"); + assertSame(map.merge(null, EXTRA_VALUE, (v, vv) -> { + assertNull(v); + assertSame(vv, EXTRA_VALUE); + return vv; + }), EXTRA_VALUE, description); + assertTrue(map.containsKey(null)); + assertSame(map.get(null), EXTRA_VALUE, description); + } + + @Test(dataProvider = "R/W Map<IntegerEnum,String>") + public void testMerge(String description, Map<IntegerEnum, String> map) { + assertTrue(map.containsKey(KEYS[1])); + Object value = map.get(KEYS[1]); + assertTrue(null == value || value == VALUES[1], description + String.valueOf(value)); + assertSame(map.merge(KEYS[1], EXTRA_VALUE, (v, vv) -> { + assertSame(v, value); + assertSame(vv, EXTRA_VALUE); + return vv; + }), EXTRA_VALUE, description); + assertSame(map.get(KEYS[1]), EXTRA_VALUE, description); + assertNull(map.merge(KEYS[1], EXTRA_VALUE, (v, vv) -> { + assertSame(v, EXTRA_VALUE); + assertSame(vv, EXTRA_VALUE); + return null; + }), description); + assertFalse(map.containsKey(KEYS[1])); + + assertFalse(map.containsKey(EXTRA_KEY)); + assertSame(map.merge(EXTRA_KEY, EXTRA_VALUE, (v, vv) -> { + assertNull(v); + assertSame(vv, EXTRA_VALUE); + return EXTRA_VALUE; + }), EXTRA_VALUE); + assertTrue(map.containsKey(EXTRA_KEY)); + assertSame(map.get(EXTRA_KEY), EXTRA_VALUE); + } + + enum IntegerEnum { + + e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, + e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, + e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, + e30, e31, e32, e33, e34, e35, e36, e37, e38, e39, + e40, e41, e42, e43, e44, e45, e46, e47, e48, e49, + e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, + e60, e61, e62, e63, e64, e65, e66, e67, e68, e69, + e70, e71, e72, e73, e74, e75, e76, e77, e78, e79, + e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, + e90, e91, e92, e93, e94, e95, e96, e97, e98, e99, + EXTRA_KEY; + public static final int SIZE = values().length; + }; + private static final int TEST_SIZE = IntegerEnum.SIZE - 1; + /** + * Realized keys ensure that there is always a hard ref to all test objects. + */ + private static final IntegerEnum[] KEYS = new IntegerEnum[TEST_SIZE]; + /** + * Realized values ensure that there is always a hard ref to all test + * objects. + */ + private static final String[] VALUES = new String[TEST_SIZE]; + + static { + IntegerEnum[] keys = IntegerEnum.values(); + for (int each = 0; each < TEST_SIZE; each++) { + KEYS[each] = keys[each]; + VALUES[each] = String.valueOf(each); + } + } + private static final IntegerEnum EXTRA_KEY = IntegerEnum.EXTRA_KEY; + private static final String EXTRA_VALUE = String.valueOf(TEST_SIZE); + + @DataProvider(name = "Map<IntegerEnum,String>", parallel = true) + public static Iterator<Object[]> allNullsMapProvider() { + return makeAllMaps().iterator(); + } + + @DataProvider(name = "Nulls Map<IntegerEnum,String>", parallel = true) + public static Iterator<Object[]> allMapProvider() { + return makeRWMaps(true).iterator(); + } + + @DataProvider(name = "R/W Map<IntegerEnum,String>", parallel = true) + public static Iterator<Object[]> rwMapProvider() { + return makeRWMapsNoNulls().iterator(); + } + + @DataProvider(name = "R/W Nulls Map<IntegerEnum,String>", parallel = true) + public static Iterator<Object[]> rwNullsMapProvider() { + return makeRWMaps(true).iterator(); + } + + private static Collection<Object[]> makeAllMapsNoNulls() { + Collection<Object[]> all = new ArrayList<>(); + + all.addAll(makeRWMaps(false)); + all.addAll(makeRWNoNullsMaps()); + all.addAll(makeROMaps(false)); + + return all; + } + + private static Collection<Object[]> makeRWMapsNoNulls() { + Collection<Object[]> all = new ArrayList<>(); + + all.addAll(makeRWMaps(false)); + all.addAll(makeRWNoNullsMaps()); + + return all; + } + + private static Collection<Object[]> makeAllMaps() { + Collection<Object[]> all = new ArrayList<>(); + + all.addAll(makeROMaps(false)); + all.addAll(makeRWMaps(false)); + all.addAll(makeRWNoNullsMaps()); + all.addAll(makeRWMaps(true)); + all.addAll(makeROMaps(true)); + + return all; + } + + private static Collection<Object[]> makeAllRWMaps() { + Collection<Object[]> all = new ArrayList<>(); + + all.addAll(makeRWMaps(false)); + all.addAll(makeRWNoNullsMaps()); + all.addAll(makeRWMaps(true)); + + return all; + } + + private static Collection<Object[]> makeRWMaps(boolean nulls) { + return Arrays.asList( + new Object[]{"HashMap", makeMap(HashMap::new, nulls)}, + new Object[]{"IdentityHashMap", makeMap(IdentityHashMap::new, nulls)}, + new Object[]{"LinkedHashMap", makeMap(LinkedHashMap::new, nulls)}, + new Object[]{"WeakHashMap", makeMap(WeakHashMap::new, nulls)}, + new Object[]{"Collections.checkedMap(HashMap)", Collections.checkedMap(makeMap(HashMap::new, nulls), IntegerEnum.class, String.class)}, + new Object[]{"Collections.synchronizedMap(HashMap)", Collections.synchronizedMap(makeMap(HashMap::new, nulls))}); + } + + private static Collection<Object[]> makeRWNoNullsMaps() { + return Arrays.asList( + // null hostile + new Object[]{"EnumMap", makeMap(() -> new EnumMap(IntegerEnum.class), false)}, + new Object[]{"Hashtable", makeMap(Hashtable::new, false)}, + new Object[]{"TreeMap", makeMap(TreeMap::new, false)}, + new Object[]{"ConcurrentHashMap", makeMap(ConcurrentHashMap::new, false)}, + new Object[]{"ConcurrentSkipListMap", makeMap(ConcurrentSkipListMap::new, false)}, + new Object[]{"Collections.checkedMap(ConcurrentHashMap)", Collections.checkedMap(makeMap(ConcurrentHashMap::new, false), IntegerEnum.class, String.class)}, + new Object[]{"Collections.synchronizedMap(EnumMap)", Collections.synchronizedMap(makeMap(() -> new EnumMap(IntegerEnum.class), false))}); + } + + private static Collection<Object[]> makeROMaps(boolean nulls) { + return Arrays.asList(new Object[][]{ + new Object[]{"Collections.unmodifiableMap(HashMap)", Collections.unmodifiableMap(makeMap(HashMap::new, nulls))} + }); + } + + private static Map<IntegerEnum, String> makeMap(Supplier<Map<IntegerEnum, String>> supplier, boolean nulls) { + Map<IntegerEnum, String> result = supplier.get(); + + for (int each = 0; each < TEST_SIZE; each++) { + if (nulls) { + result.put((each == 0) ? null : KEYS[each], null); + } else { + result.put(KEYS[each], VALUES[each]); + } + } + + return result; + } + + public interface Thrower<T extends Throwable> { + + public void run() throws T; + } + + public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) { + assertThrows(thrower, throwable, null); + } + + public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) { + Throwable result; + try { + thrower.run(); + result = null; + } catch (Throwable caught) { + result = caught; + } + + assertInstance(result, throwable, + (null != message) + ? message + : "Failed to throw " + throwable.getCanonicalName()); + } + + public static <T> void assertInstance(T actual, Class<? extends T> expected) { + assertInstance(expected.isInstance(actual), null); + } + + public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) { + assertTrue(expected.isInstance(actual), message); + } +}