Added Edwin Tellman's patch for COLLECTIONS-243.

It all seems pretty reasonable, and it should all be checked again as the project is worked through

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/branches/collections_jdk5_branch@555925 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Kestle 2007-07-13 10:39:24 +00:00
parent 85b3f4faa4
commit bd9bab2099
23 changed files with 279 additions and 242 deletions

View File

@ -55,7 +55,7 @@ public interface Bag<E> extends Collection<E> {
* @param object the object to search for
* @return the number of occurrences of the object, zero if not found
*/
int getCount(E object);
int getCount(Object object);
/**
* <i>(Violation)</i>
@ -112,7 +112,7 @@ public interface Bag<E> extends Collection<E> {
* @param nCopies the number of copies to remove
* @return <code>true</code> if this call changed the collection
*/
boolean remove(E object, int nCopies);
boolean remove(Object object, int nCopies);
/**
* Returns a {@link Set} of unique elements in the Bag.

View File

@ -85,7 +85,7 @@ public class BagUtils {
* @return a synchronized bag backed by that bag
* @throws IllegalArgumentException if the Bag is null
*/
public static Bag synchronizedBag(Bag bag) {
public static <E> Bag<E> synchronizedBag(Bag<E> bag) {
return SynchronizedBag.decorate(bag);
}
@ -98,7 +98,7 @@ public class BagUtils {
* @return an unmodifiable view of that bag
* @throws IllegalArgumentException if the Bag is null
*/
public static Bag unmodifiableBag(Bag bag) {
public static <E> Bag<E> unmodifiableBag(Bag<E> bag) {
return UnmodifiableBag.decorate(bag);
}
@ -115,7 +115,7 @@ public class BagUtils {
* @return a predicated bag backed by the given bag
* @throws IllegalArgumentException if the Bag or Predicate is null
*/
public static Bag predicatedBag(Bag bag, Predicate predicate) {
public static <E> Bag<E> predicatedBag(Bag<E> bag, Predicate<? super E> predicate) {
return PredicatedBag.decorate(bag, predicate);
}
@ -163,7 +163,7 @@ public class BagUtils {
* @return a synchronized bag backed by that bag
* @throws IllegalArgumentException if the SortedBag is null
*/
public static SortedBag synchronizedSortedBag(SortedBag bag) {
public static <E> SortedBag<E> synchronizedSortedBag(SortedBag<E> bag) {
return SynchronizedSortedBag.decorate(bag);
}
@ -176,7 +176,7 @@ public class BagUtils {
* @return an unmodifiable view of that bag
* @throws IllegalArgumentException if the SortedBag is null
*/
public static SortedBag unmodifiableSortedBag(SortedBag bag) {
public static <E> SortedBag<E> unmodifiableSortedBag(SortedBag<E> bag) {
return UnmodifiableSortedBag.decorate(bag);
}
@ -193,7 +193,7 @@ public class BagUtils {
* @return a predicated bag backed by the given bag
* @throws IllegalArgumentException if the SortedBag or Predicate is null
*/
public static SortedBag predicatedSortedBag(SortedBag bag, Predicate predicate) {
public static <E> SortedBag<E> predicatedSortedBag(SortedBag<E> bag, Predicate<? super E> predicate) {
return PredicatedSortedBag.decorate(bag, predicate);
}

View File

@ -68,7 +68,7 @@ public class ListUtils {
* @throws NullPointerException if either list is null
*/
public static List intersection(final List list1, final List list2) {
final ArrayList result = new ArrayList();
final List result = new ArrayList();
final Iterator iterator = list2.iterator();
while (iterator.hasNext()) {
@ -203,16 +203,15 @@ public class ListUtils {
* @param list the list to generate the hashCode for, may be null
* @return the hash code
*/
public static int hashCodeForList(final Collection list) {
public static <E> int hashCodeForList(final Collection<E> list) {
if (list == null) {
return 0;
}
int hashCode = 1;
Iterator it = list.iterator();
Object obj = null;
Iterator<E> it = list.iterator();
while (it.hasNext()) {
obj = it.next();
E obj = it.next();
hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
}
return hashCode;
@ -234,11 +233,10 @@ public class ListUtils {
* @throws NullPointerException if either parameter is null
* @since Commons Collections 3.2
*/
public static List retainAll(Collection collection, Collection retain) {
List list = new ArrayList(Math.min(collection.size(), retain.size()));
public static <E> List<E> retainAll(Collection<E> collection, Collection<?> retain) {
List<E> list = new ArrayList<E>(Math.min(collection.size(), retain.size()));
for (Iterator iter = collection.iterator(); iter.hasNext();) {
Object obj = iter.next();
for (E obj : collection) {
if (retain.contains(obj)) {
list.add(obj);
}
@ -262,11 +260,10 @@ public class ListUtils {
* @throws NullPointerException if either parameter is null
* @since Commons Collections 3.2
*/
public static List removeAll(Collection collection, Collection remove) {
List list = new ArrayList();
for (Iterator iter = collection.iterator(); iter.hasNext();) {
Object obj = iter.next();
if (remove.contains(obj) == false) {
public static <E> List<E> removeAll(Collection<E> collection, Collection<?> remove) {
List<E> list = new ArrayList<E>();
for (E obj : collection) {
if (!remove.contains(obj)) {
list.add(obj);
}
}
@ -296,7 +293,7 @@ public class ListUtils {
* @return a synchronized list backed by the given list
* @throws IllegalArgumentException if the list is null
*/
public static List synchronizedList(List list) {
public static <E> List<E> synchronizedList(List<E> list) {
return SynchronizedList.decorate(list);
}
@ -309,7 +306,7 @@ public class ListUtils {
* @return an unmodifiable list backed by the given list
* @throws IllegalArgumentException if the list is null
*/
public static List unmodifiableList(List list) {
public static <E> List unmodifiableList(List<E> list) {
return UnmodifiableList.decorate(list);
}
@ -326,7 +323,7 @@ public class ListUtils {
* @return a predicated list backed by the given list
* @throws IllegalArgumentException if the List or Predicate is null
*/
public static List predicatedList(List list, Predicate predicate) {
public static <E> List<E> predicatedList(List<E> list, Predicate<E> predicate) {
return PredicatedList.decorate(list, predicate);
}

View File

@ -117,16 +117,13 @@ public class SetUtils {
* @param set the set to calculate the hash code for, may be null
* @return the hash code
*/
public static int hashCodeForSet(final Collection set) {
public static <T> int hashCodeForSet(final Collection<T> set) {
if (set == null) {
return 0;
}
int hashCode = 0;
Iterator it = set.iterator();
Object obj = null;
while (it.hasNext()) {
obj = it.next();
int hashCode = 0;
for (T obj : set) {
if (obj != null) {
hashCode += obj.hashCode();
}
@ -157,7 +154,7 @@ public class SetUtils {
* @return a synchronized set backed by the given set
* @throws IllegalArgumentException if the set is null
*/
public static Set synchronizedSet(Set set) {
public static <T> Set<T> synchronizedSet(Set<T> set) {
return SynchronizedSet.decorate(set);
}
@ -187,7 +184,7 @@ public class SetUtils {
* @return a predicated set backed by the given set
* @throws IllegalArgumentException if the Set or Predicate is null
*/
public static Set predicatedSet(Set set, Predicate predicate) {
public static <T> Set<T> predicatedSet(Set<T> set, Predicate<? super T> predicate) {
return PredicatedSet.decorate(set, predicate);
}
@ -245,7 +242,7 @@ public class SetUtils {
* @return a synchronized set backed by the given set
* @throws IllegalArgumentException if the set is null
*/
public static SortedSet synchronizedSortedSet(SortedSet set) {
public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> set) {
return SynchronizedSortedSet.decorate(set);
}
@ -258,7 +255,7 @@ public class SetUtils {
* @return an unmodifiable set backed by the given set
* @throws IllegalArgumentException if the set is null
*/
public static SortedSet unmodifiableSortedSet(SortedSet set) {
public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> set) {
return UnmodifiableSortedSet.decorate(set);
}
@ -275,7 +272,7 @@ public class SetUtils {
* @return a predicated sorted set backed by the given sorted set
* @throws IllegalArgumentException if the Set or Predicate is null
*/
public static SortedSet predicatedSortedSet(SortedSet set, Predicate predicate) {
public static <T> SortedSet<T> predicatedSortedSet(SortedSet<T> set, Predicate<? super T> predicate) {
return PredicatedSortedSet.decorate(set, predicate);
}

View File

@ -31,8 +31,8 @@ import org.apache.commons.collections.collection.AbstractCollectionDecorator;
*
* @author Stephen Colebourne
*/
public abstract class AbstractBagDecorator
extends AbstractCollectionDecorator implements Bag {
public abstract class AbstractBagDecorator<E>
extends AbstractCollectionDecorator<E> implements Bag<E> {
/**
* Constructor only used in deserialization, do not use otherwise.
@ -48,7 +48,7 @@ public abstract class AbstractBagDecorator
* @param bag the bag to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
protected AbstractBagDecorator(Bag bag) {
protected AbstractBagDecorator(Bag<E> bag) {
super(bag);
}
@ -57,8 +57,8 @@ public abstract class AbstractBagDecorator
*
* @return the decorated bag
*/
protected Bag decorated() {
return (Bag) super.decorated();
protected Bag<E> decorated() {
return (Bag<E>) super.decorated();
}
//-----------------------------------------------------------------------
@ -66,7 +66,7 @@ public abstract class AbstractBagDecorator
return decorated().getCount(object);
}
public boolean add(Object object, int count) {
public boolean add(E object, int count) {
return decorated().add(object, count);
}
@ -74,7 +74,7 @@ public abstract class AbstractBagDecorator
return decorated().remove(object, count);
}
public Set uniqueSet() {
public Set<E> uniqueSet() {
return decorated().uniqueSet();
}

View File

@ -30,8 +30,8 @@ import org.apache.commons.collections.SortedBag;
*
* @author Stephen Colebourne
*/
public abstract class AbstractSortedBagDecorator
extends AbstractBagDecorator implements SortedBag {
public abstract class AbstractSortedBagDecorator<E>
extends AbstractBagDecorator<E> implements SortedBag<E> {
/**
* Constructor only used in deserialization, do not use otherwise.
@ -47,7 +47,7 @@ public abstract class AbstractSortedBagDecorator
* @param bag the bag to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
protected AbstractSortedBagDecorator(SortedBag bag) {
protected AbstractSortedBagDecorator(SortedBag<E> bag) {
super(bag);
}
@ -56,20 +56,20 @@ public abstract class AbstractSortedBagDecorator
*
* @return the decorated bag
*/
protected SortedBag decorated() {
return (SortedBag) super.decorated();
protected SortedBag<E> decorated() {
return (SortedBag<E>) super.decorated();
}
//-----------------------------------------------------------------------
public Object first() {
public E first() {
return decorated().first();
}
public Object last() {
public E last() {
return decorated().last();
}
public Comparator comparator() {
public Comparator<? super E> comparator() {
return decorated().comparator();
}

View File

@ -41,8 +41,8 @@ import org.apache.commons.collections.collection.PredicatedCollection;
* @author Stephen Colebourne
* @author Paul Jack
*/
public class PredicatedBag
extends PredicatedCollection implements Bag {
public class PredicatedBag<E>
extends PredicatedCollection<E> implements Bag<E> {
/** Serialization version */
private static final long serialVersionUID = -2575833140344736876L;
@ -59,8 +59,8 @@ public class PredicatedBag
* @throws IllegalArgumentException if bag or predicate is null
* @throws IllegalArgumentException if the bag contains invalid elements
*/
public static Bag decorate(Bag bag, Predicate predicate) {
return new PredicatedBag(bag, predicate);
public static <T> Bag<T> decorate(Bag<T> bag, Predicate<? super T> predicate) {
return new PredicatedBag<T>(bag, predicate);
}
//-----------------------------------------------------------------------
@ -75,7 +75,7 @@ public class PredicatedBag
* @throws IllegalArgumentException if bag or predicate is null
* @throws IllegalArgumentException if the bag contains invalid elements
*/
protected PredicatedBag(Bag bag, Predicate predicate) {
protected PredicatedBag(Bag<E> bag, Predicate<? super E> predicate) {
super(bag, predicate);
}
@ -84,12 +84,12 @@ public class PredicatedBag
*
* @return the decorated bag
*/
protected Bag decorated() {
return (Bag) super.decorated();
protected Bag<E> decorated() {
return (Bag<E>) super.decorated();
}
//-----------------------------------------------------------------------
public boolean add(Object object, int count) {
public boolean add(E object, int count) {
validate(object);
return decorated().add(object, count);
}
@ -98,7 +98,7 @@ public class PredicatedBag
return decorated().remove(object, count);
}
public Set uniqueSet() {
public Set<E> uniqueSet() {
return decorated().uniqueSet();
}

View File

@ -40,8 +40,8 @@ import org.apache.commons.collections.SortedBag;
* @author Stephen Colebourne
* @author Paul Jack
*/
public class PredicatedSortedBag
extends PredicatedBag implements SortedBag {
public class PredicatedSortedBag<E>
extends PredicatedBag<E> implements SortedBag<E> {
/** Serialization version */
private static final long serialVersionUID = 3448581314086406616L;
@ -58,8 +58,8 @@ public class PredicatedSortedBag
* @throws IllegalArgumentException if bag or predicate is null
* @throws IllegalArgumentException if the bag contains invalid elements
*/
public static SortedBag decorate(SortedBag bag, Predicate predicate) {
return new PredicatedSortedBag(bag, predicate);
public static <T> SortedBag<T> decorate(SortedBag<T> bag, Predicate<? super T> predicate) {
return new PredicatedSortedBag<T>(bag, predicate);
}
//-----------------------------------------------------------------------
@ -74,7 +74,7 @@ public class PredicatedSortedBag
* @throws IllegalArgumentException if bag or predicate is null
* @throws IllegalArgumentException if the bag contains invalid elements
*/
protected PredicatedSortedBag(SortedBag bag, Predicate predicate) {
protected PredicatedSortedBag(SortedBag<E> bag, Predicate<? super E> predicate) {
super(bag, predicate);
}
@ -83,20 +83,20 @@ public class PredicatedSortedBag
*
* @return the decorated bag
*/
protected SortedBag decorated() {
return (SortedBag) super.decorated();
protected SortedBag<E> decorated() {
return (SortedBag<E>) super.decorated();
}
//-----------------------------------------------------------------------
public Object first() {
public E first() {
return decorated().first();
}
public Object last() {
public E last() {
return decorated().last();
}
public Comparator comparator() {
public Comparator<? super E> comparator() {
return decorated().comparator();
}

View File

@ -36,8 +36,8 @@ import org.apache.commons.collections.set.SynchronizedSet;
*
* @author Stephen Colebourne
*/
public class SynchronizedBag
extends SynchronizedCollection implements Bag {
public class SynchronizedBag<E>
extends SynchronizedCollection<E> implements Bag<E> {
/** Serialization version */
private static final long serialVersionUID = 8084674570753837109L;
@ -49,8 +49,8 @@ public class SynchronizedBag
* @return a new synchronized Bag
* @throws IllegalArgumentException if bag is null
*/
public static Bag decorate(Bag bag) {
return new SynchronizedBag(bag);
public static <T> Bag<T> decorate(Bag<T> bag) {
return new SynchronizedBag<T>(bag);
}
//-----------------------------------------------------------------------
@ -60,7 +60,7 @@ public class SynchronizedBag
* @param bag the bag to decorate, must not be null
* @throws IllegalArgumentException if bag is null
*/
protected SynchronizedBag(Bag bag) {
protected SynchronizedBag(Bag<E> bag) {
super(bag);
}
@ -71,7 +71,7 @@ public class SynchronizedBag
* @param lock the lock to use, must not be null
* @throws IllegalArgumentException if bag is null
*/
protected SynchronizedBag(Bag bag, Object lock) {
protected SynchronizedBag(Bag<E> bag, Object lock) {
super(bag, lock);
}
@ -80,12 +80,12 @@ public class SynchronizedBag
*
* @return the decorated bag
*/
protected Bag getBag() {
return (Bag) collection;
protected Bag<E> getBag() {
return (Bag<E>) collection;
}
//-----------------------------------------------------------------------
public boolean add(Object object, int count) {
public boolean add(E object, int count) {
synchronized (lock) {
return getBag().add(object, count);
}
@ -97,9 +97,9 @@ public class SynchronizedBag
}
}
public Set uniqueSet() {
public Set<E> uniqueSet() {
synchronized (lock) {
Set set = getBag().uniqueSet();
Set<E> set = getBag().uniqueSet();
return new SynchronizedBagSet(set, lock);
}
}
@ -114,13 +114,13 @@ public class SynchronizedBag
/**
* Synchronized Set for the Bag class.
*/
class SynchronizedBagSet extends SynchronizedSet {
class SynchronizedBagSet extends SynchronizedSet<E> {
/**
* Constructor.
* @param set the set to decorate
* @param lock the lock to use, shared with the bag
*/
SynchronizedBagSet(Set set, Object lock) {
SynchronizedBagSet(Set<E> set, Object lock) {
super(set, lock);
}
}

View File

@ -35,8 +35,8 @@ import org.apache.commons.collections.SortedBag;
*
* @author Stephen Colebourne
*/
public class SynchronizedSortedBag
extends SynchronizedBag implements SortedBag {
public class SynchronizedSortedBag<E>
extends SynchronizedBag<E> implements SortedBag<E> {
/** Serialization version */
private static final long serialVersionUID = 722374056718497858L;
@ -48,8 +48,8 @@ public class SynchronizedSortedBag
* @return a new synchronized SortedBag
* @throws IllegalArgumentException if bag is null
*/
public static SortedBag decorate(SortedBag bag) {
return new SynchronizedSortedBag(bag);
public static <T> SortedBag<T> decorate(SortedBag<T> bag) {
return new SynchronizedSortedBag<T>(bag);
}
//-----------------------------------------------------------------------
@ -59,7 +59,7 @@ public class SynchronizedSortedBag
* @param bag the bag to decorate, must not be null
* @throws IllegalArgumentException if bag is null
*/
protected SynchronizedSortedBag(SortedBag bag) {
protected SynchronizedSortedBag(SortedBag<E> bag) {
super(bag);
}
@ -70,7 +70,7 @@ public class SynchronizedSortedBag
* @param lock the lock to use, must not be null
* @throws IllegalArgumentException if bag is null
*/
protected SynchronizedSortedBag(Bag bag, Object lock) {
protected SynchronizedSortedBag(Bag<E> bag, Object lock) {
super(bag, lock);
}
@ -79,24 +79,24 @@ public class SynchronizedSortedBag
*
* @return the decorated bag
*/
protected SortedBag getSortedBag() {
return (SortedBag) collection;
protected SortedBag<E> getSortedBag() {
return (SortedBag<E>) collection;
}
//-----------------------------------------------------------------------
public synchronized Object first() {
public synchronized E first() {
synchronized (lock) {
return getSortedBag().first();
}
}
public synchronized Object last() {
public synchronized E last() {
synchronized (lock) {
return getSortedBag().last();
}
}
public synchronized Comparator comparator() {
public synchronized Comparator<? super E> comparator() {
synchronized (lock) {
return getSortedBag().comparator();
}

View File

@ -39,8 +39,8 @@ import org.apache.commons.collections.set.UnmodifiableSet;
*
* @author Stephen Colebourne
*/
public final class UnmodifiableSortedBag
extends AbstractSortedBagDecorator implements Unmodifiable, Serializable {
public final class UnmodifiableSortedBag<E>
extends AbstractSortedBagDecorator<E> implements Unmodifiable, Serializable {
/** Serialization version */
private static final long serialVersionUID = -3190437252665717841L;
@ -54,11 +54,11 @@ public final class UnmodifiableSortedBag
* @return an unmodifiable SortedBag
* @throws IllegalArgumentException if bag is null
*/
public static SortedBag decorate(SortedBag bag) {
public static <T> SortedBag<T> decorate(SortedBag<T> bag) {
if (bag instanceof Unmodifiable) {
return bag;
}
return new UnmodifiableSortedBag(bag);
return new UnmodifiableSortedBag<T>(bag);
}
//-----------------------------------------------------------------------
@ -68,7 +68,7 @@ public final class UnmodifiableSortedBag
* @param bag the bag to decorate, must not be null
* @throws IllegalArgumentException if bag is null
*/
private UnmodifiableSortedBag(SortedBag bag) {
private UnmodifiableSortedBag(SortedBag<E> bag) {
super(bag);
}
@ -93,7 +93,7 @@ public final class UnmodifiableSortedBag
*/
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
collection = (Collection) in.readObject();
collection = (Collection<E>) in.readObject();
}
//-----------------------------------------------------------------------
@ -101,11 +101,11 @@ public final class UnmodifiableSortedBag
return UnmodifiableIterator.decorate(decorated().iterator());
}
public boolean add(Object object) {
public boolean add(E object) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection coll) {
public boolean addAll(Collection<? extends E> coll) {
throw new UnsupportedOperationException();
}
@ -117,16 +117,16 @@ public final class UnmodifiableSortedBag
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection coll) {
public boolean removeAll(Collection<?> coll) {
throw new UnsupportedOperationException();
}
public boolean retainAll(Collection coll) {
public boolean retainAll(Collection<?> coll) {
throw new UnsupportedOperationException();
}
//-----------------------------------------------------------------------
public boolean add(Object object, int count) {
public boolean add(E object, int count) {
throw new UnsupportedOperationException();
}
@ -134,8 +134,8 @@ public final class UnmodifiableSortedBag
throw new UnsupportedOperationException();
}
public Set uniqueSet() {
Set set = decorated().uniqueSet();
public Set<E> uniqueSet() {
Set<E> set = decorated().uniqueSet();
return UnmodifiableSet.decorate(set);
}

View File

@ -39,7 +39,7 @@ import org.apache.commons.collections.collection.PredicatedCollection;
* @author Stephen Colebourne
* @author Paul Jack
*/
public class PredicatedBuffer extends PredicatedCollection implements Buffer {
public class PredicatedBuffer<E> extends PredicatedCollection<E> implements Buffer<E> {
/** Serialization version */
private static final long serialVersionUID = 2307609000539943581L;
@ -56,8 +56,8 @@ public class PredicatedBuffer extends PredicatedCollection implements Buffer {
* @throws IllegalArgumentException if buffer or predicate is null
* @throws IllegalArgumentException if the buffer contains invalid elements
*/
public static Buffer decorate(Buffer buffer, Predicate predicate) {
return new PredicatedBuffer(buffer, predicate);
public static <T> Buffer<T> decorate(Buffer<T> buffer, Predicate<? super T> predicate) {
return new PredicatedBuffer<T>(buffer, predicate);
}
//-----------------------------------------------------------------------
@ -72,7 +72,7 @@ public class PredicatedBuffer extends PredicatedCollection implements Buffer {
* @throws IllegalArgumentException if buffer or predicate is null
* @throws IllegalArgumentException if the buffer contains invalid elements
*/
protected PredicatedBuffer(Buffer buffer, Predicate predicate) {
protected PredicatedBuffer(Buffer<E> buffer, Predicate<? super E> predicate) {
super(buffer, predicate);
}
@ -81,16 +81,16 @@ public class PredicatedBuffer extends PredicatedCollection implements Buffer {
*
* @return the decorated buffer
*/
protected Buffer decorated() {
return (Buffer) super.decorated();
protected Buffer<E> decorated() {
return (Buffer<E>) super.decorated();
}
//-----------------------------------------------------------------------
public Object get() {
public E get() {
return decorated().get();
}
public Object remove() {
public E remove() {
return decorated().remove();
}

View File

@ -136,10 +136,7 @@ public abstract class AbstractCollectionDecorator<E>
}
public boolean equals(Object object) {
if (object == this) {
return true;
}
return decorated().equals(object);
return object == this || decorated().equals(object);
}
public int hashCode() {

View File

@ -52,14 +52,13 @@ public class TransformedCollection<E> extends AbstractCollectionDecorator<E> {
* If there are any elements already in the collection being decorated, they
* are NOT transformed.
*
* @param <T> the type of the elements in the collection
* @param coll the collection to decorate, must not be null
* @param transformer the transformer to use for conversion, must not be null
* @return a new transformed collection
* @throws IllegalArgumentException if collection or transformer is null
*/
public static <T> Collection<T> decorate(Collection<T> coll, Transformer<T, T> transformer) {
return new TransformedCollection<T>(coll, transformer);
public static Collection decorate(Collection coll, Transformer transformer) {
return new TransformedCollection(coll, transformer);
}
//-----------------------------------------------------------------------
@ -73,7 +72,7 @@ public class TransformedCollection<E> extends AbstractCollectionDecorator<E> {
* @param transformer the transformer to use for conversion, must not be null
* @throws IllegalArgumentException if collection or transformer is null
*/
protected TransformedCollection(Collection<E> coll, Transformer<E, E> transformer) {
protected TransformedCollection(Collection coll, Transformer transformer) {
super(coll);
if (transformer == null) {
throw new IllegalArgumentException("Transformer must not be null");
@ -89,8 +88,8 @@ public class TransformedCollection<E> extends AbstractCollectionDecorator<E> {
* @param object the object to transform
* @return a transformed object
*/
protected E transform(E object) {
return transformer.transform(object);
protected E transform(Object object) {
return transformer.transform((E) object);
}
/**
@ -101,23 +100,21 @@ public class TransformedCollection<E> extends AbstractCollectionDecorator<E> {
* @param coll the collection to transform
* @return a transformed object
*/
protected Collection<E> transform(Collection<? extends E> coll) {
List<E> list = new ArrayList<E>(coll.size());
for (E item : coll) {
protected Collection transform(Collection coll) {
List list = new ArrayList(coll.size());
for (Object item : coll) {
list.add(transform(item));
}
return list;
}
//-----------------------------------------------------------------------
public boolean add(E object) {
object = transform(object);
return decorated().add(object);
public boolean add(Object object) {
return decorated().add(transform(object));
}
public boolean addAll(Collection<? extends E> coll) {
coll = transform(coll);
return decorated().addAll(coll);
public boolean addAll(Collection coll) {
return decorated().addAll(transform(coll));
}
}

View File

@ -29,10 +29,10 @@ import java.util.ListIterator;
* @author Rodney Waldhoff
* @author Stephen Colebourne
*/
public class AbstractListIteratorDecorator implements ListIterator {
public class AbstractListIteratorDecorator<E> implements ListIterator<E> {
/** The iterator being decorated */
protected final ListIterator iterator;
protected final ListIterator<E> iterator;
//-----------------------------------------------------------------------
/**
@ -41,7 +41,7 @@ public class AbstractListIteratorDecorator implements ListIterator {
* @param iterator the iterator to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
public AbstractListIteratorDecorator(ListIterator iterator) {
public AbstractListIteratorDecorator(ListIterator<E> iterator) {
super();
if (iterator == null) {
throw new IllegalArgumentException("ListIterator must not be null");
@ -54,7 +54,7 @@ public class AbstractListIteratorDecorator implements ListIterator {
*
* @return the decorated iterator
*/
protected ListIterator getListIterator() {
protected ListIterator<E> getListIterator() {
return iterator;
}
@ -63,7 +63,7 @@ public class AbstractListIteratorDecorator implements ListIterator {
return iterator.hasNext();
}
public Object next() {
public E next() {
return iterator.next();
}
@ -75,7 +75,7 @@ public class AbstractListIteratorDecorator implements ListIterator {
return iterator.hasPrevious();
}
public Object previous() {
public E previous() {
return iterator.previous();
}
@ -87,11 +87,11 @@ public class AbstractListIteratorDecorator implements ListIterator {
iterator.remove();
}
public void set(Object obj) {
public void set(E obj) {
iterator.set(obj);
}
public void add(Object obj) {
public void add(E obj) {
iterator.add(obj);
}

View File

@ -16,15 +16,14 @@
*/
package org.apache.commons.collections.list;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.collection.PredicatedCollection;
import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
/**
* Decorates another <code>List</code> to validate that all additions
* match a specified predicate.
@ -44,7 +43,7 @@ import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
* @author Stephen Colebourne
* @author Paul Jack
*/
public class PredicatedList extends PredicatedCollection implements List {
public class PredicatedList<E> extends PredicatedCollection<E> implements List<E> {
/** Serialization version */
private static final long serialVersionUID = -5722039223898659102L;
@ -57,11 +56,12 @@ public class PredicatedList extends PredicatedCollection implements List {
*
* @param list the list to decorate, must not be null
* @param predicate the predicate to use for validation, must not be null
* @return the decorated list
* @throws IllegalArgumentException if list or predicate is null
* @throws IllegalArgumentException if the list contains invalid elements
*/
public static List decorate(List list, Predicate predicate) {
return new PredicatedList(list, predicate);
public static <T> List<T> decorate(List<T> list, Predicate<? super T> predicate) {
return new PredicatedList<T>(list, predicate);
}
//-----------------------------------------------------------------------
@ -76,7 +76,7 @@ public class PredicatedList extends PredicatedCollection implements List {
* @throws IllegalArgumentException if list or predicate is null
* @throws IllegalArgumentException if the list contains invalid elements
*/
protected PredicatedList(List list, Predicate predicate) {
protected PredicatedList(List<E> list, Predicate<? super E> predicate) {
super(list, predicate);
}
@ -85,12 +85,12 @@ public class PredicatedList extends PredicatedCollection implements List {
*
* @return the decorated list
*/
protected List decorated() {
return (List) super.decorated();
protected List<E> decorated() {
return (List<E>) super.decorated();
}
//-----------------------------------------------------------------------
public Object get(int index) {
public E get(int index) {
return decorated().get(index);
}
@ -102,56 +102,56 @@ public class PredicatedList extends PredicatedCollection implements List {
return decorated().lastIndexOf(object);
}
public Object remove(int index) {
public E remove(int index) {
return decorated().remove(index);
}
//-----------------------------------------------------------------------
public void add(int index, Object object) {
public void add(int index, E object) {
validate(object);
decorated().add(index, object);
}
public boolean addAll(int index, Collection coll) {
for (Iterator it = coll.iterator(); it.hasNext(); ) {
validate(it.next());
public boolean addAll(int index, Collection<? extends E> coll) {
for (E aColl : coll) {
validate(aColl);
}
return decorated().addAll(index, coll);
}
public ListIterator listIterator() {
public ListIterator<E> listIterator() {
return listIterator(0);
}
public ListIterator listIterator(int i) {
public ListIterator<E> listIterator(int i) {
return new PredicatedListIterator(decorated().listIterator(i));
}
public Object set(int index, Object object) {
public E set(int index, E object) {
validate(object);
return decorated().set(index, object);
}
public List subList(int fromIndex, int toIndex) {
List sub = decorated().subList(fromIndex, toIndex);
return new PredicatedList(sub, predicate);
public List<E> subList(int fromIndex, int toIndex) {
List<E> sub = decorated().subList(fromIndex, toIndex);
return new PredicatedList<E>(sub, predicate);
}
/**
* Inner class Iterator for the PredicatedList
*/
protected class PredicatedListIterator extends AbstractListIteratorDecorator {
protected class PredicatedListIterator extends AbstractListIteratorDecorator<E> {
protected PredicatedListIterator(ListIterator iterator) {
protected PredicatedListIterator(ListIterator<E> iterator) {
super(iterator);
}
public void add(Object object) {
public void add(E object) {
validate(object);
iterator.add(object);
}
public void set(Object object) {
public void set(E object) {
validate(object);
iterator.set(object);
}

View File

@ -35,7 +35,7 @@ import org.apache.commons.collections.collection.SynchronizedCollection;
*
* @author Stephen Colebourne
*/
public class SynchronizedList extends SynchronizedCollection implements List {
public class SynchronizedList<E> extends SynchronizedCollection<E> implements List<E> {
/** Serialization version */
private static final long serialVersionUID = -1403835447328619437L;
@ -46,8 +46,8 @@ public class SynchronizedList extends SynchronizedCollection implements List {
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
public static List decorate(List list) {
return new SynchronizedList(list);
public static <T> List<T> decorate(List<T> list) {
return new SynchronizedList<T>(list);
}
//-----------------------------------------------------------------------
@ -57,7 +57,7 @@ public class SynchronizedList extends SynchronizedCollection implements List {
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
protected SynchronizedList(List list) {
protected SynchronizedList(List<E> list) {
super(list);
}
@ -68,7 +68,7 @@ public class SynchronizedList extends SynchronizedCollection implements List {
* @param lock the lock to use, must not be null
* @throws IllegalArgumentException if list is null
*/
protected SynchronizedList(List list, Object lock) {
protected SynchronizedList(List<E> list, Object lock) {
super(list, lock);
}
@ -77,24 +77,24 @@ public class SynchronizedList extends SynchronizedCollection implements List {
*
* @return the decorated list
*/
protected List getList() {
return (List) collection;
protected List<E> getList() {
return (List<E>) collection;
}
//-----------------------------------------------------------------------
public void add(int index, Object object) {
public void add(int index, E object) {
synchronized (lock) {
getList().add(index, object);
}
}
public boolean addAll(int index, Collection coll) {
public boolean addAll(int index, Collection<? extends E> coll) {
synchronized (lock) {
return getList().addAll(index, coll);
}
}
public Object get(int index) {
public E get(int index) {
synchronized (lock) {
return getList().get(index);
}
@ -140,24 +140,24 @@ public class SynchronizedList extends SynchronizedCollection implements List {
return getList().listIterator(index);
}
public Object remove(int index) {
public E remove(int index) {
synchronized (lock) {
return getList().remove(index);
}
}
public Object set(int index, Object object) {
public E set(int index, E object) {
synchronized (lock) {
return getList().set(index, object);
}
}
public List subList(int fromIndex, int toIndex) {
public List<E> subList(int fromIndex, int toIndex) {
synchronized (lock) {
List list = getList().subList(fromIndex, toIndex);
List<E> list = getList().subList(fromIndex, toIndex);
// the lock is passed into the constructor here to ensure that the sublist is
// synchronized on the same lock as the parent list
return new SynchronizedList(list, lock);
return new SynchronizedList<E>(list, lock);
}
}

View File

@ -40,7 +40,7 @@ import org.apache.commons.collections.collection.PredicatedCollection;
* @author Stephen Colebourne
* @author Paul Jack
*/
public class PredicatedSet extends PredicatedCollection implements Set {
public class PredicatedSet<E> extends PredicatedCollection<E> implements Set<E> {
/** Serialization version */
private static final long serialVersionUID = -684521469108685117L;
@ -53,11 +53,12 @@ public class PredicatedSet extends PredicatedCollection implements Set {
*
* @param set the set to decorate, must not be null
* @param predicate the predicate to use for validation, must not be null
* @return a decorated set
* @throws IllegalArgumentException if set or predicate is null
* @throws IllegalArgumentException if the set contains invalid elements
*/
public static Set decorate(Set set, Predicate predicate) {
return new PredicatedSet(set, predicate);
public static <T> Set<T> decorate(Set<T> set, Predicate<? super T> predicate) {
return new PredicatedSet<T>(set, predicate);
}
//-----------------------------------------------------------------------
@ -72,7 +73,7 @@ public class PredicatedSet extends PredicatedCollection implements Set {
* @throws IllegalArgumentException if set or predicate is null
* @throws IllegalArgumentException if the set contains invalid elements
*/
protected PredicatedSet(Set set, Predicate predicate) {
protected PredicatedSet(Set<E> set, Predicate<? super E> predicate) {
super(set, predicate);
}
@ -81,8 +82,8 @@ public class PredicatedSet extends PredicatedCollection implements Set {
*
* @return the decorated set
*/
protected Set decorated() {
return (Set) super.decorated();
protected Set<E> decorated() {
return (Set<E>) super.decorated();
}
}

View File

@ -40,7 +40,7 @@ import org.apache.commons.collections.Predicate;
* @author Stephen Colebourne
* @author Paul Jack
*/
public class PredicatedSortedSet extends PredicatedSet implements SortedSet {
public class PredicatedSortedSet<E> extends PredicatedSet<E> implements SortedSet<E> {
/** Serialization version */
private static final long serialVersionUID = -9110948148132275052L;
@ -53,11 +53,12 @@ public class PredicatedSortedSet extends PredicatedSet implements SortedSet {
*
* @param set the set to decorate, must not be null
* @param predicate the predicate to use for validation, must not be null
* @return a new predicated sorted set.
* @throws IllegalArgumentException if set or predicate is null
* @throws IllegalArgumentException if the set contains invalid elements
*/
public static SortedSet decorate(SortedSet set, Predicate predicate) {
return new PredicatedSortedSet(set, predicate);
public static <T> SortedSet<T> decorate(SortedSet<T> set, Predicate<? super T> predicate) {
return new PredicatedSortedSet<T>(set, predicate);
}
//-----------------------------------------------------------------------
@ -72,7 +73,7 @@ public class PredicatedSortedSet extends PredicatedSet implements SortedSet {
* @throws IllegalArgumentException if set or predicate is null
* @throws IllegalArgumentException if the set contains invalid elements
*/
protected PredicatedSortedSet(SortedSet set, Predicate predicate) {
protected PredicatedSortedSet(SortedSet<E> set, Predicate<? super E> predicate) {
super(set, predicate);
}
@ -81,36 +82,36 @@ public class PredicatedSortedSet extends PredicatedSet implements SortedSet {
*
* @return the decorated sorted set
*/
protected SortedSet decorated() {
return (SortedSet) super.decorated();
protected SortedSet<E> decorated() {
return (SortedSet<E>) super.decorated();
}
//-----------------------------------------------------------------------
public Comparator comparator() {
public Comparator<? super E> comparator() {
return decorated().comparator();
}
public Object first() {
public E first() {
return decorated().first();
}
public Object last() {
public E last() {
return decorated().last();
}
public SortedSet subSet(Object fromElement, Object toElement) {
SortedSet sub = decorated().subSet(fromElement, toElement);
return new PredicatedSortedSet(sub, predicate);
public SortedSet<E> subSet(E fromElement, E toElement) {
SortedSet<E> sub = decorated().subSet(fromElement, toElement);
return new PredicatedSortedSet<E>(sub, predicate);
}
public SortedSet headSet(Object toElement) {
SortedSet sub = decorated().headSet(toElement);
return new PredicatedSortedSet(sub, predicate);
public SortedSet<E> headSet(E toElement) {
SortedSet<E> sub = decorated().headSet(toElement);
return new PredicatedSortedSet<E>(sub, predicate);
}
public SortedSet tailSet(Object fromElement) {
SortedSet sub = decorated().tailSet(fromElement);
return new PredicatedSortedSet(sub, predicate);
public SortedSet<E> tailSet(E fromElement) {
SortedSet<E> sub = decorated().tailSet(fromElement);
return new PredicatedSortedSet<E>(sub, predicate);
}
}

View File

@ -33,7 +33,7 @@ import org.apache.commons.collections.collection.SynchronizedCollection;
*
* @author Stephen Colebourne
*/
public class SynchronizedSet extends SynchronizedCollection implements Set {
public class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set<E> {
/** Serialization version */
private static final long serialVersionUID = -8304417378626543635L;
@ -44,8 +44,8 @@ public class SynchronizedSet extends SynchronizedCollection implements Set {
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
public static Set decorate(Set set) {
return new SynchronizedSet(set);
public static <T> Set<T> decorate(Set<T> set) {
return new SynchronizedSet<T>(set);
}
//-----------------------------------------------------------------------
@ -55,7 +55,7 @@ public class SynchronizedSet extends SynchronizedCollection implements Set {
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
protected SynchronizedSet(Set set) {
protected SynchronizedSet(Set<E> set) {
super(set);
}
@ -66,7 +66,7 @@ public class SynchronizedSet extends SynchronizedCollection implements Set {
* @param lock the lock object to use, must not be null
* @throws IllegalArgumentException if set is null
*/
protected SynchronizedSet(Set set, Object lock) {
protected SynchronizedSet(Set<E> set, Object lock) {
super(set, lock);
}

View File

@ -34,7 +34,7 @@ import org.apache.commons.collections.collection.SynchronizedCollection;
*
* @author Stephen Colebourne
*/
public class SynchronizedSortedSet extends SynchronizedCollection implements SortedSet {
public class SynchronizedSortedSet<E> extends SynchronizedCollection<E> implements SortedSet<E> {
/** Serialization version */
private static final long serialVersionUID = 2775582861954500111L;
@ -45,8 +45,8 @@ public class SynchronizedSortedSet extends SynchronizedCollection implements Sor
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
public static SortedSet decorate(SortedSet set) {
return new SynchronizedSortedSet(set);
public static <T> SortedSet<T> decorate(SortedSet<T> set) {
return new SynchronizedSortedSet<T>(set);
}
//-----------------------------------------------------------------------
@ -56,7 +56,7 @@ public class SynchronizedSortedSet extends SynchronizedCollection implements Sor
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
protected SynchronizedSortedSet(SortedSet set) {
protected SynchronizedSortedSet(SortedSet<E> set) {
super(set);
}
@ -67,7 +67,7 @@ public class SynchronizedSortedSet extends SynchronizedCollection implements Sor
* @param lock the lock object to use, must not be null
* @throws IllegalArgumentException if set is null
*/
protected SynchronizedSortedSet(SortedSet set, Object lock) {
protected SynchronizedSortedSet(SortedSet<E> set, Object lock) {
super(set, lock);
}
@ -76,12 +76,12 @@ public class SynchronizedSortedSet extends SynchronizedCollection implements Sor
*
* @return the decorated set
*/
protected SortedSet getSortedSet() {
return (SortedSet) collection;
protected SortedSet<E> getSortedSet() {
return (SortedSet<E>) collection;
}
//-----------------------------------------------------------------------
public SortedSet subSet(Object fromElement, Object toElement) {
public SortedSet<E> subSet(E fromElement, E toElement) {
synchronized (lock) {
SortedSet set = getSortedSet().subSet(fromElement, toElement);
// the lock is passed into the constructor here to ensure that the
@ -90,7 +90,7 @@ public class SynchronizedSortedSet extends SynchronizedCollection implements Sor
}
}
public SortedSet headSet(Object toElement) {
public SortedSet<E> headSet(E toElement) {
synchronized (lock) {
SortedSet set = getSortedSet().headSet(toElement);
// the lock is passed into the constructor here to ensure that the
@ -99,7 +99,7 @@ public class SynchronizedSortedSet extends SynchronizedCollection implements Sor
}
}
public SortedSet tailSet(Object fromElement) {
public SortedSet<E> tailSet(E fromElement) {
synchronized (lock) {
SortedSet set = getSortedSet().tailSet(fromElement);
// the lock is passed into the constructor here to ensure that the
@ -108,13 +108,13 @@ public class SynchronizedSortedSet extends SynchronizedCollection implements Sor
}
}
public Object first() {
public E first() {
synchronized (lock) {
return getSortedSet().first();
}
}
public Object last() {
public E last() {
synchronized (lock) {
return getSortedSet().last();
}

View File

@ -37,8 +37,8 @@ import org.apache.commons.collections.iterators.UnmodifiableIterator;
*
* @author Stephen Colebourne
*/
public final class UnmodifiableSortedSet
extends AbstractSortedSetDecorator
public final class UnmodifiableSortedSet<E>
extends AbstractSortedSetDecorator<E>
implements Unmodifiable, Serializable {
/** Serialization version */
@ -50,7 +50,7 @@ public final class UnmodifiableSortedSet
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
public static SortedSet decorate(SortedSet set) {
public static <T> SortedSet<T> decorate(SortedSet<T> set) {
if (set instanceof Unmodifiable) {
return set;
}
@ -88,7 +88,7 @@ public final class UnmodifiableSortedSet
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
private UnmodifiableSortedSet(SortedSet set) {
private UnmodifiableSortedSet(SortedSet<E> set) {
super(set);
}
@ -97,11 +97,11 @@ public final class UnmodifiableSortedSet
return UnmodifiableIterator.decorate(decorated().iterator());
}
public boolean add(Object object) {
public boolean add(E object) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection coll) {
public boolean addAll(Collection<? extends E> coll) {
throw new UnsupportedOperationException();
}
@ -113,27 +113,27 @@ public final class UnmodifiableSortedSet
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection coll) {
public boolean removeAll(Collection<?> coll) {
throw new UnsupportedOperationException();
}
public boolean retainAll(Collection coll) {
public boolean retainAll(Collection<?> coll) {
throw new UnsupportedOperationException();
}
//-----------------------------------------------------------------------
public SortedSet subSet(Object fromElement, Object toElement) {
SortedSet sub = decorated().subSet(fromElement, toElement);
public SortedSet<E> subSet(E fromElement, E toElement) {
SortedSet<E> sub = decorated().subSet(fromElement, toElement);
return new UnmodifiableSortedSet(sub);
}
public SortedSet headSet(Object toElement) {
SortedSet sub = decorated().headSet(toElement);
public SortedSet<E> headSet(E toElement) {
SortedSet<E> sub = decorated().headSet(toElement);
return new UnmodifiableSortedSet(sub);
}
public SortedSet tailSet(Object fromElement) {
SortedSet sub = decorated().tailSet(fromElement);
public SortedSet<E> tailSet(E fromElement) {
SortedSet<E> sub = decorated().tailSet(fromElement);
return new UnmodifiableSortedSet(sub);
}

View File

@ -19,6 +19,7 @@ package org.apache.commons.collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import junit.framework.Test;
@ -44,7 +45,7 @@ public class TestListUtils extends BulkTest {
private static final String x = "x";
private String[] fullArray;
private List fullList;
private List<String> fullList;
public TestListUtils(String name) {
super(name);
@ -62,8 +63,54 @@ public class TestListUtils extends BulkTest {
public void testNothing() {
}
public void testpredicatedList() {
/**
* Tests intersecting a non-empty list with an empty list.
*/
public void testIntersectNonEmptyWithEmptyList() {
final List<?> empty = Collections.EMPTY_LIST;
assertTrue("result not empty", ListUtils.intersection(empty, fullList).isEmpty());
}
/**
* Tests intersecting a non-empty list with an empty list.
*/
public void testIntersectEmptyWithEmptyList() {
final List<?> empty = Collections.EMPTY_LIST;
assertTrue("result not empty", ListUtils.intersection(empty, empty).isEmpty());
}
/**
* Tests intersecting a non-empty list with an subset of iteself.
*/
public void testIntersectNonEmptySubset() {
// create a copy
final List<String> other = new ArrayList(fullList);
// remove a few items
assertNotNull(other.remove(0));
assertNotNull(other.remove(1));
// make sure the intersection is equal to the copy
assertEquals(other, ListUtils.intersection(fullList, other));
}
/**
* Tests intersecting a non-empty list with an subset of iteself.
*/
public void testIntersectListWithNoOverlapAndDifferentTypes() {
final List<Integer> other = Arrays.asList(1, 23);
assertTrue(ListUtils.intersection(fullList, other).isEmpty());
}
/**
* Tests intersecting a non-empty list with iteself.
*/
public void testIntersectListWithSelf() {
assertEquals(fullList, ListUtils.intersection(fullList, fullList));
}
public void testPredicatedList() {
Predicate predicate = new Predicate() {
public boolean evaluate(Object o) {
return o instanceof String;