Generify and remove AbstractSerializableCollectionDecorator

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/branches/collections_jdk5_branch@471575 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-11-05 23:58:08 +00:00
parent 937e261e65
commit 8eab22e729
7 changed files with 90 additions and 139 deletions

View File

@ -16,6 +16,7 @@
*/ */
package org.apache.commons.collections.collection; package org.apache.commons.collections.collection;
import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
@ -41,7 +42,11 @@ import java.util.Iterator;
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Paul Jack * @author Paul Jack
*/ */
public abstract class AbstractCollectionDecorator<E> implements Collection<E> { public abstract class AbstractCollectionDecorator<E>
implements Collection<E>, Serializable {
/** Serialization version */
private static final long serialVersionUID = 6249888059822088500L;
/** The collection being decorated */ /** The collection being decorated */
protected Collection<E> collection; protected Collection<E> collection;

View File

@ -1,69 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.collection;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
/**
* Serializable subclass of AbstractCollectionDecorator.
*
* @author Stephen Colebourne
* @since Commons Collections 3.1
*/
public abstract class AbstractSerializableCollectionDecorator
extends AbstractCollectionDecorator
implements Serializable {
/** Serialization version */
private static final long serialVersionUID = 6249888059822088500L;
/**
* Constructor.
*/
protected AbstractSerializableCollectionDecorator(Collection coll) {
super(coll);
}
//-----------------------------------------------------------------------
/**
* Write the collection out using a custom routine.
*
* @param out the output stream
* @throws IOException
*/
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(collection);
}
/**
* Read the collection in using a custom routine.
*
* @param in the input stream
* @throws IOException
* @throws ClassNotFoundException
*/
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
collection = (Collection) in.readObject();
}
}

View File

