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@471173 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-11-04 12:07:39 +00:00
parent 88cbfb7236
commit 6677c85986
6 changed files with 138 additions and 73 deletions

View File

@ -38,6 +38,10 @@ Changes from commons-collections
- Removed Typed* containers such as TypedList and TypedMap
- use generics for type safety, or Collections.checked*()
- Switch Abstract*Decorator classes to expose decorated() protected method
instead of the decorated collection directly. Each class overrides decorated()
to add its type covariantly, thus getList()/getSet() etc. methods are removed
Feedback
--------

View File

@ -53,18 +53,28 @@ public abstract class AbstractBufferDecorator extends AbstractCollectionDecorato
* Gets the buffer being decorated.
*
* @return the decorated buffer
* @deprecated use decorated()
*/
protected Buffer getBuffer() {
return (Buffer) getCollection();
return decorated();
}
/**
* Gets the buffer being decorated.
*
* @return the decorated buffer
*/
protected Buffer decorated() {
return (Buffer) super.decorated();
}
//-----------------------------------------------------------------------
public Object get() {
return getBuffer().get();
return decorated().get();
}
public Object remove() {
return getBuffer().remove();
return decorated().remove();
}
}

View File

@ -34,16 +34,17 @@ import java.util.Iterator;
* wrapped collection. This may be undesirable, for example if you are trying
* to write an unmodifiable implementation it might provide a loophole.
*
* @param <E> the type of the elements in the collection
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Paul Jack
*/
public abstract class AbstractCollectionDecorator implements Collection {
public abstract class AbstractCollectionDecorator<E> implements Collection<E> {
/** The collection being decorated */
protected Collection collection;
protected Collection<E> collection;
/**
* Constructor only used in deserialization, do not use otherwise.
@ -59,7 +60,7 @@ public abstract class AbstractCollectionDecorator implements Collection {
* @param coll the collection to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
protected AbstractCollectionDecorator(Collection coll) {
protected AbstractCollectionDecorator(Collection<E> coll) {
if (coll == null) {
throw new IllegalArgumentException("Collection must not be null");
}
@ -70,77 +71,88 @@ public abstract class AbstractCollectionDecorator implements Collection {
* Gets the collection being decorated.
*
* @return the decorated collection
* @deprecated use decorated()
*/
protected Collection getCollection() {
protected Collection<E> getCollection() {
return decorated();
}
/**
* Gets the collection being decorated.
* All access to the decorated collection goes via this method.
*
* @return the decorated collection
*/
protected Collection<E> decorated() {
return collection;
}
//-----------------------------------------------------------------------
public boolean add(Object object) {
return collection.add(object);
public boolean add(E object) {
return decorated().add(object);
}
public boolean addAll(Collection coll) {
return collection.addAll(coll);
public boolean addAll(Collection<? extends E> coll) {
return decorated().addAll(coll);
}
public void clear() {
collection.clear();
decorated().clear();
}
public boolean contains(Object object) {
return collection.contains(object);
return decorated().contains(object);
}
public boolean isEmpty() {
return collection.isEmpty();
return decorated().isEmpty();
}
public Iterator iterator() {
return collection.iterator();
public Iterator<E> iterator() {
return decorated().iterator();
}
public boolean remove(Object object) {
return collection.remove(object);
return decorated().remove(object);
}
public int size() {
return collection.size();
return decorated().size();
}
public Object[] toArray() {
return collection.toArray();
return decorated().toArray();
}
public Object[] toArray(Object[] object) {
return collection.toArray(object);
public <T> T[] toArray(T[] object) {
return decorated().toArray(object);
}
public boolean containsAll(Collection coll) {
return collection.containsAll(coll);
public boolean containsAll(Collection<?> coll) {
return decorated().containsAll(coll);
}
public boolean removeAll(Collection coll) {
return collection.removeAll(coll);
public boolean removeAll(Collection<?> coll) {
return decorated().removeAll(coll);
}
public boolean retainAll(Collection coll) {
return collection.retainAll(coll);
public boolean retainAll(Collection<?> coll) {
return decorated().retainAll(coll);
}
public boolean equals(Object object) {
if (object == this) {
return true;
}
return collection.equals(object);
return decorated().equals(object);
}
public int hashCode() {
return collection.hashCode();
return decorated().hashCode();
}
public String toString() {
return collection.toString();
return decorated().toString();
}
}

View File

@ -27,12 +27,15 @@ import org.apache.commons.collections.collection.AbstractCollectionDecorator;
* <p>
* Methods are forwarded directly to the decorated list.
*
* @param <E> the type of the elements in the list
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
*/
public abstract class AbstractListDecorator extends AbstractCollectionDecorator implements List {
public abstract class AbstractListDecorator<E>
extends AbstractCollectionDecorator<E>
implements List<E> {
/**
* Constructor only used in deserialization, do not use otherwise.
@ -48,7 +51,7 @@ public abstract class AbstractListDecorator extends AbstractCollectionDecorator
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
protected AbstractListDecorator(List list) {
protected AbstractListDecorator(List<E> list) {
super(list);
}
@ -56,50 +59,60 @@ public abstract class AbstractListDecorator extends AbstractCollectionDecorator
* Gets the list being decorated.
*
* @return the decorated list
* @deprecated use decorated()
*/
protected List getList() {
return (List) getCollection();
protected List<E> getList() {
return decorated();
}
/**
* Gets the list being decorated.
*
* @return the decorated list
*/
protected List<E> decorated() {
return (List<E>) super.decorated();
}
//-----------------------------------------------------------------------
public void add(int index, Object object) {
getList().add(index, object);
public void add(int index, E object) {
decorated().add(index, object);
}
public boolean addAll(int index, Collection coll) {
return getList().addAll(index, coll);
public boolean addAll(int index, Collection<? extends E> coll) {
return decorated().addAll(index, coll);
}
public Object get(int index) {
return getList().get(index);
public E get(int index) {
return decorated().get(index);
}
public int indexOf(Object object) {
return getList().indexOf(object);
return decorated().indexOf(object);
}
public int lastIndexOf(Object object) {
return getList().lastIndexOf(object);
return decorated().lastIndexOf(object);
}
public ListIterator listIterator() {
return getList().listIterator();
public ListIterator<E> listIterator() {
return decorated().listIterator();
}
public ListIterator listIterator(int index) {
return getList().listIterator(index);
public ListIterator<E> listIterator(int index) {
return decorated().listIterator(index);
}
public Object remove(int index) {
return getList().remove(index);
public E remove(int index) {
return decorated().remove(index);
}
public Object set(int index, Object object) {
return getList().set(index, object);
public E set(int index, E object) {
return decorated().set(index, object);
}
public List subList(int fromIndex, int toIndex) {
return getList().subList(fromIndex, toIndex);
public List<E> subList(int fromIndex, int toIndex) {
return decorated().subList(fromIndex, toIndex);
}
}

View File

@ -25,12 +25,15 @@ import org.apache.commons.collections.collection.AbstractCollectionDecorator;
* <p>
* Methods are forwarded directly to the decorated set.
*
* @param <E> the type of the elements in the set
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
*/
public abstract class AbstractSetDecorator extends AbstractCollectionDecorator implements Set {
public abstract class AbstractSetDecorator<E>
extends AbstractCollectionDecorator<E>
implements Set<E> {
/**
* Constructor only used in deserialization, do not use otherwise.
@ -46,7 +49,7 @@ public abstract class AbstractSetDecorator extends AbstractCollectionDecorator i
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
protected AbstractSetDecorator(Set set) {
protected AbstractSetDecorator(Set<E> set) {
super(set);
}
@ -54,9 +57,19 @@ public abstract class AbstractSetDecorator extends AbstractCollectionDecorator i
* Gets the set being decorated.
*
* @return the decorated set
* @deprecated use decorated()
*/
protected Set getSet() {
return (Set) getCollection();
protected Set<E> getSet() {
return decorated();
}
/**
* Gets the set being decorated.
*
* @return the decorated set
*/
protected Set<E> decorated() {
return (Set<E>) super.decorated();
}
}

View File

@ -25,12 +25,15 @@ import java.util.SortedSet;
* <p>
* Methods are forwarded directly to the decorated set.
*
* @param <E> the type of the elements in the sorted set
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
*/
public abstract class AbstractSortedSetDecorator extends AbstractSetDecorator implements SortedSet {
public abstract class AbstractSortedSetDecorator<E>
extends AbstractSetDecorator<E>
implements SortedSet<E> {
/**
* Constructor only used in deserialization, do not use otherwise.
@ -46,7 +49,7 @@ public abstract class AbstractSortedSetDecorator extends AbstractSetDecorator im
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
protected AbstractSortedSetDecorator(Set set) {
protected AbstractSortedSetDecorator(Set<E> set) {
super(set);
}
@ -54,34 +57,44 @@ public abstract class AbstractSortedSetDecorator extends AbstractSetDecorator im
* Gets the sorted set being decorated.
*
* @return the decorated set
* @deprecated use decorated()
*/
protected SortedSet getSortedSet() {
return (SortedSet) getCollection();
protected SortedSet<E> getSortedSet() {
return decorated();
}
/**
* Gets the set being decorated.
*
* @return the decorated set
*/
protected SortedSet<E> decorated() {
return (SortedSet<E>) super.decorated();
}
//-----------------------------------------------------------------------
public SortedSet subSet(Object fromElement, Object toElement) {
return getSortedSet().subSet(fromElement, toElement);
public SortedSet<E> subSet(E fromElement, E toElement) {
return decorated().subSet(fromElement, toElement);
}
public SortedSet headSet(Object toElement) {
return getSortedSet().headSet(toElement);
public SortedSet<E> headSet(E toElement) {
return decorated().headSet(toElement);
}
public SortedSet tailSet(Object fromElement) {
return getSortedSet().tailSet(fromElement);
public SortedSet<E> tailSet(E fromElement) {
return decorated().tailSet(fromElement);
}
public Object first() {
return getSortedSet().first();
public E first() {
return decorated().first();
}
public Object last() {
return getSortedSet().last();
public E last() {
return decorated().last();
}
public Comparator comparator() {
return getSortedSet().comparator();
public Comparator<? super E> comparator() {
return decorated().comparator();
}
}