Author: dlaha Date: Fri Oct 17 11:27:23 2014 New Revision: 1632539 URL: http://svn.apache.org/r1632539 Log: [COLLECTIONS-533] Changing SetValuedLinkedHashMap's default collection to LinkedHashSet to preserve insertion order & some more tests
Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMap.java commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMapTest.java Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMap.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMap.java?rev=1632539&r1=1632538&r2=1632539&view=diff ============================================================================== --- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMap.java (original) +++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMap.java Fri Oct 17 11:27:23 2014 @@ -16,13 +16,7 @@ */ package org.apache.commons.collections4.multimap; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import org.apache.commons.collections4.ListValuedMap; import org.apache.commons.collections4.MultiValuedMap; @@ -96,7 +90,8 @@ public class MultiValuedLinkedHashMap<K, /** * Creates a {@link SetValuedMap} with a {@link LinkedHashMap} as its internal - * storage + * storage. This <code>SetValuedMap</code> implementation uses {@link LinkedHashSet} as its + * underlying <code>Collection</code> to preserve the item insertion order * * @param <K> the key type * @param <V> the value type @@ -104,7 +99,7 @@ public class MultiValuedLinkedHashMap<K, */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static <K, V> SetValuedMap<K, V> setValuedLinkedHashMap() { - return new SetValuedLinkedHashMap(HashSet.class); + return new SetValuedLinkedHashMap(LinkedHashSet.class); } /** Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMapTest.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMapTest.java?rev=1632539&r1=1632538&r2=1632539&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMapTest.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/multimap/MultiValuedLinkedHashMapTest.java Fri Oct 17 11:27:23 2014 @@ -109,6 +109,21 @@ public class MultiValuedLinkedHashMapTes assertFalse(setMap.containsKey("A")); } + public void testSetValuedMapIterationOrder() { + SetValuedMap<K, V> setMap = MultiValuedLinkedHashMap.setValuedLinkedHashMap(); + addSampleMappings(setMap); + + MapIterator<K, V> mapIt = setMap.mapIterator(); + Iterator keyIt = Arrays.asList(getSampleKeys()).iterator(); + Iterator valueIt = Arrays.asList(getSampleValues()).iterator(); + + while(mapIt.hasNext()) { + mapIt.next(); + assertEquals(mapIt.getKey(), keyIt.next()); + assertEquals(mapIt.getValue(), valueIt.next()); + } + } + @SuppressWarnings("unchecked") public void testListValuedMapAdd() { final ListValuedMap<K, V> listMap = MultiValuedLinkedHashMap.listValuedLinkedHashMap(); @@ -170,6 +185,21 @@ public class MultiValuedLinkedHashMapTes assertEquals(2, listMap.get("B").size()); } + public void testListValuedMapIterationOrder() { + ListValuedMap<K, V> listMap = MultiValuedLinkedHashMap.listValuedLinkedHashMap(); + addSampleMappings(listMap); + + MapIterator<K, V> mapIt = listMap.mapIterator(); + Iterator keyIt = Arrays.asList(getSampleKeys()).iterator(); + Iterator valueIt = Arrays.asList(getSampleValues()).iterator(); + + while(mapIt.hasNext()) { + mapIt.next(); + assertEquals(mapIt.getKey(), keyIt.next()); + assertEquals(mapIt.getValue(), valueIt.next()); + } + } + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testEqualsHashCodeContract() { MultiValuedMap map1 = new MultiValuedLinkedHashMap();