Add transformed decorators
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131071 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
340c5f0af9
commit
530e458fb7
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TransformedBag.java,v 1.1 2003/05/11 13:17:57 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.decorators;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.collections.Bag;
|
||||
import org.apache.commons.collections.Transformer;
|
||||
|
||||
/**
|
||||
* <code>TransformedBag</code> decorates another <code>Bag</code>
|
||||
* to transform objects that are added.
|
||||
* <p>
|
||||
* The add methods are affected by this class.
|
||||
* Thus objects must be removed or searched for using their transformed form.
|
||||
* For example, if the transformation converts Strings to Integers, you must
|
||||
* use the Integer form to remove objects.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/11 13:17:57 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TransformedBag extends TransformedCollection implements Bag {
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming bag.
|
||||
* <p>
|
||||
* If there are any elements already in the bag being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param bag the bag to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if bag or transformer is null
|
||||
*/
|
||||
public static Bag decorate(Bag bag, Transformer transformer) {
|
||||
return new TransformedBag(bag, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
* <p>
|
||||
* If there are any elements already in the bag being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param bag the bag to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if bag or transformer is null
|
||||
*/
|
||||
protected TransformedBag(Bag bag, Transformer transformer) {
|
||||
super(bag, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the decorated bag.
|
||||
*
|
||||
* @return the decorated bag
|
||||
*/
|
||||
protected Bag getBag() {
|
||||
return (Bag) collection;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public int getCount(Object object) {
|
||||
return getBag().getCount(object);
|
||||
}
|
||||
|
||||
public boolean remove(Object object, int nCopies) {
|
||||
return getBag().remove(object, nCopies);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public boolean add(Object object, int nCopies) {
|
||||
object = transform(object);
|
||||
return getBag().add(object, nCopies);
|
||||
}
|
||||
|
||||
public Set uniqueSet() {
|
||||
Set set = getBag().uniqueSet();
|
||||
return new TransformedSet(set, transformer);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TransformedBuffer.java,v 1.1 2003/05/11 13:17:57 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.decorators;
|
||||
|
||||
import org.apache.commons.collections.Buffer;
|
||||
import org.apache.commons.collections.Transformer;
|
||||
|
||||
/**
|
||||
* <code>TransformedBuffer</code> decorates another <code>Buffer</code>
|
||||
* to transform objects that are added.
|
||||
* <p>
|
||||
* The add methods are affected by this class.
|
||||
* Thus objects must be removed or searched for using their transformed form.
|
||||
* For example, if the transformation converts Strings to Integers, you must
|
||||
* use the Integer form to remove objects.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/11 13:17:57 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TransformedBuffer extends TransformedCollection implements Buffer {
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming buffer.
|
||||
* <p>
|
||||
* If there are any elements already in the buffer being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param buffer the buffer to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if buffer or transformer is null
|
||||
*/
|
||||
public static Buffer decorate(Buffer buffer, Transformer transformer) {
|
||||
return new TransformedBuffer(buffer, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
* <p>
|
||||
* If there are any elements already in the buffer being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param buffer the buffer to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if buffer or transformer is null
|
||||
*/
|
||||
protected TransformedBuffer(Buffer buffer, Transformer transformer) {
|
||||
super(buffer, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the decorated buffer.
|
||||
*
|
||||
* @return the decorated buffer
|
||||
*/
|
||||
protected Buffer getBuffer() {
|
||||
return (Buffer) collection;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public Object get() {
|
||||
return getBuffer().get();
|
||||
}
|
||||
|
||||
public Object remove() {
|
||||
return getBuffer().remove();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TransformedCollection.java,v 1.1 2003/05/11 13:17:57 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.decorators;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
|
||||
/**
|
||||
* <code>TransformedCollection</code> decorates another <code>Collection</code>
|
||||
* to transform objects that are added.
|
||||
* <p>
|
||||
* The add methods are affected by this class.
|
||||
* Thus objects must be removed or searched for using their transformed form.
|
||||
* For example, if the transformation converts Strings to Integers, you must
|
||||
* use the Integer form to remove objects.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/11 13:17:57 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TransformedCollection extends AbstractCollectionDecorator {
|
||||
|
||||
/** The transformer to use */
|
||||
protected final Transformer transformer;
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming collection.
|
||||
* <p>
|
||||
* If there are any elements already in the collection being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param coll the collection to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if collection or transformer is null
|
||||
*/
|
||||
public static Collection decorate(Collection coll, Transformer transformer) {
|
||||
return new TransformedCollection(coll, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
* <p>
|
||||
* If there are any elements already in the collection being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param coll the collection to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if collection or transformer is null
|
||||
*/
|
||||
protected TransformedCollection(Collection coll, Transformer transformer) {
|
||||
super(coll);
|
||||
if (transformer == null) {
|
||||
throw new IllegalArgumentException("Transformer must not be null");
|
||||
}
|
||||
this.transformer = transformer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an object.
|
||||
* <p>
|
||||
* The transformer itself may throw an exception if necessary.
|
||||
*
|
||||
* @param object the object being added
|
||||
* @throws the transformed object
|
||||
*/
|
||||
protected Object transform(Object object) {
|
||||
return transformer.transform(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a collection.
|
||||
* <p>
|
||||
* The transformer itself may throw an exception if necessary.
|
||||
*
|
||||
* @param object the object being added
|
||||
* @throws the transformed object
|
||||
*/
|
||||
protected Collection transform(Collection coll) {
|
||||
List list = new ArrayList(coll.size());
|
||||
for (Iterator it = coll.iterator(); it.hasNext(); ) {
|
||||
list.add(transform(it.next()));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public boolean add(Object object) {
|
||||
object = transform(object);
|
||||
return getCollection().add(object);
|
||||
}
|
||||
|
||||
public boolean addAll(Collection coll) {
|
||||
coll = transform(coll);
|
||||
return getCollection().addAll(coll);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,185 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TransformedList.java,v 1.1 2003/05/11 13:17:57 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.decorators;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
|
||||
/**
|
||||
* <code>TransformedList</code> decorates another <code>List</code>
|
||||
* to transform objects that are added.
|
||||
* <p>
|
||||
* The add and set methods are affected by this class.
|
||||
* Thus objects must be removed or searched for using their transformed form.
|
||||
* For example, if the transformation converts Strings to Integers, you must
|
||||
* use the Integer form to remove objects.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/11 13:17:57 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TransformedList extends TransformedCollection implements List {
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming list.
|
||||
* <p>
|
||||
* If there are any elements already in the list being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param list the list to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if list or transformer is null
|
||||
*/
|
||||
public static List decorate(List list, Transformer transformer) {
|
||||
return new TransformedList(list, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
* <p>
|
||||
* If there are any elements already in the list being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param list the list to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if list or transformer is null
|
||||
*/
|
||||
protected TransformedList(List list, Transformer transformer) {
|
||||
super(list, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the decorated list.
|
||||
*
|
||||
* @return the decorated list
|
||||
*/
|
||||
protected List getList() {
|
||||
return (List) collection;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public Object get(int index) {
|
||||
return getList().get(index);
|
||||
}
|
||||
|
||||
public int indexOf(Object object) {
|
||||
return getList().indexOf(object);
|
||||
}
|
||||
|
||||
public int lastIndexOf(Object object) {
|
||||
return getList().lastIndexOf(object);
|
||||
}
|
||||
|
||||
public Object remove(int index) {
|
||||
return getList().remove(index);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void add(int index, Object object) {
|
||||
object = transform(object);
|
||||
getList().add(index, object);
|
||||
}
|
||||
|
||||
public boolean addAll(int index, Collection coll) {
|
||||
coll = transform(coll);
|
||||
return getList().addAll(index, coll);
|
||||
}
|
||||
|
||||
public ListIterator listIterator() {
|
||||
return listIterator(0);
|
||||
}
|
||||
|
||||
public ListIterator listIterator(int i) {
|
||||
return new TransformedListIterator(getList().listIterator(i));
|
||||
}
|
||||
|
||||
public Object set(int index, Object object) {
|
||||
object = transform(object);
|
||||
return getList().set(index, object);
|
||||
}
|
||||
|
||||
public List subList(int fromIndex, int toIndex) {
|
||||
List sub = getList().subList(fromIndex, toIndex);
|
||||
return new TransformedList(sub, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inner class Iterator for the TransformedList
|
||||
*/
|
||||
protected class TransformedListIterator extends AbstractListIteratorDecorator {
|
||||
|
||||
protected TransformedListIterator(ListIterator iterator) {
|
||||
super(iterator);
|
||||
}
|
||||
|
||||
public void add(Object object) {
|
||||
object = transform(object);
|
||||
getIterator().add(object);
|
||||
}
|
||||
|
||||
public void set(Object object) {
|
||||
object = transform(object);
|
||||
getIterator().set(object);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TransformedSet.java,v 1.1 2003/05/11 13:17:57 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.decorators;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
|
||||
/**
|
||||
* <code>TransformedSet</code> decorates another <code>Set</code>
|
||||
* to transform objects that are added.
|
||||
* <p>
|
||||
* The add methods are affected by this class.
|
||||
* Thus objects must be removed or searched for using their transformed form.
|
||||
* For example, if the transformation converts Strings to Integers, you must
|
||||
* use the Integer form to remove objects.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/11 13:17:57 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TransformedSet extends TransformedCollection implements Set {
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming set.
|
||||
* <p>
|
||||
* If there are any elements already in the set being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param set the set to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if set or transformer is null
|
||||
*/
|
||||
public static Set decorate(Set set, Transformer transformer) {
|
||||
return new TransformedSet(set, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
* <p>
|
||||
* If there are any elements already in the set being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param set the set to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if set or transformer is null
|
||||
*/
|
||||
protected TransformedSet(Set set, Transformer transformer) {
|
||||
super(set, transformer);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TransformedSortedBag.java,v 1.1 2003/05/11 13:17:57 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.decorators;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.apache.commons.collections.SortedBag;
|
||||
import org.apache.commons.collections.Transformer;
|
||||
|
||||
/**
|
||||
* <code>TransformedSortedBag</code> decorates another <code>SortedBag</code>
|
||||
* to transform objects that are added.
|
||||
* <p>
|
||||
* The add methods are affected by this class.
|
||||
* Thus objects must be removed or searched for using their transformed form.
|
||||
* For example, if the transformation converts Strings to Integers, you must
|
||||
* use the Integer form to remove objects.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/11 13:17:57 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TransformedSortedBag extends TransformedBag implements SortedBag {
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming sorted bag.
|
||||
* <p>
|
||||
* If there are any elements already in the bag being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param bag the bag to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if bag or transformer is null
|
||||
*/
|
||||
public static SortedBag decorate(SortedBag bag, Transformer transformer) {
|
||||
return new TransformedSortedBag(bag, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
* <p>
|
||||
* If there are any elements already in the bag being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param bag the bag to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if bag or transformer is null
|
||||
*/
|
||||
protected TransformedSortedBag(SortedBag bag, Transformer transformer) {
|
||||
super(bag, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the decorated bag.
|
||||
*
|
||||
* @return the decorated bag
|
||||
*/
|
||||
protected SortedBag getSortedBag() {
|
||||
return (SortedBag) collection;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public Object first() {
|
||||
return getSortedBag().first();
|
||||
}
|
||||
|
||||
public Object last() {
|
||||
return getSortedBag().last();
|
||||
}
|
||||
|
||||
public Comparator comparator() {
|
||||
return getSortedBag().comparator();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TransformedSortedSet.java,v 1.1 2003/05/11 13:17:57 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.decorators;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
|
||||
/**
|
||||
* <code>TransformedSortedSet</code> decorates another <code>SortedSet</code>
|
||||
* to transform objects that are added.
|
||||
* <p>
|
||||
* The add methods are affected by this class.
|
||||
* Thus objects must be removed or searched for using their transformed form.
|
||||
* For example, if the transformation converts Strings to Integers, you must
|
||||
* use the Integer form to remove objects.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/11 13:17:57 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TransformedSortedSet extends TransformedSet implements SortedSet {
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming sorted set.
|
||||
* <p>
|
||||
* If there are any elements already in the set being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param set the set to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if set or transformer is null
|
||||
*/
|
||||
public static SortedSet decorate(SortedSet set, Transformer transformer) {
|
||||
return new TransformedSortedSet(set, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
* <p>
|
||||
* If there are any elements already in the set being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param set the set to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if set or transformer is null
|
||||
*/
|
||||
protected TransformedSortedSet(SortedSet set, Transformer transformer) {
|
||||
super(set, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the decorated set.
|
||||
*
|
||||
* @return the decorated set
|
||||
*/
|
||||
protected SortedSet getSortedSet() {
|
||||
return (SortedSet) collection;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public Object first() {
|
||||
return getSortedSet().first();
|
||||
}
|
||||
|
||||
public Object last() {
|
||||
return getSortedSet().last();
|
||||
}
|
||||
|
||||
public Comparator comparator() {
|
||||
return getSortedSet().comparator();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public SortedSet subSet(Object fromElement, Object toElement) {
|
||||
SortedSet set = getSortedSet().subSet(fromElement, toElement);
|
||||
return new TransformedSortedSet(set, transformer);
|
||||
}
|
||||
|
||||
public SortedSet headSet(Object toElement) {
|
||||
SortedSet set = getSortedSet().headSet(toElement);
|
||||
return new TransformedSortedSet(set, transformer);
|
||||
}
|
||||
|
||||
public SortedSet tailSet(Object fromElement) {
|
||||
SortedSet set = getSortedSet().tailSet(fromElement);
|
||||
return new TransformedSortedSet(set, transformer);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue