You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2009/01/29 19:48:47 UTC
svn commit: r738956 [16/34] - in
/commons/proper/collections/branches/collections_jdk5_branch/src:
java/org/apache/commons/collections/ java/org/apache/commons/collections/bag/
java/org/apache/commons/collections/bidimap/
java/org/apache/commons/collec...
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/MultiKeyMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/MultiKeyMap.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/MultiKeyMap.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/MultiKeyMap.java Thu Jan 29 18:48:37 2009
@@ -17,10 +17,7 @@
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;
import org.apache.commons.collections.IterableMap;
import org.apache.commons.collections.MapIterator;
@@ -77,14 +74,15 @@
*
* @author Stephen Colebourne
*/
-public class MultiKeyMap
- implements IterableMap, Serializable {
+public class MultiKeyMap<K, V> extends AbstractMapDecorator<MultiKey<? extends K>, V>
+ implements IterableMap<MultiKey<? extends K>, V>, Serializable {
/** Serialisation version */
private static final long serialVersionUID = -1788199231038721040L;
/** The decorated map */
- protected final AbstractHashedMap map;
+ //keep this member around for serialization BC with older Collections releases assuming we want to do that
+ protected AbstractHashedMap<MultiKey<? extends K>, V> map;
//-----------------------------------------------------------------------
/**
@@ -94,14 +92,14 @@
* @param map the map to decorate, not null
* @throws IllegalArgumentException if the map is null or not empty
*/
- public static MultiKeyMap decorate(AbstractHashedMap map) {
+ public static <K, V> MultiKeyMap<K, V> decorate(AbstractHashedMap<MultiKey<? extends K>, V> map) {
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
if (map.size() > 0) {
throw new IllegalArgumentException("Map must be empty");
}
- return new MultiKeyMap(map);
+ return new MultiKeyMap<K, V>(map);
}
//-----------------------------------------------------------------------
@@ -109,8 +107,7 @@
* Constructs a new MultiKeyMap that decorates a <code>HashedMap</code>.
*/
public MultiKeyMap() {
- super();
- map = new HashedMap();
+ this(new HashedMap<MultiKey<? extends K>, V>());
}
/**
@@ -121,8 +118,8 @@
*
* @param map the map to decorate
*/
- protected MultiKeyMap(AbstractHashedMap map) {
- super();
+ protected MultiKeyMap(AbstractHashedMap<MultiKey<? extends K>, V> map) {
+ super(map);
this.map = map;
}
@@ -134,9 +131,9 @@
* @param key2 the second key
* @return the mapped value, null if no match
*/
- public Object get(Object key1, Object key2) {
+ public V get(Object key1, Object key2) {
int hashCode = hash(key1, key2);
- AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[decorated().hashIndex(hashCode, decorated().data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2)) {
return entry.getValue();
@@ -155,7 +152,7 @@
*/
public boolean containsKey(Object key1, Object key2) {
int hashCode = hash(key1, key2);
- AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[decorated().hashIndex(hashCode, decorated().data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2)) {
return true;
@@ -173,20 +170,19 @@
* @param value the value to store
* @return the value previously mapped to this combined key, null if none
*/
- public Object put(Object key1, Object key2, Object value) {
+ public V put(K key1, K key2, V value) {
int hashCode = hash(key1, key2);
- int index = map.hashIndex(hashCode, map.data.length);
- AbstractHashedMap.HashEntry entry = map.data[index];
+ int index = decorated().hashIndex(hashCode, decorated().data.length);
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2)) {
- Object oldValue = entry.getValue();
- map.updateEntry(entry, value);
+ V oldValue = entry.getValue();
+ decorated().updateEntry(entry, value);
return oldValue;
}
entry = entry.next;
}
-
- map.addMapping(index, hashCode, new MultiKey(key1, key2), value);
+ decorated().addMapping(index, hashCode, new MultiKey<K>(key1, key2), value);
return null;
}
@@ -197,15 +193,15 @@
* @param key2 the second key
* @return the value mapped to the removed key, null if key not in map
*/
- public Object remove(Object key1, Object key2) {
+ public V remove(Object key1, Object key2) {
int hashCode = hash(key1, key2);
- int index = map.hashIndex(hashCode, map.data.length);
- AbstractHashedMap.HashEntry entry = map.data[index];
- AbstractHashedMap.HashEntry previous = null;
+ int index = decorated().hashIndex(hashCode, decorated().data.length);
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> previous = null;
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2)) {
- Object oldValue = entry.getValue();
- map.removeMapping(entry, index, previous);
+ V oldValue = entry.getValue();
+ decorated().removeMapping(entry, index, previous);
return oldValue;
}
previous = entry;
@@ -244,12 +240,13 @@
* @param key2 the second key
* @return true if the key matches
*/
- protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2) {
- MultiKey multi = (MultiKey) entry.getKey();
+ protected boolean isEqualKey(AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry,
+ Object key1, Object key2) {
+ MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 2 &&
- (key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
- (key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1)));
+ (key1 == multi.getKey(0) || key1 != null && key1.equals(multi.getKey(0))) &&
+ (key2 == multi.getKey(1) || key1 != null && key2.equals(multi.getKey(1)));
}
//-----------------------------------------------------------------------
@@ -261,9 +258,9 @@
* @param key3 the third key
* @return the mapped value, null if no match
*/
- public Object get(Object key1, Object key2, Object key3) {
+ public V get(Object key1, Object key2, Object key3) {
int hashCode = hash(key1, key2, key3);
- AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[decorated().hashIndex(hashCode, decorated().data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3)) {
return entry.getValue();
@@ -283,7 +280,7 @@
*/
public boolean containsKey(Object key1, Object key2, Object key3) {
int hashCode = hash(key1, key2, key3);
- AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[decorated().hashIndex(hashCode, decorated().data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3)) {
return true;
@@ -302,20 +299,19 @@
* @param value the value to store
* @return the value previously mapped to this combined key, null if none
*/
- public Object put(Object key1, Object key2, Object key3, Object value) {
+ public V put(K key1, K key2, K key3, V value) {
int hashCode = hash(key1, key2, key3);
- int index = map.hashIndex(hashCode, map.data.length);
- AbstractHashedMap.HashEntry entry = map.data[index];
+ int index = decorated().hashIndex(hashCode, decorated().data.length);
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3)) {
- Object oldValue = entry.getValue();
- map.updateEntry(entry, value);
+ V oldValue = entry.getValue();
+ decorated().updateEntry(entry, value);
return oldValue;
}
entry = entry.next;
}
-
- map.addMapping(index, hashCode, new MultiKey(key1, key2, key3), value);
+ decorated().addMapping(index, hashCode, new MultiKey<K>(key1, key2, key3), value);
return null;
}
@@ -327,15 +323,15 @@
* @param key3 the third key
* @return the value mapped to the removed key, null if key not in map
*/
- public Object remove(Object key1, Object key2, Object key3) {
+ public V remove(Object key1, Object key2, Object key3) {
int hashCode = hash(key1, key2, key3);
- int index = map.hashIndex(hashCode, map.data.length);
- AbstractHashedMap.HashEntry entry = map.data[index];
- AbstractHashedMap.HashEntry previous = null;
+ int index = decorated().hashIndex(hashCode, decorated().data.length);
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> previous = null;
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3)) {
- Object oldValue = entry.getValue();
- map.removeMapping(entry, index, previous);
+ V oldValue = entry.getValue();
+ decorated().removeMapping(entry, index, previous);
return oldValue;
}
previous = entry;
@@ -379,13 +375,13 @@
* @param key3 the third key
* @return true if the key matches
*/
- protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3) {
- MultiKey multi = (MultiKey) entry.getKey();
+ protected boolean isEqualKey(AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry, Object key1, Object key2, Object key3) {
+ MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 3 &&
- (key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
- (key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
- (key3 == null ? multi.getKey(2) == null : key3.equals(multi.getKey(2)));
+ (key1 == multi.getKey(0) || key1 != null && key1.equals(multi.getKey(0))) &&
+ (key2 == multi.getKey(1) || key2 != null && key2.equals(multi.getKey(1))) &&
+ (key3 == multi.getKey(2) || key3 != null && key3.equals(multi.getKey(2)));
}
//-----------------------------------------------------------------------
@@ -398,9 +394,9 @@
* @param key4 the fourth key
* @return the mapped value, null if no match
*/
- public Object get(Object key1, Object key2, Object key3, Object key4) {
+ public V get(Object key1, Object key2, Object key3, Object key4) {
int hashCode = hash(key1, key2, key3, key4);
- AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[decorated().hashIndex(hashCode, decorated().data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4)) {
return entry.getValue();
@@ -421,7 +417,7 @@
*/
public boolean containsKey(Object key1, Object key2, Object key3, Object key4) {
int hashCode = hash(key1, key2, key3, key4);
- AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[decorated().hashIndex(hashCode, decorated().data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4)) {
return true;
@@ -441,20 +437,19 @@
* @param value the value to store
* @return the value previously mapped to this combined key, null if none
*/
- public Object put(Object key1, Object key2, Object key3, Object key4, Object value) {
+ public V put(K key1, K key2, K key3, K key4, V value) {
int hashCode = hash(key1, key2, key3, key4);
- int index = map.hashIndex(hashCode, map.data.length);
- AbstractHashedMap.HashEntry entry = map.data[index];
+ int index = decorated().hashIndex(hashCode, decorated().data.length);
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4)) {
- Object oldValue = entry.getValue();
- map.updateEntry(entry, value);
+ V oldValue = entry.getValue();
+ decorated().updateEntry(entry, value);
return oldValue;
}
entry = entry.next;
}
-
- map.addMapping(index, hashCode, new MultiKey(key1, key2, key3, key4), value);
+ decorated().addMapping(index, hashCode, new MultiKey<K>(key1, key2, key3, key4), value);
return null;
}
@@ -467,15 +462,15 @@
* @param key4 the fourth key
* @return the value mapped to the removed key, null if key not in map
*/
- public Object remove(Object key1, Object key2, Object key3, Object key4) {
+ public V remove(Object key1, Object key2, Object key3, Object key4) {
int hashCode = hash(key1, key2, key3, key4);
- int index = map.hashIndex(hashCode, map.data.length);
- AbstractHashedMap.HashEntry entry = map.data[index];
- AbstractHashedMap.HashEntry previous = null;
+ int index = decorated().hashIndex(hashCode, decorated().data.length);
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> previous = null;
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4)) {
- Object oldValue = entry.getValue();
- map.removeMapping(entry, index, previous);
+ V oldValue = entry.getValue();
+ decorated().removeMapping(entry, index, previous);
return oldValue;
}
previous = entry;
@@ -524,14 +519,14 @@
* @param key4 the fourth key
* @return true if the key matches
*/
- protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3, Object key4) {
- MultiKey multi = (MultiKey) entry.getKey();
+ protected boolean isEqualKey(AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry, Object key1, Object key2, Object key3, Object key4) {
+ MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 4 &&
- (key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
- (key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
- (key3 == null ? multi.getKey(2) == null : key3.equals(multi.getKey(2))) &&
- (key4 == null ? multi.getKey(3) == null : key4.equals(multi.getKey(3)));
+ (key1 == multi.getKey(0) || key1 != null && key1.equals(multi.getKey(0))) &&
+ (key2 == multi.getKey(1) || key2 != null && key2.equals(multi.getKey(1))) &&
+ (key3 == multi.getKey(2) || key3 != null && key3.equals(multi.getKey(2))) &&
+ (key4 == multi.getKey(3) || key4 != null && key4.equals(multi.getKey(3)));
}
//-----------------------------------------------------------------------
@@ -545,9 +540,9 @@
* @param key5 the fifth key
* @return the mapped value, null if no match
*/
- public Object get(Object key1, Object key2, Object key3, Object key4, Object key5) {
+ public V get(Object key1, Object key2, Object key3, Object key4, Object key5) {
int hashCode = hash(key1, key2, key3, key4, key5);
- AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[decorated().hashIndex(hashCode, decorated().data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4, key5)) {
return entry.getValue();
@@ -569,7 +564,7 @@
*/
public boolean containsKey(Object key1, Object key2, Object key3, Object key4, Object key5) {
int hashCode = hash(key1, key2, key3, key4, key5);
- AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[decorated().hashIndex(hashCode, decorated().data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4, key5)) {
return true;
@@ -590,20 +585,19 @@
* @param value the value to store
* @return the value previously mapped to this combined key, null if none
*/
- public Object put(Object key1, Object key2, Object key3, Object key4, Object key5, Object value) {
+ public V put(K key1, K key2, K key3, K key4, K key5, V value) {
int hashCode = hash(key1, key2, key3, key4, key5);
- int index = map.hashIndex(hashCode, map.data.length);
- AbstractHashedMap.HashEntry entry = map.data[index];
+ int index = decorated().hashIndex(hashCode, decorated().data.length);
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4, key5)) {
- Object oldValue = entry.getValue();
- map.updateEntry(entry, value);
+ V oldValue = entry.getValue();
+ decorated().updateEntry(entry, value);
return oldValue;
}
entry = entry.next;
}
-
- map.addMapping(index, hashCode, new MultiKey(key1, key2, key3, key4, key5), value);
+ decorated().addMapping(index, hashCode, new MultiKey<K>(key1, key2, key3, key4, key5), value);
return null;
}
@@ -617,15 +611,15 @@
* @param key5 the fifth key
* @return the value mapped to the removed key, null if key not in map
*/
- public Object remove(Object key1, Object key2, Object key3, Object key4, Object key5) {
+ public V remove(Object key1, Object key2, Object key3, Object key4, Object key5) {
int hashCode = hash(key1, key2, key3, key4, key5);
- int index = map.hashIndex(hashCode, map.data.length);
- AbstractHashedMap.HashEntry entry = map.data[index];
- AbstractHashedMap.HashEntry previous = null;
+ int index = decorated().hashIndex(hashCode, decorated().data.length);
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index];
+ AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> previous = null;
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4, key5)) {
- Object oldValue = entry.getValue();
- map.removeMapping(entry, index, previous);
+ V oldValue = entry.getValue();
+ decorated().removeMapping(entry, index, previous);
return oldValue;
}
previous = entry;
@@ -679,15 +673,16 @@
* @param key5 the fifth key
* @return true if the key matches
*/
- protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3, Object key4, Object key5) {
- MultiKey multi = (MultiKey) entry.getKey();
+ protected boolean isEqualKey(AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry,
+ Object key1, Object key2, Object key3, Object key4, Object key5) {
+ MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 5 &&
- (key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
- (key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
- (key3 == null ? multi.getKey(2) == null : key3.equals(multi.getKey(2))) &&
- (key4 == null ? multi.getKey(3) == null : key4.equals(multi.getKey(3))) &&
- (key5 == null ? multi.getKey(4) == null : key5.equals(multi.getKey(4)));
+ (key1 == multi.getKey(0) || key1 != null && key1.equals(multi.getKey(0))) &&
+ (key2 == multi.getKey(1) || key2 != null && key2.equals(multi.getKey(1))) &&
+ (key3 == multi.getKey(2) || key3 != null && key3.equals(multi.getKey(2))) &&
+ (key4 == multi.getKey(3) || key4 != null && key4.equals(multi.getKey(3))) &&
+ (key5 == multi.getKey(4) || key5 != null && key5.equals(multi.getKey(4)));
}
//-----------------------------------------------------------------------
@@ -702,9 +697,9 @@
*/
public boolean removeAll(Object key1) {
boolean modified = false;
- MapIterator it = mapIterator();
+ MapIterator<MultiKey<? extends K>, V> it = mapIterator();
while (it.hasNext()) {
- MultiKey multi = (MultiKey) it.next();
+ MultiKey<? extends K> multi = it.next();
if (multi.size() >= 1 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0)))) {
it.remove();
@@ -726,9 +721,9 @@
*/
public boolean removeAll(Object key1, Object key2) {
boolean modified = false;
- MapIterator it = mapIterator();
+ MapIterator<MultiKey<? extends K>, V> it = mapIterator();
while (it.hasNext()) {
- MultiKey multi = (MultiKey) it.next();
+ MultiKey<? extends K> multi = it.next();
if (multi.size() >= 2 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1)))) {
@@ -752,9 +747,9 @@
*/
public boolean removeAll(Object key1, Object key2, Object key3) {
boolean modified = false;
- MapIterator it = mapIterator();
+ MapIterator<MultiKey<? extends K>, V> it = mapIterator();
while (it.hasNext()) {
- MultiKey multi = (MultiKey) it.next();
+ MultiKey<? extends K> multi = it.next();
if (multi.size() >= 3 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
@@ -780,9 +775,9 @@
*/
public boolean removeAll(Object key1, Object key2, Object key3, Object key4) {
boolean modified = false;
- MapIterator it = mapIterator();
+ MapIterator<MultiKey<? extends K>, V> it = mapIterator();
while (it.hasNext()) {
- MultiKey multi = (MultiKey) it.next();
+ MultiKey<? extends K> multi = it.next();
if (multi.size() >= 4 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
@@ -801,13 +796,10 @@
*
* @param key the key to check
*/
- protected void checkKey(Object key) {
+ protected void checkKey(MultiKey<?> key) {
if (key == null) {
throw new NullPointerException("Key must not be null");
}
- if (key instanceof MultiKey == false) {
- throw new ClassCastException("Key must be a MultiKey");
- }
}
/**
@@ -815,8 +807,8 @@
*
* @return a shallow clone
*/
- public Object clone() {
- return new MultiKeyMap((AbstractHashedMap) map.clone());
+ public MultiKeyMap<K, V> clone() {
+ return new MultiKeyMap<K, V>(decorated().clone());
}
/**
@@ -829,9 +821,9 @@
* @throws NullPointerException if the key is null
* @throws ClassCastException if the key is not a MultiKey
*/
- public Object put(Object key, Object value) {
+ public V put(MultiKey<? extends K> key, V value) {
checkKey(key);
- return map.put(key, value);
+ return super.put(key, value);
}
/**
@@ -842,72 +834,24 @@
* @throws NullPointerException if the mapToCopy or any key within is null
* @throws ClassCastException if any key in mapToCopy is not a MultiKey
*/
- public void putAll(Map mapToCopy) {
- for (Iterator it = mapToCopy.keySet().iterator(); it.hasNext();) {
- Object key = it.next();
+ @Override
+ public void putAll(Map<? extends MultiKey<? extends K>, ? extends V> mapToCopy) {
+ for (MultiKey<? extends K> key : mapToCopy.keySet()) {
checkKey(key);
}
- map.putAll(mapToCopy);
+ super.putAll(mapToCopy);
}
//-----------------------------------------------------------------------
- public MapIterator mapIterator() {
- return map.mapIterator();
- }
-
- public int size() {
- return map.size();
- }
-
- public boolean isEmpty() {
- return map.isEmpty();
- }
-
- public boolean containsKey(Object key) {
- return map.containsKey(key);
- }
-
- public boolean containsValue(Object value) {
- return map.containsValue(value);
- }
-
- public Object get(Object key) {
- return map.get(key);
- }
-
- public Object remove(Object key) {
- return map.remove(key);
- }
-
- public void clear() {
- map.clear();
+ public MapIterator<MultiKey<? extends K>, V> mapIterator() {
+ return decorated().mapIterator();
}
- public Set keySet() {
- return map.keySet();
- }
-
- public Collection values() {
- return map.values();
- }
-
- public Set entrySet() {
- return map.entrySet();
- }
-
- public boolean equals(Object obj) {
- if (obj == this) {
- return true;
- }
- return map.equals(obj);
- }
-
- public int hashCode() {
- return map.hashCode();
- }
-
- public String toString() {
- return map.toString();
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected AbstractHashedMap<MultiKey<? extends K>, V> decorated() {
+ return map;
}
-
}
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/MultiValueMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/MultiValueMap.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/MultiValueMap.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/MultiValueMap.java Thu Jan 29 18:48:37 2009
@@ -24,6 +24,7 @@
import java.util.Map;
import java.util.Set;
+import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.FunctorException;
import org.apache.commons.collections.MultiMap;
@@ -61,12 +62,12 @@
* @version $Revision$ $Date$
* @since Commons Collections 3.2
*/
-public class MultiValueMap extends AbstractMapDecorator implements MultiMap {
+public class MultiValueMap<K, V> extends AbstractMapDecorator<K, Object> implements MultiMap<K, V> {
/** The factory for creating value collections. */
- private final Factory collectionFactory;
+ private final Factory<? extends Collection<V>> collectionFactory;
/** The cached values. */
- private transient Collection values;
+ private transient Collection<V> values;
/**
* Creates a map which wraps the given map and
@@ -74,8 +75,9 @@
*
* @param map the map to wrap
*/
- public static MultiValueMap decorate(Map map) {
- return new MultiValueMap(map, new ReflectionFactory(ArrayList.class));
+ @SuppressWarnings("unchecked")
+ public static <K, V> MultiValueMap<K, V> decorate(Map<K, ? super Collection<V>> map) {
+ return MultiValueMap.<K, V, ArrayList>decorate((Map<K, ? super Collection>) map, ArrayList.class);
}
/**
@@ -85,8 +87,8 @@
* @param map the map to wrap
* @param collectionClass the type of the collection class
*/
- public static MultiValueMap decorate(Map map, Class collectionClass) {
- return new MultiValueMap(map, new ReflectionFactory(collectionClass));
+ public static <K, V, C extends Collection<V>> MultiValueMap<K, V> decorate(Map<K, ? super C> map, Class<C> collectionClass) {
+ return new MultiValueMap<K, V>(map, new ReflectionFactory<C>(collectionClass));
}
/**
@@ -96,8 +98,8 @@
* @param map the map to decorate
* @param collectionFactory the collection factory (must return a Collection object).
*/
- public static MultiValueMap decorate(Map map, Factory collectionFactory) {
- return new MultiValueMap(map, collectionFactory);
+ public static <K, V, C extends Collection<V>> MultiValueMap<K, V> decorate(Map<K, ? super C> map, Factory<C> collectionFactory) {
+ return new MultiValueMap<K, V>(map, collectionFactory);
}
//-----------------------------------------------------------------------
@@ -105,6 +107,7 @@
* Creates a MultiValueMap based on a <code>HashMap</code> and
* storing the multiple values in an <code>ArrayList</code>.
*/
+ @SuppressWarnings("unchecked")
public MultiValueMap() {
this(new HashMap(), new ReflectionFactory(ArrayList.class));
}
@@ -116,8 +119,9 @@
* @param map the map to decorate
* @param collectionFactory the collection factory which must return a Collection instance
*/
- protected MultiValueMap(Map map, Factory collectionFactory) {
- super(map);
+ @SuppressWarnings("unchecked")
+ protected <C extends Collection<V>> MultiValueMap(Map<K, ? super C> map, Factory<C> collectionFactory) {
+ super((Map<K, Object>) map);
if (collectionFactory == null) {
throw new IllegalArgumentException("The factory must not be null");
}
@@ -153,8 +157,9 @@
* @param value the value to remove
* @return the value removed (which was passed in), null if nothing removed
*/
- public Object remove(Object key, Object value) {
- Collection valuesForKey = getCollection(key);
+ @SuppressWarnings("unchecked")
+ public V remove(Object key, Object value) {
+ Collection<V> valuesForKey = getCollection(key);
if (valuesForKey == null) {
return null;
}
@@ -165,7 +170,7 @@
if (valuesForKey.isEmpty()) {
remove(key);
}
- return value;
+ return (V) value;
}
/**
@@ -176,17 +181,14 @@
* @param value the value to search for
* @return true if the map contains the value
*/
+ @SuppressWarnings("unchecked")
public boolean containsValue(Object value) {
- Set pairs = decorated().entrySet();
- if (pairs == null) {
- return false;
- }
- Iterator pairsIterator = pairs.iterator();
- while (pairsIterator.hasNext()) {
- Map.Entry keyValuePair = (Map.Entry) pairsIterator.next();
- Collection coll = (Collection) keyValuePair.getValue();
- if (coll.contains(value)) {
- return true;
+ Set<Map.Entry<K, Object>> pairs = decorated().entrySet();
+ if (pairs != null) {
+ for (Map.Entry<K, Object> entry : pairs) {
+ if (((Collection<V>) entry.getValue()).contains(value)) {
+ return true;
+ }
}
}
return false;
@@ -202,19 +204,20 @@
* @param value the value to add to the collection at the key
* @return the value added if the map changed and null if the map did not change
*/
- public Object put(Object key, Object value) {
+ @SuppressWarnings("unchecked")
+ public Object put(K key, Object value) {
boolean result = false;
- Collection coll = getCollection(key);
+ Collection<V> coll = getCollection(key);
if (coll == null) {
coll = createCollection(1); // might produce a non-empty collection
- coll.add(value);
+ coll.add((V) value);
if (coll.size() > 0) {
// only add if non-zero size to maintain class state
decorated().put(key, coll);
result = true; // map definitely changed
}
} else {
- result = coll.add(value);
+ result = coll.add((V) value);
}
return (result ? value : null);
}
@@ -230,17 +233,15 @@
*
* @param map the map to copy (either a normal or multi map)
*/
- public void putAll(Map map) {
+ @SuppressWarnings("unchecked")
+ public void putAll(Map<? extends K, ?> map) {
if (map instanceof MultiMap) {
- for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
- Map.Entry entry = (Map.Entry) it.next();
- Collection coll = (Collection) entry.getValue();
- putAll(entry.getKey(), coll);
+ for (Map.Entry<? extends K, Object> entry : ((MultiMap<? extends K, V>) map).entrySet()) {
+ putAll(entry.getKey(), (Collection<V>) entry.getValue());
}
} else {
- for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
- Map.Entry entry = (Map.Entry) it.next();
- put(entry.getKey(), entry.getValue());
+ for (Map.Entry<? extends K, ?> entry : map.entrySet()) {
+ put(entry.getKey(), (V) entry.getValue());
}
}
}
@@ -252,9 +253,10 @@
*
* @return a collection view of the values contained in this map
*/
- public Collection values() {
- Collection vs = values;
- return (vs != null ? vs : (values = new Values()));
+ @SuppressWarnings("unchecked")
+ public Collection<Object> values() {
+ Collection<V> vs = values;
+ return (Collection<Object>) (vs != null ? vs : (values = new Values()));
}
/**
@@ -264,7 +266,7 @@
* @return true if the map contains the value
*/
public boolean containsValue(Object key, Object value) {
- Collection coll = getCollection(key);
+ Collection<V> coll = getCollection(key);
if (coll == null) {
return false;
}
@@ -278,8 +280,9 @@
* @param key the key to retrieve
* @return the collection mapped to the key, null if no mapping
*/
- public Collection getCollection(Object key) {
- return (Collection) decorated().get(key);
+ @SuppressWarnings("unchecked")
+ public Collection<V> getCollection(Object key) {
+ return (Collection<V>) decorated().get(key);
}
/**
@@ -289,7 +292,7 @@
* @return the size of the collection at the key, zero if key not in map
*/
public int size(Object key) {
- Collection coll = getCollection(key);
+ Collection<V> coll = getCollection(key);
if (coll == null) {
return 0;
}
@@ -304,12 +307,12 @@
* @param values the values to add to the collection at the key, null ignored
* @return true if this map changed
*/
- public boolean putAll(Object key, Collection values) {
+ public boolean putAll(K key, Collection<V> values) {
if (values == null || values.size() == 0) {
return false;
}
boolean result = false;
- Collection coll = getCollection(key);
+ Collection<V> coll = getCollection(key);
if (coll == null) {
coll = createCollection(values.size()); // might produce a non-empty collection
coll.addAll(values);
@@ -330,12 +333,11 @@
* @param key the key to get an iterator for
* @return the iterator of the collection at the key, empty iterator if key not in map
*/
- public Iterator iterator(Object key) {
+ public Iterator<V> iterator(Object key) {
if (!containsKey(key)) {
- return EmptyIterator.INSTANCE;
- } else {
- return new ValuesIterator(key);
+ return EmptyIterator.<V>getInstance();
}
+ return new ValuesIterator(key);
}
/**
@@ -345,10 +347,8 @@
*/
public int totalSize() {
int total = 0;
- Collection values = decorated().values();
- for (Iterator it = values.iterator(); it.hasNext();) {
- Collection coll = (Collection) it.next();
- total += coll.size();
+ for (Object v : decorated().values()) {
+ total += CollectionUtils.size(v);
}
return total;
}
@@ -363,18 +363,18 @@
* @param size the collection size that is about to be added
* @return the new collection
*/
- protected Collection createCollection(int size) {
- return (Collection) collectionFactory.create();
+ protected Collection<V> createCollection(int size) {
+ return collectionFactory.create();
}
//-----------------------------------------------------------------------
/**
* Inner class that provides the values view.
*/
- private class Values extends AbstractCollection {
- public Iterator iterator() {
- final IteratorChain chain = new IteratorChain();
- for (Iterator it = keySet().iterator(); it.hasNext();) {
+ private class Values extends AbstractCollection<V> {
+ public Iterator<V> iterator() {
+ final IteratorChain<V> chain = new IteratorChain<V>();
+ for (Iterator<K> it = keySet().iterator(); it.hasNext();) {
chain.addIterator(new ValuesIterator(it.next()));
}
return chain;
@@ -392,10 +392,10 @@
/**
* Inner class that provides the values iterator.
*/
- private class ValuesIterator implements Iterator {
+ private class ValuesIterator implements Iterator<V> {
private final Object key;
- private final Collection values;
- private final Iterator iterator;
+ private final Collection<V> values;
+ private final Iterator<V> iterator;
public ValuesIterator(Object key) {
this.key = key;
@@ -414,7 +414,7 @@
return iterator.hasNext();
}
- public Object next() {
+ public V next() {
return iterator.next();
}
}
@@ -422,14 +422,14 @@
/**
* Inner class that provides a simple reflection factory.
*/
- private static class ReflectionFactory implements Factory {
- private final Class clazz;
+ private static class ReflectionFactory<T extends Collection<?>> implements Factory<T> {
+ private final Class<T> clazz;
- public ReflectionFactory(Class clazz) {
+ public ReflectionFactory(Class<T> clazz) {
this.clazz = clazz;
}
- public Object create() {
+ public T create() {
try {
return clazz.newInstance();
} catch (Exception ex) {
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/PredicatedMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/PredicatedMap.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/PredicatedMap.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/PredicatedMap.java Thu Jan 29 18:48:37 2009
@@ -50,17 +50,18 @@
* @author Stephen Colebourne
* @author Paul Jack
*/
-public class PredicatedMap
- extends AbstractInputCheckedMapDecorator
+public class PredicatedMap<K, V>
+ extends AbstractInputCheckedMapDecorator<K, V>
implements Serializable {
/** Serialization version */
private static final long serialVersionUID = 7412622456128415156L;
/** The key predicate to use */
- protected final Predicate keyPredicate;
+ protected final Predicate<? super K> keyPredicate;
+
/** The value predicate to use */
- protected final Predicate valuePredicate;
+ protected final Predicate<? super V> valuePredicate;
/**
* Factory method to create a predicated (validating) map.
@@ -73,8 +74,8 @@
* @param valuePredicate the predicate to validate to values, null means no check
* @throws IllegalArgumentException if the map is null
*/
- public static Map decorate(Map map, Predicate keyPredicate, Predicate valuePredicate) {
- return new PredicatedMap(map, keyPredicate, valuePredicate);
+ public static <K, V> Map<K, V> decorate(Map<K, V> map, Predicate<? super K> keyPredicate, Predicate<? super V> valuePredicate) {
+ return new PredicatedMap<K, V>(map, keyPredicate, valuePredicate);
}
//-----------------------------------------------------------------------
@@ -86,17 +87,15 @@
* @param valuePredicate the predicate to validate to values, null means no check
* @throws IllegalArgumentException if the map is null
*/
- protected PredicatedMap(Map map, Predicate keyPredicate, Predicate valuePredicate) {
+ protected PredicatedMap(Map<K, V> map, Predicate<? super K> keyPredicate, Predicate<? super V> valuePredicate) {
super(map);
this.keyPredicate = keyPredicate;
this.valuePredicate = valuePredicate;
- Iterator it = map.entrySet().iterator();
+ Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
while (it.hasNext()) {
- Map.Entry entry = (Map.Entry) it.next();
- Object key = entry.getKey();
- Object value = entry.getValue();
- validate(key, value);
+ Map.Entry<K, V> entry = it.next();
+ validate(entry.getKey(), entry.getValue());
}
}
@@ -121,6 +120,7 @@
* @throws ClassNotFoundException
* @since Commons Collections 3.1
*/
+ @SuppressWarnings("unchecked")
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
map = (Map) in.readObject();
@@ -134,7 +134,7 @@
* @param value the value to validate
* @throws IllegalArgumentException if invalid
*/
- protected void validate(Object key, Object value) {
+ protected void validate(K key, V value) {
if (keyPredicate != null && keyPredicate.evaluate(key) == false) {
throw new IllegalArgumentException("Cannot add key - Predicate rejected it");
}
@@ -150,7 +150,7 @@
* @throws IllegalArgumentException if invalid
* @since Commons Collections 3.1
*/
- protected Object checkSetValue(Object value) {
+ protected V checkSetValue(V value) {
if (valuePredicate.evaluate(value) == false) {
throw new IllegalArgumentException("Cannot set value - Predicate rejected it");
}
@@ -168,20 +168,16 @@
}
//-----------------------------------------------------------------------
- public Object put(Object key, Object value) {
+ public V put(K key, V value) {
validate(key, value);
return map.put(key, value);
}
- public void putAll(Map mapToCopy) {
- Iterator it = mapToCopy.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry entry = (Map.Entry) it.next();
- Object key = entry.getKey();
- Object value = entry.getValue();
- validate(key, value);
+ public void putAll(Map<? extends K, ? extends V> mapToCopy) {
+ for (Map.Entry<? extends K, ? extends V> entry : mapToCopy.entrySet()) {
+ validate(entry.getKey(), entry.getValue());
}
- map.putAll(mapToCopy);
+ super.putAll(mapToCopy);
}
}
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/PredicatedSortedMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/PredicatedSortedMap.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/PredicatedSortedMap.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/PredicatedSortedMap.java Thu Jan 29 18:48:37 2009
@@ -46,9 +46,7 @@
* @author Stephen Colebourne
* @author Paul Jack
*/
-public class PredicatedSortedMap
- extends PredicatedMap
- implements SortedMap {
+public class PredicatedSortedMap<K, V> extends PredicatedMap<K, V> implements SortedMap<K, V> {
/** Serialization version */
private static final long serialVersionUID = 3359846175935304332L;
@@ -64,8 +62,9 @@
* @param valuePredicate the predicate to validate to values, null means no check
* @throws IllegalArgumentException if the map is null
*/
- public static SortedMap decorate(SortedMap map, Predicate keyPredicate, Predicate valuePredicate) {
- return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
+ public static <K, V> SortedMap<K, V> decorate(SortedMap<K, V> map,
+ Predicate<? super K> keyPredicate, Predicate<? super V> valuePredicate) {
+ return new PredicatedSortedMap<K, V>(map, keyPredicate, valuePredicate);
}
//-----------------------------------------------------------------------
@@ -77,7 +76,8 @@
* @param valuePredicate the predicate to validate to values, null means no check
* @throws IllegalArgumentException if the map is null
*/
- protected PredicatedSortedMap(SortedMap map, Predicate keyPredicate, Predicate valuePredicate) {
+ protected PredicatedSortedMap(SortedMap<K, V> map, Predicate<? super K> keyPredicate,
+ Predicate<? super V> valuePredicate) {
super(map, keyPredicate, valuePredicate);
}
@@ -87,36 +87,36 @@
*
* @return the decorated map
*/
- protected SortedMap getSortedMap() {
- return (SortedMap) map;
+ protected SortedMap<K, V> getSortedMap() {
+ return (SortedMap<K, V>) map;
}
//-----------------------------------------------------------------------
- public Object firstKey() {
+ public K firstKey() {
return getSortedMap().firstKey();
}
- public Object lastKey() {
+ public K lastKey() {
return getSortedMap().lastKey();
}
- public Comparator comparator() {
+ public Comparator<? super K> comparator() {
return getSortedMap().comparator();
}
- public SortedMap subMap(Object fromKey, Object toKey) {
- SortedMap map = getSortedMap().subMap(fromKey, toKey);
- return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
+ public SortedMap<K, V> subMap(K fromKey, K toKey) {
+ SortedMap<K, V> map = getSortedMap().subMap(fromKey, toKey);
+ return new PredicatedSortedMap<K, V>(map, keyPredicate, valuePredicate);
}
- public SortedMap headMap(Object toKey) {
- SortedMap map = getSortedMap().headMap(toKey);
- return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
+ public SortedMap<K, V> headMap(K toKey) {
+ SortedMap<K, V> map = getSortedMap().headMap(toKey);
+ return new PredicatedSortedMap<K, V>(map, keyPredicate, valuePredicate);
}
- public SortedMap tailMap(Object fromKey) {
- SortedMap map = getSortedMap().tailMap(fromKey);
- return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
+ public SortedMap<K, V> tailMap(K fromKey) {
+ SortedMap<K, V> map = getSortedMap().tailMap(fromKey);
+ return new PredicatedSortedMap<K, V>(map, keyPredicate, valuePredicate);
}
}
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/ReferenceIdentityMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/ReferenceIdentityMap.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/ReferenceIdentityMap.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/ReferenceIdentityMap.java Thu Jan 29 18:48:37 2009
@@ -70,7 +70,7 @@
*
* @author Stephen Colebourne
*/
-public class ReferenceIdentityMap extends AbstractReferenceMap implements Serializable {
+public class ReferenceIdentityMap<K, V> extends AbstractReferenceMap<K, V> implements Serializable {
/** Serialization version */
private static final long serialVersionUID = -1266190134568365852L;
@@ -80,7 +80,8 @@
* use hard references to keys and soft references to values.
*/
public ReferenceIdentityMap() {
- super(HARD, SOFT, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
+ super(ReferenceStrength.HARD, ReferenceStrength.SOFT, DEFAULT_CAPACITY,
+ DEFAULT_LOAD_FACTOR, false);
}
/**
@@ -92,7 +93,7 @@
* @param valueType the type of reference to use for values;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
*/
- public ReferenceIdentityMap(int keyType, int valueType) {
+ public ReferenceIdentityMap(ReferenceStrength keyType, ReferenceStrength valueType) {
super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
}
@@ -107,7 +108,8 @@
* @param purgeValues should the value be automatically purged when the
* key is garbage collected
*/
- public ReferenceIdentityMap(int keyType, int valueType, boolean purgeValues) {
+ public ReferenceIdentityMap(ReferenceStrength keyType, ReferenceStrength valueType,
+ boolean purgeValues) {
super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, purgeValues);
}
@@ -122,7 +124,8 @@
* @param capacity the initial capacity for the map
* @param loadFactor the load factor for the map
*/
- public ReferenceIdentityMap(int keyType, int valueType, int capacity, float loadFactor) {
+ public ReferenceIdentityMap(ReferenceStrength keyType, ReferenceStrength valueType,
+ int capacity, float loadFactor) {
super(keyType, valueType, capacity, loadFactor, false);
}
@@ -139,8 +142,8 @@
* @param purgeValues should the value be automatically purged when the
* key is garbage collected
*/
- public ReferenceIdentityMap(int keyType, int valueType, int capacity,
- float loadFactor, boolean purgeValues) {
+ public ReferenceIdentityMap(ReferenceStrength keyType, ReferenceStrength valueType,
+ int capacity, float loadFactor, boolean purgeValues) {
super(keyType, valueType, capacity, loadFactor, purgeValues);
}
@@ -182,8 +185,8 @@
* @return true if equal by identity
*/
protected boolean isEqualKey(Object key1, Object key2) {
- key2 = (keyType > HARD ? ((Reference) key2).get() : key2);
- return (key1 == key2);
+ key2 = keyType == ReferenceStrength.HARD ? key2 : ((Reference<?>) key2).get();
+ return key1 == key2;
}
/**
@@ -196,7 +199,7 @@
* @return true if equal by identity
*/
protected boolean isEqualValue(Object value1, Object value2) {
- return (value1 == value2);
+ return value1 == value2;
}
//-----------------------------------------------------------------------
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/ReferenceMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/ReferenceMap.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/ReferenceMap.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/ReferenceMap.java Thu Jan 29 18:48:37 2009
@@ -73,7 +73,7 @@
* @author Paul Jack
* @author Stephen Colebourne
*/
-public class ReferenceMap extends AbstractReferenceMap implements Serializable {
+public class ReferenceMap<K, V> extends AbstractReferenceMap<K, V> implements Serializable {
/** Serialization version */
private static final long serialVersionUID = 1555089888138299607L;
@@ -83,7 +83,8 @@
* use hard references to keys and soft references to values.
*/
public ReferenceMap() {
- super(HARD, SOFT, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
+ super(ReferenceStrength.HARD, ReferenceStrength.SOFT, DEFAULT_CAPACITY,
+ DEFAULT_LOAD_FACTOR, false);
}
/**
@@ -95,7 +96,7 @@
* @param valueType the type of reference to use for values;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
*/
- public ReferenceMap(int keyType, int valueType) {
+ public ReferenceMap(ReferenceStrength keyType, ReferenceStrength valueType) {
super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
}
@@ -110,7 +111,7 @@
* @param purgeValues should the value be automatically purged when the
* key is garbage collected
*/
- public ReferenceMap(int keyType, int valueType, boolean purgeValues) {
+ public ReferenceMap(ReferenceStrength keyType, ReferenceStrength valueType, boolean purgeValues) {
super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, purgeValues);
}
@@ -126,7 +127,8 @@
* @param capacity the initial capacity for the map
* @param loadFactor the load factor for the map
*/
- public ReferenceMap(int keyType, int valueType, int capacity, float loadFactor) {
+ public ReferenceMap(ReferenceStrength keyType, ReferenceStrength valueType, int capacity,
+ float loadFactor) {
super(keyType, valueType, capacity, loadFactor, false);
}
@@ -144,8 +146,8 @@
* @param purgeValues should the value be automatically purged when the
* key is garbage collected
*/
- public ReferenceMap(int keyType, int valueType, int capacity,
- float loadFactor, boolean purgeValues) {
+ public ReferenceMap(ReferenceStrength keyType, ReferenceStrength valueType, int capacity,
+ float loadFactor, boolean purgeValues) {
super(keyType, valueType, capacity, loadFactor, purgeValues);
}
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/SingletonMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/SingletonMap.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/SingletonMap.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/SingletonMap.java Thu Jan 29 18:48:37 2009
@@ -27,7 +27,6 @@
import org.apache.commons.collections.BoundedMap;
import org.apache.commons.collections.KeyValue;
-import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.OrderedMap;
import org.apache.commons.collections.OrderedMapIterator;
import org.apache.commons.collections.ResettableIterator;
@@ -59,16 +58,16 @@
*
* @author Stephen Colebourne
*/
-public class SingletonMap
- implements OrderedMap, BoundedMap, KeyValue, Serializable, Cloneable {
+public class SingletonMap<K, V>
+ implements OrderedMap<K, V>, BoundedMap<K, V>, KeyValue<K, V>, Serializable, Cloneable {
/** Serialization version */
private static final long serialVersionUID = -8931271118676803261L;
/** Singleton key */
- private final Object key;
+ private final K key;
/** Singleton value */
- private Object value;
+ private V value;
/**
* Constructor that creates a map of <code>null</code> to <code>null</code>.
@@ -84,7 +83,7 @@
* @param key the key to use
* @param value the value to use
*/
- public SingletonMap(Object key, Object value) {
+ public SingletonMap(K key, V value) {
super();
this.key = key;
this.value = value;
@@ -95,7 +94,7 @@
*
* @param keyValue the key value pair to use
*/
- public SingletonMap(KeyValue keyValue) {
+ public SingletonMap(KeyValue<K, V> keyValue) {
super();
this.key = keyValue.getKey();
this.value = keyValue.getValue();
@@ -106,7 +105,7 @@
*
* @param mapEntry the mapEntry to use
*/
- public SingletonMap(Map.Entry mapEntry) {
+ public SingletonMap(Map.Entry<K, V> mapEntry) {
super();
this.key = mapEntry.getKey();
this.value = mapEntry.getValue();
@@ -119,12 +118,12 @@
* @throws NullPointerException if the map is null
* @throws IllegalArgumentException if the size is not 1
*/
- public SingletonMap(Map map) {
+ public SingletonMap(Map<K, V> map) {
super();
if (map.size() != 1) {
throw new IllegalArgumentException("The map size must be 1");
}
- Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
+ Map.Entry<K, V> entry = map.entrySet().iterator().next();
this.key = entry.getKey();
this.value = entry.getValue();
}
@@ -136,7 +135,7 @@
*
* @return the key
*/
- public Object getKey() {
+ public K getKey() {
return key;
}
@@ -145,7 +144,7 @@
*
* @return the value
*/
- public Object getValue() {
+ public V getValue() {
return value;
}
@@ -155,8 +154,8 @@
* @param value the new value to set
* @return the old value
*/
- public Object setValue(Object value) {
- Object old = this.value;
+ public V setValue(V value) {
+ V old = this.value;
this.value = value;
return old;
}
@@ -189,7 +188,7 @@
* @param key the key
* @return the mapped value, null if no match
*/
- public Object get(Object key) {
+ public V get(Object key) {
if (isEqualKey(key)) {
return value;
}
@@ -247,7 +246,7 @@
* @return the value previously mapped to this key, null if none
* @throws IllegalArgumentException if the key does not match
*/
- public Object put(Object key, Object value) {
+ public V put(K key, V value) {
if (isEqualKey(key)) {
return setValue(value);
}
@@ -265,21 +264,21 @@
* @throws NullPointerException if the map is null
* @throws IllegalArgumentException if the key does not match
*/
- public void putAll(Map map) {
+ public void putAll(Map<? extends K, ? extends V> map) {
switch (map.size()) {
case 0:
return;
-
+
case 1:
- Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
+ Map.Entry<? extends K, ? extends V> entry = map.entrySet().iterator().next();
put(entry.getKey(), entry.getValue());
return;
-
+
default:
throw new IllegalArgumentException("The map size must be 0 or 1");
}
}
-
+
/**
* Unsupported operation.
*
@@ -287,7 +286,7 @@
* @return the value mapped to the removed key, null if key not in map
* @throws UnsupportedOperationException always
*/
- public Object remove(Object key) {
+ public V remove(Object key) {
throw new UnsupportedOperationException();
}
@@ -306,8 +305,8 @@
*
* @return the entrySet view
*/
- public Set entrySet() {
- Map.Entry entry = new TiedMapEntry(this, getKey());
+ public Set<Map.Entry<K, V>> entrySet() {
+ Map.Entry<K, V> entry = new TiedMapEntry<K, V>(this, getKey());
return Collections.singleton(entry);
}
@@ -318,7 +317,7 @@
*
* @return the keySet view
*/
- public Set keySet() {
+ public Set<K> keySet() {
return Collections.singleton(key);
}
@@ -329,38 +328,15 @@
*
* @return the values view
*/
- public Collection values() {
- return new SingletonValues(this);
+ public Collection<V> values() {
+ return new SingletonValues<V>(this);
}
/**
- * Gets an iterator over the map.
- * Changes made to the iterator using <code>setValue</code> affect this map.
- * The <code>remove</code> method is unsupported.
- * <p>
- * A MapIterator returns the keys in the map. It also provides convenient
- * methods to get the key and value, and set the value.
- * It avoids the need to create an entrySet/keySet/values object.
- * It also avoids creating the Map Entry object.
- *
- * @return the map iterator
- */
- public MapIterator mapIterator() {
- return new SingletonMapIterator(this);
- }
-
- // OrderedMap
- //-----------------------------------------------------------------------
- /**
- * Obtains an <code>OrderedMapIterator</code> over the map.
- * <p>
- * A ordered map iterator is an efficient way of iterating over maps
- * in both directions.
- *
- * @return an ordered map iterator
+ * {@inheritDoc}
*/
- public OrderedMapIterator orderedMapIterator() {
- return new SingletonMapIterator(this);
+ public OrderedMapIterator<K, V> mapIterator() {
+ return new SingletonMapIterator<K, V>(this);
}
/**
@@ -368,7 +344,7 @@
*
* @return the key
*/
- public Object firstKey() {
+ public K firstKey() {
return getKey();
}
@@ -377,7 +353,7 @@
*
* @return the key
*/
- public Object lastKey() {
+ public K lastKey() {
return getKey();
}
@@ -387,7 +363,7 @@
* @param key the next key
* @return null always
*/
- public Object nextKey(Object key) {
+ public K nextKey(K key) {
return null;
}
@@ -397,7 +373,7 @@
* @param key the next key
* @return null always
*/
- public Object previousKey(Object key) {
+ public K previousKey(K key) {
return null;
}
@@ -426,12 +402,12 @@
/**
* SingletonMapIterator.
*/
- static class SingletonMapIterator implements OrderedMapIterator, ResettableIterator {
- private final SingletonMap parent;
+ static class SingletonMapIterator<K, V> implements OrderedMapIterator<K, V>, ResettableIterator<K> {
+ private final SingletonMap<K, V> parent;
private boolean hasNext = true;
private boolean canGetSet = false;
- SingletonMapIterator(SingletonMap parent) {
+ SingletonMapIterator(SingletonMap<K, V> parent) {
super();
this.parent = parent;
}
@@ -440,7 +416,7 @@
return hasNext;
}
- public Object next() {
+ public K next() {
if (hasNext == false) {
throw new NoSuchElementException(AbstractHashedMap.NO_NEXT_ENTRY);
}
@@ -453,7 +429,7 @@
return (hasNext == false);
}
- public Object previous() {
+ public K previous() {
if (hasNext == true) {
throw new NoSuchElementException(AbstractHashedMap.NO_PREVIOUS_ENTRY);
}
@@ -465,21 +441,21 @@
throw new UnsupportedOperationException();
}
- public Object getKey() {
+ public K getKey() {
if (canGetSet == false) {
throw new IllegalStateException(AbstractHashedMap.GETKEY_INVALID);
}
return parent.getKey();
}
- public Object getValue() {
+ public V getValue() {
if (canGetSet == false) {
throw new IllegalStateException(AbstractHashedMap.GETVALUE_INVALID);
}
return parent.getValue();
}
- public Object setValue(Object value) {
+ public V setValue(V value) {
if (canGetSet == false) {
throw new IllegalStateException(AbstractHashedMap.SETVALUE_INVALID);
}
@@ -493,9 +469,8 @@
public String toString() {
if (hasNext) {
return "Iterator[]";
- } else {
- return "Iterator[" + getKey() + "=" + getValue() + "]";
}
+ return "Iterator[" + getKey() + "=" + getValue() + "]";
}
}
@@ -503,11 +478,11 @@
* Values implementation for the SingletonMap.
* This class is needed as values is a view that must update as the map updates.
*/
- static class SingletonValues extends AbstractSet implements Serializable {
+ static class SingletonValues<V> extends AbstractSet<V> implements Serializable {
private static final long serialVersionUID = -3689524741863047872L;
- private final SingletonMap parent;
+ private final SingletonMap<?, V> parent;
- SingletonValues(SingletonMap parent) {
+ SingletonValues(SingletonMap<?, V> parent) {
super();
this.parent = parent;
}
@@ -524,8 +499,8 @@
public void clear() {
throw new UnsupportedOperationException();
}
- public Iterator iterator() {
- return new SingletonIterator(parent.getValue(), false);
+ public Iterator<V> iterator() {
+ return new SingletonIterator<V>(parent.getValue(), false);
}
}
@@ -535,10 +510,10 @@
*
* @return a shallow clone
*/
- public Object clone() {
+ @SuppressWarnings("unchecked")
+ public SingletonMap<K, V> clone() {
try {
- SingletonMap cloned = (SingletonMap) super.clone();
- return cloned;
+ return (SingletonMap<K, V>) super.clone();
} catch (CloneNotSupportedException ex) {
throw new InternalError();
}
@@ -550,6 +525,7 @@
* @param obj the object to compare to
* @return true if equal
*/
+ @SuppressWarnings("unchecked")
public boolean equals(Object obj) {
if (obj == this) {
return true;
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/StaticBucketMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/StaticBucketMap.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/StaticBucketMap.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/StaticBucketMap.java Thu Jan 29 18:48:37 2009
@@ -101,12 +101,12 @@
* @author Janek Bogucki
* @author Kazuya Ujihara
*/
-public final class StaticBucketMap implements Map {
+public final class StaticBucketMap<K, V> implements Map<K, V> {
/** The default number of buckets to use */
private static final int DEFAULT_BUCKETS = 255;
/** The array of buckets, where the actual data is held */
- private Node[] buckets;
+ private Node<K, V>[] buckets;
/** The matching array of locks */
private Lock[] locks;
@@ -127,6 +127,7 @@
*
* @param numBuckets the number of buckets for this map
*/
+ @SuppressWarnings("unchecked")
public StaticBucketMap(int numBuckets) {
int size = Math.max(17, numBuckets);
@@ -202,11 +203,11 @@
* @param key the key to retrieve
* @return the associated value
*/
- public Object get(final Object key) {
+ public V get(final Object key) {
int hash = getHash(key);
synchronized (locks[hash]) {
- Node n = buckets[hash];
+ Node<K, V> n = buckets[hash];
while (n != null) {
if (n.key == key || (n.key != null && n.key.equals(key))) {
@@ -229,7 +230,7 @@
int hash = getHash(key);
synchronized (locks[hash]) {
- Node n = buckets[hash];
+ Node<K, V> n = buckets[hash];
while (n != null) {
if (n.key == key || (n.key != null && n.key.equals(key))) {
@@ -251,7 +252,7 @@
public boolean containsValue(final Object value) {
for (int i = 0; i < buckets.length; i++) {
synchronized (locks[i]) {
- Node n = buckets[i];
+ Node<K, V> n = buckets[i];
while (n != null) {
if (n.value == value || (n.value != null && n.value.equals(value))) {
@@ -273,14 +274,14 @@
* @param value the value to use
* @return the previous mapping for the key
*/
- public Object put(final Object key, final Object value) {
+ public V put(final K key, final V value) {
int hash = getHash(key);
synchronized (locks[hash]) {
- Node n = buckets[hash];
+ Node<K, V> n = buckets[hash];
if (n == null) {
- n = new Node();
+ n = new Node<K, V>();
n.key = key;
n.value = value;
buckets[hash] = n;
@@ -291,11 +292,11 @@
// Set n to the last node in the linked list. Check each key along the way
// If the key is found, then change the value of that node and return
// the old value.
- for (Node next = n; next != null; next = next.next) {
+ for (Node<K, V> next = n; next != null; next = next.next) {
n = next;
if (n.key == key || (n.key != null && n.key.equals(key))) {
- Object returnVal = n.value;
+ V returnVal = n.value;
n.value = value;
return returnVal;
}
@@ -303,7 +304,7 @@
// The key was not found in the current list of nodes, add it to the end
// in a new node.
- Node newNode = new Node();
+ Node<K, V> newNode = new Node<K, V>();
newNode.key = key;
newNode.value = value;
n.next = newNode;
@@ -318,12 +319,12 @@
* @param key the key to remove
* @return the previous value at this key
*/
- public Object remove(Object key) {
+ public V remove(Object key) {
int hash = getHash(key);
synchronized (locks[hash]) {
- Node n = buckets[hash];
- Node prev = null;
+ Node<K, V> n = buckets[hash];
+ Node<K, V> prev = null;
while (n != null) {
if (n.key == key || (n.key != null && n.key.equals(key))) {
@@ -345,14 +346,14 @@
}
return null;
}
-
+
//-----------------------------------------------------------------------
/**
* Gets the key set.
*
* @return the key set
*/
- public Set keySet() {
+ public Set<K> keySet() {
return new KeySet();
}
@@ -361,7 +362,7 @@
*
* @return the values
*/
- public Collection values() {
+ public Collection<V> values() {
return new Values();
}
@@ -370,7 +371,7 @@
*
* @return the entry set
*/
- public Set entrySet() {
+ public Set<Map.Entry<K, V>> entrySet() {
return new EntrySet();
}
@@ -381,11 +382,11 @@
*
* @param map the map of entries to add
*/
- public void putAll(Map map) {
- Iterator i = map.keySet().iterator();
+ public void putAll(Map<? extends K, ? extends V> map) {
+ Iterator<? extends K> i = map.keySet().iterator();
while (i.hasNext()) {
- Object key = i.next();
+ K key = i.next();
put(key, map.get(key));
}
}
@@ -416,7 +417,7 @@
if (obj instanceof Map == false) {
return false;
}
- Map other = (Map) obj;
+ Map<?, ?> other = (Map<?, ?>) obj;
return entrySet().equals(other.entrySet());
}
@@ -430,7 +431,7 @@
for (int i = 0; i < buckets.length; i++) {
synchronized (locks[i]) {
- Node n = buckets[i];
+ Node<K, V> n = buckets[i];
while (n != null) {
hashCode += n.hashCode();
@@ -445,16 +446,16 @@
/**
* The Map.Entry for the StaticBucketMap.
*/
- private static final class Node implements Map.Entry, KeyValue {
- protected Object key;
- protected Object value;
- protected Node next;
+ private static final class Node<K, V> implements Map.Entry<K, V>, KeyValue<K, V> {
+ protected K key;
+ protected V value;
+ protected Node<K, V> next;
- public Object getKey() {
+ public K getKey() {
return key;
}
- public Object getValue() {
+ public V getValue() {
return value;
}
@@ -471,20 +472,19 @@
return false;
}
- Map.Entry e2 = (Map.Entry) obj;
+ Map.Entry<?, ?> e2 = (Map.Entry<?, ?>) obj;
return (
(key == null ? e2.getKey() == null : key.equals(e2.getKey())) &&
(value == null ? e2.getValue() == null : value.equals(e2.getValue())));
}
- public Object setValue(Object obj) {
- Object retVal = value;
+ public V setValue(V obj) {
+ V retVal = value;
value = obj;
return retVal;
}
}
-
/**
* The lock object, which also includes a count of the nodes in this lock.
*/
@@ -492,20 +492,17 @@
public int size;
}
-
//-----------------------------------------------------------------------
- private class EntryIterator implements Iterator {
-
- private ArrayList current = new ArrayList();
+ private class BaseIterator {
+ private ArrayList<Map.Entry<K, V>> current = new ArrayList<Map.Entry<K,V>>();
private int bucket;
- private Map.Entry last;
-
+ private Map.Entry<K, V> last;
public boolean hasNext() {
if (current.size() > 0) return true;
while (bucket < buckets.length) {
synchronized (locks[bucket]) {
- Node n = buckets[bucket];
+ Node<K, V> n = buckets[bucket];
while (n != null) {
current.add(n);
n = n.next;
@@ -517,41 +514,44 @@
return false;
}
- protected Map.Entry nextEntry() {
+ protected Map.Entry<K, V> nextEntry() {
if (!hasNext()) throw new NoSuchElementException();
- last = (Map.Entry)current.remove(current.size() - 1);
+ last = current.remove(current.size() - 1);
return last;
}
- public Object next() {
- return nextEntry();
- }
-
public void remove() {
if (last == null) throw new IllegalStateException();
StaticBucketMap.this.remove(last.getKey());
last = null;
}
+ }
+
+ private class EntryIterator extends BaseIterator implements Iterator<Map.Entry<K, V>> {
+
+ public Map.Entry<K, V> next() {
+ return nextEntry();
+ }
}
- private class ValueIterator extends EntryIterator {
+ private class ValueIterator extends BaseIterator implements Iterator<V> {
- public Object next() {
+ public V next() {
return nextEntry().getValue();
}
}
- private class KeyIterator extends EntryIterator {
+ private class KeyIterator extends BaseIterator implements Iterator<K> {
- public Object next() {
+ public K next() {
return nextEntry().getKey();
}
}
- private class EntrySet extends AbstractSet {
+ private class EntrySet extends AbstractSet<Map.Entry<K, V>> {
public int size() {
return StaticBucketMap.this.size();
@@ -561,15 +561,15 @@
StaticBucketMap.this.clear();
}
- public Iterator iterator() {
+ public Iterator<Map.Entry<K, V>> iterator() {
return new EntryIterator();
}
public boolean contains(Object obj) {
- Map.Entry entry = (Map.Entry) obj;
+ Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;
int hash = getHash(entry.getKey());
synchronized (locks[hash]) {
- for (Node n = buckets[hash]; n != null; n = n.next) {
+ for (Node<K, V> n = buckets[hash]; n != null; n = n.next) {
if (n.equals(entry)) return true;
}
}
@@ -580,10 +580,10 @@
if (obj instanceof Map.Entry == false) {
return false;
}
- Map.Entry entry = (Map.Entry) obj;
+ Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;
int hash = getHash(entry.getKey());
synchronized (locks[hash]) {
- for (Node n = buckets[hash]; n != null; n = n.next) {
+ for (Node<K, V> n = buckets[hash]; n != null; n = n.next) {
if (n.equals(entry)) {
StaticBucketMap.this.remove(n.getKey());
return true;
@@ -595,8 +595,7 @@
}
-
- private class KeySet extends AbstractSet {
+ private class KeySet extends AbstractSet<K> {
public int size() {
return StaticBucketMap.this.size();
@@ -606,7 +605,7 @@
StaticBucketMap.this.clear();
}
- public Iterator iterator() {
+ public Iterator<K> iterator() {
return new KeyIterator();
}
@@ -617,7 +616,7 @@
public boolean remove(Object obj) {
int hash = getHash(obj);
synchronized (locks[hash]) {
- for (Node n = buckets[hash]; n != null; n = n.next) {
+ for (Node<K, V> n = buckets[hash]; n != null; n = n.next) {
Object k = n.getKey();
if ((k == obj) || ((k != null) && k.equals(obj))) {
StaticBucketMap.this.remove(k);
@@ -626,13 +625,12 @@
}
}
return false;
-
}
}
- private class Values extends AbstractCollection {
+ private class Values extends AbstractCollection<V> {
public int size() {
return StaticBucketMap.this.size();
@@ -642,13 +640,12 @@
StaticBucketMap.this.clear();
}
- public Iterator iterator() {
+ public Iterator<V> iterator() {
return new ValueIterator();
}
}
-
/**
* Prevents any operations from occurring on this map while the
* given {@link Runnable} executes. This method can be used, for
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/TransformedMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/TransformedMap.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/TransformedMap.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/TransformedMap.java Thu Jan 29 18:48:37 2009
@@ -20,7 +20,6 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
-import java.util.Iterator;
import java.util.Map;
import org.apache.commons.collections.Transformer;
@@ -46,17 +45,17 @@
*
* @author Stephen Colebourne
*/
-public class TransformedMap
- extends AbstractInputCheckedMapDecorator
+public class TransformedMap<K, V>
+ extends AbstractInputCheckedMapDecorator<K, V>
implements Serializable {
/** Serialization version */
private static final long serialVersionUID = 7023152376788900464L;
/** The transformer to use for the key */
- protected final Transformer keyTransformer;
+ protected final Transformer<? super K, ? extends K> keyTransformer;
/** The transformer to use for the value */
- protected final Transformer valueTransformer;
+ protected final Transformer<? super V, ? extends V> valueTransformer;
/**
* Factory method to create a transforming map.
@@ -70,8 +69,10 @@
* @param valueTransformer the transformer to use for value conversion, null means no transformation
* @throws IllegalArgumentException if map is null
*/
- public static Map decorate(Map map, Transformer keyTransformer, Transformer valueTransformer) {
- return new TransformedMap(map, keyTransformer, valueTransformer);
+ public static <K, V> Map<K, V> decorate(Map<K, V> map,
+ Transformer<? super K, ? extends K> keyTransformer,
+ Transformer<? super V, ? extends V> valueTransformer) {
+ return new TransformedMap<K, V>(map, keyTransformer, valueTransformer);
}
/**
@@ -88,10 +89,12 @@
* @throws IllegalArgumentException if map is null
* @since Commons Collections 3.2
*/
- public static Map decorateTransform(Map map, Transformer keyTransformer, Transformer valueTransformer) {
- TransformedMap decorated = new TransformedMap(map, keyTransformer, valueTransformer);
+ public static <K, V> Map<K, V> decorateTransform(Map<K, V> map,
+ Transformer<? super K, ? extends K> keyTransformer,
+ Transformer<? super V, ? extends V> valueTransformer) {
+ TransformedMap<K, V> decorated = new TransformedMap<K, V>(map, keyTransformer, valueTransformer);
if (map.size() > 0) {
- Map transformed = decorated.transformMap(map);
+ Map<K, V> transformed = decorated.transformMap(map);
decorated.clear();
decorated.decorated().putAll(transformed); // avoids double transformation
}
@@ -110,7 +113,8 @@
* @param valueTransformer the transformer to use for value conversion, null means no conversion
* @throws IllegalArgumentException if map is null
*/
- protected TransformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer) {
+ protected TransformedMap(Map<K, V> map, Transformer<? super K, ? extends K> keyTransformer,
+ Transformer<? super V, ? extends V> valueTransformer) {
super(map);
this.keyTransformer = keyTransformer;
this.valueTransformer = valueTransformer;
@@ -137,6 +141,7 @@
* @throws ClassNotFoundException
* @since Commons Collections 3.1
*/
+ @SuppressWarnings("unchecked")
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
map = (Map) in.readObject();
@@ -151,7 +156,7 @@
* @param object the object to transform
* @throws the transformed object
*/
- protected Object transformKey(Object object) {
+ protected K transformKey(K object) {
if (keyTransformer == null) {
return object;
}
@@ -166,7 +171,7 @@
* @param object the object to transform
* @throws the transformed object
*/
- protected Object transformValue(Object object) {
+ protected V transformValue(V object) {
if (valueTransformer == null) {
return object;
}
@@ -181,14 +186,15 @@
* @param map the map to transform
* @throws the transformed object
*/
- protected Map transformMap(Map map) {
+ @SuppressWarnings("unchecked")
+ protected Map<K, V> transformMap(Map<? extends K, ? extends V> map) {
if (map.isEmpty()) {
- return map;
+ return (Map<K, V>) map;
}
- Map result = new LinkedMap(map.size());
- for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
- Map.Entry entry = (Map.Entry) it.next();
- result.put(transformKey(entry.getKey()), transformValue(entry.getValue()));
+ Map<K, V> result = new LinkedMap<K, V>(map.size());
+
+ for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
+ result.put((K) transformKey(entry.getKey()), transformValue(entry.getValue()));
}
return result;
}
@@ -200,7 +206,7 @@
* @return the transformed value
* @since Commons Collections 3.1
*/
- protected Object checkSetValue(Object value) {
+ protected V checkSetValue(V value) {
return valueTransformer.transform(value);
}
@@ -215,13 +221,13 @@
}
//-----------------------------------------------------------------------
- public Object put(Object key, Object value) {
+ public V put(K key, V value) {
key = transformKey(key);
value = transformValue(value);
return decorated().put(key, value);
}
- public void putAll(Map mapToCopy) {
+ public void putAll(Map<? extends K, ? extends V> mapToCopy) {
mapToCopy = transformMap(mapToCopy);
decorated().putAll(mapToCopy);
}