@ -17,7 +17,6 @@
package org.apache.commons.collections.collection; package org.apache.commons.collections.collection;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.collections.Predicate; import org.apache.commons.collections.Predicate;
@ -34,19 +33,20 @@ import org.apache.commons.collections.Predicate;
* <p> * <p>
* This class is Serializable from Commons Collections 3.1. * This class is Serializable from Commons Collections 3.1.
* *
* @param <E> the type of the elements in the collection
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Paul Jack * @author Paul Jack
*/ */
public class PredicatedCollection extends AbstractSerializableCollectionDecorator { public class PredicatedCollection<E> extends AbstractCollectionDecorator<E> {
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = -5259182142076705162L; private static final long serialVersionUID = -5259182142076705162L;
/** The predicate to use */ /** The predicate to use */
protected final Predicate predicate; protected final Predicate<? super E> predicate;
/** /**
* Factory method to create a predicated (validating) collection. * Factory method to create a predicated (validating) collection.
@ -54,14 +54,15 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
* If there are any elements already in the collection being decorated, they * If there are any elements already in the collection being decorated, they
* are validated. * are validated.
* *
* @param <T> the type of the elements in the collection
* @param coll the collection to decorate, must not be null * @param coll the collection to decorate, must not be null
* @param predicate the predicate to use for validation, must not be null * @param predicate the predicate to use for validation, must not be null
* @return a new predicated collection * @return a new predicated collection
* @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if collection or predicate is null
* @throws IllegalArgumentException if the collection contains invalid elements * @throws IllegalArgumentException if the collection contains invalid elements
*/ */
public static Collection decorate(Collection coll, Predicate predicate) { public static <T> Collection<T> decorate(Collection<T> coll, Predicate<? super T> predicate) {
return new PredicatedCollection(coll, predicate); return new PredicatedCollection<T>(coll, predicate);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -76,14 +77,14 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
* @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if collection or predicate is null
* @throws IllegalArgumentException if the collection contains invalid elements * @throws IllegalArgumentException if the collection contains invalid elements
*/ */
protected PredicatedCollection(Collection coll, Predicate predicate) { protected PredicatedCollection(Collection<E> coll, Predicate<? super E> predicate) {
super(coll); super(coll);
if (predicate == null) { if (predicate == null) {
throw new IllegalArgumentException("Predicate must not be null"); throw new IllegalArgumentException("Predicate must not be null");
} }
this.predicate = predicate; this.predicate = predicate;
for (Iterator it = coll.iterator(); it.hasNext(); ) { for (E item : coll) {
validate(it.next()); validate(item);
} }
} }
@ -96,7 +97,7 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
* @param object the object being added * @param object the object being added
* @throws IllegalArgumentException if the add is invalid * @throws IllegalArgumentException if the add is invalid
*/ */
protected void validate(Object object) { protected void validate(E object) {
if (predicate.evaluate(object) == false) { if (predicate.evaluate(object) == false) {
throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate rejected it"); throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate rejected it");
} }
@ -111,7 +112,7 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
* @return the result of adding to the underlying collection * @return the result of adding to the underlying collection
* @throws IllegalArgumentException if the add is invalid * @throws IllegalArgumentException if the add is invalid
*/ */
public boolean add(Object object) { public boolean add(E object) {
validate(object); validate(object);
return decorated().add(object); return decorated().add(object);
} }
@ -125,9 +126,9 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
* @return the result of adding to the underlying collection * @return the result of adding to the underlying collection
* @throws IllegalArgumentException if the add is invalid * @throws IllegalArgumentException if the add is invalid
*/ */
public boolean addAll(Collection coll) { public boolean addAll(Collection<? extends E> coll) {
for (Iterator it = coll.iterator(); it.hasNext(); ) { for (E item : coll) {
validate(it.next()); validate(item);
} }
return decorated().addAll(coll); return decorated().addAll(coll);
} }

View File

@ -34,32 +34,34 @@ import java.util.Iterator;
* <p> * <p>
* This class is Serializable from Commons Collections 3.1. * This class is Serializable from Commons Collections 3.1.
* *
* @param <E> the type of the elements in the collection
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
public class SynchronizedCollection implements Collection, Serializable { public class SynchronizedCollection<E> implements Collection<E>, Serializable {
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = 2412805092710877986L; private static final long serialVersionUID = 2412805092710877986L;
/** The collection to decorate */ /** The collection to decorate */
protected final Collection collection; protected final Collection<E> collection;
/** The object to lock on, needed for List/SortedSet views */ /** The object to lock on, needed for List/SortedSet views */
protected final Object lock; protected final Object lock;
/** /**
* Factory method to create a synchronized collection. * Factory method to create a synchronized collection.
* *
* @param <T> the type of the elements in the collection
* @param coll the collection to decorate, must not be null * @param coll the collection to decorate, must not be null
* @return a new synchronized collection * @return a new synchronized collection
* @throws IllegalArgumentException if collection is null * @throws IllegalArgumentException if collection is null
*/ */
public static Collection decorate(Collection coll) { public static <T> Collection<T> decorate(Collection<T> coll) {
return new SynchronizedCollection(coll); return new SynchronizedCollection<T>(coll);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Constructor that wraps (not copies). * Constructor that wraps (not copies).
@ -67,7 +69,7 @@ public class SynchronizedCollection implements Collection, Serializable {
* @param collection the collection to decorate, must not be null * @param collection the collection to decorate, must not be null
* @throws IllegalArgumentException if the collection is null * @throws IllegalArgumentException if the collection is null
*/ */
protected SynchronizedCollection(Collection collection) { protected SynchronizedCollection(Collection<E> collection) {
if (collection == null) { if (collection == null) {
throw new IllegalArgumentException("Collection must not be null"); throw new IllegalArgumentException("Collection must not be null");
} }
@ -82,7 +84,7 @@ public class SynchronizedCollection implements Collection, Serializable {
* @param lock the lock object to use, must not be null * @param lock the lock object to use, must not be null
* @throws IllegalArgumentException if the collection is null * @throws IllegalArgumentException if the collection is null
*/ */
protected SynchronizedCollection(Collection collection, Object lock) { protected SynchronizedCollection(Collection<E> collection, Object lock) {
if (collection == null) { if (collection == null) {
throw new IllegalArgumentException("Collection must not be null"); throw new IllegalArgumentException("Collection must not be null");
} }
@ -90,40 +92,49 @@ public class SynchronizedCollection implements Collection, Serializable {
this.lock = lock; this.lock = lock;
} }
/**
* Gets the collection being decorated.
*
* @return the decorated collection
*/
protected Collection<E> decorated() {
return collection;
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public boolean add(Object object) { public boolean add(E object) {
synchronized (lock) { synchronized (lock) {
return collection.add(object); return decorated().add(object);
} }
} }
public boolean addAll(Collection coll) { public boolean addAll(Collection<? extends E> coll) {
synchronized (lock) { synchronized (lock) {
return collection.addAll(coll); return decorated().addAll(coll);
} }
} }
public void clear() { public void clear() {
synchronized (lock) { synchronized (lock) {
collection.clear(); decorated().clear();
} }
} }
public boolean contains(Object object) { public boolean contains(Object object) {
synchronized (lock) { synchronized (lock) {
return collection.contains(object); return decorated().contains(object);
} }
} }
public boolean containsAll(Collection coll) { public boolean containsAll(Collection<?> coll) {
synchronized (lock) { synchronized (lock) {
return collection.containsAll(coll); return decorated().containsAll(coll);
} }
} }
public boolean isEmpty() { public boolean isEmpty() {
synchronized (lock) { synchronized (lock) {
return collection.isEmpty(); return decorated().isEmpty();
} }
} }
@ -137,43 +148,43 @@ public class SynchronizedCollection implements Collection, Serializable {
* *
* @return an iterator that must be manually synchronized on the collection * @return an iterator that must be manually synchronized on the collection
*/ */
public Iterator iterator() { public Iterator<E> iterator() {
return collection.iterator(); return decorated().iterator();
} }
public Object[] toArray() { public Object[] toArray() {
synchronized (lock) { synchronized (lock) {
return collection.toArray(); return decorated().toArray();
} }
} }
public Object[] toArray(Object[] object) { public <T> T[] toArray(T[] object) {
synchronized (lock) { synchronized (lock) {
return collection.toArray(object); return decorated().toArray(object);
} }
} }
public boolean remove(Object object) { public boolean remove(Object object) {
synchronized (lock) { synchronized (lock) {
return collection.remove(object); return decorated().remove(object);
} }
} }
public boolean removeAll(Collection coll) { public boolean removeAll(Collection<?> coll) {
synchronized (lock) { synchronized (lock) {
return collection.removeAll(coll); return decorated().removeAll(coll);
} }
} }
public boolean retainAll(Collection coll) { public boolean retainAll(Collection<?> coll) {
synchronized (lock) { synchronized (lock) {
return collection.retainAll(coll); return decorated().retainAll(coll);
} }
} }
public int size() { public int size() {
synchronized (lock) { synchronized (lock) {
return collection.size(); return decorated().size();
} }
} }
@ -182,19 +193,19 @@ public class SynchronizedCollection implements Collection, Serializable {
if (object == this) { if (object == this) {
return true; return true;
} }
return collection.equals(object); return decorated().equals(object);
} }
} }
public int hashCode() { public int hashCode() {
synchronized (lock) { synchronized (lock) {
return collection.hashCode(); return decorated().hashCode();
} }
} }
public String toString() { public String toString() {
synchronized (lock) { synchronized (lock) {
return collection.toString(); return decorated().toString();
} }
} }

View File

@ -18,7 +18,6 @@ package org.apache.commons.collections.collection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.List; import java.util.List;
import org.apache.commons.collections.Transformer; import org.apache.commons.collections.Transformer;
@ -33,18 +32,19 @@ import org.apache.commons.collections.Transformer;
* <p> * <p>
* This class is Serializable from Commons Collections 3.1. * This class is Serializable from Commons Collections 3.1.
* *
* @param <E> the type of the elements in the collection
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
public class TransformedCollection extends AbstractSerializableCollectionDecorator { public class TransformedCollection<E> extends AbstractCollectionDecorator<E> {
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = 8692300188161871514L; private static final long serialVersionUID = 8692300188161871514L;
/** The transformer to use */ /** The transformer to use */
protected final Transformer transformer; protected final Transformer<E, E> transformer;
/** /**
* Factory method to create a transforming collection. * Factory method to create a transforming collection.
@ -52,15 +52,16 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
* If there are any elements already in the collection being decorated, they * If there are any elements already in the collection being decorated, they
* are NOT transformed. * are NOT transformed.
* *
* @param <T> the type of the elements in the collection
* @param coll the collection to decorate, must not be null * @param coll the collection to decorate, must not be null
* @param transformer the transformer to use for conversion, must not be null * @param transformer the transformer to use for conversion, must not be null
* @return a new transformed collection * @return a new transformed collection
* @throws IllegalArgumentException if collection or transformer is null * @throws IllegalArgumentException if collection or transformer is null
*/ */
public static Collection decorate(Collection coll, Transformer transformer) { public static <T> Collection<T> decorate(Collection<T> coll, Transformer<T, T> transformer) {
return new TransformedCollection(coll, transformer); return new TransformedCollection<T>(coll, transformer);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Constructor that wraps (not copies). * Constructor that wraps (not copies).
@ -72,7 +73,7 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
* @param transformer the transformer to use for conversion, must not be null * @param transformer the transformer to use for conversion, must not be null
* @throws IllegalArgumentException if collection or transformer is null * @throws IllegalArgumentException if collection or transformer is null
*/ */
protected TransformedCollection(Collection coll, Transformer transformer) { protected TransformedCollection(Collection<E> coll, Transformer<E, E> transformer) {
super(coll); super(coll);
if (transformer == null) { if (transformer == null) {
throw new IllegalArgumentException("Transformer must not be null"); throw new IllegalArgumentException("Transformer must not be null");
@ -88,7 +89,7 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
* @param object the object to transform * @param object the object to transform
* @return a transformed object * @return a transformed object
*/ */
protected Object transform(Object object) { protected E transform(E object) {
return transformer.transform(object); return transformer.transform(object);
} }
@ -100,21 +101,21 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
* @param coll the collection to transform * @param coll the collection to transform
* @return a transformed object * @return a transformed object
*/ */
protected Collection transform(Collection coll) { protected Collection<E> transform(Collection<? extends E> coll) {
List list = new ArrayList(coll.size()); List<E> list = new ArrayList<E>(coll.size());
for (Iterator it = coll.iterator(); it.hasNext(); ) { for (E item : coll) {
list.add(transform(it.next())); list.add(transform(item));
} }
return list; return list;
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public boolean add(Object object) { public boolean add(E object) {
object = transform(object); object = transform(object);
return decorated().add(object); return decorated().add(object);
} }
public boolean addAll(Collection coll) { public boolean addAll(Collection<? extends E> coll) {
coll = transform(coll); coll = transform(coll);
return decorated().addAll(coll); return decorated().addAll(coll);
} }

View File

@ -40,7 +40,7 @@ import org.apache.commons.collections.iterators.UnmodifiableIterator;
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
public final class UnmodifiableBoundedCollection public final class UnmodifiableBoundedCollection
extends AbstractSerializableCollectionDecorator extends AbstractCollectionDecorator
implements BoundedCollection { implements BoundedCollection {
/** Serialization version */ /** Serialization version */

View File

@ -27,13 +27,14 @@ import org.apache.commons.collections.iterators.UnmodifiableIterator;
* <p> * <p>
* This class is Serializable from Commons Collections 3.1. * This class is Serializable from Commons Collections 3.1.
* *
* @param <E> the type of the elements in the collection
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
public final class UnmodifiableCollection public final class UnmodifiableCollection<E>
extends AbstractSerializableCollectionDecorator extends AbstractCollectionDecorator<E>
implements Unmodifiable { implements Unmodifiable {
/** Serialization version */ /** Serialization version */
@ -44,17 +45,18 @@ public final class UnmodifiableCollection
* <p> * <p>
* If the collection passed in is already unmodifiable, it is returned. * If the collection passed in is already unmodifiable, it is returned.
* *
* @param <T> the type of the elements in the collection
* @param coll the collection to decorate, must not be null * @param coll the collection to decorate, must not be null
* @return an unmodifiable collection * @return an unmodifiable collection
* @throws IllegalArgumentException if collection is null * @throws IllegalArgumentException if collection is null
*/ */
public static Collection decorate(Collection coll) { public static <T> Collection<T> decorate(Collection<T> coll) {
if (coll instanceof Unmodifiable) { if (coll instanceof Unmodifiable) {
return coll; return coll;
} }
return new UnmodifiableCollection(coll); return new UnmodifiableCollection<T>(coll);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Constructor that wraps (not copies). * Constructor that wraps (not copies).
@ -62,20 +64,20 @@ public final class UnmodifiableCollection
* @param coll the collection to decorate, must not be null * @param coll the collection to decorate, must not be null
* @throws IllegalArgumentException if collection is null * @throws IllegalArgumentException if collection is null
*/ */
private UnmodifiableCollection(Collection coll) { private UnmodifiableCollection(Collection<E> coll) {
super(coll); super(coll);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public Iterator iterator() { public Iterator<E> iterator() {
return UnmodifiableIterator.decorate(decorated().iterator()); return UnmodifiableIterator.decorate(decorated().iterator());
} }
public boolean add(Object object) { public boolean add(E object) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public boolean addAll(Collection coll) { public boolean addAll(Collection<? extends E> coll) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -87,11 +89,11 @@ public final class UnmodifiableCollection
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public boolean removeAll(Collection coll) { public boolean removeAll(Collection<?> coll) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public boolean retainAll(Collection coll) { public boolean retainAll(Collection<?> coll) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }