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@815077 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-15 05:56:05 +00:00
parent ff9ab0abd6
commit 027b776f7e
1 changed files with 20 additions and 19 deletions

View File

@ -21,11 +21,11 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.commons.collections.BoundedMap; import org.apache.commons.collections.BoundedMap;
import org.apache.commons.collections.IterableMap;
import org.apache.commons.collections.collection.UnmodifiableCollection; import org.apache.commons.collections.collection.UnmodifiableCollection;
import org.apache.commons.collections.set.UnmodifiableSet; import org.apache.commons.collections.set.UnmodifiableSet;
@ -56,9 +56,9 @@ import org.apache.commons.collections.set.UnmodifiableSet;
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Paul Jack * @author Paul Jack
*/ */
public class FixedSizeMap public class FixedSizeMap<K, V>
extends AbstractMapDecorator extends AbstractMapDecorator<K, V>
implements Map, BoundedMap, Serializable { implements Map<K, V>, BoundedMap<K, V>, Serializable {
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = 7450927208116179316L; private static final long serialVersionUID = 7450927208116179316L;
@ -69,8 +69,8 @@ public class FixedSizeMap
* @param map the map to decorate, must not be null * @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
*/ */
public static Map decorate(Map map) { public static <K, V> IterableMap<K, V> decorate(Map<K, V> map) {
return new FixedSizeMap(map); return new FixedSizeMap<K, V>(map);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -80,7 +80,7 @@ public class FixedSizeMap
* @param map the map to decorate, must not be null * @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
*/ */
protected FixedSizeMap(Map map) { protected FixedSizeMap(Map<K, V> map) {
super(map); super(map);
} }
@ -105,22 +105,23 @@ public class FixedSizeMap
* @throws ClassNotFoundException * @throws ClassNotFoundException
* @since Commons Collections 3.1 * @since Commons Collections 3.1
*/ */
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject(); in.defaultReadObject();
map = (Map) in.readObject(); map = (Map) in.readObject();
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public Object put(Object key, Object value) { public V put(K key, V value) {
if (map.containsKey(key) == false) { if (map.containsKey(key) == false) {
throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size"); throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
} }
return map.put(key, value); return map.put(key, value);
} }
public void putAll(Map mapToCopy) { public void putAll(Map<? extends K, ? extends V> mapToCopy) {
for (Iterator it = mapToCopy.keySet().iterator(); it.hasNext(); ) { for (K key : mapToCopy.keySet()) {
if (mapToCopy.containsKey(it.next()) == false) { if (!containsKey(key)) {
throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size"); throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
} }
} }
@ -131,23 +132,23 @@ public class FixedSizeMap
throw new UnsupportedOperationException("Map is fixed size"); throw new UnsupportedOperationException("Map is fixed size");
} }
public Object remove(Object key) { public V remove(Object key) {
throw new UnsupportedOperationException("Map is fixed size"); throw new UnsupportedOperationException("Map is fixed size");
} }
public Set entrySet() { public Set<Map.Entry<K, V>> entrySet() {
Set set = map.entrySet(); Set<Map.Entry<K, V>> set = map.entrySet();
// unmodifiable set will still allow modification via Map.Entry objects // unmodifiable set will still allow modification via Map.Entry objects
return UnmodifiableSet.decorate(set); return UnmodifiableSet.decorate(set);
} }
public Set keySet() { public Set<K> keySet() {
Set set = map.keySet(); Set<K> set = map.keySet();
return UnmodifiableSet.decorate(set); return UnmodifiableSet.decorate(set);
} }
public Collection values() { public Collection<V> values() {
Collection coll = map.values(); Collection<V> coll = map.values();
return UnmodifiableCollection.decorate(coll); return UnmodifiableCollection.decorate(coll);
} }