Remove decorator inner classes.

Transfer static method calls to use new decorators package.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131059 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-05-09 18:41:34 +00:00
parent 9f2689243b
commit ff648b6cda
6 changed files with 133 additions and 1444 deletions

View File

@ -1,10 +1,10 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/BagUtils.java,v 1.9 2003/04/04 22:22:29 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/BagUtils.java,v 1.10 2003/05/09 18:41:34 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -57,15 +57,21 @@
*/
package org.apache.commons.collections;
import java.util.Comparator;
import java.util.Set;
import org.apache.commons.collections.decorators.PredicatedBag;
import org.apache.commons.collections.decorators.PredicatedSortedBag;
import org.apache.commons.collections.decorators.SynchronizedBag;
import org.apache.commons.collections.decorators.SynchronizedSortedBag;
import org.apache.commons.collections.decorators.TypedBag;
import org.apache.commons.collections.decorators.TypedSortedBag;
import org.apache.commons.collections.decorators.UnmodifiableBag;
import org.apache.commons.collections.decorators.UnmodifiableSortedBag;
/**
* Provides utility methods and decorators for {@link Bag}
* and {@link SortedBag} instances.
*
* @since Commons Collections 2.1
* @version $Revision: 1.9 $ $Date: 2003/04/04 22:22:29 $
* @version $Revision: 1.10 $ $Date: 2003/05/09 18:41:34 $
*
* @author Paul Jack
* @author Stephen Colebourne
@ -74,6 +80,16 @@ import java.util.Set;
*/
public class BagUtils {
/**
* An empty unmodifiable bag.
*/
public static final Bag EMPTY_BAG = UnmodifiableBag.decorate(new HashBag());
/**
* An empty unmodifiable sorted bag.
*/
public static final Bag EMPTY_SORTED_BAG = UnmodifiableSortedBag.decorate(new TreeBag());
/**
* Instantiation of BagUtils is not intended or required.
* However, some tools require an instance to operate.
@ -81,193 +97,6 @@ public class BagUtils {
public BagUtils() {
}
//-----------------------------------------------------------------------
/**
* Implementation of a Bag that validates elements before they are added.
*/
static class PredicatedBag
extends CollectionUtils.PredicatedCollection
implements Bag {
public PredicatedBag(Bag b, Predicate p) {
super(b, p);
}
public boolean add(Object o, int count) {
validate(o);
return getBag().add(o, count);
}
public boolean remove(Object o, int count) {
return getBag().remove(o, count);
}
public Set uniqueSet() {
return getBag().uniqueSet();
}
public int getCount(Object o) {
return getBag().getCount(o);
}
private Bag getBag() {
return (Bag) collection;
}
}
/**
* Implementation of a Bag that is synchronized.
*/
static class SynchronizedBag
extends CollectionUtils.SynchronizedCollection
implements Bag {
public SynchronizedBag(Bag bag) {
super(bag);
}
public synchronized boolean add(Object o, int count) {
return getBag().add(o, count);
}
public synchronized boolean remove(Object o, int count) {
return getBag().remove(o, count);
}
public synchronized Set uniqueSet() {
return getBag().uniqueSet();
}
public synchronized int getCount(Object o) {
return getBag().getCount(o);
}
private Bag getBag() {
return (Bag) collection;
}
}
/**
* Implementation of a Bag that is unmodifiable.
*/
static class UnmodifiableBag
extends CollectionUtils.UnmodifiableCollection
implements Bag {
public UnmodifiableBag(Bag bag) {
super(bag);
}
public boolean add(Object o, int count) {
throw new UnsupportedOperationException();
}
public boolean remove(Object o, int count) {
throw new UnsupportedOperationException();
}
public Set uniqueSet() {
return getBag().uniqueSet();
}
public int getCount(Object o) {
return getBag().getCount(o);
}
private Bag getBag() {
return (Bag) collection;
}
}
/**
* Implementation of a SortedBag that validates elements before they are added.
*/
static class PredicatedSortedBag
extends PredicatedBag
implements SortedBag {
public PredicatedSortedBag(SortedBag sb, Predicate p) {
super(sb, p);
}
public Comparator comparator() {
return getSortedBag().comparator();
}
public Object first() {
return getSortedBag().first();
}
public Object last() {
return getSortedBag().last();
}
private SortedBag getSortedBag() {
return (SortedBag) collection;
}
}
/**
* Implementation of a SortedBag that is synchronized.
*/
static class SynchronizedSortedBag
extends SynchronizedBag
implements SortedBag {
public SynchronizedSortedBag(SortedBag bag) {
super(bag);
}
public synchronized Comparator comparator() {
return getSortedBag().comparator();
}
public synchronized Object first() {
return getSortedBag().first();
}
public synchronized Object last() {
return getSortedBag().last();
}
private SortedBag getSortedBag() {
return (SortedBag) collection;
}
}
/**
* Implementation of a SortedBag that is unmodifiable.
*/
static class UnmodifiableSortedBag
extends UnmodifiableBag
implements SortedBag {
public UnmodifiableSortedBag(SortedBag bag) {
super(bag);
}
public Comparator comparator() {
return getSortedBag().comparator();
}
public Object first() {
return getSortedBag().first();
}
public Object last() {
return getSortedBag().last();
}
private SortedBag getSortedBag() {
return (SortedBag) collection;
}
}
//-----------------------------------------------------------------------
/**
* Returns a synchronized (thread-safe) bag backed by the given bag.
@ -296,7 +125,7 @@ public class BagUtils {
* @throws IllegalArgumentException if the Bag is null
*/
public static Bag synchronizedBag(Bag bag) {
return new SynchronizedBag(bag);
return SynchronizedBag.decorate(bag);
}
/**
@ -309,7 +138,7 @@ public class BagUtils {
* @throws IllegalArgumentException if the Bag is null
*/
public static Bag unmodifiableBag(Bag bag) {
return new UnmodifiableBag(bag);
return UnmodifiableBag.decorate(bag);
}
/**
@ -324,7 +153,7 @@ public class BagUtils {
* @throws IllegalArgumentException if the Bag or Predicate is null
*/
public static Bag predicatedBag(Bag bag, Predicate predicate) {
return new PredicatedBag(bag, predicate);
return PredicatedBag.decorate(bag, predicate);
}
/**
@ -337,7 +166,7 @@ public class BagUtils {
* @return a typed bag backed by the specified bag
*/
public static Bag typedBag(Bag bag, Class type) {
return predicatedBag(bag, new CollectionUtils.InstanceofPredicate(type));
return TypedBag.decorate(bag, type);
}
//-----------------------------------------------------------------------
@ -369,7 +198,7 @@ public class BagUtils {
* @throws IllegalArgumentException if the SortedBag is null
*/
public static SortedBag synchronizedSortedBag(SortedBag bag) {
return new SynchronizedSortedBag(bag);
return SynchronizedSortedBag.decorate(bag);
}
/**
@ -382,7 +211,7 @@ public class BagUtils {
* @throws IllegalArgumentException if the SortedBag is null
*/
public static SortedBag unmodifiableSortedBag(SortedBag bag) {
return new UnmodifiableSortedBag(bag);
return UnmodifiableSortedBag.decorate(bag);
}
/**
@ -398,7 +227,7 @@ public class BagUtils {
* @throws IllegalArgumentException if the SortedBag or Predicate is null
*/
public static SortedBag predicatedSortedBag(SortedBag bag, Predicate predicate) {
return new PredicatedSortedBag(bag, predicate);
return PredicatedSortedBag.decorate(bag, predicate);
}
/**
@ -411,7 +240,7 @@ public class BagUtils {
* @return a typed bag backed by the specified bag
*/
public static SortedBag typedSortedBag(SortedBag bag, Class type) {
return predicatedSortedBag(bag, new CollectionUtils.InstanceofPredicate(type));
return TypedSortedBag.decorate(bag, type);
}
}

View File

@ -1,13 +1,10 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/BufferUtils.java,v 1.10 2002/12/15 13:05:03 scolebourne Exp $
* $Revision: 1.10 $
* $Date: 2002/12/15 13:05:03 $
*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/BufferUtils.java,v 1.11 2003/05/09 18:41:34 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -23,11 +20,11 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
* 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
@ -36,7 +33,7 @@
*
* 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 Group.
* 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
@ -60,21 +57,27 @@
*/
package org.apache.commons.collections;
import java.util.Collection;
import org.apache.commons.collections.decorators.BlockingBuffer;
import org.apache.commons.collections.decorators.PredicatedBuffer;
import org.apache.commons.collections.decorators.SynchronizedBuffer;
import org.apache.commons.collections.decorators.TypedBuffer;
import org.apache.commons.collections.decorators.UnmodifiableBuffer;
/**
* Contains static utility methods for operating on {@link Buffer} objects.
*
* @since Commons Collections 2.1
* @version $Revision: 1.11 $ $Date: 2003/05/09 18:41:34 $
*
* @author Paul Jack
* @author Stephen Colebourne
* @version $Id: BufferUtils.java,v 1.10 2002/12/15 13:05:03 scolebourne Exp $
* @since 2.1
*/
public class BufferUtils {
/**
* An empty unmodifiable buffer.
*/
public static final Buffer EMPTY_BUFFER = BufferUtils.unmodifiableBuffer(new ArrayStack());
public static final Buffer EMPTY_BUFFER = UnmodifiableBuffer.decorate(new ArrayStack());
/**
* <code>BufferUtils</code> should not normally be instantiated.
@ -102,8 +105,8 @@ public class BufferUtils {
* @return a synchronized buffer backed by that buffer
* @throws IllegalArgumentException if the Buffer is null
*/
public static Buffer synchronizedBuffer(final Buffer buffer) {
return new SynchronizedBuffer(buffer);
public static Buffer synchronizedBuffer(Buffer buffer) {
return SynchronizedBuffer.decorate(buffer);
}
/**
@ -119,42 +122,7 @@ public class BufferUtils {
* @throws IllegalArgumentException if the Buffer is null
*/
public static Buffer blockingBuffer(Buffer buffer) {
return new SynchronizedBuffer(buffer) {
public synchronized boolean add(Object o) {
boolean r = collection.add(o);
notify();
return r;
}
public synchronized boolean addAll(Collection c) {
boolean r = collection.addAll(c);
notifyAll();
return r;
}
public synchronized Object get() {
while (collection.isEmpty()) {
try {
wait();
} catch (InterruptedException e) {
throw new BufferUnderflowException();
}
}
return ((Buffer)collection).get();
}
public synchronized Object remove() {
while (collection.isEmpty()) {
try {
wait();
} catch (InterruptedException e) {
throw new BufferUnderflowException();
}
}
return ((Buffer)collection).remove();
}
};
return BlockingBuffer.decorate(buffer);
}
/**
@ -165,7 +133,7 @@ public class BufferUtils {
* @throws IllegalArgumentException if the Buffer is null
*/
public static Buffer unmodifiableBuffer(Buffer buffer) {
return new UnmodifiableBuffer(buffer);
return UnmodifiableBuffer.decorate(buffer);
}
/**
@ -180,66 +148,22 @@ public class BufferUtils {
* @return a predicated buffer
* @throws IllegalArgumentException if the Buffer or Predicate is null
*/
public static Buffer predicatedBuffer(Buffer buffer, final Predicate predicate) {
return new PredicatedBuffer(buffer, predicate);
public static Buffer predicatedBuffer(Buffer buffer, Predicate predicate) {
return PredicatedBuffer.decorate(buffer, predicate);
}
static class SynchronizedBuffer
extends CollectionUtils.SynchronizedCollection
implements Buffer {
public SynchronizedBuffer(Buffer b) {
super(b);
}
public synchronized Object get() {
return ((Buffer)collection).get();
}
public synchronized Object remove() {
return ((Buffer)collection).remove();
}
/**
* Returns a typed buffer backed by the given buffer.
* <p>
* Only elements of the specified type can be added to the buffer.
*
* @param buffer the buffer to predicate, must not be null
* @param type the type to allow into the buffer, must not be null
* @return a typed buffer
* @throws IllegalArgumentException if the buffer or type is null
*/
public static Buffer typedBuffer(Buffer buffer, Class type) {
return TypedBuffer.decorate(buffer, type);
}
static class UnmodifiableBuffer
extends CollectionUtils.UnmodifiableCollection
implements Buffer {
public UnmodifiableBuffer(Buffer b) {
super(b);
}
public Object get() {
return ((Buffer)collection).get();
}
public Object remove() {
throw new UnsupportedOperationException();
}
}
static class PredicatedBuffer
extends CollectionUtils.PredicatedCollection
implements Buffer {
public PredicatedBuffer(Buffer b, Predicate p) {
super(b, p);
}
public Object get() {
return ((Buffer)collection).get();
}
public Object remove() {
return ((Buffer)collection).remove();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.29 2003/04/04 22:22:29 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.30 2003/05/09 18:41:34 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -57,7 +57,6 @@
*/
package org.apache.commons.collections;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -71,6 +70,9 @@ import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.apache.commons.collections.decorators.PredicatedCollection;
import org.apache.commons.collections.decorators.TypedCollection;
import org.apache.commons.collections.decorators.UnmodifiableBoundedCollection;
import org.apache.commons.collections.iterators.ArrayIterator;
import org.apache.commons.collections.iterators.EnumerationIterator;
@ -78,7 +80,7 @@ import org.apache.commons.collections.iterators.EnumerationIterator;
* A set of {@link Collection} related utility methods.
*
* @since Commons Collections 1.0
* @version $Revision: 1.29 $ $Date: 2003/04/04 22:22:29 $
* @version $Revision: 1.30 $ $Date: 2003/05/09 18:41:34 $
*
* @author Rodney Waldhoff
* @author Paul Jack
@ -828,9 +830,6 @@ public class CollectionUtils {
* This method uses the {@link BoundedCollection} class to determine the
* full status. If the collection does not implement this interface then
* false is returned.
* <p>
* This method handles the synchronized, blocking, unmodifiable
* and predicated decorators.
*
* @return true if the Collection is full
* @throws NullPointerException if the collection is null
@ -839,24 +838,13 @@ public class CollectionUtils {
if (coll == null) {
throw new NullPointerException("The collection must not be null");
}
Collection unwrappedCollection = coll;
// handle decorators
while (true) {
if (unwrappedCollection instanceof CollectionUtils.CollectionWrapper) {
unwrappedCollection = ((CollectionUtils.CollectionWrapper) unwrappedCollection).collection;
} else if (unwrappedCollection instanceof CollectionUtils.SynchronizedCollection) {
unwrappedCollection = ((CollectionUtils.SynchronizedCollection) unwrappedCollection).collection;
} else {
break;
}
try {
BoundedCollection bcoll = UnmodifiableBoundedCollection.decorateUsing(coll);
return bcoll.isFull();
} catch (IllegalArgumentException ex) {
return false;
}
// is it full
if (unwrappedCollection instanceof BoundedCollection) {
return ((BoundedCollection) unwrappedCollection).isFull();
}
return false;
}
/**
@ -865,9 +853,6 @@ public class CollectionUtils {
* This method uses the {@link BoundedCollection} class to determine the
* maximum size. If the collection does not implement this interface then
* -1 is returned.
* <p>
* This method handles the synchronized, blocking, unmodifiable
* and predicated decorators.
*
* @return the maximum size of the Collection, -1 if no maximum size
* @throws NullPointerException if the collection is null
@ -876,314 +861,12 @@ public class CollectionUtils {
if (coll == null) {
throw new NullPointerException("The collection must not be null");
}
Collection unwrappedCollection = coll;
// handle decorators
while (true) {
if (unwrappedCollection instanceof CollectionUtils.CollectionWrapper) {
unwrappedCollection = ((CollectionUtils.CollectionWrapper) unwrappedCollection).collection;
} else if (unwrappedCollection instanceof CollectionUtils.SynchronizedCollection) {
unwrappedCollection = ((CollectionUtils.SynchronizedCollection) unwrappedCollection).collection;
} else {
break;
}
}
// get max size
if (unwrappedCollection instanceof BoundedCollection) {
return ((BoundedCollection) unwrappedCollection).maxSize();
}
return -1;
}
//-----------------------------------------------------------------------
/**
* Base class for collection decorators. I decided to do it this way
* because it seemed to result in the most reuse.
*
* Inner class tree looks like:
* <pre>
* CollectionWrapper
* PredicatedCollection
* PredicatedSet
* PredicatedList
* PredicatedBag
* PredicatedBuffer
* UnmodifiableCollection
* UnmodifiableBag
* UnmodifiableBuffer
* LazyCollection
* LazyList
* LazyBag
* SynchronizedCollection
* SynchronizedBuffer
* SynchronizedBag
* SynchronizedBuffer
* </pre>
*/
static class CollectionWrapper
implements Collection {
protected final Collection collection;
public CollectionWrapper(Collection collection) {
if (collection == null) {
throw new IllegalArgumentException("Collection must not be null");
}
this.collection = collection;
}
public int size() {
return collection.size();
}
public boolean isEmpty() {
return collection.isEmpty();
}
public boolean contains(Object o) {
return collection.contains(o);
}
public Iterator iterator() {
return collection.iterator();
}
public Object[] toArray() {
return collection.toArray();
}
public Object[] toArray(Object[] o) {
return collection.toArray(o);
}
public boolean add(Object o) {
return collection.add(o);
}
public boolean remove(Object o) {
return collection.remove(o);
}
public boolean containsAll(Collection c2) {
return collection.containsAll(c2);
}
public boolean addAll(Collection c2) {
return collection.addAll(c2);
}
public boolean removeAll(Collection c2) {
return collection.removeAll(c2);
}
public boolean retainAll(Collection c2) {
return collection.retainAll(c2);
}
public void clear() {
collection.clear();
}
public boolean equals(Object o) {
if (o == this) return true;
return collection.equals(o);
}
public int hashCode() {
return collection.hashCode();
}
public String toString() {
return collection.toString();
}
}
/**
* Implementation of a collection that checks entries.
*/
static class PredicatedCollection
extends CollectionWrapper {
protected final Predicate predicate;
public PredicatedCollection(Collection c, Predicate p) {
super(c);
if (p == null) {
throw new IllegalArgumentException("Predicate must not be null");
}
this.predicate = p;
for (Iterator iter = c.iterator(); iter.hasNext(); ) {
validate(iter.next());
}
}
public boolean add(Object o) {
validate(o);
return collection.add(o);
}
public boolean addAll(Collection c2) {
for (Iterator iter = c2.iterator(); iter.hasNext(); ) {
validate(iter.next());
}
return collection.addAll(c2);
}
protected void validate(Object o) {
if (!predicate.evaluate(o)) {
throw new IllegalArgumentException("Cannot add Object - Predicate rejected it");
}
}
}
/**
* Implementation of a collection that is unmodifiable.
*/
static class UnmodifiableCollection
extends CollectionWrapper {
public UnmodifiableCollection(Collection c) {
super(c);
}
public boolean add(Object o) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection c) {
throw new UnsupportedOperationException();
}
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException();
}
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException();
}
public void clear() {
throw new UnsupportedOperationException();
}
public Iterator iterator() {
return new IteratorUtils.UnmodifiableIterator(collection.iterator());
}
}
/**
* Implementation of a collection that is synchronized.
*/
static class SynchronizedCollection {
protected final Collection collection;
public SynchronizedCollection(Collection collection) {
if (collection == null) {
throw new IllegalArgumentException("Collection must not be null");
}
this.collection = collection;
}
public synchronized int size() {
return collection.size();
}
public synchronized boolean isEmpty() {
return collection.isEmpty();
}
public synchronized boolean contains(Object o) {
return collection.contains(o);
}
public Iterator iterator() {
return collection.iterator();
}
public synchronized Object[] toArray() {
return collection.toArray();
}
public synchronized Object[] toArray(Object[] o) {
return collection.toArray(o);
}
public synchronized boolean add(Object o) {
return collection.add(o);
}
public synchronized boolean remove(Object o) {
return collection.remove(o);
}
public synchronized boolean containsAll(Collection c2) {
return collection.containsAll(c2);
}
public synchronized boolean addAll(Collection c2) {
return collection.addAll(c2);
}
public synchronized boolean removeAll(Collection c2) {
return collection.removeAll(c2);
}
public synchronized boolean retainAll(Collection c2) {
return collection.retainAll(c2);
}
public synchronized void clear() {
collection.clear();
}
public synchronized boolean equals(Object o) {
return collection.equals(o);
}
public synchronized int hashCode() {
return collection.hashCode();
}
public synchronized String toString() {
return collection.toString();
}
}
/**
* <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, Serializable {
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);
try {
BoundedCollection bcoll = UnmodifiableBoundedCollection.decorateUsing(coll);
return bcoll.maxSize();
} catch (IllegalArgumentException ex) {
return -1;
}
}
@ -1240,7 +923,7 @@ public class CollectionUtils {
* @throws IllegalArgumentException if the Collection is null
*/
public static Collection predicatedCollection(Collection collection, Predicate predicate) {
return new PredicatedCollection(collection, predicate);
return PredicatedCollection.decorate(collection, predicate);
}
/**
@ -1253,7 +936,7 @@ public class CollectionUtils {
* @return a typed collection backed by the specified collection
*/
public static Collection typedCollection(Collection collection, Class type) {
return predicatedCollection(collection, new InstanceofPredicate(type));
return TypedCollection.decorate(collection, type);
}
}

View File

@ -1,10 +1,10 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/ListUtils.java,v 1.16 2003/04/09 23:37:54 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/ListUtils.java,v 1.17 2003/05/09 18:41:34 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -62,14 +62,18 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections.decorators.FixedSizeList;
import org.apache.commons.collections.decorators.LazyList;
import org.apache.commons.collections.decorators.PredicatedList;
import org.apache.commons.collections.decorators.TypedList;
/**
* Contains static utility methods and decorators for {@link List}
* instances.
*
* @since Commons Collections 1.0
* @version $Revision: 1.16 $ $Date: 2003/04/09 23:37:54 $
* @version $Revision: 1.17 $ $Date: 2003/05/09 18:41:34 $
*
* @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
@ -253,297 +257,6 @@ public class ListUtils {
return hashCode;
}
//-----------------------------------------------------------------------
/**
* Implementation of a ListIterator that wraps an original.
*/
static class ListIteratorWrapper
implements ListIterator {
final protected ListIterator iterator;
public ListIteratorWrapper(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 set(Object o) {
iterator.set(o);
}
public void add(Object o) {
iterator.add(o);
}
}
/**
* Implementation of a list that checks (predicates) each entry.
*/
static class PredicatedList
extends CollectionUtils.PredicatedCollection
implements List {
public PredicatedList(List list, Predicate p) {
super(list, p);
}
public boolean addAll(int i, Collection c) {
for (Iterator iter = c.iterator(); iter.hasNext(); ) {
validate(iter.next());
}
return getList().addAll(i, c);
}
public Object get(int i) {
return getList().get(i);
}
public Object set(int i, Object o) {
validate(o);
return getList().set(i, o);
}
public void add(int i, Object o) {
validate(o);
getList().add(i, o);
}
public Object remove(int i) {
return getList().remove(i);
}
public int indexOf(Object o) {
return getList().indexOf(o);
}
public int lastIndexOf(Object o) {
return getList().lastIndexOf(o);
}
public ListIterator listIterator() {
return listIterator(0);
}
public ListIterator listIterator(int i) {
return new ListIteratorWrapper(getList().listIterator(i)) {
public void add(Object o) {
validate(o);
iterator.add(o);
}
public void set(Object o) {
validate(o);
iterator.set(o);
}
};
}
public List subList(int i1, int i2) {
List sub = getList().subList(i1, i2);
return new PredicatedList(sub, predicate);
}
private List getList() {
return (List)collection;
}
}
/**
* Implementation of a list that has a fixed size.
*/
static class FixedSizeList
extends CollectionUtils.UnmodifiableCollection
implements List {
public FixedSizeList(List list) {
super(list);
}
public boolean addAll(int i, Collection c) {
throw new UnsupportedOperationException();
}
public Object get(int i) {
return getList().get(i);
}
public Object set(int i, Object o) {
return getList().set(i, o);
}
public void add(int i, Object o) {
throw new UnsupportedOperationException();
}
public Object remove(int i) {
throw new UnsupportedOperationException();
}
public int indexOf(Object o) {
return getList().indexOf(o);
}
public int lastIndexOf(Object o) {
return getList().lastIndexOf(o);
}
public ListIterator listIterator() {
return listIterator(0);
}
public ListIterator listIterator(int i) {
return new ListIteratorWrapper(getList().listIterator(i)) {
public void remove() {
throw new UnsupportedOperationException();
}
public void add(Object o) {
throw new UnsupportedOperationException();
}
public void remove(Object o) {
throw new UnsupportedOperationException();
}
};
}
public List subList(int i1, int i2) {
List sub = getList().subList(i1, i2);
return new FixedSizeList(sub);
}
private List getList() {
return (List)collection;
}
}
/**
* Implementation of a list that creates objects on demand.
*/
static class LazyList
extends CollectionUtils.CollectionWrapper
implements List {
protected final Factory factory;
public LazyList(List list, Factory factory) {
super(list);
if (factory == null) {
throw new IllegalArgumentException("Factory must not be null");
}
this.factory = factory;
}
/* Proxy method to the impl's get method. With the exception that if it's out
* of bounds, then the collection will grow, leaving place-holders in its
* wake, so that an item can be set at any given index. Later the
* place-holders are removed to return to a pure collection.
*
* If there's a place-holder at the index, then it's replaced with a proper
* object to be used.
*/
public Object get(int index) {
Object obj;
if (index < (getList().size())) {
/* within bounds, get the object */
obj = getList().get(index);
if (obj == null) {
/* item is a place holder, create new one, set and return */
obj = this.factory.create();
this.getList().set(index, obj);
return obj;
} else {
/* good and ready to go */
return obj;
}
} else {
/* we have to grow the list */
for (int i = getList().size(); i < index; i++) {
getList().add(null);
}
/* create our last object, set and return */
obj = this.factory.create();
getList().add(obj);
return obj;
}
}
/* proxy the call to the provided list implementation. */
public List subList(int fromIndex, int toIndex) {
/* wrap the returned sublist so it can continue the functionality */
return new LazyList(getList().subList(fromIndex, toIndex), factory);
}
public boolean addAll(int i, Collection c) {
return getList().addAll(i, c);
}
public Object set(int i, Object o) {
return getList().set(i, o);
}
public void add(int i, Object o) {
getList().add(i, o);
}
public Object remove(int i) {
return getList().remove(i);
}
public int indexOf(Object o) {
return getList().indexOf(o);
}
public int lastIndexOf(Object o) {
return getList().lastIndexOf(o);
}
public ListIterator listIterator() {
return getList().listIterator();
}
public ListIterator listIterator(int i) {
return getList().listIterator(i);
}
private List getList() {
return (List)collection;
}
}
//-----------------------------------------------------------------------
/**
* Returns a synchronized list backed by the given list.
@ -596,7 +309,7 @@ public class ListUtils {
* @throws IllegalArgumentException if the List or Predicate is null
*/
public static List predicatedList(List list, Predicate predicate) {
return new PredicatedList(list, predicate);
return PredicatedList.decorate(list, predicate);
}
/**
@ -609,7 +322,7 @@ public class ListUtils {
* @return a typed list backed by the specified list
*/
public static List typedList(List list, Class type) {
return predicatedList(list, new CollectionUtils.InstanceofPredicate(type));
return TypedList.decorate(list, type);
}
/**
@ -642,7 +355,7 @@ public class ListUtils {
* @throws IllegalArgumentException if the List or Factory is null
*/
public static List lazyList(List list, Factory factory) {
return new LazyList(list, factory);
return LazyList.decorate(list, factory);
}
/**
@ -656,7 +369,7 @@ public class ListUtils {
* @throws IllegalArgumentException if the List is null
*/
public static List fixedSizeList(List list) {
return new FixedSizeList(list);
return FixedSizeList.decorate(list);
}
}

View File

@ -1,10 +1,10 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.22 2003/04/26 14:28:31 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.23 2003/05/09 18:41:34 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -20,11 +20,11 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
* 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
@ -33,7 +33,7 @@
*
* 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 Group.
* 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
@ -61,17 +61,24 @@ import java.io.PrintStream;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.collections.decorators.FixedSizeMap;
import org.apache.commons.collections.decorators.FixedSizeSortedMap;
import org.apache.commons.collections.decorators.LazyMap;
import org.apache.commons.collections.decorators.LazySortedMap;
import org.apache.commons.collections.decorators.PredicatedMap;
import org.apache.commons.collections.decorators.PredicatedSortedMap;
import org.apache.commons.collections.decorators.TypedMap;
import org.apache.commons.collections.decorators.TypedSortedMap;
/**
* A helper class for using {@link Map Map} instances.
* <p>
@ -92,7 +99,7 @@ import java.util.TreeMap;
* </ul>
*
* @since Commons Collections 1.0
* @version $Revision: 1.22 $ $Date: 2003/04/26 14:28:31 $
* @version $Revision: 1.23 $ $Date: 2003/05/09 18:41:34 $
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
@ -800,413 +807,6 @@ public class MapUtils {
}
}
//-----------------------------------------------------------------------
/**
* Implementation of a map that checks (predicates) additions.
*/
static class PredicatedMap
extends ProxyMap {
protected final Predicate keyPredicate;
protected final Predicate valuePredicate;
public PredicatedMap(Map map, Predicate keyPred, Predicate valuePred) {
super(map);
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
if (keyPred == null) {
throw new IllegalArgumentException("Key Predicate must not be null");
}
if (valuePred == null) {
throw new IllegalArgumentException("Value Predicate must not be null");
}
this.keyPredicate = keyPred;
this.valuePredicate = valuePred;
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
validate(key, value);
}
}
public Object put(Object key, Object value) {
validate(key, value);
return map.put(key, value);
}
public void putAll(Map m) {
Iterator iter = m.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
validate(key, value);
}
map.putAll(m);
}
public Set entrySet() {
return new PredicatedMapEntrySet(map.entrySet(), valuePredicate);
}
private void validate(Object key, Object value) {
if (!keyPredicate.evaluate(key)) {
throw new IllegalArgumentException("Cannot add key - Predicate rejected it");
}
if (!valuePredicate.evaluate(value)) {
throw new IllegalArgumentException("Cannot add value - Predicate rejected it");
}
}
}
/**
* Implementation of an entry set that checks (predicates) additions.
*/
static class PredicatedMapEntrySet
extends CollectionUtils.CollectionWrapper
implements Set {
private final Predicate predicate;
public PredicatedMapEntrySet(Set set, Predicate p) {
super(set);
this.predicate = p;
}
public Iterator iterator() {
final Iterator iterator = collection.iterator();
return new Iterator() {
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
Map.Entry entry = (Map.Entry)iterator.next();
return new PredicatedMapEntry(entry, predicate);
}
public void remove() {
iterator.remove();
}
};
}
}
/**
* Implementation of a map entry that checks (predicates) additions.
*/
static class PredicatedMapEntry
implements Map.Entry {
private final Map.Entry entry;
private final Predicate predicate;
public PredicatedMapEntry(Map.Entry entry, Predicate p) {
if (entry == null) {
throw new IllegalArgumentException("Map.Entry must not be null");
}
if (p == null) {
throw new IllegalArgumentException("Predicate must not be null");
}
this.entry = entry;
this.predicate = p;
}
public boolean equals(Object o) {
return entry.equals(o);
}
public int hashCode() {
return entry.hashCode();
}
public String toString() {
return entry.toString();
}
public Object getKey() {
return entry.getKey();
}
public Object getValue() {
return entry.getValue();
}
public Object setValue(Object o) {
if (!predicate.evaluate(o)) {
throw new IllegalArgumentException("Cannot set value - Predicate rejected it");
}
return entry.setValue(o);
}
}
/**
* Implementation of a map that is fixed in size.
*/
static class FixedSizeMap
extends ProxyMap {
public FixedSizeMap(Map map) {
super(map);
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
}
public Object put(Object key, Object value) {
if (!map.containsKey(key)) {
throw new IllegalArgumentException("Cannot put new key/value pair - List is fixed size");
}
return map.put(key, value);
}
public void putAll(Map m) {
for (Iterator iter = m.keySet().iterator(); iter.hasNext(); ) {
if (!map.containsKey(iter.next())) {
throw new IllegalArgumentException("Cannot put new key/value pair - List is fixed size");
}
}
map.putAll(m);
}
}
/**
* Implementation of a map that creates objects on demand.
*/
static class LazyMap
extends ProxyMap {
protected final Factory factory;
public LazyMap(Map map, Factory factory) {
super(map);
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
if (factory == null) {
throw new IllegalArgumentException("Factory must not be null");
}
this.factory = factory;
}
public Object get(Object key) {
if (!map.containsKey(key)) {
Object value = factory.create();
map.put(key, value);
return value;
}
return map.get(key);
}
}
/**
* Implementation of a map that creates objects on demand.
*/
static class LazyTransformerMap
extends ProxyMap {
protected final Transformer transformer;
public LazyTransformerMap(Map map, Transformer transformer) {
super(map);
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
if (transformer == null) {
throw new IllegalArgumentException("Transformer must not be null");
}
this.transformer = transformer;
}
public Object get(Object key) {
if (!map.containsKey(key)) {
Object value = transformer.transform(key);
map.put(key, value);
return value;
}
return map.get(key);
}
}
/**
* Implementation of a sorted map that checks additions.
*/
static class PredicatedSortedMap
extends PredicatedMap
implements SortedMap {
public PredicatedSortedMap(SortedMap map, Predicate k, Predicate v) {
super(map, k, v);
}
public Object firstKey() {
return getSortedMap().firstKey();
}
public Object lastKey() {
return getSortedMap().lastKey();
}
public Comparator comparator() {
return getSortedMap().comparator();
}
public SortedMap subMap(Object o1, Object o2) {
SortedMap sub = getSortedMap().subMap(o1, o2);
return new PredicatedSortedMap(sub, keyPredicate, valuePredicate);
}
public SortedMap headMap(Object o1) {
SortedMap sub = getSortedMap().headMap(o1);
return new PredicatedSortedMap(sub, keyPredicate, valuePredicate);
}
public SortedMap tailMap(Object o1) {
SortedMap sub = getSortedMap().tailMap(o1);
return new PredicatedSortedMap(sub, keyPredicate, valuePredicate);
}
private SortedMap getSortedMap() {
return (SortedMap)map;
}
}
/**
* Implementation of a sorted map that is fixed in size.
*/
static class FixedSizeSortedMap
extends FixedSizeMap
implements SortedMap {
public FixedSizeSortedMap(SortedMap m) {
super(m);
}
public Object firstKey() {
return getSortedMap().firstKey();
}
public Object lastKey() {
return getSortedMap().lastKey();
}
public Comparator comparator() {
return getSortedMap().comparator();
}
public SortedMap subMap(Object o1, Object o2) {
return new FixedSizeSortedMap(getSortedMap().subMap(o1, o2));
}
public SortedMap headMap(Object o1) {
return new FixedSizeSortedMap(getSortedMap().headMap(o1));
}
public SortedMap tailMap(Object o1) {
return new FixedSizeSortedMap(getSortedMap().tailMap(o1));
}
private SortedMap getSortedMap() {
return (SortedMap)map;
}
}
/**
* Implementation of a sorted map that creates objects on demand.
*/
static class LazySortedMap
extends LazyMap
implements SortedMap {
public LazySortedMap(SortedMap m, Factory factory) {
super(m, factory);
}
public Object firstKey() {
return getSortedMap().firstKey();
}
public Object lastKey() {
return getSortedMap().lastKey();
}
public Comparator comparator() {
return getSortedMap().comparator();
}
public SortedMap subMap(Object o1, Object o2) {
return new LazySortedMap(getSortedMap().subMap(o1, o2), factory);
}
public SortedMap headMap(Object o1) {
return new LazySortedMap(getSortedMap().headMap(o1), factory);
}
public SortedMap tailMap(Object o1) {
return new LazySortedMap(getSortedMap().tailMap(o1), factory);
}
private SortedMap getSortedMap() {
return (SortedMap)map;
}
}
/**
* Implementation of a sorted map that creates objects on demand.
*/
static class LazyTransformerSortedMap
extends LazyTransformerMap
implements SortedMap {
public LazyTransformerSortedMap(SortedMap m, Transformer transformer) {
super(m, transformer);
}
public Object firstKey() {
return getSortedMap().firstKey();
}
public Object lastKey() {
return getSortedMap().lastKey();
}
public Comparator comparator() {
return getSortedMap().comparator();
}
public SortedMap subMap(Object o1, Object o2) {
return new LazyTransformerSortedMap(getSortedMap().subMap(o1, o2), transformer);
}
public SortedMap headMap(Object o1) {
return new LazyTransformerSortedMap(getSortedMap().headMap(o1), transformer);
}
public SortedMap tailMap(Object o1) {
return new LazyTransformerSortedMap(getSortedMap().tailMap(o1), transformer);
}
private SortedMap getSortedMap() {
return (SortedMap)map;
}
}
//-----------------------------------------------------------------------
/**
* Returns a synchronized map backed by the given map.
@ -1261,7 +861,7 @@ public class MapUtils {
* @throws IllegalArgumentException if the Map or Predicates are null
*/
public static Map predicatedMap(Map map, Predicate keyPred, Predicate valuePred) {
return new PredicatedMap(map, keyPred, valuePred);
return PredicatedMap.decorate(map, keyPred, valuePred);
}
/**
@ -1275,10 +875,7 @@ public class MapUtils {
* @return a typed map backed by the specified map
*/
public static Map typedMap(Map map, Class keyType, Class valueType) {
return predicatedMap(
map,
new CollectionUtils.InstanceofPredicate(keyType),
new CollectionUtils.InstanceofPredicate(valueType));
return TypedMap.decorate(map, keyType, valueType);
}
/**
@ -1292,7 +889,7 @@ public class MapUtils {
* @throws IllegalArgumentException if the Map is null
*/
public static Map fixedSizeMap(Map map) {
return new FixedSizeMap(map);
return FixedSizeMap.decorate(map);
}
/**
@ -1324,7 +921,7 @@ public class MapUtils {
* @throws IllegalArgumentException if the Map or Factory is null
*/
public static Map lazyMap(Map map, Factory factory) {
return new LazyMap(map, factory);
return LazyMap.decorate(map, factory);
}
/**
@ -1363,7 +960,7 @@ public class MapUtils {
* @throws IllegalArgumentException if the Map or Transformer is null
*/
public static Map lazyMap(Map map, Transformer transformerFactory) {
return new LazyTransformerMap(map, transformerFactory);
return LazyMap.decorate(map, transformerFactory);
}
//-----------------------------------------------------------------------
@ -1420,7 +1017,7 @@ public class MapUtils {
* @throws IllegalArgumentException if the SortedMap or Predicates are null
*/
public static SortedMap predicatedSortedMap(SortedMap map, Predicate keyPred, Predicate valuePred) {
return new PredicatedSortedMap(map, keyPred, valuePred);
return PredicatedSortedMap.decorate(map, keyPred, valuePred);
}
/**
@ -1434,10 +1031,7 @@ public class MapUtils {
* @return a typed map backed by the specified map
*/
public static SortedMap typedSortedMap(SortedMap map, Class keyType, Class valueType) {
return predicatedSortedMap(
map,
new CollectionUtils.InstanceofPredicate(keyType),
new CollectionUtils.InstanceofPredicate(valueType));
return TypedSortedMap.decorate(map, keyType, valueType);
}
/**
@ -1451,7 +1045,7 @@ public class MapUtils {
* @throws IllegalArgumentException if the SortedMap is null
*/
public static SortedMap fixedSizeSortedMap(SortedMap map) {
return new FixedSizeSortedMap(map);
return FixedSizeSortedMap.decorate(map);
}
/**
@ -1484,7 +1078,7 @@ public class MapUtils {
* @throws IllegalArgumentException if the SortedMap or Factory is null
*/
public static SortedMap lazySortedMap(SortedMap map, Factory factory) {
return new LazySortedMap(map, factory);
return LazySortedMap.decorate(map, factory);
}
/**
@ -1523,7 +1117,7 @@ public class MapUtils {
* @throws IllegalArgumentException if the Map or Transformer is null
*/
public static SortedMap lazySortedMap(SortedMap map, Transformer transformerFactory) {
return new LazyTransformerSortedMap(map, transformerFactory);
return LazySortedMap.decorate(map, transformerFactory);
}
}

View File

@ -1,10 +1,10 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/SetUtils.java,v 1.12 2003/04/09 23:37:54 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/SetUtils.java,v 1.13 2003/05/09 18:41:34 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -59,18 +59,22 @@ package org.apache.commons.collections;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.apache.commons.collections.decorators.PredicatedSet;
import org.apache.commons.collections.decorators.PredicatedSortedSet;
import org.apache.commons.collections.decorators.TypedSet;
import org.apache.commons.collections.decorators.TypedSortedSet;
/**
* Provides static utility methods and decorators for {@link Set}
* and {@link SortedSet} instances.
*
* @version $Revision: 1.12 $ $Date: 2003/04/09 23:37:54 $
* @since Commons Collection 2.1
* @since Commons Collections 2.1
* @version $Revision: 1.13 $ $Date: 2003/05/09 18:41:34 $
*
* @author Paul Jack
* @author Stephen Colebourne
@ -166,64 +170,6 @@ public class SetUtils {
return hashCode;
}
//-----------------------------------------------------------------------
/**
* Implementation of a set that checks new entries.
*/
static class PredicatedSet
extends CollectionUtils.PredicatedCollection
implements Set {
public PredicatedSet(Set set, Predicate predicate) {
super(set, predicate);
}
}
/**
* Implementation of a sorted set that checks new entries.
*/
static class PredicatedSortedSet
extends PredicatedSet
implements SortedSet {
public PredicatedSortedSet(SortedSet set, Predicate predicate) {
super(set, predicate);
}
public SortedSet subSet(Object o1, Object o2) {
SortedSet sub = getSortedSet().subSet(o1, o2);
return new PredicatedSortedSet(sub, predicate);
}
public SortedSet headSet(Object o1) {
SortedSet sub = getSortedSet().headSet(o1);
return new PredicatedSortedSet(sub, predicate);
}
public SortedSet tailSet(Object o1) {
SortedSet sub = getSortedSet().tailSet(o1);
return new PredicatedSortedSet(sub, predicate);
}
public Object first() {
return getSortedSet().first();
}
public Object last() {
return getSortedSet().last();
}
public Comparator comparator() {
return getSortedSet().comparator();
}
private SortedSet getSortedSet() {
return (SortedSet)collection;
}
}
//-----------------------------------------------------------------------
/**
* Returns a synchronized set backed by the given set.
@ -276,7 +222,7 @@ public class SetUtils {
* @throws IllegalArgumentException if the Set or Predicate is null
*/
public static Set predicatedSet(Set set, Predicate predicate) {
return new PredicatedSet(set, predicate);
return PredicatedSet.decorate(set, predicate);
}
/**
@ -289,7 +235,7 @@ public class SetUtils {
* @return a typed set backed by the specified set
*/
public static Set typedSet(Set set, Class type) {
return predicatedSet(set, new CollectionUtils.InstanceofPredicate(type));
return TypedSet.decorate(set, type);
}
//-----------------------------------------------------------------------
@ -345,7 +291,7 @@ public class SetUtils {
* @throws IllegalArgumentException if the Set or Predicate is null
*/
public static SortedSet predicatedSortedSet(SortedSet set, Predicate predicate) {
return new PredicatedSortedSet(set, predicate);
return PredicatedSortedSet.decorate(set, predicate);
}
/**
@ -358,7 +304,7 @@ public class SetUtils {
* @return a typed set backed by the specified set
*/
public static SortedSet typedSortedSet(SortedSet set, Class type) {
return predicatedSortedSet(set, new CollectionUtils.InstanceofPredicate(type));
return TypedSortedSet.decorate(set, type);
}
}