COLLECTIONS-533 Added ArrayListValuedLinkedHashMap
This commit is contained in:
parent
e4eaa0e815
commit
bde0d12c51
|
@ -0,0 +1,155 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.collections4.multimap;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections4.MultiValuedMap;
|
||||
|
||||
/**
|
||||
* Implements a {@code ListValuedMap}, using a {@link LinkedHashMap} to provide data
|
||||
* storage and {@link ArrayList}s as value collections. This is the standard
|
||||
* implementation of a ListValuedMap.
|
||||
* <p>
|
||||
* <strong>Note that ArrayListValuedLinkedHashMap is not synchronized and is not
|
||||
* thread-safe.</strong> If you wish to use this map from multiple threads
|
||||
* concurrently, you must use appropriate synchronization. This class may throw
|
||||
* exceptions when accessed by concurrent threads without synchronization.
|
||||
* </p>
|
||||
*
|
||||
* @param <K> the type of the keys in this map
|
||||
* @param <V> the type of the values in this map
|
||||
* @since 4.5
|
||||
*/
|
||||
public class ArrayListValuedLinkedHashMap<K, V> extends AbstractListValuedMap<K, V>
|
||||
implements Serializable {
|
||||
|
||||
/** Serialization Version */
|
||||
private static final long serialVersionUID = 20241014L;
|
||||
|
||||
/**
|
||||
* The initial map capacity used when none specified in constructor.
|
||||
*/
|
||||
private static final int DEFAULT_INITIAL_MAP_CAPACITY = 16;
|
||||
|
||||
/**
|
||||
* The initial list capacity when using none specified in constructor.
|
||||
*/
|
||||
private static final int DEFAULT_INITIAL_LIST_CAPACITY = 3;
|
||||
|
||||
/**
|
||||
* The initial list capacity when creating a new value collection.
|
||||
*/
|
||||
private final int initialListCapacity;
|
||||
|
||||
/**
|
||||
* Creates an empty ArrayListValuedHashMap with the default initial
|
||||
* map capacity (16) and the default initial list capacity (3).
|
||||
*/
|
||||
public ArrayListValuedLinkedHashMap() {
|
||||
this(DEFAULT_INITIAL_MAP_CAPACITY, DEFAULT_INITIAL_LIST_CAPACITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an empty ArrayListValuedHashMap with the default initial
|
||||
* map capacity (16) and the specified initial list capacity.
|
||||
*
|
||||
* @param initialListCapacity the initial capacity used for value collections
|
||||
*/
|
||||
public ArrayListValuedLinkedHashMap(final int initialListCapacity) {
|
||||
this(DEFAULT_INITIAL_MAP_CAPACITY, initialListCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an empty ArrayListValuedHashMap with the specified initial
|
||||
* map and list capacities.
|
||||
*
|
||||
* @param initialMapCapacity the initial hashmap capacity
|
||||
* @param initialListCapacity the initial capacity used for value collections
|
||||
*/
|
||||
public ArrayListValuedLinkedHashMap(final int initialMapCapacity, final int initialListCapacity) {
|
||||
super(new LinkedHashMap<>(initialMapCapacity));
|
||||
this.initialListCapacity = initialListCapacity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an ArrayListValuedHashMap copying all the mappings of the given map.
|
||||
*
|
||||
* @param map a {@code Map} to copy into this map
|
||||
*/
|
||||
public ArrayListValuedLinkedHashMap(final Map<? extends K, ? extends V> map) {
|
||||
this(map.size(), DEFAULT_INITIAL_LIST_CAPACITY);
|
||||
super.putAll(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an ArrayListValuedHashMap copying all the mappings of the given map.
|
||||
*
|
||||
* @param map a {@code MultiValuedMap} to copy into this map
|
||||
*/
|
||||
public ArrayListValuedLinkedHashMap(final MultiValuedMap<? extends K, ? extends V> map) {
|
||||
this(map.size(), DEFAULT_INITIAL_LIST_CAPACITY);
|
||||
super.putAll(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ArrayList<V> createCollection() {
|
||||
return new ArrayList<>(initialListCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes an instance from an ObjectInputStream.
|
||||
*
|
||||
* @param in The source ObjectInputStream.
|
||||
* @throws IOException Any of the usual Input/Output related exceptions.
|
||||
* @throws ClassNotFoundException A class of a serialized object cannot be found.
|
||||
*/
|
||||
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||
in.defaultReadObject();
|
||||
setMap(new LinkedHashMap<>());
|
||||
doReadObject(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the capacity of all value collections to their current size.
|
||||
*/
|
||||
public void trimToSize() {
|
||||
for (final Collection<V> coll : getMap().values()) {
|
||||
final ArrayList<V> list = (ArrayList<V>) coll;
|
||||
list.trimToSize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes this object to an ObjectOutputStream.
|
||||
*
|
||||
* @param out the target ObjectOutputStream.
|
||||
* @throws IOException thrown when an I/O errors occur writing to the target stream.
|
||||
*/
|
||||
private void writeObject(final ObjectOutputStream out) throws IOException {
|
||||
out.defaultWriteObject();
|
||||
doWriteObject(out);
|
||||
}
|
||||
|
||||
}
|
|
@ -254,11 +254,19 @@ public class ArrayListValuedHashMapTest<K, V> extends AbstractMultiValuedMapTest
|
|||
assertEquals("Q", list3.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyConstructorWithMultiValuedMap() {
|
||||
final ListValuedMap<K, V> map = makeObject();
|
||||
map.put((K) "key", (V) "sleutel");
|
||||
final ListValuedMap<K, V> copy = new ArrayListValuedHashMap<>(map);
|
||||
assertEquals(map, copy);
|
||||
}
|
||||
|
||||
// public void testCreate() throws Exception {
|
||||
// writeExternalFormToDisk((java.io.Serializable) makeObject(),
|
||||
// "src/test/resources/data/test/ArrayListValuedHashMap.emptyCollection.version4.1.obj");
|
||||
// "src/test/resources/org/apache/commons/collections4/data/test/ArrayListValuedHashMap.emptyCollection.version4.1.obj");
|
||||
// writeExternalFormToDisk((java.io.Serializable) makeFullMap(),
|
||||
// "src/test/resources/data/test/ArrayListValuedHashMap.fullCollection.version4.1.obj");
|
||||
// "src/test/resources/org/apache/commons/collections4/data/test/ArrayListValuedHashMap.fullCollection.version4.1.obj");
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,295 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.collections4.multimap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections4.ListValuedMap;
|
||||
import org.apache.commons.collections4.MapIterator;
|
||||
import org.apache.commons.collections4.MultiValuedMap;
|
||||
import org.apache.commons.collections4.collection.AbstractCollectionTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link ArrayListValuedLinkedHashMap}.
|
||||
*/
|
||||
public class ArrayListValuedLinkedHashMapTest<K, V> extends AbstractMultiValuedMapTest<K, V> {
|
||||
|
||||
public ArrayListValuedLinkedHashMapTest() {
|
||||
super(ArrayListValuedLinkedHashMapTest.class.getSimpleName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getIterationBehaviour() {
|
||||
return AbstractCollectionTest.UNORDERED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListValuedMap<K, V> makeObject() {
|
||||
return new ArrayListValuedLinkedHashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCompatibilityVersion() {
|
||||
return "4.5"; // ArrayListValuedLinkedHashMap has been added in version 4.5
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayListValuedLinkedHashMap() {
|
||||
final ListValuedMap<K, V> listMap;
|
||||
final ListValuedMap<K, V> listMap1;
|
||||
final Map<K, V> map = new HashMap<>();
|
||||
final Map<K, V> map1 = new HashMap<>();
|
||||
map.put((K) "A", (V) "W");
|
||||
map.put((K) "B", (V) "X");
|
||||
map.put((K) "C", (V) "F");
|
||||
|
||||
listMap = new ArrayListValuedLinkedHashMap<>(map);
|
||||
assertEquals(1, listMap.get((K) "A").size());
|
||||
assertEquals(1, listMap.get((K) "B").size());
|
||||
assertEquals(1, listMap.get((K) "C").size());
|
||||
|
||||
listMap1 = new ArrayListValuedLinkedHashMap<>(map1);
|
||||
assertEquals("{}", listMap1.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Test
|
||||
public void testEqualsHashCodeContract() {
|
||||
final MultiValuedMap map1 = makeObject();
|
||||
final MultiValuedMap map2 = makeObject();
|
||||
|
||||
map1.put("a", "a1");
|
||||
map1.put("a", "a2");
|
||||
map2.put("a", "a1");
|
||||
map2.put("a", "a2");
|
||||
assertEquals(map1, map2);
|
||||
assertEquals(map1.hashCode(), map2.hashCode());
|
||||
|
||||
map2.put("a", "a2");
|
||||
assertNotSame(map1, map2);
|
||||
assertNotSame(map1.hashCode(), map2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testListValuedMapAdd() {
|
||||
final ListValuedMap<K, V> listMap = makeObject();
|
||||
assertTrue(listMap.get((K) "whatever") instanceof List);
|
||||
final List<V> list = listMap.get((K) "A");
|
||||
list.add((V) "a1");
|
||||
assertEquals(1, listMap.size());
|
||||
assertTrue(listMap.containsKey("A"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testListValuedMapAddViaListIterator() {
|
||||
final ListValuedMap<K, V> listMap = makeObject();
|
||||
final ListIterator<V> listIt = listMap.get((K) "B").listIterator();
|
||||
assertFalse(listIt.hasNext());
|
||||
listIt.add((V) "b1");
|
||||
listIt.add((V) "b2");
|
||||
listIt.add((V) "b3");
|
||||
assertEquals(3, listMap.size());
|
||||
assertTrue(listMap.containsKey("B"));
|
||||
// As ListIterator always adds before the current cursor
|
||||
assertFalse(listIt.hasNext());
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Test
|
||||
public void testListValuedMapEqualsHashCodeContract() {
|
||||
final ListValuedMap map1 = makeObject();
|
||||
final ListValuedMap map2 = makeObject();
|
||||
|
||||
map1.put("a", "a1");
|
||||
map1.put("a", "a2");
|
||||
map2.put("a", "a1");
|
||||
map2.put("a", "a2");
|
||||
assertEquals(map1, map2);
|
||||
assertEquals(map1.hashCode(), map2.hashCode());
|
||||
|
||||
map1.put("b", "b1");
|
||||
map1.put("b", "b2");
|
||||
map2.put("b", "b2");
|
||||
map2.put("b", "b1");
|
||||
assertNotSame(map1, map2);
|
||||
assertNotSame(map1.hashCode(), map2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testListValuedMapRemove() {
|
||||
final ListValuedMap<K, V> listMap = makeObject();
|
||||
final List<V> list = listMap.get((K) "A");
|
||||
list.add((V) "a1");
|
||||
list.add((V) "a2");
|
||||
list.add((V) "a3");
|
||||
assertEquals(3, listMap.size());
|
||||
assertEquals("a1", list.remove(0));
|
||||
assertEquals(2, listMap.size());
|
||||
assertEquals("a2", list.remove(0));
|
||||
assertEquals(1, listMap.size());
|
||||
assertEquals("a3", list.remove(0));
|
||||
assertEquals(0, listMap.size());
|
||||
assertFalse(listMap.containsKey("A"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testListValuedMapRemoveViaListIterator() {
|
||||
final ListValuedMap<K, V> listMap = makeObject();
|
||||
ListIterator<V> listIt = listMap.get((K) "B").listIterator();
|
||||
listIt.add((V) "b1");
|
||||
listIt.add((V) "b2");
|
||||
assertEquals(2, listMap.size());
|
||||
assertTrue(listMap.containsKey("B"));
|
||||
listIt = listMap.get((K) "B").listIterator();
|
||||
while (listIt.hasNext()) {
|
||||
listIt.next();
|
||||
listIt.remove();
|
||||
}
|
||||
assertFalse(listMap.containsKey("B"));
|
||||
listIt.add((V) "b1");
|
||||
listIt.add((V) "b2");
|
||||
assertTrue(listMap.containsKey("B"));
|
||||
assertEquals(2, listMap.get((K) "B").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrimToSize() {
|
||||
final ArrayListValuedLinkedHashMap<K, V> listMap = new ArrayListValuedLinkedHashMap<>(4);
|
||||
|
||||
assertEquals("{}", listMap.toString());
|
||||
listMap.put((K) "A", (V) "W");
|
||||
listMap.put((K) "A", (V) "X");
|
||||
listMap.put((K) "B", (V) "F");
|
||||
assertEquals(2, listMap.get((K) "A").size());
|
||||
assertEquals(1, listMap.get((K) "B").size());
|
||||
|
||||
listMap.trimToSize();
|
||||
assertEquals(2, listMap.get((K) "A").size());
|
||||
assertEquals(1, listMap.get((K) "B").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValuesListIteratorMethods() {
|
||||
final ListValuedMap<K, V> listMap = makeObject();
|
||||
final List<V> listA = listMap.get((K) "A");
|
||||
final List<V> list = Arrays.asList((V) "W", (V) "X", (V) "F", (V) "Q", (V) "Q", (V) "F");
|
||||
listA.addAll(0, list);
|
||||
final ListIterator<V> it = listMap.get((K) "A").listIterator(1);
|
||||
assertTrue(it.hasNext());
|
||||
assertEquals("X", it.next());
|
||||
assertEquals("F", it.next());
|
||||
assertTrue(it.hasPrevious());
|
||||
assertEquals("F", it.previous());
|
||||
assertEquals(2, it.nextIndex());
|
||||
assertEquals(1, it.previousIndex());
|
||||
it.set((V) "Z");
|
||||
assertEquals("Z", it.next());
|
||||
assertEquals("Q", it.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrappedListAdd() {
|
||||
final ListValuedMap<K, V> listMap = makeObject();
|
||||
final List<V> listA = listMap.get((K) "A");
|
||||
listA.add(0, (V) "W");
|
||||
listA.add(1, (V) "X");
|
||||
listA.add(2, (V) "F");
|
||||
assertEquals("{A=[W, X, F]}", listMap.toString());
|
||||
listMap.get((K) "A").set(1, (V) "Q");
|
||||
assertEquals("{A=[W, Q, F]}", listMap.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrappedListAddAll() {
|
||||
final ListValuedMap<K, V> listMap = makeObject();
|
||||
final List<V> listA = listMap.get((K) "A");
|
||||
final List<V> list = Arrays.asList((V) "W", (V) "X", (V) "F");
|
||||
listA.addAll(0, list);
|
||||
assertEquals("{A=[W, X, F]}", listMap.toString());
|
||||
|
||||
final List<V> list1 = Arrays.asList((V) "Q", (V) "Q", (V) "L");
|
||||
listA.addAll(3, list1);
|
||||
assertEquals("{A=[W, X, F, Q, Q, L]}", listMap.toString());
|
||||
assertEquals("W", listMap.get((K) "A").get(0));
|
||||
assertEquals("X", listMap.get((K) "A").get(1));
|
||||
assertEquals("F", listMap.get((K) "A").get(2));
|
||||
assertEquals("Q", listMap.get((K) "A").get(3));
|
||||
assertEquals("Q", listMap.get((K) "A").get(4));
|
||||
assertEquals("L", listMap.get((K) "A").get(5));
|
||||
assertEquals(0, listMap.get((K) "A").indexOf("W"));
|
||||
assertEquals(2, listMap.get((K) "A").indexOf("F"));
|
||||
assertEquals(-1, listMap.get((K) "A").indexOf("C"));
|
||||
assertEquals(3, listMap.get((K) "A").indexOf("Q"));
|
||||
assertEquals(4, listMap.get((K) "A").lastIndexOf("Q"));
|
||||
assertEquals(-1, listMap.get((K) "A").lastIndexOf("A"));
|
||||
|
||||
final List<V> list2 = new ArrayList<>();
|
||||
listMap.get((K) "B").addAll(0, list2);
|
||||
assertEquals("{A=[W, X, F, Q, Q, L]}", listMap.toString());
|
||||
final List<V> list3 = listMap.get((K) "A").subList(1, 4);
|
||||
assertEquals(3, list3.size());
|
||||
assertEquals("Q", list3.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreservesKeyInsertionOrder() {
|
||||
final ListValuedMap<K, V> map = makeObject();
|
||||
map.put((K) Integer.valueOf(5), (V) "five");
|
||||
map.put((K) Integer.valueOf(1), (V) "one");
|
||||
map.put((K) Integer.valueOf(5), (V) "vijf"); // "vijf" = "five" in Dutch
|
||||
MapIterator<K, V> mapIterator = map.mapIterator();
|
||||
assertEquals(5, mapIterator.next());
|
||||
assertEquals("five", mapIterator.getValue());
|
||||
assertEquals(5, mapIterator.next());
|
||||
assertEquals("vijf", mapIterator.getValue());
|
||||
assertEquals(1, mapIterator.next());
|
||||
assertEquals("one", mapIterator.getValue());
|
||||
assertFalse(mapIterator.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyConstructorWithMultiValuedMap() {
|
||||
final ListValuedMap<K, V> map = makeObject();
|
||||
map.put((K) "key", (V) "sleutel");
|
||||
final ListValuedMap<K, V> copy = new ArrayListValuedLinkedHashMap<>(map);
|
||||
assertEquals(map, copy);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testCreate() throws Exception {
|
||||
// writeExternalFormToDisk((java.io.Serializable) makeObject(),
|
||||
// "src/test/resources/org/apache/commons/collections4/data/test/ArrayListValuedLinkedHashMap.emptyCollection.version4.5.obj");
|
||||
// writeExternalFormToDisk((java.io.Serializable) makeFullMap(),
|
||||
// "src/test/resources/org/apache/commons/collections4/data/test/ArrayListValuedLinkedHashMap.fullCollection.version4.5.obj");
|
||||
// }
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue