You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/09/15 07:30:02 UTC
svn commit: r814997 [5/18] - in /commons/proper/collections/trunk/src:
java/org/apache/commons/collections/ java/org/apache/commons/collections/bag/
java/org/apache/commons/collections/bidimap/
java/org/apache/commons/collections/buffer/ java/org/apach...
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/SwitchClosure.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/SwitchClosure.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/SwitchClosure.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/SwitchClosure.java Tue Sep 15 05:29:56 2009
@@ -17,7 +17,6 @@
package org.apache.commons.collections.functors;
import java.io.Serializable;
-import java.util.Iterator;
import java.util.Map;
import org.apache.commons.collections.Closure;
@@ -32,21 +31,21 @@
*
* @author Stephen Colebourne
*/
-public class SwitchClosure implements Closure, Serializable {
+public class SwitchClosure<E> implements Closure<E>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = 3518477308466486130L;
/** The tests to consider */
- private final Predicate[] iPredicates;
+ private final Predicate<? super E>[] iPredicates;
/** The matching closures to call */
- private final Closure[] iClosures;
+ private final Closure<? super E>[] iClosures;
/** The default closure to call if no tests match */
- private final Closure iDefault;
+ private final Closure<? super E> iDefault;
/**
* Factory method that performs validation and copies the parameter arrays.
- *
+ *
* @param predicates array of predicates, cloned, no nulls
* @param closures matching array of closures, cloned, no nulls
* @param defaultClosure the closure to use if no match, null means nop
@@ -54,85 +53,82 @@
* @throws IllegalArgumentException if array is null
* @throws IllegalArgumentException if any element in the array is null
*/
- public static Closure getInstance(Predicate[] predicates, Closure[] closures, Closure defaultClosure) {
+ @SuppressWarnings("unchecked")
+ public static <E> Closure<E> getInstance(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure) {
FunctorUtils.validate(predicates);
FunctorUtils.validate(closures);
if (predicates.length != closures.length) {
throw new IllegalArgumentException("The predicate and closure arrays must be the same size");
}
if (predicates.length == 0) {
- return (defaultClosure == null ? NOPClosure.INSTANCE : defaultClosure);
+ return (Closure<E>) (defaultClosure == null ? NOPClosure.<E>getInstance(): defaultClosure);
}
predicates = FunctorUtils.copy(predicates);
closures = FunctorUtils.copy(closures);
- return new SwitchClosure(predicates, closures, defaultClosure);
+ return new SwitchClosure<E>(predicates, closures, defaultClosure);
}
/**
- * Create a new Closure that calls one of the closures depending
- * on the predicates.
+ * Create a new Closure that calls one of the closures depending
+ * on the predicates.
* <p>
- * The Map consists of Predicate keys and Closure values. A closure
+ * The Map consists of Predicate keys and Closure values. A closure
* is called if its matching predicate returns true. Each predicate is evaluated
* until one returns true. If no predicates evaluate to true, the default
- * closure is called. The default closure is set in the map with a
- * null key. The ordering is that of the iterator() method on the entryset
+ * closure is called. The default closure is set in the map with a
+ * null key. The ordering is that of the iterator() method on the entryset
* collection of the map.
- *
+ *
* @param predicatesAndClosures a map of predicates to closures
* @return the <code>switch</code> closure
* @throws IllegalArgumentException if the map is null
* @throws IllegalArgumentException if any closure in the map is null
* @throws ClassCastException if the map elements are of the wrong type
*/
- public static Closure getInstance(Map predicatesAndClosures) {
- Closure[] closures = null;
- Predicate[] preds = null;
+ @SuppressWarnings("unchecked")
+ public static <E> Closure<E> getInstance(Map<Predicate<E>, Closure<E>> predicatesAndClosures) {
if (predicatesAndClosures == null) {
throw new IllegalArgumentException("The predicate and closure map must not be null");
}
- if (predicatesAndClosures.size() == 0) {
- return NOPClosure.INSTANCE;
- }
// convert to array like this to guarantee iterator() ordering
- Closure defaultClosure = (Closure) predicatesAndClosures.remove(null);
+ Closure<? super E> defaultClosure = predicatesAndClosures.remove(null);
int size = predicatesAndClosures.size();
if (size == 0) {
- return (defaultClosure == null ? NOPClosure.INSTANCE : defaultClosure);
+ return (Closure<E>) (defaultClosure == null ? NOPClosure.<E>getInstance() : defaultClosure);
}
- closures = new Closure[size];
- preds = new Predicate[size];
+ Closure<E>[] closures = new Closure[size];
+ Predicate<E>[] preds = new Predicate[size];
int i = 0;
- for (Iterator it = predicatesAndClosures.entrySet().iterator(); it.hasNext();) {
- Map.Entry entry = (Map.Entry) it.next();
- preds[i] = (Predicate) entry.getKey();
- closures[i] = (Closure) entry.getValue();
+ for (Map.Entry<Predicate<E>, Closure<E>> entry : predicatesAndClosures.entrySet()) {
+ preds[i] = entry.getKey();
+ closures[i] = entry.getValue();
i++;
}
- return new SwitchClosure(preds, closures, defaultClosure);
+ return new SwitchClosure<E>(preds, closures, defaultClosure);
}
-
+
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
- *
+ *
* @param predicates array of predicates, not cloned, no nulls
* @param closures matching array of closures, not cloned, no nulls
* @param defaultClosure the closure to use if no match, null means nop
*/
- public SwitchClosure(Predicate[] predicates, Closure[] closures, Closure defaultClosure) {
+ @SuppressWarnings("unchecked")
+ public SwitchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure) {
super();
iPredicates = predicates;
iClosures = closures;
- iDefault = (defaultClosure == null ? NOPClosure.INSTANCE : defaultClosure);
+ iDefault = (Closure<? super E>) (defaultClosure == null ? NOPClosure.<E>getInstance() : defaultClosure);
}
/**
* Executes the closure whose matching predicate returns true
- *
+ *
* @param input the input object
*/
- public void execute(Object input) {
+ public void execute(E input) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(input) == true) {
iClosures[i].execute(input);
@@ -144,32 +140,32 @@
/**
* Gets the predicates, do not modify the array.
- *
+ *
* @return the predicates
* @since Commons Collections 3.1
*/
- public Predicate[] getPredicates() {
+ public Predicate<? super E>[] getPredicates() {
return iPredicates;
}
/**
* Gets the closures, do not modify the array.
- *
+ *
* @return the closures
* @since Commons Collections 3.1
*/
- public Closure[] getClosures() {
+ public Closure<? super E>[] getClosures() {
return iClosures;
}
/**
* Gets the default closure.
- *
+ *
* @return the default closure
* @since Commons Collections 3.1
*/
- public Closure getDefaultClosure() {
+ public Closure<? super E> getDefaultClosure() {
return iDefault;
}
-
+
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/SwitchTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/SwitchTransformer.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/SwitchTransformer.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/SwitchTransformer.java Tue Sep 15 05:29:56 2009
@@ -17,7 +17,6 @@
package org.apache.commons.collections.functors;
import java.io.Serializable;
-import java.util.Iterator;
import java.util.Map;
import org.apache.commons.collections.Predicate;
@@ -32,17 +31,17 @@
*
* @author Stephen Colebourne
*/
-public class SwitchTransformer implements Transformer, Serializable {
+public class SwitchTransformer<I, O> implements Transformer<I, O>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -6404460890903469332L;
/** The tests to consider */
- private final Predicate[] iPredicates;
+ private final Predicate<? super I>[] iPredicates;
/** The matching transformers to call */
- private final Transformer[] iTransformers;
+ private final Transformer<? super I, ? extends O>[] iTransformers;
/** The default transformer to call if no tests match */
- private final Transformer iDefault;
+ private final Transformer<? super I, ? extends O> iDefault;
/**
* Factory method that performs validation and copies the parameter arrays.
@@ -54,18 +53,21 @@
* @throws IllegalArgumentException if array is null
* @throws IllegalArgumentException if any element in the array is null
*/
- public static Transformer getInstance(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
+ @SuppressWarnings("unchecked")
+ public static <I, O> Transformer<I, O> getInstance(Predicate<? super I>[] predicates,
+ Transformer<? super I, ? extends O>[] transformers,
+ Transformer<? super I, ? extends O> defaultTransformer) {
FunctorUtils.validate(predicates);
FunctorUtils.validate(transformers);
if (predicates.length != transformers.length) {
throw new IllegalArgumentException("The predicate and transformer arrays must be the same size");
}
if (predicates.length == 0) {
- return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer);
+ return (Transformer<I, O>) (defaultTransformer == null ? ConstantTransformer.<I, O>getNullInstance() : defaultTransformer);
}
predicates = FunctorUtils.copy(predicates);
transformers = FunctorUtils.copy(transformers);
- return new SwitchTransformer(predicates, transformers, defaultTransformer);
+ return new SwitchTransformer<I, O>(predicates, transformers, defaultTransformer);
}
/**
@@ -85,31 +87,30 @@
* @throws IllegalArgumentException if any transformer in the map is null
* @throws ClassCastException if the map elements are of the wrong type
*/
- public static Transformer getInstance(Map predicatesAndTransformers) {
- Transformer[] transformers = null;
- Predicate[] preds = null;
+ @SuppressWarnings("unchecked")
+ public static <I, O> Transformer<I, O> getInstance(
+ Map<? extends Predicate<? super I>, ? extends Transformer<? super I, ? extends O>> predicatesAndTransformers) {
if (predicatesAndTransformers == null) {
throw new IllegalArgumentException("The predicate and transformer map must not be null");
}
if (predicatesAndTransformers.size() == 0) {
- return ConstantTransformer.NULL_INSTANCE;
+ return ConstantTransformer.<I, O>getNullInstance();
}
// convert to array like this to guarantee iterator() ordering
- Transformer defaultTransformer = (Transformer) predicatesAndTransformers.remove(null);
+ Transformer<? super I, ? extends O> defaultTransformer = predicatesAndTransformers.remove(null);
int size = predicatesAndTransformers.size();
if (size == 0) {
- return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer);
+ return (Transformer<I, O>) (defaultTransformer == null ? ConstantTransformer.<I, O>getNullInstance() : defaultTransformer);
}
- transformers = new Transformer[size];
- preds = new Predicate[size];
+ Transformer<? super I, ? extends O>[] transformers = new Transformer[size];
+ Predicate<? super I>[] preds = new Predicate[size];
int i = 0;
- for (Iterator it = predicatesAndTransformers.entrySet().iterator(); it.hasNext();) {
- Map.Entry entry = (Map.Entry) it.next();
- preds[i] = (Predicate) entry.getKey();
- transformers[i] = (Transformer) entry.getValue();
+ for (Map.Entry<? extends Predicate<? super I>, ? extends Transformer<? super I, ? extends O>> entry : predicatesAndTransformers.entrySet()) {
+ preds[i] = entry.getKey();
+ transformers[i] = entry.getValue();
i++;
}
- return new SwitchTransformer(preds, transformers, defaultTransformer);
+ return new SwitchTransformer<I, O>(preds, transformers, defaultTransformer);
}
/**
@@ -120,11 +121,14 @@
* @param transformers matching array of transformers, not cloned, no nulls
* @param defaultTransformer the transformer to use if no match, null means return null
*/
- public SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
+ @SuppressWarnings("unchecked")
+ public SwitchTransformer(Predicate<? super I>[] predicates,
+ Transformer<? super I, ? extends O>[] transformers,
+ Transformer<? super I, ? extends O> defaultTransformer) {
super();
iPredicates = predicates;
iTransformers = transformers;
- iDefault = (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer);
+ iDefault = (Transformer<? super I, ? extends O>) (defaultTransformer == null ? ConstantTransformer.<I, O>getNullInstance() : defaultTransformer);
}
/**
@@ -134,7 +138,7 @@
* @param input the input object to transform
* @return the transformed result
*/
- public Object transform(Object input) {
+ public O transform(I input) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(input) == true) {
return iTransformers[i].transform(input);
@@ -149,7 +153,7 @@
* @return the predicates
* @since Commons Collections 3.1
*/
- public Predicate[] getPredicates() {
+ public Predicate<? super I>[] getPredicates() {
return iPredicates;
}
@@ -159,7 +163,7 @@
* @return the transformers
* @since Commons Collections 3.1
*/
- public Transformer[] getTransformers() {
+ public Transformer<? super I, ? extends O>[] getTransformers() {
return iTransformers;
}
@@ -169,7 +173,7 @@
* @return the default transformer
* @since Commons Collections 3.1
*/
- public Transformer getDefaultTransformer() {
+ public Transformer<? super I, ? extends O> getDefaultTransformer() {
return iDefault;
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformedPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformedPredicate.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformedPredicate.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformedPredicate.java Tue Sep 15 05:29:56 2009
@@ -30,74 +30,76 @@
* @author Alban Peignier
* @author Stephen Colebourne
*/
-public final class TransformedPredicate implements Predicate, PredicateDecorator, Serializable {
+public final class TransformedPredicate<T> implements Predicate<T>, PredicateDecorator<T>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -5596090919668315834L;
-
+
/** The transformer to call */
- private final Transformer iTransformer;
+ private final Transformer<? super T, ? extends T> iTransformer;
+
/** The predicate to call */
- private final Predicate iPredicate;
+ private final Predicate<? super T> iPredicate;
/**
* Factory to create the predicate.
- *
+ *
* @param transformer the transformer to call
* @param predicate the predicate to call with the result of the transform
* @return the predicate
* @throws IllegalArgumentException if the transformer or the predicate is null
*/
- public static Predicate getInstance(Transformer transformer, Predicate predicate) {
+ public static <T> Predicate<T> getInstance(Transformer<? super T, ? extends T> transformer, Predicate<? super T> predicate) {
if (transformer == null) {
throw new IllegalArgumentException("The transformer to call must not be null");
}
if (predicate == null) {
throw new IllegalArgumentException("The predicate to call must not be null");
}
- return new TransformedPredicate(transformer, predicate);
+ return new TransformedPredicate<T>(transformer, predicate);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
- *
+ *
* @param transformer the transformer to use
* @param predicate the predicate to decorate
*/
- public TransformedPredicate(Transformer transformer, Predicate predicate) {
+ public TransformedPredicate(Transformer<? super T, ? extends T> transformer, Predicate<? super T> predicate) {
iTransformer = transformer;
iPredicate = predicate;
}
-
+
/**
* Evaluates the predicate returning the result of the decorated predicate
* once the input has been transformed
- *
+ *
* @param object the input object which will be transformed
* @return true if decorated predicate returns true
*/
- public boolean evaluate(Object object) {
- Object result = iTransformer.transform(object);
+ public boolean evaluate(T object) {
+ T result = iTransformer.transform(object);
return iPredicate.evaluate(result);
}
/**
* Gets the predicate being decorated.
- *
+ *
* @return the predicate as the only element in an array
* @since Commons Collections 3.1
*/
- public Predicate[] getPredicates() {
+ @SuppressWarnings("unchecked")
+ public Predicate<? super T>[] getPredicates() {
return new Predicate[] {iPredicate};
}
/**
* Gets the transformer in use.
- *
+ *
* @return the transformer
*/
- public Transformer getTransformer() {
+ public Transformer<? super T, ? extends T> getTransformer() {
return iTransformer;
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformerClosure.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformerClosure.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformerClosure.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformerClosure.java Tue Sep 15 05:29:56 2009
@@ -30,13 +30,13 @@
*
* @author Stephen Colebourne
*/
-public class TransformerClosure implements Closure, Serializable {
+public class TransformerClosure<E> implements Closure<E>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -5194992589193388969L;
/** The transformer to wrap */
- private final Transformer iTransformer;
+ private final Transformer<? super E, ?> iTransformer;
/**
* Factory method that performs validation.
@@ -46,11 +46,11 @@
* @param transformer the transformer to call, null means nop
* @return the <code>transformer</code> closure
*/
- public static Closure getInstance(Transformer transformer) {
+ public static <E> Closure<E> getInstance(Transformer<? super E, ?> transformer) {
if (transformer == null) {
- return NOPClosure.INSTANCE;
+ return NOPClosure.<E>getInstance();
}
- return new TransformerClosure(transformer);
+ return new TransformerClosure<E>(transformer);
}
/**
@@ -59,7 +59,7 @@
*
* @param transformer the transformer to call, not null
*/
- public TransformerClosure(Transformer transformer) {
+ public TransformerClosure(Transformer<? super E, ?> transformer) {
super();
iTransformer = transformer;
}
@@ -69,7 +69,7 @@
*
* @param input the input object
*/
- public void execute(Object input) {
+ public void execute(E input) {
iTransformer.transform(input);
}
@@ -79,7 +79,7 @@
* @return the transformer
* @since Commons Collections 3.1
*/
- public Transformer getTransformer() {
+ public Transformer<? super E, ?> getTransformer() {
return iTransformer;
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformerPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformerPredicate.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformerPredicate.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/TransformerPredicate.java Tue Sep 15 05:29:56 2009
@@ -30,63 +30,62 @@
*
* @author Stephen Colebourne
*/
-public final class TransformerPredicate implements Predicate, Serializable {
+public final class TransformerPredicate<T> implements Predicate<T>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -2407966402920578741L;
-
+
/** The transformer to call */
- private final Transformer iTransformer;
-
+ private final Transformer<? super T, Boolean> iTransformer;
+
/**
* Factory to create the predicate.
- *
+ *
* @param transformer the transformer to decorate
* @return the predicate
* @throws IllegalArgumentException if the transformer is null
*/
- public static Predicate getInstance(Transformer transformer) {
+ public static <T> Predicate<T> getInstance(Transformer<? super T, Boolean> transformer) {
if (transformer == null) {
throw new IllegalArgumentException("The transformer to call must not be null");
}
- return new TransformerPredicate(transformer);
+ return new TransformerPredicate<T>(transformer);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
- *
+ *
* @param transformer the transformer to decorate
*/
- public TransformerPredicate(Transformer transformer) {
+ public TransformerPredicate(Transformer<? super T, Boolean> transformer) {
super();
iTransformer = transformer;
}
/**
* Evaluates the predicate returning the result of the decorated transformer.
- *
+ *
* @param object the input object
* @return true if decorated transformer returns Boolean.TRUE
* @throws FunctorException if the transformer returns an invalid type
*/
- public boolean evaluate(Object object) {
- Object result = iTransformer.transform(object);
- if (result instanceof Boolean == false) {
+ public boolean evaluate(T object) {
+ Boolean result = iTransformer.transform(object);
+ if (result == null) {
throw new FunctorException(
- "Transformer must return an instanceof Boolean, it was a "
- + (result == null ? "null object" : result.getClass().getName()));
+ "Transformer must return an instanceof Boolean, it was a null object");
}
- return ((Boolean) result).booleanValue();
+ return result;
}
/**
* Gets the transformer.
- *
+ *
* @return the transformer
* @since Commons Collections 3.1
*/
- public Transformer getTransformer() {
+ public Transformer<? super T, Boolean> getTransformer() {
return iTransformer;
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/UniquePredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/UniquePredicate.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/UniquePredicate.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/UniquePredicate.java Tue Sep 15 05:29:56 2009
@@ -31,22 +31,22 @@
*
* @author Stephen Colebourne
*/
-public final class UniquePredicate implements Predicate, Serializable {
+public final class UniquePredicate<T> implements Predicate<T>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -3319417438027438040L;
-
+
/** The set of previously seen objects */
- private final Set iSet = new HashSet();
-
+ private final Set<T> iSet = new HashSet<T>();
+
/**
* Factory to create the predicate.
- *
+ *
* @return the predicate
* @throws IllegalArgumentException if the predicate is null
*/
- public static Predicate getInstance() {
- return new UniquePredicate();
+ public static <E> Predicate<E> getInstance() {
+ return new UniquePredicate<E>();
}
/**
@@ -60,11 +60,11 @@
/**
* Evaluates the predicate returning true if the input object hasn't been
* received yet.
- *
+ *
* @param object the input object
* @return true if this is the first time the object is seen
*/
- public boolean evaluate(Object object) {
+ public boolean evaluate(T object) {
return iSet.add(object);
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/WhileClosure.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/WhileClosure.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/WhileClosure.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/WhileClosure.java Tue Sep 15 05:29:56 2009
@@ -30,15 +30,15 @@
*
* @author Stephen Colebourne
*/
-public class WhileClosure implements Closure, Serializable {
+public class WhileClosure<E> implements Closure<E>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -3110538116913760108L;
/** The test condition */
- private final Predicate iPredicate;
+ private final Predicate<? super E> iPredicate;
/** The closure to call */
- private final Closure iClosure;
+ private final Closure<? super E> iClosure;
/** The flag, true is a do loop, false is a while */
private final boolean iDoLoop;
@@ -51,14 +51,14 @@
* @return the <code>while</code> closure
* @throws IllegalArgumentException if the predicate or closure is null
*/
- public static Closure getInstance(Predicate predicate, Closure closure, boolean doLoop) {
+ public static <E> Closure<E> getInstance(Predicate<? super E> predicate, Closure<? super E> closure, boolean doLoop) {
if (predicate == null) {
throw new IllegalArgumentException("Predicate must not be null");
}
if (closure == null) {
throw new IllegalArgumentException("Closure must not be null");
}
- return new WhileClosure(predicate, closure, doLoop);
+ return new WhileClosure<E>(predicate, closure, doLoop);
}
/**
@@ -69,7 +69,7 @@
* @param closure the closure the execute, not null
* @param doLoop true to act as a do-while loop, always executing the closure once
*/
- public WhileClosure(Predicate predicate, Closure closure, boolean doLoop) {
+ public WhileClosure(Predicate<? super E> predicate, Closure<? super E> closure, boolean doLoop) {
super();
iPredicate = predicate;
iClosure = closure;
@@ -81,7 +81,7 @@
*
* @param input the input object
*/
- public void execute(Object input) {
+ public void execute(E input) {
if (iDoLoop) {
iClosure.execute(input);
}
@@ -96,7 +96,7 @@
* @return the predicate
* @since Commons Collections 3.1
*/
- public Predicate getPredicate() {
+ public Predicate<? super E> getPredicate() {
return iPredicate;
}
@@ -106,7 +106,7 @@
* @return the closure
* @since Commons Collections 3.1
*/
- public Closure getClosure() {
+ public Closure<? super E> getClosure() {
return iClosure;
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractEmptyIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractEmptyIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractEmptyIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractEmptyIterator.java Tue Sep 15 05:29:56 2009
@@ -26,7 +26,7 @@
*
* @author Stephen Colebourne
*/
-abstract class AbstractEmptyIterator {
+abstract class AbstractEmptyIterator<E> {
/**
* Constructor.
@@ -39,7 +39,7 @@
return false;
}
- public Object next() {
+ public E next() {
throw new NoSuchElementException("Iterator contains no elements");
}
@@ -47,7 +47,7 @@
return false;
}
- public Object previous() {
+ public E previous() {
throw new NoSuchElementException("Iterator contains no elements");
}
@@ -59,11 +59,11 @@
return -1;
}
- public void add(Object obj) {
+ public void add(E obj) {
throw new UnsupportedOperationException("add() not supported for empty Iterator");
}
- public void set(Object obj) {
+ public void set(E obj) {
throw new IllegalStateException("Iterator contains no elements");
}
@@ -71,18 +71,6 @@
throw new IllegalStateException("Iterator contains no elements");
}
- public Object getKey() {
- throw new IllegalStateException("Iterator contains no elements");
- }
-
- public Object getValue() {
- throw new IllegalStateException("Iterator contains no elements");
- }
-
- public Object setValue(Object value) {
- throw new IllegalStateException("Iterator contains no elements");
- }
-
public void reset() {
// do nothing
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractIteratorDecorator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractIteratorDecorator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractIteratorDecorator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractIteratorDecorator.java Tue Sep 15 05:29:56 2009
@@ -18,7 +18,7 @@
import java.util.Iterator;
-/**
+/**
* Provides basic behaviour for decorating an iterator with extra functionality.
* <p>
* All methods are forwarded to the decorated iterator.
@@ -29,10 +29,7 @@
* @author James Strachan
* @author Stephen Colebourne
*/
-public class AbstractIteratorDecorator implements Iterator {
-
- /** The iterator being decorated */
- protected final Iterator iterator;
+public abstract class AbstractIteratorDecorator<E> extends AbstractUntypedIteratorDecorator<E, E> {
//-----------------------------------------------------------------------
/**
@@ -41,34 +38,12 @@
* @param iterator the iterator to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
- public AbstractIteratorDecorator(Iterator iterator) {
- super();
- if (iterator == null) {
- throw new IllegalArgumentException("Iterator must not be null");
- }
- this.iterator = iterator;
- }
-
- /**
- * Gets the iterator being decorated.
- *
- * @return the decorated iterator
- */
- protected Iterator getIterator() {
- return iterator;
- }
-
- //-----------------------------------------------------------------------
- public boolean hasNext() {
- return iterator.hasNext();
- }
-
- public Object next() {
- return iterator.next();
+ protected AbstractIteratorDecorator(Iterator<E> iterator) {
+ super(iterator);
}
- public void remove() {
- iterator.remove();
+ public E next() {
+ return getIterator().next();
}
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractMapIteratorDecorator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractMapIteratorDecorator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractMapIteratorDecorator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractMapIteratorDecorator.java Tue Sep 15 05:29:56 2009
@@ -28,10 +28,10 @@
*
* @author Stephen Colebourne
*/
-public class AbstractMapIteratorDecorator implements MapIterator {
+public class AbstractMapIteratorDecorator<K, V> implements MapIterator<K, V> {
/** The iterator being decorated */
- protected final MapIterator iterator;
+ protected final MapIterator<K, V> iterator;
//-----------------------------------------------------------------------
/**
@@ -40,7 +40,7 @@
* @param iterator the iterator to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
- public AbstractMapIteratorDecorator(MapIterator iterator) {
+ public AbstractMapIteratorDecorator(MapIterator<K, V> iterator) {
super();
if (iterator == null) {
throw new IllegalArgumentException("MapIterator must not be null");
@@ -53,7 +53,7 @@
*
* @return the decorated iterator
*/
- protected MapIterator getMapIterator() {
+ protected MapIterator<K, V> getMapIterator() {
return iterator;
}
@@ -62,7 +62,7 @@
return iterator.hasNext();
}
- public Object next() {
+ public K next() {
return iterator.next();
}
@@ -70,15 +70,15 @@
iterator.remove();
}
- public Object getKey() {
+ public K getKey() {
return iterator.getKey();
}
- public Object getValue() {
+ public V getValue() {
return iterator.getValue();
}
- public Object setValue(Object obj) {
+ public V setValue(V obj) {
return iterator.setValue(obj);
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractOrderedMapIteratorDecorator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractOrderedMapIteratorDecorator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractOrderedMapIteratorDecorator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/AbstractOrderedMapIteratorDecorator.java Tue Sep 15 05:29:56 2009
@@ -28,10 +28,10 @@
*
* @author Stephen Colebourne
*/
-public class AbstractOrderedMapIteratorDecorator implements OrderedMapIterator {
+public class AbstractOrderedMapIteratorDecorator<K, V> implements OrderedMapIterator<K, V> {
/** The iterator being decorated */
- protected final OrderedMapIterator iterator;
+ protected final OrderedMapIterator<K, V> iterator;
//-----------------------------------------------------------------------
/**
@@ -40,7 +40,7 @@
* @param iterator the iterator to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
- public AbstractOrderedMapIteratorDecorator(OrderedMapIterator iterator) {
+ public AbstractOrderedMapIteratorDecorator(OrderedMapIterator<K, V> iterator) {
super();
if (iterator == null) {
throw new IllegalArgumentException("OrderedMapIterator must not be null");
@@ -53,7 +53,7 @@
*
* @return the decorated iterator
*/
- protected OrderedMapIterator getOrderedMapIterator() {
+ protected OrderedMapIterator<K, V> getOrderedMapIterator() {
return iterator;
}
@@ -62,7 +62,7 @@
return iterator.hasNext();
}
- public Object next() {
+ public K next() {
return iterator.next();
}
@@ -70,7 +70,7 @@
return iterator.hasPrevious();
}
- public Object previous() {
+ public K previous() {
return iterator.previous();
}
@@ -78,15 +78,15 @@
iterator.remove();
}
- public Object getKey() {
+ public K getKey() {
return iterator.getKey();
}
- public Object getValue() {
+ public V getValue() {
return iterator.getValue();
}
- public Object setValue(Object obj) {
+ public V setValue(V obj) {
return iterator.setValue(obj);
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/CollatingIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/CollatingIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/CollatingIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/CollatingIterator.java Tue Sep 15 05:29:56 2009
@@ -27,12 +27,12 @@
import org.apache.commons.collections.list.UnmodifiableList;
/**
- * Provides an ordered iteration over the elements contained in
- * a collection of ordered Iterators.
+ * Provides an ordered iteration over the elements contained in a collection of
+ * ordered Iterators.
* <p>
- * Given two ordered {@link Iterator} instances <code>A</code> and <code>B</code>,
- * the {@link #next} method on this iterator will return the lesser of
- * <code>A.next()</code> and <code>B.next()</code>.
+ * Given two ordered {@link Iterator} instances <code>A</code> and
+ * <code>B</code>, the {@link #next} method on this iterator will return the
+ * lesser of <code>A.next()</code> and <code>B.next()</code>.
*
* @since Commons Collections 2.1
* @version $Revision$ $Date$
@@ -40,86 +40,93 @@
* @author Rodney Waldhoff
* @author Stephen Colebourne
*/
-public class CollatingIterator implements Iterator {
+public class CollatingIterator<E> implements Iterator<E> {
/** The {@link Comparator} used to evaluate order. */
- private Comparator comparator = null;
+ private Comparator<? super E> comparator = null;
/** The list of {@link Iterator}s to evaluate. */
- private ArrayList iterators = null;
-
+ private ArrayList<Iterator<? extends E>> iterators = null;
+
/** {@link Iterator#next Next} objects peeked from each iterator. */
- private ArrayList values = null;
-
+ private ArrayList<E> values = null;
+
/** Whether or not each {@link #values} element has been set. */
private BitSet valueSet = null;
- /** Index of the {@link #iterators iterator} from whom the last returned value was obtained. */
+ /**
+ * Index of the {@link #iterators iterator} from whom the last returned
+ * value was obtained.
+ */
private int lastReturned = -1;
// Constructors
// ----------------------------------------------------------------------
/**
- * Constructs a new <code>CollatingIterator</code>. Natural sort order
- * will be used, and child iterators will have to be manually added
- * using the {@link #addIterator(Iterator)} method.
+ * Constructs a new <code>CollatingIterator</code>. Natural sort order will
+ * be used, and child iterators will have to be manually added using the
+ * {@link #addIterator(Iterator)} method.
*/
public CollatingIterator() {
- this(null,2);
+ this(null, 2);
}
-
+
/**
* Constructs a new <code>CollatingIterator</code> that will used the
- * specified comparator for ordering. Child iterators will have to be
+ * specified comparator for ordering. Child iterators will have to be
* manually added using the {@link #addIterator(Iterator)} method.
- *
- * @param comp the comparator to use to sort, or null to use natural sort order
+ *
+ * @param comp the comparator to use to sort, or null to use natural sort
+ * order
*/
- public CollatingIterator(final Comparator comp) {
- this(comp,2);
+ public CollatingIterator(final Comparator<? super E> comp) {
+ this(comp, 2);
}
-
+
/**
* Constructs a new <code>CollatingIterator</code> that will used the
* specified comparator for ordering and have the specified initial
- * capacity. Child iterators will have to be
- * manually added using the {@link #addIterator(Iterator)} method.
- *
- * @param comp the comparator to use to sort, or null to use natural sort order
- * @param initIterCapacity the initial capacity for the internal list
- * of child iterators
+ * capacity. Child iterators will have to be manually added using the
+ * {@link #addIterator(Iterator)} method.
+ *
+ * @param comp the comparator to use to sort, or null to use natural sort
+ * order
+ * @param initIterCapacity the initial capacity for the internal list of
+ * child iterators
*/
- public CollatingIterator(final Comparator comp, final int initIterCapacity) {
- iterators = new ArrayList(initIterCapacity);
+ public CollatingIterator(final Comparator<? super E> comp, final int initIterCapacity) {
+ iterators = new ArrayList<Iterator<? extends E>>(initIterCapacity);
setComparator(comp);
}
/**
* Constructs a new <code>CollatingIterator</code> that will use the
- * specified comparator to provide ordered iteration over the two
- * given iterators.
- *
- * @param comp the comparator to use to sort, or null to use natural sort order
- * @param a the first child ordered iterator
- * @param b the second child ordered iterator
+ * specified comparator to provide ordered iteration over the two given
+ * iterators.
+ *
+ * @param comp the comparator to use to sort, or null to use natural sort
+ * order
+ * @param a the first child ordered iterator
+ * @param b the second child ordered iterator
* @throws NullPointerException if either iterator is null
*/
- public CollatingIterator(final Comparator comp, final Iterator a, final Iterator b) {
- this(comp,2);
+ public CollatingIterator(final Comparator<? super E> comp, final Iterator<? extends E> a, final Iterator<? extends E> b) {
+ this(comp, 2);
addIterator(a);
addIterator(b);
}
/**
* Constructs a new <code>CollatingIterator</code> that will use the
- * specified comparator to provide ordered iteration over the array
- * of iterators.
- *
- * @param comp the comparator to use to sort, or null to use natural sort order
- * @param iterators the array of iterators
+ * specified comparator to provide ordered iteration over the array of
+ * iterators.
+ *
+ * @param comp the comparator to use to sort, or null to use natural sort
+ * order
+ * @param iterators the array of iterators
* @throws NullPointerException if iterators array is or contains null
*/
- public CollatingIterator(final Comparator comp, final Iterator[] iterators) {
+ public CollatingIterator(final Comparator<? super E> comp, final Iterator<? extends E>[] iterators) {
this(comp, iterators.length);
for (int i = 0; i < iterators.length; i++) {
addIterator(iterators[i]);
@@ -128,20 +135,21 @@
/**
* Constructs a new <code>CollatingIterator</code> that will use the
- * specified comparator to provide ordered iteration over the collection
- * of iterators.
- *
- * @param comp the comparator to use to sort, or null to use natural sort order
- * @param iterators the collection of iterators
- * @throws NullPointerException if the iterators collection is or contains null
+ * specified comparator to provide ordered iteration over the collection of
+ * iterators.
+ *
+ * @param comp the comparator to use to sort, or null to use natural sort
+ * order
+ * @param iterators the collection of iterators
+ * @throws NullPointerException if the iterators collection is or contains
+ * null
* @throws ClassCastException if the iterators collection contains an
- * element that's not an {@link Iterator}
+ * element that's not an {@link Iterator}
*/
- public CollatingIterator(final Comparator comp, final Collection iterators) {
+ public CollatingIterator(final Comparator<? super E> comp, final Collection<Iterator<? extends E>> iterators) {
this(comp, iterators.size());
- for (Iterator it = iterators.iterator(); it.hasNext();) {
- Iterator item = (Iterator) it.next();
- addIterator(item);
+ for (Iterator<? extends E> iterator : iterators) {
+ addIterator(iterator);
}
}
@@ -150,11 +158,11 @@
/**
* Adds the given {@link Iterator} to the iterators being collated.
*
- * @param iterator the iterator to add to the collation, must not be null
+ * @param iterator the iterator to add to the collation, must not be null
* @throws IllegalStateException if iteration has started
* @throws NullPointerException if the iterator is null
*/
- public void addIterator(final Iterator iterator) {
+ public void addIterator(final Iterator<? extends E> iterator) {
checkNotStarted();
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
@@ -165,13 +173,13 @@
/**
* Sets the iterator at the given index.
*
- * @param index index of the Iterator to replace
- * @param iterator Iterator to place at the given index
+ * @param index index of the Iterator to replace
+ * @param iterator Iterator to place at the given index
* @throws IndexOutOfBoundsException if index < 0 or index > size()
* @throws IllegalStateException if iteration has started
* @throws NullPointerException if the iterator is null
*/
- public void setIterator(final int index, final Iterator iterator) {
+ public void setIterator(final int index, final Iterator<? extends E> iterator) {
checkNotStarted();
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
@@ -184,14 +192,14 @@
*
* @return the unmodifiable list of iterators added
*/
- public List getIterators() {
+ public List<Iterator<? extends E>> getIterators() {
return UnmodifiableList.decorate(iterators);
}
/**
* Gets the {@link Comparator} by which collatation occurs.
*/
- public Comparator getComparator() {
+ public Comparator<? super E> getComparator() {
return comparator;
}
@@ -200,7 +208,7 @@
*
* @throws IllegalStateException if iteration has started
*/
- public void setComparator(final Comparator comp) {
+ public void setComparator(final Comparator<? super E> comp) {
checkNotStarted();
comparator = comp;
}
@@ -209,7 +217,7 @@
// -------------------------------------------------------------------
/**
* Returns <code>true</code> if any child iterator has remaining elements.
- *
+ *
* @return true if this iterator has remaining elements
*/
public boolean hasNext() {
@@ -219,38 +227,36 @@
/**
* Returns the next ordered element from a child iterator.
- *
+ *
* @return the next ordered element
* @throws NoSuchElementException if no child iterator has any more elements
*/
- public Object next() throws NoSuchElementException {
+ public E next() throws NoSuchElementException {
if (hasNext() == false) {
throw new NoSuchElementException();
}
int leastIndex = least();
if (leastIndex == -1) {
throw new NoSuchElementException();
- } else {
- Object val = values.get(leastIndex);
- clear(leastIndex);
- lastReturned = leastIndex;
- return val;
}
+ E val = values.get(leastIndex);
+ clear(leastIndex);
+ lastReturned = leastIndex;
+ return val;
}
/**
- * Removes the last returned element from the child iterator that
- * produced it.
- *
- * @throws IllegalStateException if there is no last returned element,
- * or if the last returned element has already been removed
+ * Removes the last returned element from the child iterator that produced
+ * it.
+ *
+ * @throws IllegalStateException if there is no last returned element, or if
+ * the last returned element has already been removed
*/
public void remove() {
if (lastReturned == -1) {
throw new IllegalStateException("No value can be removed at present");
}
- Iterator it = (Iterator) (iterators.get(lastReturned));
- it.remove();
+ iterators.get(lastReturned).remove();
}
/**
@@ -269,12 +275,12 @@
// Private Methods
// -------------------------------------------------------------------
- /**
+ /**
* Initializes the collating state if it hasn't been already.
*/
private void start() {
if (values == null) {
- values = new ArrayList(iterators.size());
+ values = new ArrayList<E>(iterators.size());
valueSet = new BitSet(iterators.size());
for (int i = 0; i < iterators.size(); i++) {
values.add(null);
@@ -283,40 +289,38 @@
}
}
- /**
- * Sets the {@link #values} and {@link #valueSet} attributes
- * at position <i>i</i> to the next value of the
- * {@link #iterators iterator} at position <i>i</i>, or
- * clear them if the <i>i</i><sup>th</sup> iterator
- * has no next value.
- *
+ /**
+ * Sets the {@link #values} and {@link #valueSet} attributes at position
+ * <i>i</i> to the next value of the {@link #iterators iterator} at position
+ * <i>i</i>, or clear them if the <i>i</i><sup>th</sup> iterator has no next
+ * value.
+ *
* @return <tt>false</tt> iff there was no value to set
*/
private boolean set(int i) {
- Iterator it = (Iterator)(iterators.get(i));
+ Iterator<? extends E> it = iterators.get(i);
if (it.hasNext()) {
values.set(i, it.next());
valueSet.set(i);
return true;
- } else {
- values.set(i,null);
- valueSet.clear(i);
- return false;
}
+ values.set(i, null);
+ valueSet.clear(i);
+ return false;
}
- /**
- * Clears the {@link #values} and {@link #valueSet} attributes
- * at position <i>i</i>.
+ /**
+ * Clears the {@link #values} and {@link #valueSet} attributes at position
+ * <i>i</i>.
*/
private void clear(int i) {
- values.set(i,null);
+ values.set(i, null);
valueSet.clear(i);
}
- /**
- * Throws {@link IllegalStateException} if iteration has started
- * via {@link #start}.
+ /**
+ * Throws {@link IllegalStateException} if iteration has started via
+ * {@link #start}.
*
* @throws IllegalStateException if iteration started
*/
@@ -326,7 +330,7 @@
}
}
- /**
+ /**
* Returns the index of the least element in {@link #values},
* {@link #set(int) setting} any uninitialized values.
*
@@ -334,7 +338,7 @@
*/
private int least() {
int leastIndex = -1;
- Object leastObject = null;
+ E leastObject = null;
for (int i = 0; i < values.size(); i++) {
if (valueSet.get(i) == false) {
set(i);
@@ -344,8 +348,8 @@
leastIndex = i;
leastObject = values.get(i);
} else {
- Object curObject = values.get(i);
- if (comparator.compare(curObject,leastObject) < 0) {
+ E curObject = values.get(i);
+ if (comparator.compare(curObject, leastObject) < 0) {
leastObject = curObject;
leastIndex = i;
}
@@ -356,7 +360,7 @@
}
/**
- * Returns <code>true</code> iff any bit in the given set is
+ * Returns <code>true</code> iff any bit in the given set is
* <code>true</code>.
*/
private boolean anyValueSet(BitSet set) {
@@ -369,13 +373,12 @@
}
/**
- * Returns <code>true</code> iff any {@link Iterator}
- * in the given list has a next value.
+ * Returns <code>true</code> iff any {@link Iterator} in the given list has
+ * a next value.
*/
- private boolean anyHasNext(ArrayList iters) {
- for (int i = 0; i < iters.size(); i++) {
- Iterator it = (Iterator) iters.get(i);
- if (it.hasNext()) {
+ private boolean anyHasNext(ArrayList<Iterator<? extends E>> iters) {
+ for (Iterator<? extends E> iterator : iters) {
+ if (iterator.hasNext()) {
return true;
}
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyIterator.java Tue Sep 15 05:29:56 2009
@@ -32,18 +32,39 @@
*
* @author Stephen Colebourne
*/
-public class EmptyIterator extends AbstractEmptyIterator implements ResettableIterator {
+public class EmptyIterator<E> extends AbstractEmptyIterator<E> implements ResettableIterator<E> {
/**
* Singleton instance of the iterator.
* @since Commons Collections 3.1
*/
- public static final ResettableIterator RESETTABLE_INSTANCE = new EmptyIterator();
+ public static final ResettableIterator<Object> RESETTABLE_INSTANCE = new EmptyIterator<Object>();
+
/**
* Singleton instance of the iterator.
* @since Commons Collections 2.1.1 and 3.1
*/
- public static final Iterator INSTANCE = RESETTABLE_INSTANCE;
+ public static final Iterator<Object> INSTANCE = RESETTABLE_INSTANCE;
+
+ /**
+ * Get a typed resettable empty iterator instance.
+ * @param <E>
+ * @return ResettableIterator<E>
+ */
+ @SuppressWarnings("unchecked")
+ public static <E> ResettableIterator<E> getResettableInstance() {
+ return (ResettableIterator<E>) RESETTABLE_INSTANCE;
+ }
+
+ /**
+ * Get a typed empty iterator instance.
+ * @param <E>
+ * @return Iterator<E>
+ */
+ @SuppressWarnings("unchecked")
+ public static <E> Iterator<E> getInstance() {
+ return (Iterator<E>) INSTANCE;
+ }
/**
* Constructor.
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyListIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyListIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyListIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyListIterator.java Tue Sep 15 05:29:56 2009
@@ -20,30 +20,52 @@
import org.apache.commons.collections.ResettableListIterator;
-/**
+/**
* Provides an implementation of an empty list iterator.
* <p>
- * This class provides an implementation of an empty list iterator.
- * This class provides for binary compatability between Commons Collections
- * 2.1.1 and 3.1 due to issues with <code>IteratorUtils</code>.
+ * This class provides an implementation of an empty list iterator. This class
+ * provides for binary compatability between Commons Collections 2.1.1 and 3.1
+ * due to issues with <code>IteratorUtils</code>.
*
* @since Commons Collections 2.1.1 and 3.1
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
*/
-public class EmptyListIterator extends AbstractEmptyIterator implements ResettableListIterator {
+public class EmptyListIterator<E> extends AbstractEmptyIterator<E> implements
+ ResettableListIterator<E> {
/**
* Singleton instance of the iterator.
* @since Commons Collections 3.1
*/
- public static final ResettableListIterator RESETTABLE_INSTANCE = new EmptyListIterator();
+ public static final ResettableListIterator<Object> RESETTABLE_INSTANCE = new EmptyListIterator<Object>();
+
/**
* Singleton instance of the iterator.
* @since Commons Collections 2.1.1 and 3.1
*/
- public static final ListIterator INSTANCE = RESETTABLE_INSTANCE;
+ public static final ListIterator<Object> INSTANCE = RESETTABLE_INSTANCE;
+
+ /**
+ * Get a typed instance of the iterator.
+ * @param <E>
+ * @return {@link ResettableListIterator}<E>
+ */
+ @SuppressWarnings("unchecked")
+ public static <E> ResettableListIterator<E> getResettableInstance() {
+ return (ResettableListIterator<E>) RESETTABLE_INSTANCE;
+ }
+
+ /**
+ * Get a typed instance of the iterator.
+ * @param <E>
+ * @return {@link ListIterator}<E>
+ */
+ @SuppressWarnings("unchecked")
+ public static <E> ListIterator<E> getInstance() {
+ return (ListIterator<E>) INSTANCE;
+ }
/**
* Constructor.
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyMapIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyMapIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyMapIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyMapIterator.java Tue Sep 15 05:29:56 2009
@@ -27,13 +27,25 @@
*
* @author Stephen Colebourne
*/
-public class EmptyMapIterator extends AbstractEmptyIterator implements MapIterator, ResettableIterator {
+public class EmptyMapIterator<K, V> extends AbstractEmptyMapIterator<K, V> implements
+ MapIterator<K, V>, ResettableIterator<K> {
/**
* Singleton instance of the iterator.
* @since Commons Collections 3.1
*/
- public static final MapIterator INSTANCE = new EmptyMapIterator();
+ public static final MapIterator<Object, Object> INSTANCE = new EmptyMapIterator<Object, Object>();
+
+ /**
+ * Get a typed instance of the iterator.
+ * @param <K>
+ * @param <V>
+ * @return {@link MapIterator}<K, V>
+ */
+ @SuppressWarnings("unchecked")
+ public static <K, V> MapIterator<K, V> getInstance() {
+ return (MapIterator<K, V>) INSTANCE;
+ }
/**
* Constructor.
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyOrderedIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyOrderedIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyOrderedIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyOrderedIterator.java Tue Sep 15 05:29:56 2009
@@ -27,13 +27,23 @@
*
* @author Stephen Colebourne
*/
-public class EmptyOrderedIterator extends AbstractEmptyIterator implements OrderedIterator, ResettableIterator {
+public class EmptyOrderedIterator<E> extends AbstractEmptyIterator<E> implements OrderedIterator<E>, ResettableIterator<E> {
/**
* Singleton instance of the iterator.
* @since Commons Collections 3.1
*/
- public static final OrderedIterator INSTANCE = new EmptyOrderedIterator();
+ public static final OrderedIterator<Object> INSTANCE = new EmptyOrderedIterator<Object>();
+
+ /**
+ * Typed instance of the iterator.
+ * @param <E>
+ * @return OrderedIterator<E>
+ */
+ @SuppressWarnings("unchecked")
+ public static <E> OrderedIterator<E> getInstance() {
+ return (OrderedIterator<E>) INSTANCE;
+ }
/**
* Constructor.
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyOrderedMapIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyOrderedMapIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyOrderedMapIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EmptyOrderedMapIterator.java Tue Sep 15 05:29:56 2009
@@ -27,13 +27,25 @@
*
* @author Stephen Colebourne
*/
-public class EmptyOrderedMapIterator extends AbstractEmptyIterator implements OrderedMapIterator, ResettableIterator {
+public class EmptyOrderedMapIterator<K, V> extends AbstractEmptyMapIterator<K, V> implements
+ OrderedMapIterator<K, V>, ResettableIterator<K> {
/**
* Singleton instance of the iterator.
* @since Commons Collections 3.1
*/
- public static final OrderedMapIterator INSTANCE = new EmptyOrderedMapIterator();
+ public static final OrderedMapIterator<Object, Object> INSTANCE = new EmptyOrderedMapIterator<Object, Object>();
+
+ /**
+ * Get a typed instance of the iterator.
+ * @param <K>
+ * @param <V>
+ * @return {@link OrderedMapIterator}<K, V>
+ */
+ @SuppressWarnings("unchecked")
+ public static <K, V> OrderedMapIterator<K, V> getInstance() {
+ return (OrderedMapIterator<K, V>) INSTANCE;
+ }
/**
* Constructor.
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EntrySetMapIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EntrySetMapIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EntrySetMapIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EntrySetMapIterator.java Tue Sep 15 05:29:56 2009
@@ -39,11 +39,11 @@
*
* @author Stephen Colebourne
*/
-public class EntrySetMapIterator implements MapIterator, ResettableIterator {
+public class EntrySetMapIterator<K, V> implements MapIterator<K, V>, ResettableIterator<K> {
- private final Map map;
- private Iterator iterator;
- private Map.Entry last;
+ private final Map<K, V> map;
+ private Iterator<Map.Entry<K, V>> iterator;
+ private Map.Entry<K, V> last;
private boolean canRemove = false;
/**
@@ -51,7 +51,7 @@
*
* @param map the map to iterate over
*/
- public EntrySetMapIterator(Map map) {
+ public EntrySetMapIterator(Map<K, V> map) {
super();
this.map = map;
this.iterator = map.entrySet().iterator();
@@ -73,8 +73,8 @@
* @return the next key in the iteration
* @throws java.util.NoSuchElementException if the iteration is finished
*/
- public Object next() {
- last = (Map.Entry) iterator.next();
+ public K next() {
+ last = (Map.Entry<K, V>) iterator.next();
canRemove = true;
return last.getKey();
}
@@ -107,7 +107,7 @@
* @return the current key
* @throws IllegalStateException if <code>next()</code> has not yet been called
*/
- public Object getKey() {
+ public K getKey() {
if (last == null) {
throw new IllegalStateException("Iterator getKey() can only be called after next() and before remove()");
}
@@ -121,7 +121,7 @@
* @return the current value
* @throws IllegalStateException if <code>next()</code> has not yet been called
*/
- public Object getValue() {
+ public V getValue() {
if (last == null) {
throw new IllegalStateException("Iterator getValue() can only be called after next() and before remove()");
}
@@ -138,7 +138,7 @@
* @throws IllegalStateException if <code>remove()</code> has been called since the
* last call to <code>next()</code>
*/
- public Object setValue(Object value) {
+ public V setValue(V value) {
if (last == null) {
throw new IllegalStateException("Iterator setValue() can only be called after next() and before remove()");
}
@@ -163,9 +163,8 @@
public String toString() {
if (last != null) {
return "MapIterator[" + getKey() + "=" + getValue() + "]";
- } else {
- return "MapIterator[]";
}
+ return "MapIterator[]";
}
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EnumerationIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EnumerationIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EnumerationIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/EnumerationIterator.java Tue Sep 15 05:29:56 2009
@@ -30,14 +30,14 @@
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
*/
-public class EnumerationIterator implements Iterator {
+public class EnumerationIterator<E> implements Iterator<E> {
/** The collection to remove elements from */
- private Collection collection;
+ private Collection<? super E> collection;
/** The enumeration being converted */
- private Enumeration enumeration;
+ private Enumeration<? extends E> enumeration;
/** The last object retrieved */
- private Object last;
+ private E last;
// Constructors
//-----------------------------------------------------------------------
@@ -55,7 +55,7 @@
*
* @param enumeration the enumeration to use
*/
- public EnumerationIterator(final Enumeration enumeration) {
+ public EnumerationIterator(final Enumeration<? extends E> enumeration) {
this(enumeration, null);
}
@@ -64,9 +64,9 @@
* elements from the specified collection.
*
* @param enumeration the enumeration to use
- * @param collection the collection to remove elements form
+ * @param collection the collection to remove elements from
*/
- public EnumerationIterator(final Enumeration enumeration, final Collection collection) {
+ public EnumerationIterator(final Enumeration<? extends E> enumeration, final Collection<? super E> collection) {
super();
this.enumeration = enumeration;
this.collection = collection;
@@ -91,7 +91,7 @@
* @return the next object from the enumeration
* @throws NullPointerException if the enumeration is null
*/
- public Object next() {
+ public E next() {
last = enumeration.nextElement();
return last;
}
@@ -125,7 +125,7 @@
*
* @return the underlying enumeration
*/
- public Enumeration getEnumeration() {
+ public Enumeration<? extends E> getEnumeration() {
return enumeration;
}
@@ -134,7 +134,7 @@
*
* @param enumeration the new underlying enumeration
*/
- public void setEnumeration(final Enumeration enumeration) {
+ public void setEnumeration(final Enumeration<? extends E> enumeration) {
this.enumeration = enumeration;
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/FilterIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/FilterIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/FilterIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/FilterIterator.java Tue Sep 15 05:29:56 2009
@@ -35,14 +35,14 @@
* @author Ralph Wagner
* @author Stephen Colebourne
*/
-public class FilterIterator implements Iterator {
+public class FilterIterator<E> implements Iterator<E> {
/** The iterator being used */
- private Iterator iterator;
+ private Iterator<? extends E> iterator;
/** The predicate being used */
- private Predicate predicate;
+ private Predicate<? super E> predicate;
/** The next object in the iteration */
- private Object nextObject;
+ private E nextObject;
/** Whether the next object has been calculated yet */
private boolean nextObjectSet = false;
@@ -61,7 +61,7 @@
*
* @param iterator the iterator to use
*/
- public FilterIterator(Iterator iterator) {
+ public FilterIterator(Iterator<? extends E> iterator) {
super();
this.iterator = iterator;
}
@@ -73,7 +73,7 @@
* @param iterator the iterator to use
* @param predicate the predicate to use
*/
- public FilterIterator(Iterator iterator, Predicate predicate) {
+ public FilterIterator(Iterator<? extends E> iterator, Predicate<? super E> predicate) {
super();
this.iterator = iterator;
this.predicate = predicate;
@@ -88,11 +88,7 @@
* @throws NullPointerException if either the iterator or predicate are null
*/
public boolean hasNext() {
- if (nextObjectSet) {
- return true;
- } else {
- return setNextObject();
- }
+ return nextObjectSet || setNextObject();
}
/**
@@ -103,7 +99,7 @@
* @throws NoSuchElementException if there are no more elements that
* match the predicate
*/
- public Object next() {
+ public E next() {
if (!nextObjectSet) {
if (!setNextObject()) {
throw new NoSuchElementException();
@@ -137,7 +133,7 @@
*
* @return the iterator
*/
- public Iterator getIterator() {
+ public Iterator<? extends E> getIterator() {
return iterator;
}
@@ -147,7 +143,7 @@
*
* @param iterator the iterator to use
*/
- public void setIterator(Iterator iterator) {
+ public void setIterator(Iterator<? extends E> iterator) {
this.iterator = iterator;
nextObject = null;
nextObjectSet = false;
@@ -159,7 +155,7 @@
*
* @return the predicate
*/
- public Predicate getPredicate() {
+ public Predicate<? super E> getPredicate() {
return predicate;
}
@@ -168,7 +164,7 @@
*
* @param predicate the predicate to use
*/
- public void setPredicate(Predicate predicate) {
+ public void setPredicate(Predicate<? super E> predicate) {
this.predicate = predicate;
nextObject = null;
nextObjectSet = false;
@@ -181,7 +177,7 @@
*/
private boolean setNextObject() {
while (iterator.hasNext()) {
- Object object = iterator.next();
+ E object = iterator.next();
if (predicate.evaluate(object)) {
nextObject = object;
nextObjectSet = true;
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/FilterListIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/FilterListIterator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/FilterListIterator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/FilterListIterator.java Tue Sep 15 05:29:56 2009
@@ -32,19 +32,19 @@
*
* @author Rodney Waldhoff
*/
-public class FilterListIterator implements ListIterator {
+public class FilterListIterator<E> implements ListIterator<E> {
/** The iterator being used */
- private ListIterator iterator;
+ private ListIterator<? extends E> iterator;
/** The predicate being used */
- private Predicate predicate;
+ private Predicate<? super E> predicate;
/**
* The value of the next (matching) object, when
* {@link #nextObjectSet} is true.
*/
- private Object nextObject;
+ private E nextObject;
/**
* Whether or not the {@link #nextObject} has been set
@@ -56,7 +56,7 @@
* The value of the previous (matching) object, when
* {@link #previousObjectSet} is true.
*/
- private Object previousObject;
+ private E previousObject;
/**
* Whether or not the {@link #previousObject} has been set
@@ -85,7 +85,7 @@
*
* @param iterator the iterator to use
*/
- public FilterListIterator(ListIterator iterator ) {
+ public FilterListIterator(ListIterator<? extends E> iterator ) {
super();
this.iterator = iterator;
}
@@ -96,7 +96,7 @@
* @param iterator the iterator to use
* @param predicate the predicate to use
*/
- public FilterListIterator(ListIterator iterator, Predicate predicate) {
+ public FilterListIterator(ListIterator<? extends E> iterator, Predicate<? super E> predicate) {
super();
this.iterator = iterator;
this.predicate = predicate;
@@ -108,41 +108,33 @@
*
* @param predicate the predicate to use.
*/
- public FilterListIterator(Predicate predicate) {
+ public FilterListIterator(Predicate<? super E> predicate) {
super();
this.predicate = predicate;
}
//-----------------------------------------------------------------------
/** Not supported. */
- public void add(Object o) {
+ public void add(E o) {
throw new UnsupportedOperationException("FilterListIterator.add(Object) is not supported.");
}
public boolean hasNext() {
- if(nextObjectSet) {
- return true;
- } else {
- return setNextObject();
- }
+ return nextObjectSet || setNextObject();
}
public boolean hasPrevious() {
- if(previousObjectSet) {
- return true;
- } else {
- return setPreviousObject();
- }
+ return previousObjectSet || setPreviousObject();
}
- public Object next() {
- if(!nextObjectSet) {
- if(!setNextObject()) {
+ public E next() {
+ if (!nextObjectSet) {
+ if (!setNextObject()) {
throw new NoSuchElementException();
}
}
nextIndex++;
- Object temp = nextObject;
+ E temp = nextObject;
clearNextObject();
return temp;
}
@@ -151,14 +143,14 @@
return nextIndex;
}
- public Object previous() {
- if(!previousObjectSet) {
- if(!setPreviousObject()) {
+ public E previous() {
+ if (!previousObjectSet) {
+ if (!setPreviousObject()) {
throw new NoSuchElementException();
}
}
nextIndex--;
- Object temp = previousObject;
+ E temp = previousObject;
clearPreviousObject();
return temp;
}
@@ -173,7 +165,7 @@
}
/** Not supported. */
- public void set(Object o) {
+ public void set(E o) {
throw new UnsupportedOperationException("FilterListIterator.set(Object) is not supported.");
}
@@ -183,7 +175,7 @@
*
* @return the iterator.
*/
- public ListIterator getListIterator() {
+ public ListIterator<? extends E> getListIterator() {
return iterator;
}
@@ -193,7 +185,7 @@
*
* @param iterator the iterator to use
*/
- public void setListIterator(ListIterator iterator) {
+ public void setListIterator(ListIterator<? extends E> iterator) {
this.iterator = iterator;
}
@@ -203,7 +195,7 @@
*
* @return the predicate.
*/
- public Predicate getPredicate() {
+ public Predicate<? super E> getPredicate() {
return predicate;
}
@@ -212,7 +204,7 @@
*
* @param predicate the transformer to use
*/
- public void setPredicate(Predicate predicate) {
+ public void setPredicate(Predicate<? super E> predicate) {
this.predicate = predicate;
}
@@ -227,18 +219,17 @@
// then we've walked back one step in the
// underlying list (due to a hasPrevious() call)
// so skip ahead one matching object
- if(previousObjectSet) {
+ if (previousObjectSet) {
clearPreviousObject();
- if(!setNextObject()) {
+ if (!setNextObject()) {
return false;
- } else {
- clearNextObject();
}
+ clearNextObject();
}
- while(iterator.hasNext()) {
- Object object = iterator.next();
- if(predicate.evaluate(object)) {
+ while (iterator.hasNext()) {
+ E object = iterator.next();
+ if (predicate.evaluate(object)) {
nextObject = object;
nextObjectSet = true;
return true;
@@ -257,18 +248,17 @@
// then we've walked back one step in the
// underlying list (due to a hasNext() call)
// so skip ahead one matching object
- if(nextObjectSet) {
+ if (nextObjectSet) {
clearNextObject();
- if(!setPreviousObject()) {
+ if (!setPreviousObject()) {
return false;
- } else {
- clearPreviousObject();
}
+ clearPreviousObject();
}
- while(iterator.hasPrevious()) {
- Object object = iterator.previous();
- if(predicate.evaluate(object)) {
+ while (iterator.hasPrevious()) {
+ E object = iterator.previous();
+ if (predicate.evaluate(object)) {
previousObject = object;
previousObjectSet = true;
return true;