Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.
Also see the following revisions: ------------------------------------------------------------------------ r740150 | mbenson | 2009-02-02 15:24:00 -0800 (Mon, 02 Feb 2009) | 1 line make all [collections] maps implement IterableMap ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815074 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a9447a7c72
commit
2fd0de757b
|
@ -19,7 +19,6 @@ package org.apache.commons.collections.map;
|
|||
import java.io.Serializable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -45,17 +44,21 @@ import org.apache.commons.collections.set.CompositeSet;
|
|||
*
|
||||
* @author Brian McCallister
|
||||
*/
|
||||
public class CompositeMap implements Map, Serializable {
|
||||
public class CompositeMap<K, V> extends AbstractIterableMap<K, V> implements Serializable {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = -6096931280583808322L;
|
||||
|
||||
/** Array of all maps in the composite */
|
||||
private Map[] composite;
|
||||
private Map<K, V>[] composite;
|
||||
|
||||
/** Handle mutation operations */
|
||||
private MapMutator mutator;
|
||||
private MapMutator<K, V> mutator;
|
||||
|
||||
/**
|
||||
* Create a new, empty, CompositeMap.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public CompositeMap() {
|
||||
this(new Map[] {}, null);
|
||||
}
|
||||
|
@ -67,7 +70,8 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @param two the second Map to be composited
|
||||
* @throws IllegalArgumentException if there is a key collision
|
||||
*/
|
||||
public CompositeMap(Map one, Map two) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public CompositeMap(Map<K, V> one, Map<K, V> two) {
|
||||
this(new Map[] { one, two }, null);
|
||||
}
|
||||
|
||||
|
@ -78,7 +82,8 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @param two the second Map to be composited
|
||||
* @param mutator MapMutator to be used for mutation operations
|
||||
*/
|
||||
public CompositeMap(Map one, Map two, MapMutator mutator) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public CompositeMap(Map<K, V> one, Map<K, V> two, MapMutator<K, V> mutator) {
|
||||
this(new Map[] { one, two }, mutator);
|
||||
}
|
||||
|
||||
|
@ -89,7 +94,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @param composite the Maps to be composited
|
||||
* @throws IllegalArgumentException if there is a key collision
|
||||
*/
|
||||
public CompositeMap(Map[] composite) {
|
||||
public CompositeMap(Map<K, V>[] composite) {
|
||||
this(composite, null);
|
||||
}
|
||||
|
||||
|
@ -100,7 +105,8 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @param composite Maps to be composited
|
||||
* @param mutator MapMutator to be used for mutation operations
|
||||
*/
|
||||
public CompositeMap(Map[] composite, MapMutator mutator) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public CompositeMap(Map<K, V>[] composite, MapMutator<K, V> mutator) {
|
||||
this.mutator = mutator;
|
||||
this.composite = new Map[0];
|
||||
for (int i = composite.length - 1; i >= 0; --i) {
|
||||
|
@ -114,7 +120,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
*
|
||||
* @param mutator the MapMutator to be used for mutation delegation
|
||||
*/
|
||||
public void setMutator(MapMutator mutator) {
|
||||
public void setMutator(MapMutator<K, V> mutator) {
|
||||
this.mutator = mutator;
|
||||
}
|
||||
|
||||
|
@ -125,19 +131,18 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @throws IllegalArgumentException if there is a key collision and there is no
|
||||
* MapMutator set to handle it.
|
||||
*/
|
||||
public synchronized void addComposited(Map map) throws IllegalArgumentException {
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized void addComposited(Map<K, V> map) throws IllegalArgumentException {
|
||||
for (int i = composite.length - 1; i >= 0; --i) {
|
||||
Collection intersect = CollectionUtils.intersection(this.composite[i].keySet(), map.keySet());
|
||||
Collection<K> intersect = CollectionUtils.intersection(this.composite[i].keySet(), map.keySet());
|
||||
if (intersect.size() != 0) {
|
||||
if (this.mutator == null) {
|
||||
throw new IllegalArgumentException("Key collision adding Map to CompositeMap");
|
||||
}
|
||||
else {
|
||||
this.mutator.resolveCollision(this, this.composite[i], map, intersect);
|
||||
}
|
||||
}
|
||||
}
|
||||
Map[] temp = new Map[this.composite.length + 1];
|
||||
Map<K, V>[] temp = new Map[this.composite.length + 1];
|
||||
System.arraycopy(this.composite, 0, temp, 0, this.composite.length);
|
||||
temp[temp.length - 1] = map;
|
||||
this.composite = temp;
|
||||
|
@ -149,11 +154,12 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @param map the Map to be removed from the composite
|
||||
* @return The removed Map or <code>null</code> if map is not in the composite
|
||||
*/
|
||||
public synchronized Map removeComposited(Map map) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized Map<K, V> removeComposited(Map<K, V> map) {
|
||||
int size = this.composite.length;
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (this.composite[i].equals(map)) {
|
||||
Map[] temp = new Map[size - 1];
|
||||
Map<K, V>[] temp = new Map[size - 1];
|
||||
System.arraycopy(this.composite, 0, temp, 0, i);
|
||||
System.arraycopy(this.composite, i + 1, temp, i, size - i - 1);
|
||||
this.composite = temp;
|
||||
|
@ -242,10 +248,10 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @see CompositeSet
|
||||
* @return a set view of the mappings contained in this map.
|
||||
*/
|
||||
public Set entrySet() {
|
||||
CompositeSet entries = new CompositeSet();
|
||||
for (int i = this.composite.length - 1; i >= 0; --i) {
|
||||
entries.addComposited(this.composite[i].entrySet());
|
||||
public Set<Map.Entry<K, V>> entrySet() {
|
||||
CompositeSet<Map.Entry<K, V>> entries = new CompositeSet<Map.Entry<K,V>>();
|
||||
for (int i = composite.length - 1; i >= 0; --i) {
|
||||
entries.addComposited(composite[i].entrySet());
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
@ -274,7 +280,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
*
|
||||
* @see #containsKey(Object)
|
||||
*/
|
||||
public Object get(Object key) {
|
||||
public V get(Object key) {
|
||||
for (int i = this.composite.length - 1; i >= 0; --i) {
|
||||
if (this.composite[i].containsKey(key)) {
|
||||
return this.composite[i].get(key);
|
||||
|
@ -312,8 +318,8 @@ public class CompositeMap implements Map, Serializable {
|
|||
*
|
||||
* @return a set view of the keys contained in this map.
|
||||
*/
|
||||
public Set keySet() {
|
||||
CompositeSet keys = new CompositeSet();
|
||||
public Set<K> keySet() {
|
||||
CompositeSet<K> keys = new CompositeSet<K>();
|
||||
for (int i = this.composite.length - 1; i >= 0; --i) {
|
||||
keys.addComposited(this.composite[i].keySet());
|
||||
}
|
||||
|
@ -345,7 +351,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
* keys or values, and the specified key or value is
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public Object put(Object key, Object value) {
|
||||
public V put(K key, V value) {
|
||||
if (this.mutator == null) {
|
||||
throw new UnsupportedOperationException("No mutator specified");
|
||||
}
|
||||
|
@ -374,7 +380,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
* this map does not permit <tt>null</tt> keys or values, and the
|
||||
* specified map contains <tt>null</tt> keys or values.
|
||||
*/
|
||||
public void putAll(Map map) {
|
||||
public void putAll(Map<? extends K, ? extends V> map) {
|
||||
if (this.mutator == null) {
|
||||
throw new UnsupportedOperationException("No mutator specified");
|
||||
}
|
||||
|
@ -406,7 +412,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @throws UnsupportedOperationException if the <tt>remove</tt> method is
|
||||
* not supported by the composited map containing the key
|
||||
*/
|
||||
public Object remove(Object key) {
|
||||
public V remove(Object key) {
|
||||
for (int i = this.composite.length - 1; i >= 0; --i) {
|
||||
if (this.composite[i].containsKey(key)) {
|
||||
return this.composite[i].remove(key);
|
||||
|
@ -443,12 +449,12 @@ public class CompositeMap implements Map, Serializable {
|
|||
*
|
||||
* @return a collection view of the values contained in this map.
|
||||
*/
|
||||
public Collection values() {
|
||||
CompositeCollection keys = new CompositeCollection();
|
||||
for (int i = this.composite.length - 1; i >= 0; --i) {
|
||||
keys.addComposited(this.composite[i].values());
|
||||
public Collection<V> values() {
|
||||
CompositeCollection<V> values = new CompositeCollection<V>();
|
||||
for (int i = composite.length - 1; i >= 0; --i) {
|
||||
values.addComposited(composite[i].values());
|
||||
}
|
||||
return keys;
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -457,6 +463,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @param obj the object to compare to
|
||||
* @return true if the maps are equal
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Map) {
|
||||
Map map = (Map) obj;
|
||||
|
@ -470,8 +477,8 @@ public class CompositeMap implements Map, Serializable {
|
|||
*/
|
||||
public int hashCode() {
|
||||
int code = 0;
|
||||
for (Iterator i = this.entrySet().iterator(); i.hasNext();) {
|
||||
code += i.next().hashCode();
|
||||
for (Map.Entry<K, V> entry : entrySet()) {
|
||||
code += entry.hashCode();
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
@ -481,7 +488,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
* mutators in a CompositeMap, as well as providing a hook for
|
||||
* callbacks on key collisions.
|
||||
*/
|
||||
public static interface MapMutator extends Serializable {
|
||||
public static interface MapMutator<K, V> extends Serializable {
|
||||
/**
|
||||
* Called when adding a new Composited Map results in a
|
||||
* key collision.
|
||||
|
@ -492,8 +499,8 @@ public class CompositeMap implements Map, Serializable {
|
|||
* @param added the Map being added
|
||||
* @param intersect the intersection of the keysets of the existing and added maps
|
||||
*/
|
||||
public void resolveCollision(
|
||||
CompositeMap composite, Map existing, Map added, Collection intersect);
|
||||
public void resolveCollision(CompositeMap<K, V> composite, Map<K, V> existing,
|
||||
Map<K, V> added, Collection<K> intersect);
|
||||
|
||||
/**
|
||||
* Called when the CompositeMap.put() method is invoked.
|
||||
|
@ -517,7 +524,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
* keys or values, and the specified key or value is
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public Object put(CompositeMap map, Map[] composited, Object key, Object value);
|
||||
public V put(CompositeMap<K, V> map, Map<K, V>[] composited, K key, V value);
|
||||
|
||||
/**
|
||||
* Called when the CompositeMap.putAll() method is invoked.
|
||||
|
@ -535,6 +542,7 @@ public class CompositeMap implements Map, Serializable {
|
|||
* keys or values, and the specified key or value is
|
||||
* <tt>null</tt>.
|
||||
*/
|
||||
public void putAll(CompositeMap map, Map[] composited, Map mapToAdd);
|
||||
public void putAll(CompositeMap<K, V> map, Map<K, V>[] composited,
|
||||
Map<? extends K, ? extends V> mapToAdd);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue