Abstract*Decorator - Generify and use covariant return types

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/branches/collections_jdk5_branch@471180 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-11-04 13:27:44 +00:00
parent 6677c85986
commit 21c47cc136
6 changed files with 145 additions and 74 deletions

View File

@ -38,8 +38,9 @@ import org.apache.commons.collections.map.AbstractMapDecorator;
* @author Stephen Colebourne
*/
public abstract class AbstractBidiMapDecorator
extends AbstractMapDecorator implements BidiMap {
extends AbstractMapDecorator
implements BidiMap {
/**
* Constructor that wraps (not copies).
*
@ -54,26 +55,36 @@ public abstract class AbstractBidiMapDecorator
* Gets the map being decorated.
*
* @return the decorated map
* @deprecated use decorated()
*/
protected BidiMap getBidiMap() {
return (BidiMap) map;
return decorated();
}
/**
* Gets the map being decorated.
*
* @return the decorated map
*/
protected BidiMap decorated() {
return (BidiMap) super.decorated();
}
//-----------------------------------------------------------------------
public MapIterator mapIterator() {
return getBidiMap().mapIterator();
return decorated().mapIterator();
}
public Object getKey(Object value) {
return getBidiMap().getKey(value);
return decorated().getKey(value);
}
public Object removeValue(Object value) {
return getBidiMap().removeValue(value);
return decorated().removeValue(value);
}
public BidiMap inverseBidiMap() {
return getBidiMap().inverseBidiMap();
return decorated().inverseBidiMap();
}
}

View File

@ -37,8 +37,9 @@ import org.apache.commons.collections.OrderedMapIterator;
* @author Stephen Colebourne
*/
public abstract class AbstractOrderedBidiMapDecorator
extends AbstractBidiMapDecorator implements OrderedBidiMap {
extends AbstractBidiMapDecorator
implements OrderedBidiMap {
/**
* Constructor that wraps (not copies).
*
@ -53,34 +54,44 @@ public abstract class AbstractOrderedBidiMapDecorator
* Gets the map being decorated.
*
* @return the decorated map
* @deprecated use decorated()
*/
protected OrderedBidiMap getOrderedBidiMap() {
return (OrderedBidiMap) map;
return decorated();
}
/**
* Gets the map being decorated.
*
* @return the decorated map
*/
protected OrderedBidiMap decorated() {
return (OrderedBidiMap) super.decorated();
}
//-----------------------------------------------------------------------
public OrderedMapIterator orderedMapIterator() {
return getOrderedBidiMap().orderedMapIterator();
return decorated().orderedMapIterator();
}
public Object firstKey() {
return getOrderedBidiMap().firstKey();
return decorated().firstKey();
}
public Object lastKey() {
return getOrderedBidiMap().lastKey();
return decorated().lastKey();
}
public Object nextKey(Object key) {
return getOrderedBidiMap().nextKey(key);
return decorated().nextKey(key);
}
public Object previousKey(Object key) {
return getOrderedBidiMap().previousKey(key);
return decorated().previousKey(key);
}
public OrderedBidiMap inverseOrderedBidiMap() {
return getOrderedBidiMap().inverseOrderedBidiMap();
return decorated().inverseOrderedBidiMap();
}
}

View File

@ -39,8 +39,9 @@ import org.apache.commons.collections.SortedBidiMap;
* @author Stephen Colebourne
*/
public abstract class AbstractSortedBidiMapDecorator
extends AbstractOrderedBidiMapDecorator implements SortedBidiMap {
extends AbstractOrderedBidiMapDecorator
implements SortedBidiMap {
/**
* Constructor that wraps (not copies).
*
@ -55,9 +56,19 @@ public abstract class AbstractSortedBidiMapDecorator
* Gets the map being decorated.
*
* @return the decorated map
* @deprecated use decorated()
*/
protected SortedBidiMap getSortedBidiMap() {
return (SortedBidiMap) map;
return decorated();
}
/**
* Gets the map being decorated.
*
* @return the decorated map
*/
protected SortedBidiMap decorated() {
return (SortedBidiMap) super.decorated();
}
//-----------------------------------------------------------------------
@ -66,19 +77,19 @@ public abstract class AbstractSortedBidiMapDecorator
}
public Comparator comparator() {
return getSortedBidiMap().comparator();
return decorated().comparator();
}
public SortedMap subMap(Object fromKey, Object toKey) {
return getSortedBidiMap().subMap(fromKey, toKey);
return decorated().subMap(fromKey, toKey);
}
public SortedMap headMap(Object toKey) {
return getSortedBidiMap().headMap(toKey);
return decorated().headMap(toKey);
}
public SortedMap tailMap(Object fromKey) {
return getSortedBidiMap().tailMap(fromKey);
return decorated().tailMap(fromKey);
}
}

View File

@ -33,16 +33,18 @@ import java.util.Set;
* implementation it would provide a loophole around the validation.
* But, you might want that loophole, so this class is kept simple.
*
* @param <K> the type of the keys in the map
* @param <V> the type of the values in the map
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Daniel Rall
* @author Stephen Colebourne
*/
public abstract class AbstractMapDecorator implements Map {
public abstract class AbstractMapDecorator<K, V> implements Map<K, V> {
/** The map to decorate */
protected transient Map map;
protected transient Map<K, V> map;
/**
* Constructor only used in deserialization, do not use otherwise.
@ -58,7 +60,7 @@ public abstract class AbstractMapDecorator implements Map {
* @param map the map to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
public AbstractMapDecorator(Map map) {
protected AbstractMapDecorator(Map<K, V> map) {
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
@ -69,73 +71,83 @@ public abstract class AbstractMapDecorator implements Map {
* Gets the map being decorated.
*
* @return the decorated map
* @deprecated use decorated()
*/
protected Map getMap() {
protected Map<K, V> getMap() {
return decorated();
}
/**
* Gets the map being decorated.
*
* @return the decorated map
*/
protected Map<K, V> decorated() {
return map;
}
//-----------------------------------------------------------------------
public void clear() {
map.clear();
decorated().clear();
}
public boolean containsKey(Object key) {
return map.containsKey(key);
return decorated().containsKey(key);
}
public boolean containsValue(Object value) {
return map.containsValue(value);
return decorated().containsValue(value);
}
public Set entrySet() {
return map.entrySet();
public Set<Map.Entry<K, V>> entrySet() {
return decorated().entrySet();
}
public Object get(Object key) {
return map.get(key);
public V get(Object key) {
return decorated().get(key);
}
public boolean isEmpty() {
return map.isEmpty();
return decorated().isEmpty();
}
public Set keySet() {
return map.keySet();
public Set<K> keySet() {
return decorated().keySet();
}
public Object put(Object key, Object value) {
return map.put(key, value);
public V put(K key, V value) {
return decorated().put(key, value);
}
public void putAll(Map mapToCopy) {
map.putAll(mapToCopy);
public void putAll(Map<? extends K, ? extends V> mapToCopy) {
decorated().putAll(mapToCopy);
}
public Object remove(Object key) {
return map.remove(key);
public V remove(Object key) {
return decorated().remove(key);
}
public int size() {
return map.size();
return decorated().size();
}
public Collection values() {
return map.values();
public Collection<V> values() {
return decorated().values();
}
public boolean equals(Object object) {
if (object == this) {
return true;
}
return map.equals(object);
return decorated().equals(object);
}
public int hashCode() {
return map.hashCode();
return decorated().hashCode();
}
public String toString() {
return map.toString();
return decorated().toString();
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections.map;
import java.util.Map;
import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.OrderedMap;
import org.apache.commons.collections.OrderedMapIterator;
@ -38,7 +40,8 @@ import org.apache.commons.collections.OrderedMapIterator;
* @author Stephen Colebourne
*/
public abstract class AbstractOrderedMapDecorator
extends AbstractMapDecorator implements OrderedMap {
extends AbstractMapDecorator
implements OrderedMap {
/**
* Constructor only used in deserialization, do not use otherwise.
@ -62,34 +65,44 @@ public abstract class AbstractOrderedMapDecorator
* Gets the map being decorated.
*
* @return the decorated map
* @deprecated use decorated()
*/
protected OrderedMap getOrderedMap() {
return (OrderedMap) map;
return decorated();
}
/**
* Gets the map being decorated.
*
* @return the decorated map
*/
protected OrderedMap decorated() {
return (OrderedMap) super.decorated();
}
//-----------------------------------------------------------------------
public Object firstKey() {
return getOrderedMap().firstKey();
return decorated().firstKey();
}
public Object lastKey() {
return getOrderedMap().lastKey();
return decorated().lastKey();
}
public Object nextKey(Object key) {
return getOrderedMap().nextKey(key);
return decorated().nextKey(key);
}
public Object previousKey(Object key) {
return getOrderedMap().previousKey(key);
return decorated().previousKey(key);
}
public MapIterator mapIterator() {
return getOrderedMap().mapIterator();
return decorated().mapIterator();
}
public OrderedMapIterator orderedMapIterator() {
return getOrderedMap().orderedMapIterator();
return decorated().orderedMapIterator();
}
}

View File

@ -31,13 +31,16 @@ import java.util.SortedMap;
* it would provide a loophole around the validation.
* But, you might want that loophole, so this class is kept simple.
*
* @param <K> the type of the keys in the map
* @param <V> the type of the values in the map
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
*/
public abstract class AbstractSortedMapDecorator
extends AbstractMapDecorator implements SortedMap {
public abstract class AbstractSortedMapDecorator<K, V>
extends AbstractMapDecorator<K, V>
implements SortedMap<K, V> {
/**
* Constructor only used in deserialization, do not use otherwise.
@ -53,7 +56,7 @@ public abstract class AbstractSortedMapDecorator
* @param map the map to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
public AbstractSortedMapDecorator(SortedMap map) {
public AbstractSortedMapDecorator(SortedMap<K, V> map) {
super(map);
}
@ -61,34 +64,44 @@ public abstract class AbstractSortedMapDecorator
* Gets the map being decorated.
*
* @return the decorated map
* @deprecated use decorated()
*/
protected SortedMap getSortedMap() {
return (SortedMap) map;
protected SortedMap<K, V> getSortedMap() {
return decorated();
}
/**
* Gets the map being decorated.
*
* @return the decorated map
*/
protected SortedMap<K, V> decorated() {
return (SortedMap<K, V>) super.decorated();
}
//-----------------------------------------------------------------------
public Comparator comparator() {
return getSortedMap().comparator();
public Comparator<? super K> comparator() {
return decorated().comparator();
}
public Object firstKey() {
return getSortedMap().firstKey();
public K firstKey() {
return decorated().firstKey();
}
public SortedMap headMap(Object toKey) {
return getSortedMap().headMap(toKey);
public K lastKey() {
return decorated().lastKey();
}
public Object lastKey() {
return getSortedMap().lastKey();
public SortedMap<K, V> subMap(K fromKey, K toKey) {
return decorated().subMap(fromKey, toKey);
}
public SortedMap subMap(Object fromKey, Object toKey) {
return getSortedMap().subMap(fromKey, toKey);
public SortedMap<K, V> headMap(K toKey) {
return decorated().headMap(toKey);
}
public SortedMap tailMap(Object fromKey) {
return getSortedMap().tailMap(fromKey);
public SortedMap<K, V> tailMap(K fromKey) {
return decorated().tailMap(fromKey);
}
}