You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/04/29 20:43:48 UTC
cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/decorators TypedList.java AbstractListIteratorDecorator.java SynchronizedCollection.java PredicatedList.java PredicatedCollection.java AbstractListDecorator.java UnmodifiableCollection.java SynchronizedList.java LazyList.java AbstractCollectionDecorator.java UnmodifiableList.java FixedSizeList.java TypedCollection.java
scolebourne 2003/04/29 11:43:48
Added: collections/src/java/org/apache/commons/collections/decorators
TypedList.java AbstractListIteratorDecorator.java
SynchronizedCollection.java PredicatedList.java
PredicatedCollection.java
AbstractListDecorator.java
UnmodifiableCollection.java SynchronizedList.java
LazyList.java AbstractCollectionDecorator.java
UnmodifiableList.java FixedSizeList.java
TypedCollection.java
Log:
Collection and List decorators (previously inner classes)
Revision Changes Path
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/TypedList.java
Index: TypedList.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/TypedList.java,v 1.1 2003/04/29 18:43:47 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.List;
/**
* <code>TypedList</code> decorates another <code>List</code>
* to validate that elements added are of a specific type.
* <p>
* The validation of additions is performed via an instanceof test against
* a specified <code>Class</code>. If an object cannot be addded to the
* collection, an IllegalArgumentException is thrown.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
*/
public class TypedList extends PredicatedList {
/**
* Factory method to create a typed collection.
* <p>
* If there are any elements already in the collection being decorated, they
* are validated.
*
* @param list the list to decorate, must not be null
* @param type the type to allow into the collection, must not be null
* @throws IllegalArgumentException if list or type is null
* @throws IllegalArgumentException if the list contains invalid elements
*/
public static List decorate(List list, Class type) {
return new TypedList(list, type);
}
/**
* Constructor that wraps (not copies).
* <p>
* If there are any elements already in the collection being decorated, they
* are validated.
*
* @param list the list to decorate, must not be null
* @param type the type to allow into the collection, must not be null
* @throws IllegalArgumentException if list or type is null
* @throws IllegalArgumentException if the list contains invalid elements
*/
protected TypedList(List list, Class type) {
super(list, TypedCollection.getPredicate(type));
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/AbstractListIteratorDecorator.java
Index: AbstractListIteratorDecorator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/AbstractListIteratorDecorator.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections.Predicate;
/**
* <code>AbstractListIteratorDecorator</code> decorates another
* <code>ListIterator</code>.
* <p>
* Methods are forwarded directly to the decorated iterator.
* <p>
* This class is similar to {@link org.apache.commons.collections.iterators.ProxyListIterator},
* however that class allows the list iterator to be set, which is too insecure
* for some list implementations.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
*/
public abstract class AbstractListIteratorDecorator implements ListIterator {
/** The iterator to delegate to */
protected final ListIterator iterator;
/**
* Constructor that wraps the specified iterator.
*
* @param iterator the iterator to decorate
*/
public AbstractListIteratorDecorator(ListIterator iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
return iterator.next();
}
public boolean hasPrevious() {
return iterator.hasPrevious();
}
public Object previous() {
return iterator.previous();
}
public int nextIndex() {
return iterator.nextIndex();
}
public int previousIndex() {
return iterator.previousIndex();
}
public void remove() {
iterator.remove();
}
public void add(Object object) {
iterator.add(object);
}
public void set(Object object) {
iterator.set(object);
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/SynchronizedCollection.java
Index: SynchronizedCollection.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/SynchronizedCollection.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
/**
* <code>SynchronizedCollection</code> decorates another <code>Collection</code>
* to synchronize the method calls.
* <p>
* Iterators must be manually synchronized:
* <pre>
* synchronized (coll) {
* Iterator it = coll.iterator();
* // do stuff with iterator
* }
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
*/
public class SynchronizedCollection implements Collection {
/** The collection to decorate */
protected final Collection collection;
/**
* Factory method to create a synchronized collection.
*
* @param coll the collection to decorate, must not be null
* @throws IllegalArgumentException if collection is null
*/
public static Collection decorate(Collection coll) {
return new SynchronizedCollection(coll);
}
/**
* Constructor that wraps (not copies).
*
* @param coll the collection to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
protected SynchronizedCollection(Collection collection) {
if (collection == null) {
throw new IllegalArgumentException("Collection must not be null");
}
this.collection = collection;
}
public synchronized boolean add(Object object) {
return collection.add(object);
}
public synchronized boolean addAll(Collection coll) {
return collection.addAll(coll);
}
public synchronized void clear() {
collection.clear();
}
public synchronized boolean contains(Object object) {
return collection.contains(object);
}
public synchronized boolean containsAll(Collection coll) {
return collection.containsAll(coll);
}
public synchronized boolean isEmpty() {
return collection.isEmpty();
}
/**
* Iterators must be manually synchronized.
* <pre>
* synchronized (coll) {
* Iterator it = coll.iterator();
* // do stuff with iterator
* }
*
* @return an iterator that must be manually synchronized on the collection
*/
public Iterator iterator() {
return collection.iterator();
}
public synchronized Object[] toArray() {
return collection.toArray();
}
public synchronized Object[] toArray(Object[] object) {
return collection.toArray(object);
}
public synchronized boolean remove(Object object) {
return collection.remove(object);
}
public synchronized boolean removeAll(Collection coll) {
return collection.removeAll(coll);
}
public synchronized boolean retainAll(Collection coll) {
return collection.retainAll(coll);
}
public synchronized int size() {
return collection.size();
}
public synchronized boolean equals(Object object) {
if (object == this) {
return true;
}
return collection.equals(object);
}
public synchronized int hashCode() {
return collection.hashCode();
}
public synchronized String toString() {
return collection.toString();
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/PredicatedList.java
Index: PredicatedList.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/PredicatedList.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections.Predicate;
/**
* <code>PredicatedList</code> decorates another <code>List</code>
* to validate additions match a specified predicate.
* <p>
* If an object cannot be addded to the list, an IllegalArgumentException
* is thrown.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
* @author Paul Jack
*/
public class PredicatedList extends PredicatedCollection implements List {
/**
* Factory method to create a predicated (validating) collection.
* <p>
* If there are any elements already in the list being decorated, they
* are validated.
*
* @param coll the collection to decorate, must not be null
* @param predicate the predicate to use for validation, must not be null
* @throws IllegalArgumentException if collection or predicate is null
* @throws IllegalArgumentException if the collection contains invalid elements
*/
public static List decorate(List list, Predicate predicate) {
return new PredicatedList(list, predicate);
}
/**
* Constructor that wraps (not copies).
* <p>
* If there are any elements already in the list being decorated, they
* are validated.
*
* @param list the list to decorate, must not be null
* @param predicate the predicate to use for validation, must not be null
* @throws IllegalArgumentException if collection or predicate is null
* @throws IllegalArgumentException if the collection contains invalid elements
*/
protected PredicatedList(List list, Predicate predicate) {
super(list, predicate);
}
public void add(int index, Object object) {
validate(object);
getList().add(index, object);
}
public boolean addAll(int index, Collection coll) {
for (Iterator it = coll.iterator(); it.hasNext(); ) {
validate(it.next());
}
return getList().addAll(index, coll);
}
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 ListIterator listIterator() {
return listIterator(0);
}
public ListIterator listIterator(int i) {
return new AbstractListIteratorDecorator(getList().listIterator(i)) {
public void add(Object object) {
validate(object);
iterator.add(object);
}
public void set(Object object) {
validate(object);
iterator.set(object);
}
};
}
public Object remove(int index) {
return getList().remove(index);
}
public Object set(int index, Object object) {
validate(object);
return getList().set(index, object);
}
public List subList(int fromIndex, int toIndex) {
List sub = getList().subList(fromIndex, toIndex);
return new PredicatedList(sub, predicate);
}
protected List getList() {
return (List) collection;
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/PredicatedCollection.java
Index: PredicatedCollection.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/PredicatedCollection.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
import org.apache.commons.collections.Predicate;
/**
* <code>PredicatedCollection</code> decorates another <code>Collection</code>
* to validate additions match a specified predicate.
* <p>
* If an object cannot be addded to the collection, an IllegalArgumentException
* is thrown.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
* @author Paul Jack
*/
public class PredicatedCollection extends AbstractCollectionDecorator {
/** The predicate to use */
protected final Predicate predicate;
/**
* Factory method to create a predicated (validating) collection.
* <p>
* If there are any elements already in the collection being decorated, they
* are validated.
*
* @param coll the collection to decorate, must not be null
* @param predicate the predicate to use for validation, must not be null
* @throws IllegalArgumentException if collection or predicate is null
* @throws IllegalArgumentException if the collection contains invalid elements
*/
public static Collection decorate(Collection coll, Predicate predicate) {
return new PredicatedCollection(coll, predicate);
}
/**
* Constructor that wraps (not copies).
* <p>
* If there are any elements already in the collection being decorated, they
* are validated.
*
* @param coll the collection to decorate, must not be null
* @param predicate the predicate to use for validation, must not be null
* @throws IllegalArgumentException if collection or predicate is null
* @throws IllegalArgumentException if the collection contains invalid elements
*/
protected PredicatedCollection(Collection coll, Predicate predicate) {
super(coll);
if (predicate == null) {
throw new IllegalArgumentException("Predicate must not be null");
}
this.predicate = predicate;
for (Iterator it = coll.iterator(); it.hasNext(); ) {
validate(it.next());
}
}
/**
* Override to validate the object being added to ensure it matches
* the predicate.
*
* @param object the object being added
* @return the result of adding to the underlying collection
* @throws IllegalArgumentException if the add is invalid
*/
public boolean add(Object object) {
validate(object);
return collection.add(object);
}
/**
* Override to validate the objects being added to ensure they match
* the predicate. If any one fails, no update is made to the underlying
* collection.
*
* @param coll the collection being added
* @return the result of adding to the underlying collection
* @throws IllegalArgumentException if the add is invalid
*/
public boolean addAll(Collection coll) {
for (Iterator it = coll.iterator(); it.hasNext(); ) {
validate(it.next());
}
return collection.addAll(coll);
}
/**
* Validates the object being added to ensure it matches the predicate.
* <p>
* The predicate itself should not throw an exception, but return false to
* indicate that the object cannot be added.
*
* @param object the object being added
* @throws IllegalArgumentException if the add is invalid
*/
protected void validate(Object object) {
if (predicate.evaluate(object) == false) {
throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate rejected it");
}
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/AbstractListDecorator.java
Index: AbstractListDecorator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/AbstractListDecorator.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections.Predicate;
/**
* <code>AbstractListDecorator</code> decorates another <code>List</code>.
* <p>
* Methods are forwarded directly to the decorated list.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
*/
public abstract class AbstractListDecorator extends AbstractCollectionDecorator implements List {
/**
* Constructor that wraps (not copies).
*
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
protected AbstractListDecorator(List list) {
super(list);
}
public void add(int index, Object object) {
getList().add(index, object);
}
public boolean addAll(int index, Collection coll) {
return getList().addAll(index, coll);
}
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 ListIterator listIterator() {
return getList().listIterator();
}
public ListIterator listIterator(int index) {
return getList().listIterator(index);
}
public Object remove(int index) {
return getList().remove(index);
}
public Object set(int index, Object object) {
return getList().set(index, object);
}
public List subList(int fromIndex, int toIndex) {
return getList().subList(fromIndex, toIndex);
}
protected List getList() {
return (List) collection;
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/UnmodifiableCollection.java
Index: UnmodifiableCollection.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/UnmodifiableCollection.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.collections.Predicate;
/**
* <code>PredicatedCollection</code> decorates another <code>Collection</code>
* to validate addition match a specified predicate.
* <p>
* If an object cannot be addded to the collection, an IllegalArgumentException
* is thrown.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
*/
public class UnmodifiableCollection extends AbstractCollectionDecorator {
/**
* Factory method to create an unmodifiable collection.
*
* @param coll the collection to decorate, must not be null
* @throws IllegalArgumentException if collection is null
*/
public static Collection decorate(Collection coll) {
return new UnmodifiableCollection(coll);
}
/**
* Constructor that wraps (not copies).
*
* @param coll the collection to decorate, must not be null
* @throws IllegalArgumentException if collection is null
*/
protected UnmodifiableCollection(Collection coll) {
super(coll);
}
/**
* Override as method unsupported.
* @throws UnsupportedOperationException
*/
public boolean add(Object object) {
throw new UnsupportedOperationException();
}
/**
* Override as method unsupported.
* @throws UnsupportedOperationException
*/
public boolean addAll(Collection coll) {
throw new UnsupportedOperationException();
}
/**
* Override as method unsupported.
* @throws UnsupportedOperationException
*/
public void clear() {
throw new UnsupportedOperationException();
}
/**
* Override to return an unmodifiable iterator.
*
* @return unmodifiable iterator
*/
public Iterator iterator() {
return IteratorUtils.unmodifiableIterator(collection.iterator());
}
/**
* Override as method unsupported.
* @throws UnsupportedOperationException
*/
public boolean remove(Object object) {
throw new UnsupportedOperationException();
}
/**
* Override as method unsupported.
* @throws UnsupportedOperationException
*/
public boolean removeAll(Collection coll) {
throw new UnsupportedOperationException();
}
/**
* Override as method unsupported.
* @throws UnsupportedOperationException
*/
public boolean retainAll(Collection coll) {
throw new UnsupportedOperationException();
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/SynchronizedList.java
Index: SynchronizedList.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/SynchronizedList.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections.Predicate;
/**
* <code>SynchronizedList</code> decorates another <code>List</code>.
* <p>
* Methods are synchronized, then forwarded to the decorated list.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
*/
public class SynchronizedList extends SynchronizedCollection implements List {
/**
* Factory method to create a synchronized list.
*
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if collection is null
*/
public static List decorate(List list) {
return new SynchronizedList(list);
}
/**
* Constructor that wraps (not copies).
*
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
protected SynchronizedList(List list) {
super(list);
}
public synchronized void add(int index, Object object) {
getList().add(index, object);
}
public synchronized boolean addAll(int index, Collection coll) {
return getList().addAll(index, coll);
}
public synchronized Object get(int index) {
return getList().get(index);
}
public synchronized int indexOf(Object object) {
return getList().indexOf(object);
}
public synchronized int lastIndexOf(Object object) {
return getList().lastIndexOf(object);
}
/**
* Iterators must be manually synchronized.
* <pre>
* synchronized (coll) {
* ListIterator it = coll.listIterator();
* // do stuff with iterator
* }
*
* @return an iterator that must be manually synchronized on the collection
*/
public ListIterator listIterator() {
return getList().listIterator();
}
/**
* Iterators must be manually synchronized.
* <pre>
* synchronized (coll) {
* ListIterator it = coll.listIterator(3);
* // do stuff with iterator
* }
*
* @return an iterator that must be manually synchronized on the collection
*/
public ListIterator listIterator(int index) {
return getList().listIterator(index);
}
public synchronized Object remove(int index) {
return getList().remove(index);
}
public synchronized Object set(int index, Object object) {
return getList().set(index, object);
}
public synchronized List subList(int fromIndex, int toIndex) {
return getList().subList(fromIndex, toIndex);
}
protected List getList() {
return (List) collection;
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/LazyList.java
Index: LazyList.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/LazyList.java,v 1.1 2003/04/29 18:43:47 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.List;
import org.apache.commons.collections.Factory;
/**
* <code>LazyList</code> decorates another <code>List</code>
* to create objects in the list on demand.
* <p>
* When the {@link #get(int)} method is called with an index greater than
* the size of the list, the list will automatically grow in size and return
* a new object from the specified factory. The gaps will be filled by null.
* If a get method call encounters a null, it will be replaced with a new
* object from the factory. Thus this list is unsuitable for storing null
* objects.
* <p>
* For instance:
*
* <pre>
* Factory factory = new Factory() {
* public Object create() {
* return new Date();
* }
* }
* List lazy = LazyList.decorate(new ArrayList(), factory);
* Object obj = lazy.get(3);
* </pre>
*
* After the above code is executed, <code>obj</code> will contain
* a new <code>Date</code> instance. Furthermore, that <code>Date</code>
* instance is the fourth element in the list. The first, second,
* and third element are all set to <code>null</code>.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
* @author Arron Bates
* @author Paul Jack
*/
public class LazyList extends AbstractListDecorator {
/** The factory to use to lazily instantiate the objects */
protected final Factory factory;
/**
* Factory method to create a lazily instantiating list.
*
* @param list the list to decorate, must not be null
* @param factory the factory to use for creation, must not be null
* @throws IllegalArgumentException if list or factory is null
*/
public static List decorate(List list, Factory factory) {
return new LazyList(list, factory);
}
/**
* Constructor that wraps (not copies).
*
* @param list the list to decorate, must not be null
* @param factory the factory to use for creation, must not be null
* @throws IllegalArgumentException if list or factory is null
*/
protected LazyList(List list, Factory factory) {
super(list);
if (factory == null) {
throw new IllegalArgumentException("Factory must not be null");
}
this.factory = factory;
}
/**
* Decorate the get method to perform the lazy behaviour.
* <p>
* If the requested index is greater than the current size, the list will
* grow to the new size and a new object will be returned from the factory.
* Indexes inbetween the old size and the requested size are left with a
* placeholder that is replaced with a factory object when requested.
*
* @param index the index to retrieve
*/
public Object get(int index) {
int size = getList().size();
if (index < size) {
// within bounds, get the object
Object object = getList().get(index);
if (object == null) {
// item is a place holder, create new one, set and return
object = factory.create();
getList().set(index, object);
return object;
} else {
// good and ready to go
return object;
}
} else {
// we have to grow the list
for (int i = size; i < index; i++) {
getList().add(null);
}
// create our last object, set and return
Object object = factory.create();
getList().add(object);
return object;
}
}
public List subList(int fromIndex, int toIndex) {
List sub = getList().subList(fromIndex, toIndex);
return new LazyList(sub, factory);
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/AbstractCollectionDecorator.java
Index: AbstractCollectionDecorator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/AbstractCollectionDecorator.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
/**
* <code>AbstractCollectionDecorator</code> decorates a <code>Collection</code>
* implementation.
* <p>
* Each method call made on this <code>Collection</code> is forwarded to the
* decorated <code>Collection</code>. This class is used as a framework on which
* to build to extensions such as synchronized and unmodifiable behaviour. The
* main advantage of decoration is that one decorator can wrap any implementation
* of <code>Collection</code>, whereas sub-classing requires a new class to be
* written for each implementation.
* <p>
* This implementation does not perform any special processing with
* {@link #iterator()}. Instead it simply returns the value from the
* wrapped collection. This may be undesirable, for example if you are trying
* to write an unmodifiable implementation it might provide a loophole.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
* @author Paul Jack
*/
public abstract class AbstractCollectionDecorator implements Collection {
/** The collection being decorated */
protected final Collection collection;
/**
* Constructor that wraps (not copies).
*
* @param coll the collection to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
protected AbstractCollectionDecorator(Collection coll) {
if (coll == null) {
throw new IllegalArgumentException("Collection must not be null");
}
this.collection = coll;
}
public boolean add(Object object) {
return collection.add(object);
}
public boolean addAll(Collection coll) {
return collection.addAll(coll);
}
public void clear() {
collection.clear();
}
public boolean contains(Object object) {
return collection.contains(object);
}
public boolean isEmpty() {
return collection.isEmpty();
}
public Iterator iterator() {
return collection.iterator();
}
public boolean remove(Object object) {
return collection.remove(object);
}
public int size() {
return collection.size();
}
public Object[] toArray() {
return collection.toArray();
}
public Object[] toArray(Object[] object) {
return collection.toArray(object);
}
public boolean containsAll(Collection coll) {
return collection.containsAll(coll);
}
public boolean removeAll(Collection coll) {
return collection.removeAll(coll);
}
public boolean retainAll(Collection coll) {
return collection.retainAll(coll);
}
public boolean equals(Object object) {
if (object == this) {
return true;
}
return collection.equals(object);
}
public int hashCode() {
return collection.hashCode();
}
public String toString() {
return collection.toString();
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/UnmodifiableList.java
Index: UnmodifiableList.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/UnmodifiableList.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.collections.Predicate;
/**
* <code>UnmodifiableList</code> decorates another <code>List</code> to
* ensure it can't be altered.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
*/
public class UnmodifiableList extends UnmodifiableCollection implements List {
/**
* Factory method to create an unmodifiable 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 UnmodifiableList(list);
}
/**
* Constructor that wraps (not copies).
*
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
protected UnmodifiableList(List list) {
super(list);
}
public void add(int index, Object object) {
throw new UnsupportedOperationException();
}
public boolean addAll(int index, Collection coll) {
throw new UnsupportedOperationException();
}
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 ListIterator listIterator() {
return IteratorUtils.unmodifiableListIterator(getList().listIterator());
}
public ListIterator listIterator(int index) {
return IteratorUtils.unmodifiableListIterator(getList().listIterator(index));
}
public Object remove(int index) {
throw new UnsupportedOperationException();
}
public Object set(int index, Object object) {
throw new UnsupportedOperationException();
}
public List subList(int fromIndex, int toIndex) {
List sub = getList().subList(fromIndex, toIndex);
return new UnmodifiableList(sub);
}
protected List getList() {
return (List) collection;
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/FixedSizeList.java
Index: FixedSizeList.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/FixedSizeList.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections.Predicate;
/**
* <code>FixedSizeList</code> decorates another <code>List</code> to
* fix the size.
* <p>
* The add, remove, clear and retain operations are unsupported.
* The set method is allowed (as it doesn't change the list size).
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
* @author Paul Jack
*/
public class FixedSizeList extends UnmodifiableCollection implements List {
/**
* Factory method to create a fixed size 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 FixedSizeList(list);
}
/**
* Constructor that wraps (not copies).
*
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
protected FixedSizeList(List list) {
super(list);
}
public void add(int index, Object object) {
throw new UnsupportedOperationException();
}
public boolean addAll(int index, Collection coll) {
throw new UnsupportedOperationException();
}
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 ListIterator listIterator() {
return listIterator(0);
}
public ListIterator listIterator(int index) {
return new AbstractListIteratorDecorator(getList().listIterator(index)) {
public void remove() {
throw new UnsupportedOperationException();
}
public void add(Object object) {
throw new UnsupportedOperationException();
}
public void remove(Object object) {
throw new UnsupportedOperationException();
}
};
}
public Object remove(int index) {
throw new UnsupportedOperationException();
}
public Object set(int index, Object object) {
return getList().set(index, object);
}
public List subList(int fromIndex, int toIndex) {
List sub = getList().subList(fromIndex, toIndex);
return new FixedSizeList(sub);
}
protected List getList() {
return (List) collection;
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/TypedCollection.java
Index: TypedCollection.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/TypedCollection.java,v 1.1 2003/04/29 18:43:47 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.Iterator;
import org.apache.commons.collections.Predicate;
/**
* <code>TypedCollection</code> decorates another <code>Collection</code>
* to validate that elements added are of a specific type.
* <p>
* The validation of additions is performed via an instanceof test against
* a specified <code>Class</code>. If an object cannot be addded to the
* collection, an IllegalArgumentException is thrown.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/04/29 18:43:47 $
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
*/
public class TypedCollection extends PredicatedCollection {
/**
* Factory method to create a typed collection.
* <p>
* If there are any elements already in the collection being decorated, they
* are validated.
*
* @param coll the collection to decorate, must not be null
* @param type the type to allow into the collection, must not be null
* @throws IllegalArgumentException if collection or type is null
* @throws IllegalArgumentException if the collection contains invalid elements
*/
public static Collection decorate(Collection coll, Class type) {
return new TypedCollection(coll, type);
}
/**
* Constructor that wraps (not copies).
* <p>
* If there are any elements already in the collection being decorated, they
* are validated.
*
* @param coll the collection to decorate, must not be null
* @param type the type to allow into the collection, must not be null
* @throws IllegalArgumentException if collection or type is null
* @throws IllegalArgumentException if the collection contains invalid elements
*/
protected TypedCollection(Collection coll, Class type) {
super(coll, getPredicate(type));
}
/**
* Gets a predicate to validate the additions to the collection.
*
* @param type the type, must not be null
* @throws IllegalArgumentException if type is null
*/
static Predicate getPredicate(Class type) {
return new InstanceofPredicate(type);
}
/**
* <code>Predicate</code> implementation that checks the type of an object.
* This class may eventually be replaced by
* <code>org.apache.commons.lang.functor.PredicateUtils.instanceofPredicate()</code>.
*/
static class InstanceofPredicate implements Predicate {
/** The type to check for */
private final Class type;
/**
* Constructor
*/
public InstanceofPredicate(Class type) {
if (type == null) {
throw new IllegalArgumentException("Type must not be null");
}
this.type = type;
}
/**
* Return true if the object is an instanceof the type of the predicate.
* @param object an <code>Object</code>
* @return <code>true</code> if the object is an instanceof the type of the predicate
*/
public boolean evaluate(Object object) {
return type.isInstance(object);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org