Author: tn
Date: Sat Jun 13 18:27:11 2015
New Revision: 1685299
URL: http://svn.apache.org/r1685299
Log:
Continue review of multimap package.
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/ListValuedMap.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiMapUtils.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiValuedMap.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetValuedMap.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractListValuedMap.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapDecorator.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractSetValuedMap.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedHashMap.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/TransformedMultiValuedMap.java
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/UnmodifiableMultiValuedMap.java
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/multimap/MultiValuedHashMapTest.java
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/multimap/TransformedMultiValuedMapTest.java
commons/proper/collections/trunk/src/test/resources/data/test/MultiValuedHashMap.emptyCollection.version4.1.obj
commons/proper/collections/trunk/src/test/resources/data/test/MultiValuedHashMap.fullCollection.version4.1.obj
commons/proper/collections/trunk/src/test/resources/data/test/TransformedMultiValuedMap.emptyCollection.version4.1.obj
commons/proper/collections/trunk/src/test/resources/data/test/TransformedMultiValuedMap.fullCollection.version4.1.obj
commons/proper/collections/trunk/src/test/resources/data/test/UnmodifiableMultiValuedMap.emptyCollection.version4.1.obj
commons/proper/collections/trunk/src/test/resources/data/test/UnmodifiableMultiValuedMap.fullCollection.version4.1.obj
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/ListValuedMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/ListValuedMap.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/ListValuedMap.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/ListValuedMap.java
Sat Jun 13 18:27:11 2015
@@ -43,11 +43,10 @@ public interface ListValuedMap<K, V> ext
* @param key the key to retrieve
* @return the {@code List} of values, implementations should return an
* empty {@code List} for no mapping
- * @throws ClassCastException if the key is of an invalid type
* @throws NullPointerException if the key is null and null keys are
invalid
*/
@Override
- List<V> get(Object key);
+ List<V> get(K key);
/**
* Removes all values associated with the specified key.
@@ -60,7 +59,6 @@ public interface ListValuedMap<K, V> ext
* @return the {@code List} of values removed, implementations
* typically return an empty, unmodifiable {@code List} for no mapping
found
* @throws UnsupportedOperationException if the map is unmodifiable
- * @throws ClassCastException if the key is of an invalid type
* @throws NullPointerException if the key is null and null keys are
invalid
*/
@Override
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiMapUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiMapUtils.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiMapUtils.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiMapUtils.java
Sat Jun 13 18:27:11 2015
@@ -18,6 +18,7 @@ package org.apache.commons.collections4;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -57,8 +58,8 @@ public class MultiMapUtils {
/**
* Returns immutable EMPTY_MULTI_VALUED_MAP with generic type safety.
*
- * @param <K> the type of key in the map
- * @param <V> the type of value in the map
+ * @param <K> the type of key in the map
+ * @param <V> the type of value in the map
* @return immutable and empty <code>MultiValuedMap</code>
*/
@SuppressWarnings("unchecked")
@@ -72,8 +73,8 @@ public class MultiMapUtils {
* Returns an immutable empty <code>MultiValuedMap</code> if the argument
is
* <code>null</code>, or the argument itself otherwise.
*
- * @param <K> the type of key in the map
- * @param <V> the type of value in the map
+ * @param <K> the type of key in the map
+ * @param <V> the type of value in the map
* @param map the map, may be null
* @return an empty {@link MultiValuedMap} if the argument is null
*/
@@ -100,8 +101,8 @@ public class MultiMapUtils {
/**
* Gets a Collection from <code>MultiValuedMap</code> in a null-safe
manner.
*
- * @param <K> the key type
- * @param <V> the value type
+ * @param <K> the key type
+ * @param <V> the value type
* @param map the {@link MultiValuedMap} to use
* @param key the key to look up
* @return the Collection in the {@link MultiValuedMap}, or null if input
map is null
@@ -110,7 +111,7 @@ public class MultiMapUtils {
if (map != null) {
return map.get(key);
}
- return null;
+ return Collections.emptyList();
}
// TODO: review the getValuesAsXXX methods - depending on the actual
MultiValuedMap type, changes
@@ -119,8 +120,8 @@ public class MultiMapUtils {
/**
* Gets a List from <code>MultiValuedMap</code> in a null-safe manner.
*
- * @param <K> the key type
- * @param <V> the value type
+ * @param <K> the key type
+ * @param <V> the value type
* @param map the {@link MultiValuedMap} to use
* @param key the key to look up
* @return the Collection in the {@link MultiValuedMap} as List, or null
if input map is null
@@ -139,8 +140,8 @@ public class MultiMapUtils {
/**
* Gets a Set from <code>MultiValuedMap</code> in a null-safe manner.
*
- * @param <K> the key type
- * @param <V> the value type
+ * @param <K> the key type
+ * @param <V> the value type
* @param map the {@link MultiValuedMap} to use
* @param key the key to look up
* @return the Collection in the {@link MultiValuedMap} as Set, or null if
input map is null
@@ -159,8 +160,8 @@ public class MultiMapUtils {
/**
* Gets a Bag from <code>MultiValuedMap</code> in a null-safe manner.
*
- * @param <K> the key type
- * @param <V> the value type
+ * @param <K> the key type
+ * @param <V> the value type
* @param map the {@link MultiValuedMap} to use
* @param key the key to look up
* @return the Collection in the {@link MultiValuedMap} as Bag, or null if
input map is null
@@ -183,8 +184,8 @@ public class MultiMapUtils {
* Creates a {@link ListValuedMap} with an {@link java.util.ArrayList
ArrayList} as
* collection class to store the values mapped to a key.
*
- * @param <K> the key type
- * @param <V> the value type
+ * @param <K> the key type
+ * @param <V> the value type
* @return a new <code>ListValuedMap</code>
*/
public static <K, V> ListValuedMap<K, V> newListValuedHashMap() {
@@ -195,9 +196,9 @@ public class MultiMapUtils {
* Creates a {@link ListValuedMap} with a {@link java.util.HashMap
HashMap} as its internal
* storage which maps the keys to list of type <code>listClass</code>.
*
- * @param <K> the key type
- * @param <V> the value type
- * @param <C> the List class type
+ * @param <K> the key type
+ * @param <V> the value type
+ * @param <C> the List class type
* @param listClass the class of the list
* @return a new {@link ListValuedMap}
*/
@@ -209,8 +210,8 @@ public class MultiMapUtils {
* Creates a {@link SetValuedMap} with an {@link java.util.HashSet
HashSet} as
* collection class to store the values mapped to a key.
*
- * @param <K> the key type
- * @param <V> the value type
+ * @param <K> the key type
+ * @param <V> the value type
* @return a new {@link SetValuedMap}
*/
public static <K, V> SetValuedMap<K, V> newSetValuedHashMap() {
@@ -221,9 +222,9 @@ public class MultiMapUtils {
* Creates a {@link SetValuedMap} with a {@link java.util.HashMap HashMap}
as its internal
* storage which maps the keys to a set of type <code>setClass</code>
*
- * @param <K> the key type
- * @param <V> the value type
- * @param <C> the Set class type
+ * @param <K> the key type
+ * @param <V> the value type
+ * @param <C> the Set class type
* @param setClass the class of the set
* @return a new {@link SetValuedMap}
*/
@@ -238,11 +239,11 @@ public class MultiMapUtils {
* Returns an <code>UnmodifiableMultiValuedMap</code> backed by the given
* map.
*
- * @param <K> the key type
- * @param <V> the value type
+ * @param <K> the key type
+ * @param <V> the value type
* @param map the {@link MultiValuedMap} to decorate, must not be null
* @return an unmodifiable {@link MultiValuedMap} backed by the provided
map
- * @throws IllegalArgumentException if map is null
+ * @throws NullPointerException if map is null
*/
public static <K, V> MultiValuedMap<K, V> unmodifiableMultiValuedMap(
final MultiValuedMap<? extends K, ? extends V> map) {
@@ -264,13 +265,13 @@ public class MultiMapUtils {
* If there are any elements already in the map being decorated, they are
* NOT transformed.
*
- * @param <K> the key type
- * @param <V> the value type
- * @param map the {@link MultiValuedMap} to transform, must not be null,
typically empty
- * @param keyTransformer the transformer for the map keys, null means no
transformation
- * @param valueTransformer the transformer for the map values, null means
no transformation
+ * @param <K> the key type
+ * @param <V> the value type
+ * @param map the {@link MultiValuedMap} to transform, must not be null,
typically empty
+ * @param keyTransformer the transformer for the map keys, null means no
transformation
+ * @param valueTransformer the transformer for the map values, null means
no transformation
* @return a transformed <code>MultiValuedMap</code> backed by the given
map
- * @throws IllegalArgumentException if map is null
+ * @throws NullPointerException if map is null
*/
public static <K, V> MultiValuedMap<K, V> transformedMultiValuedMap(final
MultiValuedMap<K, V> map,
final Transformer<? super K, ? extends K> keyTransformer,
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiValuedMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiValuedMap.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiValuedMap.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MultiValuedMap.java
Sat Jun 13 18:27:11 2015
@@ -73,7 +73,6 @@ public interface MultiValuedMap<K, V> {
*
* @param key key whose presence in this map is to be tested
* @return true if this map contains a mapping for the specified key
- * @throws ClassCastException if the key is of an inappropriate type for
this map (optional)
* @throws NullPointerException if the specified key is null and this map
* does not permit null keys (optional)
*/
@@ -84,8 +83,6 @@ public interface MultiValuedMap<K, V> {
*
* @param value the value to search for
* @return true if the map contains the value
- * @throws ClassCastException if the type of the specified value is not
compatible
- * with the used collection (optional)
* @throws NullPointerException if the value is null and null values are
not supported
* by the used collection types (optional)
*/
@@ -110,10 +107,9 @@ public interface MultiValuedMap<K, V> {
* @param key the key to retrieve
* @return the {@code Collection} of values, implementations should
* return an empty collection for no mapping
- * @throws ClassCastException if the key is of an invalid type (optional)
* @throws NullPointerException if the key is null and null keys are
invalid (optional)
*/
- Collection<V> get(Object key);
+ Collection<V> get(K key);
// Modification operations
@@ -135,7 +131,6 @@ public interface MultiValuedMap<K, V> {
* type does not allow duplicate values, e.g. when using a Set
* @throws UnsupportedOperationException if the put operation is not
supported by
* this multi-valued map, e.g. if it is unmodifiable
- * @throws ClassCastException if the key or value is of an invalid type
(optional)
* @throws NullPointerException if the key or value is null and null is
invalid (optional)
* @throws IllegalArgumentException if some aspect of the specified key or
value prevents
* it from being stored in this multi-valued map
@@ -168,15 +163,13 @@ public interface MultiValuedMap<K, V> {
* @param map mappings to be stored in this map, may not be null
* @throws UnsupportedOperationException if the {@code putAll} operation is
* not supported by this map
- * @throws ClassCastException if the class of a key or value in the
- * specified map prevents it from being stored in this map (optional)
* @throws NullPointerException if the specified map is null, or if this
map
* does not permit null keys or values, and the specified map
* contains null keys or values (optional)
* @throws IllegalArgumentException if some property of a key or value in
* the specified map prevents it from being stored in this map
*/
- void putAll(Map<? extends K, ? extends V> map);
+ boolean putAll(Map<? extends K, ? extends V> map);
/**
* Copies all mappings from the specified map to this multi-valued map
@@ -192,15 +185,13 @@ public interface MultiValuedMap<K, V> {
* @param map mappings to be stored in this map, may not be null
* @throws UnsupportedOperationException if the {@code putAll} operation is
* not supported by this map
- * @throws ClassCastException if the class of a key or value in the
- * specified map prevents it from being stored in this map (optional)
* @throws NullPointerException if the specified map is null, or if this
map
* does not permit null keys or values, and the specified map
* contains null keys or values (optional)
* @throws IllegalArgumentException if some property of a key or value in
* the specified map prevents it from being stored in this map
*/
- void putAll(MultiValuedMap<? extends K, ? extends V> map);
+ boolean putAll(MultiValuedMap<? extends K, ? extends V> map);
/**
* Removes all values associated with the specified key.
@@ -212,7 +203,6 @@ public interface MultiValuedMap<K, V> {
* @param key the key to remove values from
* @return the values that were removed
* @throws UnsupportedOperationException if the map is unmodifiable
- * @throws ClassCastException if the key is of an invalid type (optional)
* @throws NullPointerException if the key is null and null keys are
invalid (optional)
*/
Collection<V> remove(Object key);
@@ -230,10 +220,9 @@ public interface MultiValuedMap<K, V> {
* @param item the item to remove
* @return true if the mapping was removed, false otherwise
* @throws UnsupportedOperationException if the map is unmodifiable
- * @throws ClassCastException if the key or value is of an invalid type
(optional)
* @throws NullPointerException if the key or value is null and null is
invalid (optional)
*/
- boolean removeMapping(K key, V item);
+ boolean removeMapping(Object key, Object item);
/**
* Removes all of the mappings from this map (optional operation).
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetValuedMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetValuedMap.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetValuedMap.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetValuedMap.java
Sat Jun 13 18:27:11 2015
@@ -39,14 +39,13 @@ public interface SetValuedMap<K, V> exte
* have been mapped to the key.
* <p>
*
- * @param key the key to retrieve
+ * @param key the key to retrieve
* @return the {@code Set} of values, implementations should return an
* empty {@code Set} for no mapping
- * @throws ClassCastException if the key is of an invalid type
* @throws NullPointerException if the key is null and null keys are
invalid
*/
@Override
- Set<V> get(Object key);
+ Set<V> get(K key);
/**
* Removes all values associated with the specified key.
@@ -55,11 +54,10 @@ public interface SetValuedMap<K, V> exte
* propagated to this set-valued map. In case no mapping was stored for the
* specified key, an empty, unmodifiable set will be returned.
*
- * @param key the key to remove values from
+ * @param key the key to remove values from
* @return the {@code Set} of values removed, implementations should
* return null for no mapping found, but may return an empty collection
* @throws UnsupportedOperationException if the map is unmodifiable
- * @throws ClassCastException if the key is of an invalid type
* @throws NullPointerException if the key is null and null keys are
invalid
*/
@Override
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractListValuedMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractListValuedMap.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractListValuedMap.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractListValuedMap.java
Sat Jun 13 18:27:11 2015
@@ -18,11 +18,9 @@ package org.apache.commons.collections4.
import java.util.Collection;
import java.util.Collections;
-import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
-import java.util.Map.Entry;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.collections4.ListValuedMap;
@@ -41,14 +39,14 @@ public abstract class AbstractListValued
implements ListValuedMap<K, V> {
/** The serialization version */
- private static final long serialVersionUID = 6024950625989666915L;
+ private static final long serialVersionUID = 20150612L;
/**
* A constructor that wraps, not copies
*
* @param <C> the list type
- * @param map the map to wrap, must not be null
- * @param listClazz the collection class
+ * @param map the map to wrap, must not be null
+ * @param listClazz the collection class
* @throws NullPointerException if the map is null
*/
protected <C extends List<V>> AbstractListValuedMap(final Map<K, ? super
C> map, Class<C> listClazz) {
@@ -59,11 +57,11 @@ public abstract class AbstractListValued
* A constructor that wraps, not copies
*
* @param <C> the list type
- * @param map the map to wrap, must not be null
- * @param listClazz the collection class
- * @param initialListCapacity the initial size of the values list
- * @throws NullPointerException if the map is null
- * @throws IllegalArgumentException if initialListCapacity is negative
+ * @param map the map to wrap, must not be null
+ * @param listClazz the collection class
+ * @param initialListCapacity the initial size of the values list
+ * @throws NullPointerException if the map is null
+ * @throws IllegalArgumentException if initialListCapacity is negative
*/
protected <C extends List<V>> AbstractListValuedMap(final Map<K, ? super
C> map, Class<C> listClazz,
final int
initialListCapacity) {
@@ -71,15 +69,23 @@ public abstract class AbstractListValued
}
/**
+ * Creates a new value collection using the provided factory.
+ * @return a new list
+ */
+ @Override
+ protected List<V> createCollection() {
+ return (List<V>) super.createCollection();
+ }
+
+ /**
* Gets the list of values associated with the specified key. This would
* return an empty list in case the mapping is not present
*
- * @param key the key to retrieve
- * @return the <code>List</code> of values, will return an empty {@link
List} for no mapping
- * @throws ClassCastException if the key is of an invalid type
+ * @param key the key to retrieve
+ * @return the {@code List} of values, will return an empty {@link List}
for no mapping
*/
@Override
- public List<V> get(Object key) {
+ public List<V> get(final K key) {
return new WrappedList(key);
}
@@ -88,10 +94,9 @@ public abstract class AbstractListValued
* <p>
* A subsequent <code>get(Object)</code> would return an empty list.
*
- * @param key the key to remove values from
+ * @param key the key to remove values from
* @return the <code>List</code> of values removed, will return an empty,
- * unmodifiable list for no mapping found.
- * @throws ClassCastException if the key is of an invalid type
+ * unmodifiable list for no mapping found.
*/
@Override
public List<V> remove(Object key) {
@@ -103,42 +108,15 @@ public abstract class AbstractListValued
if (this == obj) {
return true;
}
- if (obj == null) {
- return false;
- }
- if (obj instanceof ListValuedMap == false) {
- return false;
- }
- ListValuedMap<?, ?> other = (ListValuedMap<?, ?>) obj;
- if (other.size() != size()) {
- return false;
- }
- Iterator<?> it = keySet().iterator();
- while (it.hasNext()) {
- Object key = it.next();
- List<?> list = get(key);
- List<?> otherList = other.get(key);
- if (otherList == null) {
- return false;
- }
- if (ListUtils.isEqualList(list, otherList) == false) {
- return false;
- }
+ if (obj instanceof ListValuedMap) {
+ return asMap().equals(((ListValuedMap<?, ?>) obj).asMap());
}
- return true;
+ return false;
}
@Override
public int hashCode() {
- int h = 0;
- Iterator<Entry<K, Collection<V>>> it = getMap().entrySet().iterator();
- while (it.hasNext()) {
- Entry<K, Collection<V>> entry = it.next();
- K key = entry.getKey();
- List<V> valueList = (List<V>) entry.getValue();
- h += (key == null ? 0 : key.hashCode()) ^
ListUtils.hashCodeForList(valueList);
- }
- return h;
+ return asMap().hashCode();
}
/**
@@ -146,60 +124,70 @@ public abstract class AbstractListValued
*/
private class WrappedList extends WrappedCollection implements List<V> {
- public WrappedList(Object key) {
+ public WrappedList(final K key) {
super(key);
}
- @SuppressWarnings("unchecked")
+ @Override
+ protected List<V> getMapping() {
+ return (List<V>) getMap().get(key);
+ }
+
+ @Override
public void add(int index, V value) {
- List<V> list = (List<V>) getMapping();
+ List<V> list = getMapping();
if (list == null) {
- list = (List<V>) AbstractListValuedMap.this.createCollection();
- list.add(index, value);
- getMap().put((K) key, list);
+ list = createCollection();
+ getMap().put(key, list);
}
list.add(index, value);
}
- @SuppressWarnings("unchecked")
+ @Override
public boolean addAll(int index, Collection<? extends V> c) {
- List<V> list = (List<V>) getMapping();
+ List<V> list = getMapping();
if (list == null) {
- list = (List<V>) createCollection();
- boolean result = list.addAll(index, c);
- if (result) {
- getMap().put((K) key, list);
+ list = createCollection();
+ boolean changed = list.addAll(index, c);
+ if (changed) {
+ getMap().put(key, list);
}
- return result;
+ return changed;
}
return list.addAll(index, c);
}
+ @Override
public V get(int index) {
- final List<V> list = ListUtils.emptyIfNull((List<V>) getMapping());
+ final List<V> list = ListUtils.emptyIfNull(getMapping());
return list.get(index);
}
+ @Override
public int indexOf(Object o) {
- final List<V> list = ListUtils.emptyIfNull((List<V>) getMapping());
+ final List<V> list = ListUtils.emptyIfNull(getMapping());
return list.indexOf(o);
}
+ @Override
public int lastIndexOf(Object o) {
- final List<V> list = ListUtils.emptyIfNull((List<V>) getMapping());
- return list.indexOf(o);
+ final List<V> list = ListUtils.emptyIfNull(getMapping());
+ return list.lastIndexOf(o);
}
+ @Override
public ListIterator<V> listIterator() {
return new ValuesListIterator(key);
}
+ @Override
public ListIterator<V> listIterator(int index) {
return new ValuesListIterator(key, index);
}
+ @Override
public V remove(int index) {
- final List<V> list = ListUtils.emptyIfNull((List<V>) getMapping());
+ final List<V> list = ListUtils.emptyIfNull(getMapping());
V value = list.remove(index);
if (list.isEmpty()) {
AbstractListValuedMap.this.remove(key);
@@ -207,101 +195,100 @@ public abstract class AbstractListValued
return value;
}
+ @Override
public V set(int index, V value) {
- final List<V> list = ListUtils.emptyIfNull((List<V>) getMapping());
+ final List<V> list = ListUtils.emptyIfNull(getMapping());
return list.set(index, value);
}
+ @Override
public List<V> subList(int fromIndex, int toIndex) {
- final List<V> list = ListUtils.emptyIfNull((List<V>) getMapping());
+ final List<V> list = ListUtils.emptyIfNull(getMapping());
return list.subList(fromIndex, toIndex);
}
@Override
public boolean equals(Object other) {
- final List<V> list = (List<V>) getMapping();
+ final List<V> list = getMapping();
if (list == null) {
return Collections.emptyList().equals(other);
}
- if (other == null) {
- return false;
- }
if (!(other instanceof List)) {
return false;
}
List<?> otherList = (List<?>) other;
- if (ListUtils.isEqualList(list, otherList) == false) {
- return false;
- }
- return true;
+ return ListUtils.isEqualList(list, otherList);
}
@Override
public int hashCode() {
- final List<V> list = (List<V>) getMapping();
- if (list == null) {
- return Collections.emptyList().hashCode();
- }
+ final List<V> list = getMapping();
return ListUtils.hashCodeForList(list);
}
}
- /** Values ListItrerator */
- private class ValuesListIterator implements ListIterator<V>{
-
- private final Object key;
+ /** Values ListIterator */
+ private class ValuesListIterator implements ListIterator<V> {
+ private final K key;
private List<V> values;
private ListIterator<V> iterator;
- public ValuesListIterator(Object key){
+ public ValuesListIterator(final K key) {
this.key = key;
this.values = ListUtils.emptyIfNull((List<V>) getMap().get(key));
this.iterator = values.listIterator();
}
- public ValuesListIterator(Object key, int index){
+ public ValuesListIterator(final K key, int index) {
this.key = key;
this.values = ListUtils.emptyIfNull((List<V>) getMap().get(key));
this.iterator = values.listIterator(index);
}
- @SuppressWarnings("unchecked")
+ @Override
public void add(V value) {
if (getMap().get(key) == null) {
- List<V> list = (List<V>) createCollection();
- getMap().put((K) key, list);
+ List<V> list = createCollection();
+ getMap().put(key, list);
this.values = list;
this.iterator = list.listIterator();
}
- this.iterator.add(value);
+ this.iterator.add(value);
}
+ @Override
public boolean hasNext() {
return iterator.hasNext();
}
+ @Override
public boolean hasPrevious() {
return iterator.hasPrevious();
}
+ @Override
public V next() {
return iterator.next();
}
+ @Override
public int nextIndex() {
return iterator.nextIndex();
}
+ @Override
public V previous() {
return iterator.previous();
}
+ @Override
public int previousIndex() {
return iterator.previousIndex();
}
+ @Override
public void remove() {
iterator.remove();
if (values.isEmpty()) {
@@ -309,6 +296,7 @@ public abstract class AbstractListValued
}
}
+ @Override
public void set(V value) {
iterator.set(value);
}
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java
Sat Jun 13 18:27:11 2015
@@ -50,10 +50,10 @@ import org.apache.commons.collections4.s
* @since 4.1
* @version $Id$
*/
-public class AbstractMultiValuedMap<K, V> implements MultiValuedMap<K, V>,
Serializable {
+public abstract class AbstractMultiValuedMap<K, V> implements
MultiValuedMap<K, V>, Serializable {
/** Serialization Version */
- private static final long serialVersionUID = 7994988366330224277L;
+ private static final long serialVersionUID = 20150612L;
/** The factory for creating value collections. */
private final Factory<? extends Collection<V>> collectionFactory;
@@ -73,7 +73,7 @@ public class AbstractMultiValuedMap<K, V
/**
* Constructor that wraps (not copies).
*
- * @param <C> the collection type
+ * @param <C> the collection type
* @param map the map to wrap, must not be null
* @param collectionClazz the collection class
* @throws NullPointerException if the map is null
@@ -92,11 +92,11 @@ public class AbstractMultiValuedMap<K, V
* Constructor that wraps (not copies).
*
* @param <C> the collection type
- * @param map the map to wrap, must not be null
- * @param collectionClazz the collection class
- * @param initialCollectionCapacity the initial capacity of the collection
- * @throws NullPointerException if the map is null
- * @throws IllegalArgumentException if initialCollectionCapacity is
negative
+ * @param map the map to wrap, must not be null
+ * @param collectionClazz the collection class
+ * @param initialCollectionCapacity the initial capacity of the collection
+ * @throws NullPointerException if the map is null
+ * @throws IllegalArgumentException if initialCollectionCapacity is
negative
*/
@SuppressWarnings("unchecked")
protected <C extends Collection<V>> AbstractMultiValuedMap(final Map<K, ?
super C> map,
@@ -105,10 +105,11 @@ public class AbstractMultiValuedMap<K, V
throw new NullPointerException("Map must not be null.");
}
if (initialCollectionCapacity < 0) {
- throw new IllegalArgumentException("Illegal Capacity: " +
initialCollectionCapacity);
+ throw new IllegalArgumentException("InitialCapacity must not be
negative.");
}
this.map = (Map<K, Collection<V>>) map;
- this.collectionFactory = new InstantiateFactory<C>(collectionClazz,
new Class[] { Integer.TYPE },
+ this.collectionFactory = new InstantiateFactory<C>(collectionClazz,
+ new Class[] { Integer.TYPE },
new Object[] { Integer.valueOf(initialCollectionCapacity) });
}
@@ -121,45 +122,22 @@ public class AbstractMultiValuedMap<K, V
return map;
}
- /**
- * {@inheritDoc}
- */
@Override
public boolean containsKey(Object key) {
return getMap().containsKey(key);
}
- /**
- * {@inheritDoc}
- */
@Override
public boolean containsValue(final Object value) {
- final Set<Map.Entry<K, Collection<V>>> pairs = getMap().entrySet();
- if (pairs != null) {
- for (final Map.Entry<K, Collection<V>> entry : pairs) {
- if (entry.getValue().contains(value)) {
- return true;
- }
- }
- }
- return false;
+ return values().contains(value);
}
- /**
- * {@inheritDoc}
- */
@Override
public boolean containsMapping(Object key, Object value) {
- final Collection<V> col = getMap().get(key);
- if (col == null) {
- return false;
- }
- return col.contains(value);
+ Collection<V> coll = getMap().get(key);
+ return coll != null && coll.contains(value);
}
- /**
- * {@inheritDoc}
- */
@Override
public Collection<Entry<K, V>> entries() {
return entryValuesView != null ? entryValuesView : (entryValuesView =
new EntryValues());
@@ -170,12 +148,11 @@ public class AbstractMultiValuedMap<K, V
* would return an empty collection in case the mapping is not present
*
* @param key the key to retrieve
- * @return the <code>Collection</code> of values, will return an empty
- * <code>Collection</code> for no mapping
- * @throws ClassCastException if the key is of an invalid type
+ * @return the {@code Collection} of values, will return an empty {@code
Collection} for no mapping
*/
@Override
- public Collection<V> get(Object key) {
+ public Collection<V> get(final K key) {
+ // TODO: wrap collection based on class type - needed for proper equals
return new WrappedCollection(key);
}
@@ -184,10 +161,9 @@ public class AbstractMultiValuedMap<K, V
* <p>
* A subsequent <code>get(Object)</code> would return an empty collection.
*
- * @param key the key to remove values from
+ * @param key the key to remove values from
* @return the <code>Collection</code> of values removed, will return an
- * empty, unmodifiable collection for no mapping found.
- * @throws ClassCastException if the key is of an invalid type
+ * empty, unmodifiable collection for no mapping found
*/
@Override
public Collection<V> remove(Object key) {
@@ -195,58 +171,46 @@ public class AbstractMultiValuedMap<K, V
}
/**
- * Removes a specific value from map.
+ * Removes a specific key/value mapping from the multi-valued map.
* <p>
- * The item is removed from the collection mapped to the specified key.
+ * The value is removed from the collection mapped to the specified key.
* Other values attached to that key are unaffected.
* <p>
* If the last value for a key is removed, an empty collection would be
- * returned from a subsequent <code>get(Object)</code>.
+ * returned from a subsequent {@link #get(Object)}.
*
* @param key the key to remove from
- * @param item the item to remove
- * @return {@code true} if the mapping was removed, {@code false} otherwise
+ * @param value the value to remove
+ * @return true if the mapping was removed, false otherwise
*/
@Override
- public boolean removeMapping(K key, V item) {
- boolean result = false;
- final Collection<V> col = getMap().get(key);
- if (col == null) {
- return false;
- }
- result = col.remove(item);
- if (!result) {
+ public boolean removeMapping(final Object key, final Object value) {
+ final Collection<V> coll = getMap().get(key);
+ if (coll == null) {
return false;
}
- if (col.isEmpty()) {
- remove(key);
+ boolean changed = coll.remove(value);
+ if (coll.isEmpty()) {
+ getMap().remove(key);
}
- return true;
+ return changed;
}
- /**
- * {@inheritDoc}
- */
@Override
public boolean isEmpty() {
return getMap().isEmpty();
}
- /**
- * {@inheritDoc}
- */
@Override
public Set<K> keySet() {
return getMap().keySet();
}
- /**
- * {@inheritDoc}
- */
@Override
public int size() {
+ // TODO: cache the total size
int size = 0;
- for (Collection<V> col : getMap().values()) {
+ for (final Collection<V> col : getMap().values()) {
size += col.size();
}
return size;
@@ -265,9 +229,6 @@ public class AbstractMultiValuedMap<K, V
return vs != null ? vs : (valuesView = new Values());
}
- /**
- * {@inheritDoc}
- */
@Override
public void clear() {
getMap().clear();
@@ -284,21 +245,19 @@ public class AbstractMultiValuedMap<K, V
* @return the value added if the map changed and null if the map did not
change
*/
@Override
- public boolean put(K key, V value) {
- boolean result = false;
+ public boolean put(final K key, final V value) {
Collection<V> coll = getMap().get(key);
if (coll == null) {
coll = createCollection();
- coll.add(value);
- if (coll.size() > 0) {
- // only add if non-zero size to maintain class state
+ if (coll.add(value)) {
getMap().put(key, coll);
- result = true; // map definitely changed
+ return true;
+ } else {
+ return false;
}
} else {
- result = coll.add(value);
+ return coll.add(value);
}
- return result;
}
/**
@@ -309,15 +268,19 @@ public class AbstractMultiValuedMap<K, V
* undefined if the specified map is modified while the operation is in
* progress.
*
- * @param map mappings to be stored in this map
+ * @param map mappings to be stored in this map, may not be null
+ * @throws NullPointerException if map is null
*/
@Override
- public void putAll(final Map<? extends K, ? extends V> map) {
- if (map != null) {
- for (final Map.Entry<? extends K, ? extends V> entry :
map.entrySet()) {
- put(entry.getKey(), entry.getValue());
- }
+ public boolean putAll(final Map<? extends K, ? extends V> map) {
+ if (map == null) {
+ throw new NullPointerException("Map must not be null.");
+ }
+ boolean changed = false;
+ for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
+ changed |= put(entry.getKey(), entry.getValue());
}
+ return changed;
}
/**
@@ -328,15 +291,19 @@ public class AbstractMultiValuedMap<K, V
* behavior of this operation is undefined if the specified map is modified
* while the operation is in progress.
*
- * @param map mappings to be stored in this map
+ * @param map mappings to be stored in this map, may not be null
+ * @throws NullPointerException if map is null
*/
@Override
- public void putAll(MultiValuedMap<? extends K, ? extends V> map) {
- if (map != null) {
- for (final K key : map.keySet()) {
- putAll(key, map.get(key));
- }
+ public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
+ if (map == null) {
+ throw new NullPointerException("Map must not be null.");
}
+ boolean changed = false;
+ for (Map.Entry<? extends K, ? extends V> entry : map.entries()) {
+ changed |= put(entry.getKey(), entry.getValue());
+ }
+ return changed;
}
/**
@@ -355,9 +322,6 @@ public class AbstractMultiValuedMap<K, V
: (keysMultiSetView = new
KeysMultiSet());
}
- /**
- * {@inheritDoc}
- */
@Override
public Map<K, Collection<V>> asMap() {
return getMap();
@@ -377,42 +341,19 @@ public class AbstractMultiValuedMap<K, V
throw new NullPointerException("Values must not be null.");
}
- Iterator<? extends V> it = values.iterator();
- if (!it.hasNext()) {
- return false;
- }
- boolean result = false;
- Collection<V> coll = getMap().get(key);
- if (coll == null) {
- coll = createCollection(); // might produce a non-empty collection
- while (it.hasNext()) {
- coll.add(it.next());
- }
- if (coll.size() > 0) {
- // only add if non-zero size to maintain class state
- getMap().put(key, coll);
- result = true; // map definitely changed
- }
+ if (values instanceof Collection<?>) {
+ Collection<? extends V> valueCollection = (Collection<? extends
V>) values;
+ return !valueCollection.isEmpty() &&
get(key).addAll(valueCollection);
} else {
- while (it.hasNext()) {
- boolean tmpResult = coll.add(it.next());
- if (!result && tmpResult) {
- // If any one of the values have been added, the map has
- // changed
- result = true;
- }
- }
+ Iterator<? extends V> it = values.iterator();
+ return it.hasNext() && CollectionUtils.addAll(get(key), it);
}
- return result;
}
- /**
- * {@inheritDoc}
- */
@Override
public MapIterator<K, V> mapIterator() {
if (size() == 0) {
- return EmptyMapIterator.<K, V>emptyMapIterator();
+ return EmptyMapIterator.emptyMapIterator();
}
return new MultiValuedMapIterator();
}
@@ -422,52 +363,15 @@ public class AbstractMultiValuedMap<K, V
if (this == obj) {
return true;
}
- if (obj == null) {
- return false;
- }
- if (obj instanceof MultiValuedMap == false) {
- return false;
- }
- MultiValuedMap<?, ?> other = (MultiValuedMap<?, ?>) obj;
- if (other.size() != size()) {
- return false;
- }
- Iterator<?> it = keySet().iterator();
- while (it.hasNext()) {
- Object key = it.next();
- Collection<?> col = get(key);
- Collection<?> otherCol = other.get(key);
- if (otherCol == null) {
- return false;
- }
- if (CollectionUtils.isEqualCollection(col, otherCol) == false) {
- return false;
- }
+ if (obj instanceof MultiValuedMap) {
+ return asMap().equals(((MultiValuedMap<?, ?>) obj).asMap());
}
- return true;
+ return false;
}
@Override
public int hashCode() {
- int h = 0;
- Iterator<Entry<K, Collection<V>>> it = getMap().entrySet().iterator();
- while (it.hasNext()) {
- Entry<K, Collection<V>> entry = it.next();
- K key = entry.getKey();
- Collection<V> valueCol = entry.getValue();
- int vh = 0;
- if (valueCol != null) {
- Iterator<V> colIt = valueCol.iterator();
- while (colIt.hasNext()) {
- V val = colIt.next();
- if (val != null) {
- vh += val.hashCode();
- }
- }
- }
- h += (key == null ? 0 : key.hashCode()) ^ vh;
- }
- return h;
+ return getMap().hashCode();
}
@Override
@@ -488,9 +392,9 @@ public class AbstractMultiValuedMap<K, V
*/
protected class WrappedCollection implements Collection<V> {
- protected final Object key;
+ protected final K key;
- public WrappedCollection(Object key) {
+ public WrappedCollection(final K key) {
this.key = key;
}
@@ -499,30 +403,30 @@ public class AbstractMultiValuedMap<K, V
}
@Override
- @SuppressWarnings("unchecked")
public boolean add(V value) {
- final Collection<V> col = getMapping();
- if (col == null) {
- return AbstractMultiValuedMap.this.put((K) key, value);
+ Collection<V> coll = getMapping();
+ if (coll == null) {
+ coll = createCollection();
+ AbstractMultiValuedMap.this.map.put(key, coll);
}
- return col.add(value);
+ return coll.add(value);
}
@Override
- @SuppressWarnings("unchecked")
- public boolean addAll(Collection<? extends V> c) {
- final Collection<V> col = getMapping();
- if (col == null) {
- return AbstractMultiValuedMap.this.putAll((K) key, c);
+ public boolean addAll(Collection<? extends V> other) {
+ Collection<V> coll = getMapping();
+ if (coll == null) {
+ coll = createCollection();
+ AbstractMultiValuedMap.this.map.put(key, coll);
}
- return col.addAll(c);
+ return coll.addAll(other);
}
@Override
public void clear() {
- final Collection<V> col = getMapping();
- if (col != null) {
- col.clear();
+ final Collection<V> coll = getMapping();
+ if (coll != null) {
+ coll.clear();
AbstractMultiValuedMap.this.remove(key);
}
}
@@ -530,8 +434,8 @@ public class AbstractMultiValuedMap<K, V
@Override
@SuppressWarnings("unchecked")
public Iterator<V> iterator() {
- final Collection<V> col = getMapping();
- if (col == null) {
+ final Collection<V> coll = getMapping();
+ if (coll == null) {
return IteratorUtils.EMPTY_ITERATOR;
}
return new ValuesIterator(key);
@@ -539,49 +443,37 @@ public class AbstractMultiValuedMap<K, V
@Override
public int size() {
- final Collection<V> col = getMapping();
- if (col == null) {
- return 0;
- }
- return col.size();
+ final Collection<V> coll = getMapping();
+ return coll == null ? 0 : coll.size();
}
@Override
- public boolean contains(Object o) {
- final Collection<V> col = getMapping();
- if (col == null) {
- return false;
- }
- return col.contains(o);
+ public boolean contains(Object obj) {
+ final Collection<V> coll = getMapping();
+ return coll == null ? false : coll.contains(obj);
}
@Override
- public boolean containsAll(Collection<?> o) {
- final Collection<V> col = getMapping();
- if (col == null) {
- return false;
- }
- return col.containsAll(o);
+ public boolean containsAll(Collection<?> other) {
+ final Collection<V> coll = getMapping();
+ return coll == null ? false : coll.containsAll(other);
}
@Override
public boolean isEmpty() {
- final Collection<V> col = getMapping();
- if (col == null) {
- return true;
- }
- return col.isEmpty();
+ final Collection<V> coll = getMapping();
+ return coll == null ? true : coll.isEmpty();
}
@Override
public boolean remove(Object item) {
- final Collection<V> col = getMapping();
- if (col == null) {
+ final Collection<V> coll = getMapping();
+ if (coll == null) {
return false;
}
- boolean result = col.remove(item);
- if (col.isEmpty()) {
+ boolean result = coll.remove(item);
+ if (coll.isEmpty()) {
AbstractMultiValuedMap.this.remove(key);
}
return result;
@@ -589,13 +481,13 @@ public class AbstractMultiValuedMap<K, V
@Override
public boolean removeAll(Collection<?> c) {
- final Collection<V> col = getMapping();
- if (col == null) {
+ final Collection<V> coll = getMapping();
+ if (coll == null) {
return false;
}
- boolean result = col.removeAll(c);
- if (col.isEmpty()) {
+ boolean result = coll.removeAll(c);
+ if (coll.isEmpty()) {
AbstractMultiValuedMap.this.remove(key);
}
return result;
@@ -603,13 +495,13 @@ public class AbstractMultiValuedMap<K, V
@Override
public boolean retainAll(Collection<?> c) {
- final Collection<V> col = getMapping();
- if (col == null) {
+ final Collection<V> coll = getMapping();
+ if (coll == null) {
return false;
}
- boolean result = col.retainAll(c);
- if (col.isEmpty()) {
+ boolean result = coll.retainAll(c);
+ if (coll.isEmpty()) {
AbstractMultiValuedMap.this.remove(key);
}
return result;
@@ -617,36 +509,36 @@ public class AbstractMultiValuedMap<K, V
@Override
public Object[] toArray() {
- final Collection<V> col = getMapping();
- if (col == null) {
+ final Collection<V> coll = getMapping();
+ if (coll == null) {
return CollectionUtils.EMPTY_COLLECTION.toArray();
}
- return col.toArray();
+ return coll.toArray();
}
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
- final Collection<V> col = getMapping();
- if (col == null) {
+ final Collection<V> coll = getMapping();
+ if (coll == null) {
return (T[]) CollectionUtils.EMPTY_COLLECTION.toArray(a);
}
- return col.toArray(a);
+ return coll.toArray(a);
}
@Override
public String toString() {
- final Collection<V> col = getMapping();
- if (col == null) {
+ final Collection<V> coll = getMapping();
+ if (coll == null) {
return CollectionUtils.EMPTY_COLLECTION.toString();
}
- return col.toString();
+ return coll.toString();
}
}
/**
- * Inner class that provides a MultiSet<K> keys view
+ * Inner class that provides a MultiSet<K> keys view.
*/
private class KeysMultiSet implements MultiSet<K> {
@@ -749,7 +641,7 @@ public class AbstractMultiValuedMap<K, V
@Override
public Set<K> uniqueSet() {
- return UnmodifiableSet.<K>unmodifiableSet(keySet());
+ return UnmodifiableSet.unmodifiableSet(keySet());
}
@Override
@@ -863,7 +755,7 @@ public class AbstractMultiValuedMap<K, V
}
/**
- * Inner class for MultiValuedMap Entries
+ * Inner class for MultiValuedMap Entries.
*/
private class MultiValuedMapEntry extends AbstractMapEntry<K, V> {
@@ -879,7 +771,7 @@ public class AbstractMultiValuedMap<K, V
}
/**
- * Inner class for MapIterator
+ * Inner class for MapIterator.
*/
private class MultiValuedMapIterator implements MapIterator<K, V> {
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapDecorator.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapDecorator.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapDecorator.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapDecorator.java
Sat Jun 13 18:27:11 2015
@@ -39,11 +39,11 @@ import org.apache.commons.collections4.M
* @since 4.1
* @version $Id$
*/
-public class AbstractMultiValuedMapDecorator<K, V>
+public abstract class AbstractMultiValuedMapDecorator<K, V>
implements MultiValuedMap<K, V>, Serializable {
/** Serialization version */
- private static final long serialVersionUID = -9184930955231260637L;
+ private static final long serialVersionUID = 20150612L;
/** MultiValuedMap to decorate */
private final MultiValuedMap<K, V> map;
@@ -51,7 +51,7 @@ public class AbstractMultiValuedMapDecor
/**
* Constructor that wraps (not copies).
*
- * @param map the map to decorate, must not be null
+ * @param map the map to decorate, must not be null
* @throws NullPointerException if the map is null
*/
protected AbstractMultiValuedMapDecorator(final MultiValuedMap<K, V> map) {
@@ -61,6 +61,11 @@ public class AbstractMultiValuedMapDecor
this.map = map;
}
+ /**
+ * The decorated multi-valued map.
+ *
+ * @return the map to decorate
+ */
protected MultiValuedMap<K, V> decorated() {
return map;
}
@@ -76,32 +81,32 @@ public class AbstractMultiValuedMapDecor
}
@Override
- public boolean containsKey(Object key) {
+ public boolean containsKey(final Object key) {
return decorated().containsKey(key);
}
@Override
- public boolean containsValue(Object value) {
+ public boolean containsValue(final Object value) {
return decorated().containsValue(value);
}
@Override
- public boolean containsMapping(Object key, Object value) {
+ public boolean containsMapping(final Object key, final Object value) {
return decorated().containsMapping(key, value);
}
@Override
- public Collection<V> get(Object key) {
+ public Collection<V> get(final K key) {
return decorated().get(key);
}
@Override
- public Collection<V> remove(Object key) {
+ public Collection<V> remove(final Object key) {
return decorated().remove(key);
}
@Override
- public boolean removeMapping(K key, V item) {
+ public boolean removeMapping(final Object key, final Object item) {
return decorated().removeMapping(key, item);
}
@@ -146,13 +151,13 @@ public class AbstractMultiValuedMapDecor
}
@Override
- public void putAll(Map<? extends K, ? extends V> m) {
- decorated().putAll(m);
+ public boolean putAll(Map<? extends K, ? extends V> map) {
+ return decorated().putAll(map);
}
@Override
- public void putAll(MultiValuedMap<? extends K, ? extends V> m) {
- decorated().putAll(m);
+ public boolean putAll(MultiValuedMap<? extends K, ? extends V> map) {
+ return decorated().putAll(map);
}
@Override
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractSetValuedMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractSetValuedMap.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractSetValuedMap.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/AbstractSetValuedMap.java
Sat Jun 13 18:27:11 2015
@@ -16,12 +16,9 @@
*/
package org.apache.commons.collections4.multimap;
-import java.util.Collection;
import java.util.Collections;
-import java.util.Iterator;
import java.util.Map;
import java.util.Set;
-import java.util.Map.Entry;
import org.apache.commons.collections4.SetUtils;
import org.apache.commons.collections4.SetValuedMap;
@@ -36,17 +33,18 @@ import org.apache.commons.collections4.S
* @since 4.1
* @version $Id$
*/
-public abstract class AbstractSetValuedMap<K, V> extends
AbstractMultiValuedMap<K, V> implements SetValuedMap<K, V> {
+public abstract class AbstractSetValuedMap<K, V> extends
AbstractMultiValuedMap<K, V>
+ implements SetValuedMap<K, V> {
/** Serialization version */
- private static final long serialVersionUID = 3383617478898639862L;
+ private static final long serialVersionUID = 20150612L;
/**
* A constructor that wraps, not copies
*
* @param <C> the set type
- * @param map the map to wrap, must not be null
- * @param setClazz the collection class
+ * @param map the map to wrap, must not be null
+ * @param setClazz the collection class
* @throws NullPointerException if the map is null
*/
protected <C extends Set<V>> AbstractSetValuedMap(Map<K, ? super C> map,
Class<C> setClazz) {
@@ -57,9 +55,9 @@ public abstract class AbstractSetValuedM
* A constructor that wraps, not copies
*
* @param <C> the set type
- * @param map the map to wrap, must not be null
- * @param setClazz the collection class
- * @param initialSetCapacity the initial size of the values set
+ * @param map the map to wrap, must not be null
+ * @param setClazz the collection class
+ * @param initialSetCapacity the initial size of the values set
* @throws NullPointerException if the map is null
* @throws IllegalArgumentException if initialSetCapacity is negative
*/
@@ -72,13 +70,12 @@ public abstract class AbstractSetValuedM
* Gets the set of values associated with the specified key. This would
* return an empty set in case the mapping is not present
*
- * @param key the key to retrieve
+ * @param key the key to retrieve
* @return the <code>Set</code> of values, will return an empty
- * <code>Set</code> for no mapping
- * @throws ClassCastException if the key is of an invalid type
+ * <code>Set</code> for no mapping
*/
@Override
- public Set<V> get(Object key) {
+ public Set<V> get(final K key) {
return new WrappedSet(key);
}
@@ -89,8 +86,7 @@ public abstract class AbstractSetValuedM
*
* @param key the key to remove values from
* @return the <code>Set</code> of values removed, will return an empty,
- * unmodifiable set for no mapping found.
- * @throws ClassCastException if the key is of an invalid type
+ * unmodifiable set for no mapping found.
*/
@Override
public Set<V> remove(Object key) {
@@ -102,51 +98,24 @@ public abstract class AbstractSetValuedM
if (this == obj) {
return true;
}
- if (obj == null) {
- return false;
+ if (obj instanceof SetValuedMap) {
+ return asMap().equals(((SetValuedMap<?, ?>) obj).asMap());
}
- if (obj instanceof SetValuedMap == false) {
- return false;
- }
- SetValuedMap<?, ?> other = (SetValuedMap<?, ?>) obj;
- if (other.size() != size()) {
- return false;
- }
- Iterator<?> it = keySet().iterator();
- while (it.hasNext()) {
- Object key = it.next();
- Set<?> set = get(key);
- Set<?> otherSet = other.get(key);
- if (otherSet == null) {
- return false;
- }
- if (SetUtils.isEqualSet(set, otherSet) == false) {
- return false;
- }
- }
- return true;
+ return false;
}
@Override
public int hashCode() {
- int h = 0;
- Iterator<Entry<K, Collection<V>>> it = getMap().entrySet().iterator();
- while (it.hasNext()) {
- Entry<K, Collection<V>> entry = it.next();
- K key = entry.getKey();
- Set<V> valueSet = (Set<V>) entry.getValue();
- h += (key == null ? 0 : key.hashCode()) ^
SetUtils.hashCodeForSet(valueSet);
- }
- return h;
+ return asMap().hashCode();
}
/**
* Wrapped set to handle add and remove on the collection returned by
- * get(object)
+ * {@code get(Object)}.
*/
protected class WrappedSet extends WrappedCollection implements Set<V> {
- public WrappedSet(Object key) {
+ public WrappedSet(final K key) {
super(key);
}
@@ -156,25 +125,16 @@ public abstract class AbstractSetValuedM
if (set == null) {
return Collections.emptySet().equals(other);
}
- if (other == null) {
- return false;
- }
if (!(other instanceof Set)) {
return false;
}
Set<?> otherSet = (Set<?>) other;
- if (SetUtils.isEqualSet(set, otherSet) == false) {
- return false;
- }
- return true;
+ return SetUtils.isEqualSet(set, otherSet);
}
@Override
public int hashCode() {
final Set<V> set = (Set<V>) getMapping();
- if (set == null) {
- return Collections.emptySet().hashCode();
- }
return SetUtils.hashCodeForSet(set);
}
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedHashMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedHashMap.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedHashMap.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/MultiValuedHashMap.java
Sat Jun 13 18:27:11 2015
@@ -53,7 +53,7 @@ import org.apache.commons.collections4.S
public class MultiValuedHashMap<K, V> extends AbstractMultiValuedMap<K, V> {
/** Serialization Version */
- private static final long serialVersionUID = -5845183518195365857L;
+ private static final long serialVersionUID = 20150612L;
/**
* The initial capacity used when none specified in constructor.
@@ -220,13 +220,14 @@ public class MultiValuedHashMap<K, V> ex
*/
protected <C extends Collection<V>> MultiValuedHashMap(int
initialCapacity, float loadFactor,
final Class<C> collectionClazz, int initialCollectionCapacity) {
- super(new HashMap<K, Collection<V>>(initialCapacity, loadFactor),
collectionClazz, initialCollectionCapacity);
+ super(new HashMap<K, Collection<V>>(initialCapacity, loadFactor),
collectionClazz,
+ initialCollectionCapacity);
}
/** Inner class for ListValuedMap */
private static class ListValuedHashMap<K, V> extends
AbstractListValuedMap<K, V> {
- private static final long serialVersionUID = 3667581458573135234L;
+ private static final long serialVersionUID = 20150612L;
public <C extends List<V>> ListValuedHashMap(Class<C> listClazz) {
super(new HashMap<K, List<V>>(DEFAULT_INITIAL_CAPACITY,
DEFAULT_LOAD_FACTOR), listClazz);
@@ -242,7 +243,7 @@ public class MultiValuedHashMap<K, V> ex
/** Inner class for SetValuedMap */
private static class SetValuedHashMap<K, V> extends
AbstractSetValuedMap<K, V> {
- private static final long serialVersionUID = -3817515514829894543L;
+ private static final long serialVersionUID = 20150612L;
public <C extends Set<V>> SetValuedHashMap(Class<C> setClazz) {
super(new HashMap<K, Set<V>>(DEFAULT_INITIAL_CAPACITY,
DEFAULT_LOAD_FACTOR), setClazz);
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/TransformedMultiValuedMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/TransformedMultiValuedMap.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/TransformedMultiValuedMap.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/TransformedMultiValuedMap.java
Sat Jun 13 18:27:11 2015
@@ -17,13 +17,12 @@
package org.apache.commons.collections4.multimap;
import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.FluentIterable;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.Transformer;
-import org.apache.commons.collections4.map.LinkedMap;
/**
* Decorates another <code>MultiValuedMap</code> to transform objects that are
added.
@@ -41,7 +40,7 @@ import org.apache.commons.collections4.m
public class TransformedMultiValuedMap<K, V> extends
AbstractMultiValuedMapDecorator<K, V> {
/** Serialization Version */
- private static final long serialVersionUID = -1254147899086470720L;
+ private static final long serialVersionUID = 20150612L;
/** The key transformer */
private final Transformer<? super K, ? extends K> keyTransformer;
@@ -56,8 +55,8 @@ public class TransformedMultiValuedMap<K
* NOT transformed. Contrast this with
* {@link #transformedMap(MultiValuedMap, Transformer, Transformer)}.
*
- * @param <K> the key type
- * @param <V> the value type
+ * @param <K> the key type
+ * @param <V> the value type
* @param map the MultiValuedMap to decorate, may not be null
* @param keyTransformer the transformer to use for key conversion, null
means no conversion
* @param valueTransformer the transformer to use for value conversion,
null means no conversion
@@ -78,8 +77,8 @@ public class TransformedMultiValuedMap<K
* be transformed by this method. Contrast this with
* {@link #transformingMap(MultiValuedMap, Transformer, Transformer)}.
*
- * @param <K> the key type
- * @param <V> the value type
+ * @param <K> the key type
+ * @param <V> the value type
* @param map the MultiValuedMap to decorate, may not be null
* @param keyTransformer the transformer to use for key conversion, null
means no conversion
* @param valueTransformer the transformer to use for value conversion,
null means no conversion
@@ -91,11 +90,10 @@ public class TransformedMultiValuedMap<K
final Transformer<? super V, ? extends V> valueTransformer) {
final TransformedMultiValuedMap<K, V> decorated =
new TransformedMultiValuedMap<K, V>(map, keyTransformer,
valueTransformer);
- if (map.size() > 0) {
- MultiValuedMap<K, V> transformed =
decorated.transformMultiValuedMap(map);
+ if (!map.isEmpty()) {
+ final MultiValuedMap<K, V> mapCopy = new MultiValuedHashMap<K,
V>(map);
decorated.clear();
- // to avoid double transform
- decorated.decorated().putAll(transformed);
+ decorated.putAll(mapCopy);
}
return decorated;
}
@@ -112,8 +110,9 @@ public class TransformedMultiValuedMap<K
* @param valueTransformer the transformer to use for value conversion,
null means no conversion
* @throws NullPointerException if map is null
*/
- protected TransformedMultiValuedMap(MultiValuedMap<K, V> map,
- Transformer<? super K, ? extends K> keyTransformer, Transformer<?
super V, ? extends V> valueTransformer) {
+ protected TransformedMultiValuedMap(final MultiValuedMap<K, V> map,
+ final Transformer<? super K, ? extends K> keyTransformer,
+ final Transformer<? super V, ? extends V> valueTransformer) {
super(map);
this.keyTransformer = keyTransformer;
this.valueTransformer = valueTransformer;
@@ -149,83 +148,44 @@ public class TransformedMultiValuedMap<K
return valueTransformer.transform(object);
}
- /**
- * Transforms a map.
- * <p>
- * The transformer itself may throw an exception if necessary.
- *
- * @param map the map to transform
- * @return the transformed object
- */
- @SuppressWarnings("unchecked")
- protected Map<K, V> transformMap(final Map<? extends K, ? extends V> map) {
- if (map.isEmpty()) {
- return (Map<K, V>) map;
- }
- final Map<K, V> result = new LinkedMap<K, V>(map.size());
-
- for (final Map.Entry<? extends K, ? extends V> entry : map.entrySet())
{
- result.put(transformKey(entry.getKey()),
transformValue(entry.getValue()));
- }
- return result;
- }
-
- /**
- * Transforms a MultiValuedMap.
- * <p>
- * The transformer itself may throw an exception if necessary.
- *
- * @param map the MultiValuedMap to transform
- * @return the transformed object
- */
- @SuppressWarnings("unchecked")
- protected MultiValuedMap<K, V> transformMultiValuedMap(
- final MultiValuedMap<? extends K, ? extends V> map) {
- if (map.isEmpty()) {
- return (MultiValuedMap<K, V>) map;
- }
- final MultiValuedMap<K, V> result = new MultiValuedHashMap<K, V>();
-
- for (final Map.Entry<? extends K, ? extends V> entry : map.entries()) {
- result.put(transformKey(entry.getKey()),
transformValue(entry.getValue()));
- }
- return result;
- }
-
@Override
- public boolean put(K key, V value) {
- K transformedKey = transformKey(key);
- V transformedValue = transformValue(value);
- return decorated().put(transformedKey, transformedValue);
+ public boolean put(final K key, final V value) {
+ return decorated().put(transformKey(key), transformValue(value));
}
@Override
- @SuppressWarnings("unchecked")
- public boolean putAll(K key, Iterable<? extends V> values) {
+ public boolean putAll(final K key, final Iterable<? extends V> values) {
if (values == null) {
throw new NullPointerException("Values must not be null.");
}
- Iterator<V> it = (Iterator<V>) values.iterator();
- if (!it.hasNext()) {
- return false;
- }
- K transformedKey = transformKey(key);
- List<V> transformedValues = new LinkedList<V>();
- while (it.hasNext()) {
- transformedValues.add(transformValue(it.next()));
- }
- return decorated().putAll(transformedKey, transformedValues);
+ final Iterable<V> transformedValues =
FluentIterable.of(values).transform(valueTransformer);
+ final Iterator<? extends V> it = transformedValues.iterator();
+ return it.hasNext() &&
CollectionUtils.addAll(decorated().get(transformKey(key)), it);
}
@Override
- public void putAll(Map<? extends K, ? extends V> m) {
- decorated().putAll(transformMap(m));
+ public boolean putAll(final Map<? extends K, ? extends V> map) {
+ if (map == null) {
+ throw new NullPointerException("Map must not be null.");
+ }
+ boolean changed = false;
+ for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
+ changed |= put(entry.getKey(), entry.getValue());
+ }
+ return changed;
}
@Override
- public void putAll(MultiValuedMap<? extends K, ? extends V> m) {
- decorated().putAll(transformMultiValuedMap(m));
+ public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
+ if (map == null) {
+ throw new NullPointerException("Map must not be null.");
+ }
+ boolean changed = false;
+ for (Map.Entry<? extends K, ? extends V> entry : map.entries()) {
+ changed |= put(entry.getKey(), entry.getValue());
+ }
+ return changed;
}
}
Modified:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/UnmodifiableMultiValuedMap.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/UnmodifiableMultiValuedMap.java?rev=1685299&r1=1685298&r2=1685299&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/UnmodifiableMultiValuedMap.java
(original)
+++
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/multimap/UnmodifiableMultiValuedMap.java
Sat Jun 13 18:27:11 2015
@@ -42,19 +42,19 @@ import org.apache.commons.collections4.s
* @since 4.1
* @version $Id$
*/
-public class UnmodifiableMultiValuedMap<K, V>
+public final class UnmodifiableMultiValuedMap<K, V>
extends AbstractMultiValuedMapDecorator<K, V> implements Unmodifiable {
/** Serialization version */
- private static final long serialVersionUID = 1418669828214151566L;
+ private static final long serialVersionUID = 20150612L;
/**
* Factory method to create an unmodifiable MultiValuedMap.
* <p>
* If the map passed in is already unmodifiable, it is returned.
*
- * @param <K> the type of key elements
- * @param <V> the type of value elements
+ * @param <K> the type of key elements
+ * @param <V> the type of value elements
* @param map the map to decorate, may not be null
* @return an unmodifiable MultiValuedMap
* @throws NullPointerException if map is null
@@ -80,12 +80,12 @@ public class UnmodifiableMultiValuedMap<
}
@Override
- public Collection<V> remove(Object key) {
+ public Collection<V> remove(final Object key) {
throw new UnsupportedOperationException();
}
@Override
- public boolean removeMapping(K key, V item) {
+ public boolean removeMapping(final Object key, final Object item) {
throw new UnsupportedOperationException();
}
@@ -95,12 +95,12 @@ public class UnmodifiableMultiValuedMap<
}
@Override
- public Collection<V> get(Object key) {
- return
UnmodifiableCollection.<V>unmodifiableCollection(decorated().get(key));
+ public Collection<V> get(final K key) {
+ return
UnmodifiableCollection.unmodifiableCollection(decorated().get(key));
}
@Override
- public boolean put(K key, V value) {
+ public boolean put(final K key, final V value) {
throw new UnsupportedOperationException();
}
@@ -126,26 +126,26 @@ public class UnmodifiableMultiValuedMap<
@Override
public Map<K, Collection<V>> asMap() {
- return UnmodifiableMap.<K,
Collection<V>>unmodifiableMap(decorated().asMap());
+ return UnmodifiableMap.unmodifiableMap(decorated().asMap());
}
@Override
public MapIterator<K, V> mapIterator() {
- return UnmodifiableMapIterator.<K,
V>unmodifiableMapIterator(decorated().mapIterator());
+ return
UnmodifiableMapIterator.unmodifiableMapIterator(decorated().mapIterator());
}
@Override
- public boolean putAll(K key, Iterable<? extends V> values) {
+ public boolean putAll(final K key, final Iterable<? extends V> values) {
throw new UnsupportedOperationException();
}
@Override
- public void putAll(Map<? extends K, ? extends V> m) {
+ public boolean putAll(final Map<? extends K, ? extends V> map) {
throw new UnsupportedOperationException();
}
@Override
- public void putAll(MultiValuedMap<? extends K, ? extends V> m) {
+ public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
throw new UnsupportedOperationException();
}