mirror of
https://github.com/apache/commons-collections.git
synced 2025-02-08 19:15:14 +00:00
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:
parent
937e261e65
commit
8eab22e729
@ -16,6 +16,7 @@
|
||||
*/
|
||||
package org.apache.commons.collections.collection;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
@ -41,7 +42,11 @@ import java.util.Iterator;
|
||||
* @author Stephen Colebourne
|
||||
* @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 */
|
||||
protected Collection<E> collection;
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -17,7 +17,6 @@
|
||||
package org.apache.commons.collections.collection;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.collections.Predicate;
|
||||
|
||||
@ -34,19 +33,20 @@ import org.apache.commons.collections.Predicate;
|
||||
* <p>
|
||||
* This class is Serializable from Commons Collections 3.1.
|
||||
*
|
||||
* @param <E> the type of the elements in the collection
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision$ $Date$
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
* @author Paul Jack
|
||||
*/
|
||||
public class PredicatedCollection extends AbstractSerializableCollectionDecorator {
|
||||
public class PredicatedCollection<E> extends AbstractCollectionDecorator<E> {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = -5259182142076705162L;
|
||||
|
||||
/** The predicate to use */
|
||||
protected final Predicate predicate;
|
||||
protected final Predicate<? super E> predicate;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* are validated.
|
||||
*
|
||||
* @param <T> the type of the elements in the collection
|
||||
* @param coll the collection to decorate, must not be null
|
||||
* @param predicate the predicate to use for validation, must not be null
|
||||
* @return a new predicated collection
|
||||
* @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);
|
||||
public static <T> Collection<T> decorate(Collection<T> coll, Predicate<? super T> 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 the collection contains invalid elements
|
||||
*/
|
||||
protected PredicatedCollection(Collection coll, Predicate predicate) {
|
||||
protected PredicatedCollection(Collection<E> coll, Predicate<? super E> 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());
|
||||
for (E item : coll) {
|
||||
validate(item);
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,7 +97,7 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
|
||||
* @param object the object being added
|
||||
* @throws IllegalArgumentException if the add is invalid
|
||||
*/
|
||||
protected void validate(Object object) {
|
||||
protected void validate(E object) {
|
||||
if (predicate.evaluate(object) == false) {
|
||||
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
|
||||
* @throws IllegalArgumentException if the add is invalid
|
||||
*/
|
||||
public boolean add(Object object) {
|
||||
public boolean add(E object) {
|
||||
validate(object);
|
||||
return decorated().add(object);
|
||||
}
|
||||
@ -125,9 +126,9 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
|
||||
* @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());
|
||||
public boolean addAll(Collection<? extends E> coll) {
|
||||
for (E item : coll) {
|
||||
validate(item);
|
||||
}
|
||||
return decorated().addAll(coll);
|
||||
}
|
||||
|
@ -34,32 +34,34 @@ import java.util.Iterator;
|
||||
* <p>
|
||||
* This class is Serializable from Commons Collections 3.1.
|
||||
*
|
||||
* @param <E> the type of the elements in the collection
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision$ $Date$
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class SynchronizedCollection implements Collection, Serializable {
|
||||
public class SynchronizedCollection<E> implements Collection<E>, Serializable {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = 2412805092710877986L;
|
||||
|
||||
/** The collection to decorate */
|
||||
protected final Collection collection;
|
||||
protected final Collection<E> collection;
|
||||
/** The object to lock on, needed for List/SortedSet views */
|
||||
protected final Object lock;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return a new synchronized collection
|
||||
* @throws IllegalArgumentException if collection is null
|
||||
*/
|
||||
public static Collection decorate(Collection coll) {
|
||||
return new SynchronizedCollection(coll);
|
||||
public static <T> Collection<T> decorate(Collection<T> coll) {
|
||||
return new SynchronizedCollection<T>(coll);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* 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
|
||||
* @throws IllegalArgumentException if the collection is null
|
||||
*/
|
||||
protected SynchronizedCollection(Collection collection) {
|
||||
protected SynchronizedCollection(Collection<E> collection) {
|
||||
if (collection == 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
|
||||
* @throws IllegalArgumentException if the collection is null
|
||||
*/
|
||||
protected SynchronizedCollection(Collection collection, Object lock) {
|
||||
protected SynchronizedCollection(Collection<E> collection, Object lock) {
|
||||
if (collection == null) {
|
||||
throw new IllegalArgumentException("Collection must not be null");
|
||||
}
|
||||
@ -90,40 +92,49 @@ public class SynchronizedCollection implements Collection, Serializable {
|
||||
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) {
|
||||
return collection.add(object);
|
||||
return decorated().add(object);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addAll(Collection coll) {
|
||||
public boolean addAll(Collection<? extends E> coll) {
|
||||
synchronized (lock) {
|
||||
return collection.addAll(coll);
|
||||
return decorated().addAll(coll);
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
synchronized (lock) {
|
||||
collection.clear();
|
||||
decorated().clear();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
synchronized (lock) {
|
||||
return collection.contains(object);
|
||||
return decorated().contains(object);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsAll(Collection coll) {
|
||||
public boolean containsAll(Collection<?> coll) {
|
||||
synchronized (lock) {
|
||||
return collection.containsAll(coll);
|
||||
return decorated().containsAll(coll);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
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
|
||||
*/
|
||||
public Iterator iterator() {
|
||||
return collection.iterator();
|
||||
public Iterator<E> iterator() {
|
||||
return decorated().iterator();
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
synchronized (lock) {
|
||||
return collection.toArray();
|
||||
return decorated().toArray();
|
||||
}
|
||||
}
|
||||
|
||||
public Object[] toArray(Object[] object) {
|
||||
public <T> T[] toArray(T[] object) {
|
||||
synchronized (lock) {
|
||||
return collection.toArray(object);
|
||||
return decorated().toArray(object);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
synchronized (lock) {
|
||||
return collection.remove(object);
|
||||
return decorated().remove(object);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean removeAll(Collection coll) {
|
||||
public boolean removeAll(Collection<?> coll) {
|
||||
synchronized (lock) {
|
||||
return collection.removeAll(coll);
|
||||
return decorated().removeAll(coll);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection coll) {
|
||||
public boolean retainAll(Collection<?> coll) {
|
||||
synchronized (lock) {
|
||||
return collection.retainAll(coll);
|
||||
return decorated().retainAll(coll);
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
synchronized (lock) {
|
||||
return collection.size();
|
||||
return decorated().size();
|
||||
}
|
||||
}
|
||||
|
||||
@ -182,19 +193,19 @@ public class SynchronizedCollection implements Collection, Serializable {
|
||||
if (object == this) {
|
||||
return true;
|
||||
}
|
||||
return collection.equals(object);
|
||||
return decorated().equals(object);
|
||||
}
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
synchronized (lock) {
|
||||
return collection.hashCode();
|
||||
return decorated().hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
synchronized (lock) {
|
||||
return collection.toString();
|
||||
return decorated().toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,6 @@ package org.apache.commons.collections.collection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
@ -33,18 +32,19 @@ import org.apache.commons.collections.Transformer;
|
||||
* <p>
|
||||
* This class is Serializable from Commons Collections 3.1.
|
||||
*
|
||||
* @param <E> the type of the elements in the collection
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision$ $Date$
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TransformedCollection extends AbstractSerializableCollectionDecorator {
|
||||
public class TransformedCollection<E> extends AbstractCollectionDecorator<E> {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = 8692300188161871514L;
|
||||
|
||||
/** The transformer to use */
|
||||
protected final Transformer transformer;
|
||||
protected final Transformer<E, E> transformer;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param <T> the type of the elements in the collection
|
||||
* @param coll the collection to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @return a new transformed collection
|
||||
* @throws IllegalArgumentException if collection or transformer is null
|
||||
*/
|
||||
public static Collection decorate(Collection coll, Transformer transformer) {
|
||||
return new TransformedCollection(coll, transformer);
|
||||
public static <T> Collection<T> decorate(Collection<T> coll, Transformer<T, T> transformer) {
|
||||
return new TransformedCollection<T>(coll, transformer);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* 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
|
||||
* @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);
|
||||
if (transformer == null) {
|
||||
throw new IllegalArgumentException("Transformer must not be null");
|
||||
@ -88,7 +89,7 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
|
||||
* @param object the object to transform
|
||||
* @return a transformed object
|
||||
*/
|
||||
protected Object transform(Object object) {
|
||||
protected E transform(E object) {
|
||||
return transformer.transform(object);
|
||||
}
|
||||
|
||||
@ -100,21 +101,21 @@ public class TransformedCollection extends AbstractSerializableCollectionDecorat
|
||||
* @param coll the collection to transform
|
||||
* @return a transformed object
|
||||
*/
|
||||
protected Collection transform(Collection coll) {
|
||||
List list = new ArrayList(coll.size());
|
||||
for (Iterator it = coll.iterator(); it.hasNext(); ) {
|
||||
list.add(transform(it.next()));
|
||||
protected Collection<E> transform(Collection<? extends E> coll) {
|
||||
List<E> list = new ArrayList<E>(coll.size());
|
||||
for (E item : coll) {
|
||||
list.add(transform(item));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public boolean add(Object object) {
|
||||
public boolean add(E object) {
|
||||
object = transform(object);
|
||||
return decorated().add(object);
|
||||
}
|
||||
|
||||
public boolean addAll(Collection coll) {
|
||||
public boolean addAll(Collection<? extends E> coll) {
|
||||
coll = transform(coll);
|
||||
return decorated().addAll(coll);
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ import org.apache.commons.collections.iterators.UnmodifiableIterator;
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public final class UnmodifiableBoundedCollection
|
||||
extends AbstractSerializableCollectionDecorator
|
||||
extends AbstractCollectionDecorator
|
||||
implements BoundedCollection {
|
||||
|
||||
/** Serialization version */
|
||||
|
@ -27,13 +27,14 @@ import org.apache.commons.collections.iterators.UnmodifiableIterator;
|
||||
* <p>
|
||||
* This class is Serializable from Commons Collections 3.1.
|
||||
*
|
||||
* @param <E> the type of the elements in the collection
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision$ $Date$
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public final class UnmodifiableCollection
|
||||
extends AbstractSerializableCollectionDecorator
|
||||
public final class UnmodifiableCollection<E>
|
||||
extends AbstractCollectionDecorator<E>
|
||||
implements Unmodifiable {
|
||||
|
||||
/** Serialization version */
|
||||
@ -44,17 +45,18 @@ public final class UnmodifiableCollection
|
||||
* <p>
|
||||
* 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
|
||||
* @return an unmodifiable collection
|
||||
* @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) {
|
||||
return coll;
|
||||
}
|
||||
return new UnmodifiableCollection(coll);
|
||||
return new UnmodifiableCollection<T>(coll);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
@ -62,20 +64,20 @@ public final class UnmodifiableCollection
|
||||
* @param coll the collection to decorate, must not be null
|
||||
* @throws IllegalArgumentException if collection is null
|
||||
*/
|
||||
private UnmodifiableCollection(Collection coll) {
|
||||
private UnmodifiableCollection(Collection<E> coll) {
|
||||
super(coll);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public Iterator iterator() {
|
||||
public Iterator<E> iterator() {
|
||||
return UnmodifiableIterator.decorate(decorated().iterator());
|
||||
}
|
||||
|
||||
public boolean add(Object object) {
|
||||
public boolean add(E object) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean addAll(Collection coll) {
|
||||
public boolean addAll(Collection<? extends E> coll) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@ -87,11 +89,11 @@ public final class UnmodifiableCollection
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean removeAll(Collection coll) {
|
||||
public boolean removeAll(Collection<?> coll) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection coll) {
|
||||
public boolean retainAll(Collection<?> coll) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user