Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.
Also see the following revisions: ------------------------------------------------------------------------ r555925 | skestle | 2007-07-13 03:39:24 -0700 (Fri, 13 Jul 2007) | 2 lines 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 ------------------------------------------------------------------------ r471575 | scolebourne | 2006-11-05 15:58:08 -0800 (Sun, 05 Nov 2006) | 1 line Generify and remove AbstractSerializableCollectionDecorator ------------------------------------------------------------------------ r471202 | scolebourne | 2006-11-04 06:21:44 -0800 (Sat, 04 Nov 2006) | 1 line Remove getCollection() - use covariant decorated() ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815039 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e82b822e1d
commit
c5ad6f0e08
|
@ -18,7 +18,6 @@ package org.apache.commons.collections.collection;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.collections.Transformer;
|
import org.apache.commons.collections.Transformer;
|
||||||
|
@ -33,18 +32,19 @@ import org.apache.commons.collections.Transformer;
|
||||||
* <p>
|
* <p>
|
||||||
* This class is Serializable from Commons Collections 3.1.
|
* This class is Serializable from Commons Collections 3.1.
|
||||||
*
|
*
|
||||||
|
* @param <E> the type of the elements in the collection
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
*/
|
*/
|
||||||
public class TransformedCollection extends AbstractSerializableCollectionDecorator {
|
public class TransformedCollection<E> extends AbstractCollectionDecorator<E> {
|
||||||
|
|
||||||
/** Serialization version */
|
/** Serialization version */
|
||||||
private static final long serialVersionUID = 8692300188161871514L;
|
private static final long serialVersionUID = 8692300188161871514L;
|
||||||
|
|
||||||
/** The transformer to use */
|
/** The transformer to use */
|
||||||
protected final Transformer transformer;
|
protected final Transformer<? super E, ? extends E> transformer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory method to create a transforming collection.
|
* Factory method to create a transforming collection.
|
||||||
|
@ -58,10 +58,10 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
|
||||||
* @return a new transformed collection
|
* @return a new transformed collection
|
||||||
* @throws IllegalArgumentException if collection or transformer is null
|
* @throws IllegalArgumentException if collection or transformer is null
|
||||||
*/
|
*/
|
||||||
public static Collection decorate(Collection coll, Transformer transformer) {
|
public static <E> Collection<E> decorate(Collection<E> coll, Transformer<? super E, ? extends E> transformer) {
|
||||||
return new TransformedCollection(coll, transformer);
|
return new TransformedCollection<E>(coll, transformer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory method to create a transforming collection that will transform
|
* Factory method to create a transforming collection that will transform
|
||||||
* existing contents of the specified collection.
|
* existing contents of the specified collection.
|
||||||
|
@ -76,13 +76,14 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
|
||||||
* @throws IllegalArgumentException if collection or transformer is null
|
* @throws IllegalArgumentException if collection or transformer is null
|
||||||
* @since Commons Collections 3.3
|
* @since Commons Collections 3.3
|
||||||
*/
|
*/
|
||||||
|
// TODO: Generics
|
||||||
public static Collection decorateTransform(Collection collection, Transformer transformer) {
|
public static Collection decorateTransform(Collection collection, Transformer transformer) {
|
||||||
TransformedCollection decorated = new TransformedCollection(collection, transformer);
|
TransformedCollection decorated = new TransformedCollection(collection, transformer);
|
||||||
if (transformer != null && collection != null && collection.size() > 0) {
|
if (transformer != null && collection != null && collection.size() > 0) {
|
||||||
Object[] values = collection.toArray();
|
Object[] values = collection.toArray();
|
||||||
collection.clear();
|
collection.clear();
|
||||||
for(int i=0; i<values.length; i++) {
|
for(int i=0; i<values.length; i++) {
|
||||||
decorated.getCollection().add(transformer.transform(values[i]));
|
decorated.decorated().add(transformer.transform(values[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return decorated;
|
return decorated;
|
||||||
|
@ -99,7 +100,7 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
|
||||||
* @param transformer the transformer to use for conversion, must not be null
|
* @param transformer the transformer to use for conversion, must not be null
|
||||||
* @throws IllegalArgumentException if collection or transformer is null
|
* @throws IllegalArgumentException if collection or transformer is null
|
||||||
*/
|
*/
|
||||||
protected TransformedCollection(Collection coll, Transformer transformer) {
|
protected TransformedCollection(Collection<E> coll, Transformer<? super E, ? extends E> transformer) {
|
||||||
super(coll);
|
super(coll);
|
||||||
if (transformer == null) {
|
if (transformer == null) {
|
||||||
throw new IllegalArgumentException("Transformer must not be null");
|
throw new IllegalArgumentException("Transformer must not be null");
|
||||||
|
@ -115,7 +116,7 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
|
||||||
* @param object the object to transform
|
* @param object the object to transform
|
||||||
* @return a transformed object
|
* @return a transformed object
|
||||||
*/
|
*/
|
||||||
protected Object transform(Object object) {
|
protected E transform(E object) {
|
||||||
return transformer.transform(object);
|
return transformer.transform(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,23 +128,21 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
|
||||||
* @param coll the collection to transform
|
* @param coll the collection to transform
|
||||||
* @return a transformed object
|
* @return a transformed object
|
||||||
*/
|
*/
|
||||||
protected Collection transform(Collection coll) {
|
protected Collection<E> transform(Collection<? extends E> coll) {
|
||||||
List list = new ArrayList(coll.size());
|
List<E> list = new ArrayList<E>(coll.size());
|
||||||
for (Iterator it = coll.iterator(); it.hasNext(); ) {
|
for (E item : coll) {
|
||||||
list.add(transform(it.next()));
|
list.add(transform(item));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
public boolean add(Object object) {
|
public boolean add(E object) {
|
||||||
object = transform(object);
|
return decorated().add(transform(object));
|
||||||
return getCollection().add(object);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addAll(Collection coll) {
|
public boolean addAll(Collection<? extends E> coll) {
|
||||||
coll = transform(coll);
|
return decorated().addAll(transform(coll));
|
||||||
return getCollection().addAll(coll);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue