You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2008/06/09 19:17:43 UTC
svn commit: r665786 [2/6] - in /commons/sandbox/functor/trunk/src:
main/java/org/apache/commons/functor/adapter/
main/java/org/apache/commons/functor/core/
main/java/org/apache/commons/functor/core/algorithm/
main/java/org/apache/commons/functor/core/c...
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/RightBoundProcedure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/RightBoundProcedure.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/RightBoundProcedure.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/RightBoundProcedure.java Mon Jun 9 10:17:39 2008
@@ -38,18 +38,18 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class RightBoundProcedure implements UnaryProcedure, Serializable {
+public final class RightBoundProcedure<L, R> implements UnaryProcedure<L>, Serializable {
/** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */
- private BinaryProcedure procedure = null;
+ private BinaryProcedure<? super L, ? super R> procedure;
/** The parameter to pass to that procedure. */
- private Object param = null;
+ private R param;
/**
* Create a new RightBoundProcedure.
* @param procedure the procedure to adapt
* @param arg the constant argument to use
*/
- public RightBoundProcedure(BinaryProcedure procedure, Object arg) {
+ public RightBoundProcedure(BinaryProcedure<? super L, ? super R> procedure, R arg) {
this.procedure = procedure;
this.param = arg;
}
@@ -57,7 +57,7 @@
/**
* {@inheritDoc}
*/
- public void run(Object obj) {
+ public void run(L obj) {
procedure.run(obj, param);
}
@@ -65,7 +65,7 @@
* {@inheritDoc}
*/
public boolean equals(Object that) {
- return that == this || (that instanceof RightBoundProcedure && equals((RightBoundProcedure) that));
+ return that == this || (that instanceof RightBoundProcedure && equals((RightBoundProcedure<?, ?>) that));
}
/**
@@ -73,7 +73,7 @@
* @param that RightBoundProcedure to test
* @return boolean
*/
- public boolean equals(RightBoundProcedure that) {
+ public boolean equals(RightBoundProcedure<?, ?> that) {
return null != that
&& (null == procedure ? null == that.procedure : procedure.equals(that.procedure))
&& (null == param ? null == that.param : param.equals(that.param));
@@ -108,8 +108,8 @@
* @param arg right side argument
* @return RightBoundProcedure
*/
- public static RightBoundProcedure bind(BinaryProcedure procedure, Object arg) {
- return null == procedure ? null : new RightBoundProcedure(procedure, arg);
+ public static <L, R> RightBoundProcedure<L, R> bind(BinaryProcedure<? super L, ? super R> procedure, R arg) {
+ return null == procedure ? null : new RightBoundProcedure<L, R>(procedure, arg);
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryFunctionUnaryPredicate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryFunctionUnaryPredicate.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryFunctionUnaryPredicate.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryFunctionUnaryPredicate.java Mon Jun 9 10:17:39 2008
@@ -37,16 +37,16 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class UnaryFunctionUnaryPredicate implements UnaryPredicate, Serializable {
+public final class UnaryFunctionUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
/** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
- private UnaryFunction function = null;
+ private UnaryFunction<A, Boolean> function;
/**
* Create an {@link UnaryPredicate UnaryPredicate} wrapping
* the given {@link UnaryFunction UnaryFunction}.
* @param function the {@link UnaryFunction UnaryFunction} to wrap
*/
- public UnaryFunctionUnaryPredicate(UnaryFunction function) {
+ public UnaryFunctionUnaryPredicate(UnaryFunction<A, Boolean> function) {
this.function = function;
}
@@ -59,8 +59,8 @@
* @throws NullPointerException if my underlying function returns <code>null</code>
* @throws ClassCastException if my underlying function returns a non-<code>Boolean</code>
*/
- public boolean test(Object obj) {
- return ((Boolean) (function.evaluate(obj))).booleanValue();
+ public boolean test(A obj) {
+ return function.evaluate(obj);
}
/**
@@ -68,7 +68,7 @@
*/
public boolean equals(Object that) {
return that == this
- || (that instanceof UnaryFunctionUnaryPredicate && equals((UnaryFunctionUnaryPredicate) that));
+ || (that instanceof UnaryFunctionUnaryPredicate && equals((UnaryFunctionUnaryPredicate<?>) that));
}
/**
@@ -76,7 +76,7 @@
* @param that UnaryFunctionUnaryPredicate to test
* @return boolean
*/
- public boolean equals(UnaryFunctionUnaryPredicate that) {
+ public boolean equals(UnaryFunctionUnaryPredicate<?> that) {
return null != that && (null == function ? null == that.function : function.equals(that.function));
}
@@ -111,8 +111,8 @@
* {@link UnaryFunction UnaryFunction}, or <code>null</code>
* if the given <code>UnaryFunction</code> is <code>null</code>
*/
- public static UnaryFunctionUnaryPredicate adapt(UnaryFunction function) {
- return null == function ? null : new UnaryFunctionUnaryPredicate(function);
+ public static <P> UnaryFunctionUnaryPredicate<P> adapt(UnaryFunction<P, Boolean> function) {
+ return null == function ? null : new UnaryFunctionUnaryPredicate<P>(function);
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryFunctionUnaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryFunctionUnaryProcedure.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryFunctionUnaryProcedure.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryFunctionUnaryProcedure.java Mon Jun 9 10:17:39 2008
@@ -37,17 +37,17 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class UnaryFunctionUnaryProcedure implements UnaryProcedure, Serializable {
+public final class UnaryFunctionUnaryProcedure<A> implements UnaryProcedure<A>, Serializable {
/** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
- private UnaryFunction function = null;
+ private UnaryFunction<? super A, ?> function;
/**
* Create an {@link UnaryProcedure UnaryProcedure} wrapping
* the given {@link UnaryFunction UnaryFunction}.
* @param function the {@link UnaryFunction UnaryFunction} to wrap
*/
- public UnaryFunctionUnaryProcedure(UnaryFunction function) {
+ public UnaryFunctionUnaryProcedure(UnaryFunction<? super A, ?> function) {
this.function = function;
}
@@ -56,7 +56,7 @@
* ignore its returned value.
* {@inheritDoc}
*/
- public void run(Object obj) {
+ public void run(A obj) {
function.evaluate(obj);
}
@@ -65,7 +65,7 @@
*/
public boolean equals(Object that) {
return that == this
- || (that instanceof UnaryFunctionUnaryProcedure && equals((UnaryFunctionUnaryProcedure) that));
+ || (that instanceof UnaryFunctionUnaryProcedure && equals((UnaryFunctionUnaryProcedure<?>) that));
}
/**
@@ -73,7 +73,7 @@
* @param that the UnaryFunctionUnaryPredicate to test
* @return boolean
*/
- public boolean equals(UnaryFunctionUnaryProcedure that) {
+ public boolean equals(UnaryFunctionUnaryProcedure<?> that) {
return null != that && (null == function ? null == that.function : function.equals(that.function));
}
@@ -108,8 +108,8 @@
* {@link UnaryFunction UnaryFunction}, or <code>null</code>
* if the given <code>UnaryFunction</code> is <code>null</code>
*/
- public static UnaryFunctionUnaryProcedure adapt(UnaryFunction function) {
- return null == function ? null : new UnaryFunctionUnaryProcedure(function);
+ public static <A> UnaryFunctionUnaryProcedure<A> adapt(UnaryFunction<? super A, ?> function) {
+ return null == function ? null : new UnaryFunctionUnaryProcedure<A>(function);
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryPredicateUnaryFunction.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryPredicateUnaryFunction.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryPredicateUnaryFunction.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryPredicateUnaryFunction.java Mon Jun 9 10:17:39 2008
@@ -37,15 +37,15 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class UnaryPredicateUnaryFunction implements UnaryFunction, Serializable {
+public final class UnaryPredicateUnaryFunction<A> implements UnaryFunction<A, Boolean>, Serializable {
/** The {@link UnaryPredicate UnaryPredicate} I'm wrapping. */
- private UnaryPredicate predicate = null;
+ private UnaryPredicate<? super A> predicate;
/**
* Create a new UnaryPredicateUnaryFunction.
* @param predicate to adapt
*/
- public UnaryPredicateUnaryFunction(UnaryPredicate predicate) {
+ public UnaryPredicateUnaryFunction(UnaryPredicate<? super A> predicate) {
this.predicate = predicate;
}
@@ -57,8 +57,8 @@
*
* @return a non-<code>null</code> <code>Boolean</code> instance
*/
- public Object evaluate(Object obj) {
- return predicate.test(obj) ? Boolean.TRUE : Boolean.FALSE;
+ public Boolean evaluate(A obj) {
+ return predicate.test(obj);
}
/**
@@ -66,7 +66,7 @@
*/
public boolean equals(Object that) {
return that == this
- || (that instanceof UnaryPredicateUnaryFunction && equals((UnaryPredicateUnaryFunction) that));
+ || (that instanceof UnaryPredicateUnaryFunction && equals((UnaryPredicateUnaryFunction<?>) that));
}
/**
@@ -74,7 +74,7 @@
* @param that UnaryPredicateUnaryFunction to test
* @return boolean
*/
- public boolean equals(UnaryPredicateUnaryFunction that) {
+ public boolean equals(UnaryPredicateUnaryFunction<?> that) {
return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate));
}
@@ -101,8 +101,8 @@
* @param predicate to adapt
* @return UnaryPredicateUnaryFunction
*/
- public static UnaryPredicateUnaryFunction adapt(UnaryPredicate predicate) {
- return null == predicate ? null : new UnaryPredicateUnaryFunction(predicate);
+ public static <A> UnaryPredicateUnaryFunction<A> adapt(UnaryPredicate<? super A> predicate) {
+ return null == predicate ? null : new UnaryPredicateUnaryFunction<A>(predicate);
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryProcedureUnaryFunction.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryProcedureUnaryFunction.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryProcedureUnaryFunction.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/UnaryProcedureUnaryFunction.java Mon Jun 9 10:17:39 2008
@@ -38,22 +38,22 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class UnaryProcedureUnaryFunction implements UnaryFunction, Serializable {
+public final class UnaryProcedureUnaryFunction<A, T> implements UnaryFunction<A, T>, Serializable {
/** The {@link UnaryProcedure UnaryProcedure} I'm wrapping. */
- private UnaryProcedure procedure = null;
+ private UnaryProcedure<? super A> procedure;
/**
* Create a new UnaryProcedureUnaryFunction.
* @param procedure to adapt
*/
- public UnaryProcedureUnaryFunction(UnaryProcedure procedure) {
+ public UnaryProcedureUnaryFunction(UnaryProcedure<? super A> procedure) {
this.procedure = procedure;
}
/**
* {@inheritDoc}
*/
- public Object evaluate(Object obj) {
+ public T evaluate(A obj) {
procedure.run(obj);
return null;
}
@@ -63,7 +63,7 @@
*/
public boolean equals(Object that) {
return that == this
- || (that instanceof UnaryProcedureUnaryFunction && equals((UnaryProcedureUnaryFunction) that));
+ || (that instanceof UnaryProcedureUnaryFunction && equals((UnaryProcedureUnaryFunction<?, ?>) that));
}
/**
@@ -71,7 +71,7 @@
* @param that UnaryProcedureUnaryFunction to test
* @return boolean
*/
- public boolean equals(UnaryProcedureUnaryFunction that) {
+ public boolean equals(UnaryProcedureUnaryFunction<?, ?> that) {
return null != that && (null == procedure ? null == that.procedure : procedure.equals(that.procedure));
}
@@ -98,8 +98,8 @@
* @param procedure to adapt
* @return UnaryProcedureUnaryFunction
*/
- public static UnaryProcedureUnaryFunction adapt(UnaryProcedure procedure) {
- return null == procedure ? null : new UnaryProcedureUnaryFunction(procedure);
+ public static <A, T> UnaryProcedureUnaryFunction<A, T> adapt(UnaryProcedure<? super A> procedure) {
+ return null == procedure ? null : new UnaryProcedureUnaryFunction<A, T>(procedure);
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Constant.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Constant.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Constant.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Constant.java Mon Jun 9 10:17:39 2008
@@ -41,33 +41,33 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class Constant implements Function, UnaryFunction, BinaryFunction, Predicate, UnaryPredicate,
- BinaryPredicate, Serializable {
+public final class Constant<T> implements Function<T>, UnaryFunction<Object, T>, BinaryFunction<Object, Object, T>,
+ Predicate, UnaryPredicate<Object>, BinaryPredicate<Object, Object>, Serializable {
// static attributes
// ------------------------------------------------------------------------
- private static final Constant TRUE_PREDICATE = new Constant(true);
- private static final Constant FALSE_PREDICATE = new Constant(false);
+ /**
+ * Constant for <code>true</code>.
+ */
+ public static final Constant<Boolean> TRUE = Constant.of(true);
+
+ /**
+ * Constant for <code>false</code>.
+ */
+ public static final Constant<Boolean> FALSE = Constant.of(false);
// attributes
// ------------------------------------------------------------------------
- private Object value;
+ private T value;
// constructor
// ------------------------------------------------------------------------
- /**
- * Create a new Constant.
- * @param value boolean
- */
- public Constant(boolean value) {
- this(value ? Boolean.TRUE : Boolean.FALSE);
- }
/**
* Create a new Constant.
* @param value Object
*/
- public Constant(Object value) {
+ public Constant(T value) {
this.value = value;
}
@@ -76,21 +76,21 @@
/**
* {@inheritDoc}
*/
- public Object evaluate() {
+ public T evaluate() {
return value;
}
/**
* {@inheritDoc}
*/
- public Object evaluate(Object obj) {
+ public T evaluate(Object obj) {
return evaluate();
}
/**
* {@inheritDoc}
*/
- public Object evaluate(Object left, Object right) {
+ public T evaluate(Object left, Object right) {
return evaluate();
}
@@ -119,7 +119,7 @@
* {@inheritDoc}
*/
public boolean equals(Object that) {
- return that == this || (that instanceof Constant && equals((Constant) that));
+ return that == this || (that instanceof Constant && equals((Constant<?>) that));
}
/**
@@ -127,7 +127,7 @@
* @param that Constant to test
* @return boolean
*/
- public boolean equals(Constant that) {
+ public boolean equals(Constant<?> that) {
return (null != that && (null == this.value ? null == that.value : this.value.equals(that.value)));
}
@@ -158,8 +158,8 @@
* @return a <code>Constant</code> that always
* returns <code>true</code>
*/
- public static Constant truePredicate() {
- return TRUE_PREDICATE;
+ public static Constant<Boolean> truePredicate() {
+ return TRUE;
}
/**
@@ -168,8 +168,8 @@
* @return a <code>Constant</code> that always
* returns <code>false</code>
*/
- public static Constant falsePredicate() {
- return FALSE_PREDICATE;
+ public static Constant<Boolean> falsePredicate() {
+ return FALSE;
}
/**
@@ -179,8 +179,8 @@
* @return a <code>Constant</code> that always
* returns <i>value</i>
*/
- public static Constant predicate(boolean value) {
- return value ? TRUE_PREDICATE : FALSE_PREDICATE;
+ public static Constant<Boolean> predicate(boolean value) {
+ return value ? TRUE : FALSE;
}
/**
@@ -188,8 +188,8 @@
* @param value Object
* @return Constant
*/
- public static Constant instance(Object value) {
- return new Constant(value);
+ public static <Z extends Object> Constant<Z> of(Z value) {
+ return new Constant<Z>(value);
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Identity.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Identity.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Identity.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Identity.java Mon Jun 9 10:17:39 2008
@@ -33,11 +33,11 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class Identity implements UnaryFunction, UnaryPredicate, Serializable {
+public final class Identity<T> implements UnaryFunction<T, T>, UnaryPredicate<T>, Serializable {
// static attributes
// ------------------------------------------------------------------------
- private static final Identity INSTANCE = new Identity();
+ public static final Identity<Object> INSTANCE = new Identity<Object>();
// constructor
// ------------------------------------------------------------------------
@@ -54,7 +54,7 @@
/**
* {@inheritDoc}
*/
- public Object evaluate(Object obj) {
+ public T evaluate(T obj) {
return obj;
}
@@ -102,8 +102,8 @@
* Get an Identity instance.
* @return Identity
*/
- public static Identity instance() {
- return INSTANCE;
+ public static <T> Identity<T> instance() {
+ return new Identity<T>();
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsEqual.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsEqual.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsEqual.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsEqual.java Mon Jun 9 10:17:39 2008
@@ -19,6 +19,8 @@
import java.io.Serializable;
import org.apache.commons.functor.BinaryPredicate;
+import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.adapter.RightBoundPredicate;
/**
* {@link #test Tests}
@@ -35,10 +37,13 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class IsEqual implements BinaryPredicate, Serializable {
+public final class IsEqual<L, R> implements BinaryPredicate<L, R>, Serializable {
// static attributes
// ------------------------------------------------------------------------
- private static final IsEqual INSTANCE = new IsEqual();
+ /**
+ * Basic IsEqual<Object, Object> instance.
+ */
+ public static final IsEqual<Object, Object> INSTANCE = IsEqual.<Object, Object>instance();
// constructor
// ------------------------------------------------------------------------
@@ -53,8 +58,8 @@
/**
* {@inheritDoc}
*/
- public boolean test(Object left, Object right) {
- return (null == left ? null == right : left.equals(right));
+ public boolean test(L left, R right) {
+ return left == right || left != null && left.equals(right);
}
/**
@@ -84,8 +89,18 @@
* Get an IsEqual instance.
* @return IsEqual
*/
- public static IsEqual instance() {
- return INSTANCE;
+ public static <L, R> IsEqual<L, R> instance() {
+ return new IsEqual<L, R>();
}
+ /**
+ * Get an IsEqual UnaryPredicate.
+ * @param <L>
+ * @param <R>
+ * @param object bound comparison object
+ * @return UnaryPredicate<L>
+ */
+ public static <L, R> UnaryPredicate<L> to(R object) {
+ return new RightBoundPredicate<L, R>(new IsEqual<L, R>(), object);
+ }
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNotEqual.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNotEqual.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNotEqual.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNotEqual.java Mon Jun 9 10:17:39 2008
@@ -19,6 +19,8 @@
import java.io.Serializable;
import org.apache.commons.functor.BinaryPredicate;
+import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.adapter.RightBoundPredicate;
/**
* {@link #test Tests}
@@ -32,10 +34,13 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class IsNotEqual implements BinaryPredicate, Serializable {
+public final class IsNotEqual<L, R> implements BinaryPredicate<L, R>, Serializable {
// static attributes
// ------------------------------------------------------------------------
- private static final IsNotEqual INSTANCE = new IsNotEqual();
+ /**
+ * Basic IsNotEqual<Object, Object> instance.
+ */
+ public static final IsNotEqual<Object, Object> INSTANCE = IsNotEqual.<Object, Object>instance();
// constructor
// ------------------------------------------------------------------------
@@ -50,7 +55,7 @@
/**
* {@inheritDoc}
*/
- public boolean test(Object left, Object right) {
+ public boolean test(L left, R right) {
return (null == left ? null != right : !left.equals(right));
}
@@ -76,13 +81,26 @@
}
// static methods
- // ------------------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Get an IsNotEqual instance.
- * @return IsNotEqual
+ * @param <L>
+ * @param <R>
+ * @return IsNotEqual<L, R>
*/
- public static IsNotEqual instance() {
- return INSTANCE;
+ public static <L, R> IsNotEqual<L, R> instance() {
+ return new IsNotEqual<L, R>();
}
+ /**
+ * Get an IsNotEqual UnaryPredicate.
+ * @param <L>
+ * @param <R>
+ * @param object bound comparison object
+ * @return UnaryPredicate<L>
+ */
+ public static <L, R> UnaryPredicate<L> to(R object) {
+ return new RightBoundPredicate<L, R>(new IsNotEqual<L, R>(), object);
+ }
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNotNull.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNotNull.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNotNull.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNotNull.java Mon Jun 9 10:17:39 2008
@@ -18,7 +18,10 @@
import java.io.Serializable;
+import org.apache.commons.functor.BinaryPredicate;
import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.adapter.IgnoreLeftPredicate;
+import org.apache.commons.functor.adapter.IgnoreRightPredicate;
/**
* {@link #test Tests}
@@ -28,10 +31,23 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class IsNotNull implements UnaryPredicate, Serializable {
+public final class IsNotNull<T> implements UnaryPredicate<T>, Serializable {
// static attributes
// ------------------------------------------------------------------------
- private static final IsNotNull INSTANCE = new IsNotNull();
+ /**
+ * Basic IsNotNull instance.
+ */
+ public static final IsNotNull<Object> INSTANCE = IsNotNull.<Object>instance();
+
+ /**
+ * Left-handed BinaryPredicate.
+ */
+ public static final BinaryPredicate<Object, Object> LEFT = IsNotNull.<Object>left();
+
+ /**
+ * Right-handed BinaryPredicate.
+ */
+ public static final BinaryPredicate<Object, Object> RIGHT = IsNotNull.<Object>right();
// constructor
// ------------------------------------------------------------------------
@@ -77,8 +93,24 @@
* Get an IsNotNull instance.
* @return IsNotNull
*/
- public static IsNotNull instance() {
- return INSTANCE;
+ public static <T> IsNotNull<T> instance() {
+ return new IsNotNull<T>();
+ }
+
+ /**
+ * Get a BinaryPredicate that matches if the left argument is not null.
+ * @return BinaryPredicate<A, Object>
+ */
+ public static <A> BinaryPredicate<A, Object> left() {
+ return IgnoreRightPredicate.adapt(new IsNotNull<A>());
+ }
+
+ /**
+ * Get a BinaryPredicate that matches if the right argument is null.
+ * @return BinaryPredicate<Object, A>
+ */
+ public static <A> BinaryPredicate<Object, A> right() {
+ return IgnoreLeftPredicate.adapt(new IsNotNull<A>());
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNull.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNull.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNull.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsNull.java Mon Jun 9 10:17:39 2008
@@ -31,12 +31,23 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class IsNull implements UnaryPredicate, Serializable {
+public final class IsNull<A> implements UnaryPredicate<A>, Serializable {
// static attributes
// ------------------------------------------------------------------------
- private static final IsNull INSTANCE = new IsNull();
- private static final BinaryPredicate LEFT = IgnoreRightPredicate.adapt(instance());
- private static final BinaryPredicate RIGHT = IgnoreLeftPredicate.adapt(instance());
+ /**
+ * Basic IsNull instance.
+ */
+ public static final IsNull<Object> INSTANCE = IsNull.<Object>instance();
+
+ /**
+ * Left-handed BinaryPredicate.
+ */
+ public static final BinaryPredicate<Object, Object> LEFT = IsNull.<Object>left();
+
+ /**
+ * Right-handed BinaryPredicate.
+ */
+ public static final BinaryPredicate<Object, Object> RIGHT = IsNull.<Object>right();
// constructor
// ------------------------------------------------------------------------
@@ -51,7 +62,7 @@
/**
* {@inheritDoc}
*/
- public boolean test(Object obj) {
+ public boolean test(A obj) {
return (null == obj);
}
@@ -82,24 +93,24 @@
* Get an IsNull instance.
* @return IsNull
*/
- public static IsNull instance() {
- return INSTANCE;
+ public static <T> IsNull<T> instance() {
+ return new IsNull<T>();
}
/**
* Get a BinaryPredicate that matches if the left argument is null.
* @return BinaryPredicate
*/
- public static BinaryPredicate left() {
- return LEFT;
+ public static <A> BinaryPredicate<A, Object> left() {
+ return IgnoreRightPredicate.adapt(new IsNull<A>());
}
/**
* Get a BinaryPredicate that matches if the right argument is null.
* @return BinaryPredicate
*/
- public static BinaryPredicate right() {
- return RIGHT;
+ public static <A> BinaryPredicate<Object, A> right() {
+ return IgnoreLeftPredicate.adapt(new IsNull<A>());
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/LeftIdentity.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/LeftIdentity.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/LeftIdentity.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/LeftIdentity.java Mon Jun 9 10:17:39 2008
@@ -16,90 +16,60 @@
*/
package org.apache.commons.functor.core;
-import java.io.Serializable;
-
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.BinaryPredicate;
+import org.apache.commons.functor.adapter.BinaryFunctionBinaryPredicate;
+import org.apache.commons.functor.adapter.IgnoreRightFunction;
/**
- * {@link #evaluate Evaluates} to its first argument.
- *
- * {@link #test Tests} to the <code>boolean</code>
- * value of the <code>Boolean</code>-valued first
- * argument. The {@link #test test} method
- * throws an exception if the parameter isn't a
- * non-<code>null</code> <code>Boolean</code>.
+ * Holder class for a left-identity <code>BinaryFunction</code> (evaluates to the left argument) and a left-identity
+ * <code>BinaryPredicate</code> (tests whether left <code>Boolean</code> argument equals <code>Boolean.TRUE</code>).
*
* @version $Revision$ $Date$
* @author Rodney Waldhoff
+ * @author Matt Benson
*/
-public final class LeftIdentity implements BinaryPredicate, BinaryFunction, Serializable {
- // static attributes
- // ------------------------------------------------------------------------
- private static final LeftIdentity INSTANCE = new LeftIdentity();
+public final class LeftIdentity {
- // constructor
- // ------------------------------------------------------------------------
- /**
- * Create a new LeftIdentity.
- */
- public LeftIdentity() {
- }
-
- // functor interface
+ // static attributes
// ------------------------------------------------------------------------
/**
- * {@inheritDoc}
+ * Left-identity function.
*/
- public Object evaluate(Object left, Object right) {
- return left;
- }
+ public static final BinaryFunction<Object, Object, Object> FUNCTION = LeftIdentity.<Object, Object>function();
/**
- * {@inheritDoc}
+ * Left-identity predicate.
*/
- public boolean test(Object left, Object right) {
- return test((Boolean) left);
- }
-
- /**
- * Test a Boolean
- * @param bool to test
- * @return boolean
- */
- private boolean test(Boolean bool) {
- return bool.booleanValue();
- }
+ public static final BinaryPredicate<Boolean, Object> PREDICATE = LeftIdentity.<Object>predicate();
+ // constructor
+ // ------------------------------------------------------------------------
/**
- * {@inheritDoc}
+ * Create a new LeftIdentity (for clients that require an object).
*/
- public boolean equals(Object that) {
- return (that instanceof LeftIdentity);
+ public LeftIdentity() {
}
- /**
- * {@inheritDoc}
- */
- public int hashCode() {
- return "LeftIdentity".hashCode();
- }
+ // static methods
+ // ------------------------------------------------------------------------
/**
- * {@inheritDoc}
+ * Get a Left-identity BinaryFunction.
+ * @param <L>
+ * @param <R>
+ * @return BinaryFunction<L, R, L>
*/
- public String toString() {
- return "LeftIdentity";
+ public static <L, R> BinaryFunction<L, R, L> function() {
+ return IgnoreRightFunction.adapt(new Identity<L>());
}
- // static methods
- // ------------------------------------------------------------------------
/**
- * Get a LeftIdentity instance.
- * @return LeftIdentity
+ * Get a left-identity BinaryPredicate.
+ * @param <R>
+ * @return BinaryPredicate<Boolean, R>
*/
- public static LeftIdentity instance() {
- return INSTANCE;
+ public static <R> BinaryPredicate<Boolean, R> predicate() {
+ return BinaryFunctionBinaryPredicate.adapt(IgnoreRightFunction.<Boolean, R, Boolean>adapt(new Identity<Boolean>()));
}
-
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Limit.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Limit.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Limit.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Limit.java Mon Jun 9 10:17:39 2008
@@ -30,11 +30,11 @@
* @author Rodney Waldhoff
*/
-public final class Limit implements Predicate, UnaryPredicate, BinaryPredicate {
+public final class Limit implements Predicate, UnaryPredicate<Object>, BinaryPredicate<Object, Object> {
// instance variables
//---------------------------------------------------------------
private int max;
- private int current = 0;
+ private int current;
/**
* Create a new Limit.
@@ -55,9 +55,8 @@
if (current < max) {
current++;
return true;
- } else {
- return false;
}
+ return false;
}
/**
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/NoOp.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/NoOp.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/NoOp.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/NoOp.java Mon Jun 9 10:17:39 2008
@@ -31,10 +31,13 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class NoOp implements Procedure, UnaryProcedure, BinaryProcedure, Serializable {
+public final class NoOp implements Procedure, UnaryProcedure<Object>, BinaryProcedure<Object, Object>, Serializable {
// static attributes
// ------------------------------------------------------------------------
- private static final NoOp INSTANCE = new NoOp();
+ /**
+ * Basic NoOp instance.
+ */
+ public static final NoOp INSTANCE = new NoOp();
// constructor
// ------------------------------------------------------------------------
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Offset.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Offset.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Offset.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/Offset.java Mon Jun 9 10:17:39 2008
@@ -31,11 +31,11 @@
* @author Rodney Waldhoff
*/
-public final class Offset implements Predicate, UnaryPredicate, BinaryPredicate {
+public final class Offset implements Predicate, UnaryPredicate<Object>, BinaryPredicate<Object, Object> {
// instance variables
//---------------------------------------------------------------
private int min;
- private int current = 0;
+ private int current;
/**
* Create a new Offset.
@@ -56,9 +56,8 @@
if (current < min) {
current++;
return false;
- } else {
- return true;
}
+ return true;
}
/**
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/RightIdentity.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/RightIdentity.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/RightIdentity.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/RightIdentity.java Mon Jun 9 10:17:39 2008
@@ -16,91 +16,60 @@
*/
package org.apache.commons.functor.core;
-import java.io.Serializable;
-
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.BinaryPredicate;
+import org.apache.commons.functor.adapter.BinaryFunctionBinaryPredicate;
+import org.apache.commons.functor.adapter.IgnoreLeftFunction;
/**
- * {@link #evaluate Evaluates} to its second argument.
- *
- * {@link #test Tests} to the <code>boolean</code>
- * value of the <code>Boolean</code>-valued second
- * argument. The {@link #test test} method
- * throws an exception if the parameter isn't a
- * non-<code>null</code> <code>Boolean</code>.
+ * Holder class for a right-identity <code>BinaryFunction</code> (evaluates to the right argument) and a right-identity
+ * <code>BinaryPredicate</code> (tests whether right <code>Boolean</code> argument equals <code>Boolean.TRUE</code>).
*
* @version $Revision$ $Date$
* @author Rodney Waldhoff
+ * @author Matt Benson
*/
-public final class RightIdentity implements BinaryPredicate, BinaryFunction, Serializable {
+public final class RightIdentity {
// static attributes
// ------------------------------------------------------------------------
- private static final RightIdentity INSTANCE = new RightIdentity();
-
- // constructor
- // ------------------------------------------------------------------------
- /**
- * Create a new RightIdentity.
- */
- public RightIdentity() {
- }
-
- // functor interface
- // ------------------------------------------------------------------------
- /**
- * {@inheritDoc}
- */
- public Object evaluate(Object left, Object right) {
- return right;
- }
-
/**
- * {@inheritDoc}
+ * Right-identity function.
*/
- public boolean test(Object left, Object right) {
- return test((Boolean) right);
- }
+ public static final BinaryFunction<Object, Object, Object> FUNCTION = RightIdentity.<Object, Object>function();
/**
- * Test a Boolean.
- * @param bool to test
- * @return boolean
+ * Right-identity predicate.
*/
- private boolean test(Boolean bool) {
- return bool.booleanValue();
- }
+ public static final BinaryPredicate<Object, Boolean> PREDICATE = BinaryFunctionBinaryPredicate.adapt(IgnoreLeftFunction.adapt(new Identity<Boolean>()));
+ // constructor
+ // ------------------------------------------------------------------------
/**
- * {@inheritDoc}
+ * Create a new RightIdentity.
*/
- public boolean equals(Object that) {
- return (that instanceof RightIdentity);
+ public RightIdentity() {
}
- /**
- * {@inheritDoc}
- */
- public int hashCode() {
- return "RightIdentity".hashCode();
- }
+ // static methods
+ // ------------------------------------------------------------------------
/**
- * {@inheritDoc}
+ * Get a typed right-identity BinaryFunction.
+ * @param <L>
+ * @param <R>
+ * @return BinaryFunction<L, R, R>
*/
- public String toString() {
- return "RightIdentity";
+ public static <L, R> BinaryFunction<L, R, R> function() {
+ return IgnoreLeftFunction.adapt(new Identity<R>());
}
- // static methods
- // ------------------------------------------------------------------------
/**
- * Get a RightIdentity instance.
- * @return RightIdentity
+ * Get a typed right-identity BinaryPredicate.
+ * @param <L>
+ * @return BinaryPredicate<L, Boolean>
*/
- public static RightIdentity instance() {
- return INSTANCE;
+ public static <L> BinaryPredicate<L, Boolean> predicate() {
+ return BinaryFunctionBinaryPredicate.adapt(IgnoreLeftFunction.<L, Boolean, Boolean>adapt(new Identity<Boolean>()));
}
-
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java Mon Jun 9 10:17:39 2008
@@ -29,29 +29,32 @@
*
* @version $Revision$ $Date$
*/
-public final class FindWithinGenerator implements BinaryFunction, Serializable {
- private static final FindWithinGenerator INSTANCE = new FindWithinGenerator();
+public final class FindWithinGenerator<E> implements BinaryFunction<Generator<E>, UnaryPredicate<E>, E>, Serializable {
+ /**
+ * Basic instance.
+ */
+ public static final FindWithinGenerator<Object> INSTANCE = new FindWithinGenerator<Object>();
/**
* Helper procedure.
*/
- private class FindProcedure implements UnaryProcedure {
- private Object found;
+ private static class FindProcedure<T> implements UnaryProcedure<T> {
+ private T found;
private boolean wasFound;
- private UnaryPredicate pred;
+ private UnaryPredicate<T> pred;
/**
* Create a new FindProcedure.
* @pred test
*/
- public FindProcedure(UnaryPredicate pred) {
+ public FindProcedure(UnaryPredicate<T> pred) {
this.pred = pred;
}
/**
* {@inheritDoc}
*/
- public void run(Object obj) {
+ public void run(T obj) {
if (!wasFound && pred.test(obj)) {
wasFound = true;
found = obj;
@@ -60,7 +63,7 @@
}
private boolean useIfNone;
- private Object ifNone;
+ private E ifNone;
/**
* Create a new FindWithinGenerator.
@@ -73,7 +76,7 @@
* Create a new FindWithinGenerator.
* @param ifNone object to return if the Generator contains no matches.
*/
- public FindWithinGenerator(Object ifNone) {
+ public FindWithinGenerator(E ifNone) {
this();
this.ifNone = ifNone;
useIfNone = true;
@@ -84,15 +87,14 @@
* @param left Generator
* @param right UnaryPredicate
*/
- public Object evaluate(Object left, Object right) {
- UnaryPredicate pred = (UnaryPredicate) right;
- FindProcedure findProcedure = new FindProcedure(pred);
- ((Generator) left).run(findProcedure);
+ public E evaluate(Generator<E> left, UnaryPredicate<E> right) {
+ FindProcedure<E> findProcedure = new FindProcedure<E>(right);
+ left.run(findProcedure);
if (!findProcedure.wasFound) {
if (useIfNone) {
return ifNone;
}
- throw new NoSuchElementException("No element matching " + pred + " was found.");
+ throw new NoSuchElementException("No element matching " + right + " was found.");
}
return findProcedure.found;
}
@@ -107,7 +109,7 @@
if (obj instanceof FindWithinGenerator == false) {
return false;
}
- FindWithinGenerator other = (FindWithinGenerator) obj;
+ FindWithinGenerator<?> other = (FindWithinGenerator<?>) obj;
return other.useIfNone == useIfNone && !useIfNone
|| (other.ifNone == this.ifNone || other.ifNone != null && other.ifNone.equals(this.ifNone));
}
@@ -128,7 +130,7 @@
* Get a static {@link FindWithinGenerator} instance.
* @return {@link FindWithinGenerator}
*/
- public static FindWithinGenerator instance() {
+ public static FindWithinGenerator<Object> instance() {
return INSTANCE;
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java Mon Jun 9 10:17:39 2008
@@ -31,25 +31,69 @@
*
* @version $Revision$ $Date$
*/
-public class FoldLeft implements UnaryFunction, BinaryFunction, Serializable {
+public class FoldLeft<T> implements UnaryFunction<Generator<T>, T>, BinaryFunction<Generator<T>, T, T>, Serializable {
- private BinaryFunction func;
+ /**
+ * Helper procedure
+ */
+ private class FoldLeftHelper implements UnaryProcedure<T> {
+ private T seed;
+ private boolean started;
+
+ /**
+ * Create a seedless FoldLeftHelper.
+ */
+ public FoldLeftHelper() {
+ }
+
+ /**
+ * Create a new FoldLeftHelper.
+ * @param seed initial left argument
+ */
+ FoldLeftHelper(T seed) {
+ this.seed = seed;
+ started = true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run(T obj) {
+ if (!started) {
+ seed = obj;
+ started = true;
+ } else {
+ seed = function.evaluate(seed, obj);
+ }
+ }
+
+ /**
+ * Get current result.
+ * @return Object
+ */
+ T getResult() {
+ return started ? seed : null;
+ }
+
+ }
+
+ private BinaryFunction<? super T, ? super T, ? extends T> function;
/**
* Create a new FoldLeft.
* @param func {@link BinaryFunction} to apply to each (seed, next)
*/
- public FoldLeft(BinaryFunction func) {
- this.func = func;
+ public FoldLeft(BinaryFunction<? super T, ? super T, ? extends T> func) {
+ this.function = func;
}
/**
* {@inheritDoc}
* @param obj {@link Generator} to transform
*/
- public Object evaluate(Object obj) {
- FoldLeftHelper helper = new FoldLeftHelper(func);
- ((Generator) obj).run(helper);
+ public T evaluate(Generator<T> obj) {
+ FoldLeftHelper helper = new FoldLeftHelper();
+ obj.run(helper);
return helper.getResult();
}
@@ -58,9 +102,9 @@
* @param left {@link Generator} to transform
* @param right seed object
*/
- public Object evaluate(Object left, Object right) {
- FoldLeftHelper helper = new FoldLeftHelper(func, right);
- ((Generator) left).run(helper);
+ public T evaluate(Generator<T> left, T right) {
+ FoldLeftHelper helper = new FoldLeftHelper(right);
+ left.run(helper);
return helper.getResult();
}
@@ -74,62 +118,14 @@
if (obj instanceof FoldLeft == false) {
return false;
}
- return ((FoldLeft) obj).func.equals(func);
+ return ((FoldLeft<?>) obj).function.equals(function);
}
/**
* {@inheritDoc}
*/
public int hashCode() {
- return "FoldLeft".hashCode() << 2 ^ func.hashCode();
+ return "FoldLeft".hashCode() << 2 ^ function.hashCode();
}
- /**
- * Helper procedure
- */
- private static class FoldLeftHelper implements UnaryProcedure {
- private BinaryFunction function;
- private Object seed;
- private boolean started;
-
- /**
- * Create a seedless FoldLeftHelper.
- * @param function to apply
- */
- public FoldLeftHelper(BinaryFunction function) {
- this.function = function;
- }
-
- /**
- * Create a new FoldLeftHelper.
- * @param function to apply
- * @param seed initial left argument
- */
- FoldLeftHelper(BinaryFunction function, Object seed) {
- this(function);
- this.seed = seed;
- started = true;
- }
-
- /**
- * {@inheritDoc}
- */
- public void run(Object obj) {
- if (!started) {
- seed = obj;
- started = true;
- } else {
- seed = function.evaluate(seed, obj);
- }
- }
-
- /**
- * Get current result.
- * @return Object
- */
- Object getResult() {
- return started ? seed : null;
- }
-
- }
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java Mon Jun 9 10:17:39 2008
@@ -31,83 +31,27 @@
* elements have been expended.
* @version $Revision$ $Date$
*/
-public class FoldRight implements UnaryFunction, BinaryFunction, Serializable {
-
- private BinaryFunction func;
-
- /**
- * Create a new FoldLeft.
- * @param func {@link BinaryFunction} to apply to each (seed, next)
- */
- public FoldRight(BinaryFunction func) {
- this.func = func;
- }
-
- /**
- * {@inheritDoc}
- * @param obj {@link Generator} to transform
- */
- public Object evaluate(Object obj) {
- FoldRightHelper helper = new FoldRightHelper(func);
- ((Generator) obj).run(helper);
- return helper.getResult();
- }
-
- /**
- * {@inheritDoc}
- * @param left {@link Generator} to transform
- * @param right seed object
- */
- public Object evaluate(Object left, Object right) {
- FoldRightHelper helper = new FoldRightHelper(func, right);
- ((Generator) left).run(helper);
- return helper.getResult();
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean equals(Object obj) {
- if (obj == this) {
- return true;
- }
- if (obj instanceof FoldRight == false) {
- return false;
- }
- return ((FoldRight) obj).func.equals(func);
- }
-
- /**
- * {@inheritDoc}
- */
- public int hashCode() {
- return "FoldRight".hashCode() << 2 ^ func.hashCode();
- }
+public class FoldRight<T> implements UnaryFunction<Generator<T>, T>, BinaryFunction<Generator<T>, T, T>, Serializable {
/**
* Helper class
*/
- private static class FoldRightHelper implements UnaryProcedure {
- private BinaryFunction function;
- private Object seed;
- private Stack stk = new Stack();
+ private class FoldRightHelper implements UnaryProcedure<T> {
+ private T seed;
+ private Stack<T> stk = new Stack<T>();
private boolean hasSeed;
/**
* Create a seedless FoldRightHelper.
- * @param function to apply
*/
- public FoldRightHelper(BinaryFunction function) {
- this.function = function;
+ public FoldRightHelper() {
}
/**
* Create a new FoldRightHelper.
- * @param function to apply
* @param seed initial left argument
*/
- FoldRightHelper(BinaryFunction function, Object seed) {
- this(function);
+ FoldRightHelper(T seed) {
this.seed = seed;
hasSeed = true;
}
@@ -115,7 +59,7 @@
/**
* {@inheritDoc}
*/
- public void run(Object obj) {
+ public void run(T obj) {
stk.push(obj);
}
@@ -124,8 +68,8 @@
* Get current result.
* @return Object
*/
- Object getResult() {
- Object right = seed;
+ T getResult() {
+ T right = seed;
if (!hasSeed) {
if (stk.isEmpty()) {
return null;
@@ -139,4 +83,56 @@
}
}
+
+ private BinaryFunction<? super T, ? super T, ? extends T> function;
+
+ /**
+ * Create a new FoldLeft.
+ * @param function {@link BinaryFunction} to apply to each (seed, next)
+ */
+ public FoldRight(BinaryFunction<? super T, ? super T, ? extends T> function) {
+ this.function = function;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param obj {@link Generator} to transform
+ */
+ public T evaluate(Generator<T> obj) {
+ FoldRightHelper helper = new FoldRightHelper();
+ obj.run(helper);
+ return helper.getResult();
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param left {@link Generator} to transform
+ * @param right seed object
+ */
+ public T evaluate(Generator<T> left, T right) {
+ FoldRightHelper helper = new FoldRightHelper(right);
+ left.run(helper);
+ return helper.getResult();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof FoldRight == false) {
+ return false;
+ }
+ return ((FoldRight<?>) obj).function.equals(function);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return "FoldRight".hashCode() << 2 ^ function.hashCode();
+ }
+
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java Mon Jun 9 10:17:39 2008
@@ -28,28 +28,29 @@
*
* @version $Revision$ $Date$
*/
-public final class GeneratorContains implements BinaryPredicate, Serializable {
- private static final GeneratorContains INSTANCE = new GeneratorContains();
+public final class GeneratorContains<T> implements BinaryPredicate<Generator<? extends T>, UnaryPredicate<? super T>>,
+ Serializable {
+ private static final GeneratorContains<Object> INSTANCE = new GeneratorContains<Object>();
/**
* Helper procedure.
*/
- private class ContainsProcedure implements UnaryProcedure {
+ private class ContainsProcedure implements UnaryProcedure<T> {
private boolean found;
- private UnaryPredicate pred;
+ private UnaryPredicate<? super T> pred;
/**
* Create a new ContainsProcedure.
* @pred test
*/
- public ContainsProcedure(UnaryPredicate pred) {
+ public ContainsProcedure(UnaryPredicate<? super T> pred) {
this.pred = pred;
}
/**
* {@inheritDoc}
*/
- public void run(Object obj) {
+ public void run(T obj) {
found |= pred.test(obj);
}
}
@@ -59,9 +60,9 @@
* @param left Generator
* @param right UnaryPredicate
*/
- public boolean test(Object left, Object right) {
- ContainsProcedure findProcedure = new ContainsProcedure((UnaryPredicate) right);
- ((Generator) left).run(findProcedure);
+ public boolean test(Generator<? extends T> left, UnaryPredicate<? super T> right) {
+ ContainsProcedure findProcedure = new ContainsProcedure(right);
+ left.run(findProcedure);
return findProcedure.found;
}
@@ -83,7 +84,7 @@
* Get a static {@link GeneratorContains} instance.
* @return {@link GeneratorContains}
*/
- public static GeneratorContains instance() {
+ public static GeneratorContains<Object> instance() {
return INSTANCE;
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java Mon Jun 9 10:17:39 2008
@@ -27,19 +27,17 @@
*
* @version $Revision$ $Date$
*/
-public final class InPlaceTransform implements BinaryProcedure, Serializable {
- private static final InPlaceTransform INSTANCE = new InPlaceTransform();
+public final class InPlaceTransform<T> implements BinaryProcedure<ListIterator<T>, UnaryFunction<? super T, ? extends T>>, Serializable {
+ private static final InPlaceTransform<Object> INSTANCE = new InPlaceTransform<Object>();
/**
* {@inheritDoc}
* @param left {@link ListIterator}
* @param right {@link UnaryFunction}
*/
- public void run(Object left, Object right) {
- ListIterator li = (ListIterator) left;
- UnaryFunction func = (UnaryFunction) right;
- while (li.hasNext()) {
- li.set(func.evaluate(li.next()));
+ public void run(ListIterator<T> left, UnaryFunction<? super T, ? extends T> right) {
+ while (left.hasNext()) {
+ left.set(right.evaluate(left.next()));
}
}
@@ -61,7 +59,7 @@
* Get an {@link InPlaceTransform} instance.
* @return InPlaceTransform
*/
- public static InPlaceTransform instance() {
+ public static InPlaceTransform<Object> instance() {
return INSTANCE;
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java Mon Jun 9 10:17:39 2008
@@ -28,31 +28,34 @@
*
* @version $Revision$ $Date$
*/
-public final class IndexOfInGenerator implements BinaryFunction, Serializable {
- private static final IndexOfInGenerator INSTANCE = new IndexOfInGenerator();
+public final class IndexOfInGenerator<T> implements BinaryFunction<Generator<? extends T>, UnaryPredicate<? super T>, Number>, Serializable {
+ private static final IndexOfInGenerator<Object> INSTANCE = new IndexOfInGenerator<Object>();
/**
* Helper procedure.
*/
- private class IndexProcedure implements UnaryProcedure {
- private int index = -1;
- private int current = 0;
- private UnaryPredicate pred;
+ private class IndexProcedure implements UnaryProcedure<T> {
+ private Generator<? extends T> generator;
+ private long index = -1L;
+ private long current = 0L;
+ private UnaryPredicate<? super T> pred;
/**
* Create a new IndexProcedure.
* @pred test
*/
- public IndexProcedure(UnaryPredicate pred) {
+ IndexProcedure(Generator<? extends T> generator, UnaryPredicate<? super T> pred) {
+ this.generator = generator;
this.pred = pred;
}
/**
* {@inheritDoc}
*/
- public void run(Object obj) {
+ public void run(T obj) {
if (index < 0 && pred.test(obj)) {
index = current;
+ generator.stop();
}
current++;
}
@@ -63,10 +66,10 @@
* @param left Generator
* @param right UnaryPredicate
*/
- public Object evaluate(Object left, Object right) {
- IndexProcedure findProcedure = new IndexProcedure((UnaryPredicate) right);
- ((Generator) left).run(findProcedure);
- return new Integer(findProcedure.index);
+ public Number evaluate(Generator<? extends T> left, UnaryPredicate<? super T> right) {
+ IndexProcedure findProcedure = new IndexProcedure(left, right);
+ left.run(findProcedure);
+ return findProcedure.index;
}
/**
@@ -87,7 +90,7 @@
* Get a static {@link IndexOfInGenerator} instance.
* @return {@link IndexOfInGenerator}
*/
- public static IndexOfInGenerator instance() {
+ public static IndexOfInGenerator<Object> instance() {
return INSTANCE;
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java Mon Jun 9 10:17:39 2008
@@ -31,7 +31,7 @@
private Predicate test;
/**
- * Create a new DoWithTest.
+ * Create a new PredicatedLoop.
* @param body to execute
* @param test whether to keep going
*/
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java Mon Jun 9 10:17:39 2008
@@ -26,16 +26,16 @@
* is executed. Functions are executed until a non function value or a
* function of a type other than that expected is returned.
*/
-public class RecursiveEvaluation implements Function, Serializable {
- private Function function;
- private Class functionType;
+public class RecursiveEvaluation implements Function<Object>, Serializable {
+ private Function<?> function;
+ private Class<?> functionType;
/**
* Create a new RecursiveEvaluation. Recursion will continue while the
* returned value is of the same runtime class as <code>function</code>.
* @param function initial, potentially recursive Function
*/
- public RecursiveEvaluation(Function function) {
+ public RecursiveEvaluation(Function<?> function) {
this(function, getClass(function));
}
@@ -44,7 +44,7 @@
* @param function initial, potentially recursive Function
* @param functionType as long as result is an instance, keep processing.
*/
- public RecursiveEvaluation(Function function, Class functionType) {
+ public RecursiveEvaluation(Function<?> function, Class<?> functionType) {
if (function == null) {
throw new IllegalArgumentException("Function argument was null");
}
@@ -65,7 +65,7 @@
while (true) {
result = function.evaluate();
if (functionType.isInstance(result)) {
- function = (Function) result;
+ function = (Function<?>) result;
continue;
} else {
break;
@@ -99,7 +99,7 @@
* @param o Object to check
* @return Class found
*/
- private static Class getClass(Object o) {
- return o == null ? null : o.getClass();
+ private static <T> Class<?> getClass(Function<?> f) {
+ return f == null ? null : f.getClass();
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java Mon Jun 9 10:17:39 2008
@@ -27,19 +27,18 @@
*
* @version $Revision$ $Date$
*/
-public final class RemoveMatching implements BinaryProcedure, Serializable {
- private static final RemoveMatching INSTANCE = new RemoveMatching();
+public final class RemoveMatching<T> implements BinaryProcedure<Iterator<T>, UnaryPredicate<? super T>>, Serializable {
+ private static final RemoveMatching<Object> INSTANCE = new RemoveMatching<Object>();
/**
* {@inheritDoc}
* @param left {@link Iterator}
* @param right {@link UnaryPredicate}
*/
- public void run(Object left, Object right) {
- UnaryPredicate test = (UnaryPredicate) right;
- for (Iterator iter = (Iterator) left; iter.hasNext();) {
- if (test.test(iter.next())) {
- iter.remove();
+ public void run(Iterator<T> left, UnaryPredicate<? super T> right) {
+ while (left.hasNext()) {
+ if (right.test(left.next())) {
+ left.remove();
}
}
}
@@ -62,7 +61,7 @@
* Get a static {@link RemoveMatching} instance.
* @return {@link RemoveMatching}
*/
- public static final RemoveMatching instance() {
+ public static final RemoveMatching<Object> instance() {
return INSTANCE;
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java Mon Jun 9 10:17:39 2008
@@ -28,16 +28,18 @@
*
* @version $Revision$ $Date$
*/
-public final class RetainMatching implements BinaryProcedure, Serializable {
- private static final RetainMatching INSTANCE = new RetainMatching();
+public final class RetainMatching<T> implements BinaryProcedure<Iterator<T>, UnaryPredicate<? super T>>, Serializable {
+ private static final RetainMatching<Object> INSTANCE = new RetainMatching<Object>();
+
+ private RemoveMatching<T> removeMatching = new RemoveMatching<T>();
/**
* {@inheritDoc}
* @param left {@link Iterator}
* @param right {@link UnaryPredicate}
*/
- public void run(Object left, Object right) {
- RemoveMatching.instance().run(left, new UnaryNot((UnaryPredicate) right));
+ public void run(Iterator<T> left, UnaryPredicate<? super T> right) {
+ removeMatching.run(left, UnaryNot.not(right));
}
/**
@@ -58,7 +60,7 @@
* Get a static {@link RetainMatching} instance.
* @return {@link RetainMatching}
*/
- public static final RetainMatching instance() {
+ public static final RetainMatching<Object> instance() {
return INSTANCE;
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/FilteredIterator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/FilteredIterator.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/FilteredIterator.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/FilteredIterator.java Mon Jun 9 10:17:39 2008
@@ -27,13 +27,13 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class FilteredIterator implements Iterator {
+public final class FilteredIterator<T> implements Iterator<T> {
// attributes
// ------------------------------------------------------------------------
- private UnaryPredicate predicate = null;
- private Iterator iterator = null;
- private Object next = null;
+ private UnaryPredicate<? super T> predicate = null;
+ private Iterator<? extends T> iterator = null;
+ private T next = null;
private boolean nextSet = false;
private boolean canRemove = false;
@@ -44,7 +44,7 @@
* @param iterator to filter
* @param predicate to apply
*/
- public FilteredIterator(Iterator iterator, UnaryPredicate predicate) {
+ public FilteredIterator(Iterator<? extends T> iterator, UnaryPredicate<? super T> predicate) {
if (null == iterator) {
throw new IllegalArgumentException("Iterator argument was null");
}
@@ -63,23 +63,18 @@
* @see java.util.Iterator#hasNext()
*/
public boolean hasNext() {
- if (nextSet) {
- return true;
- } else {
- return setNext();
- }
+ return nextSet || setNext();
}
/**
* {@inheritDoc}
* @see java.util.Iterator#next()
*/
- public Object next() {
+ public T next() {
if (hasNext()) {
return returnNext();
- } else {
- throw new NoSuchElementException();
}
+ throw new NoSuchElementException();
}
/**
@@ -105,7 +100,7 @@
if (obj instanceof FilteredIterator == false) {
return false;
}
- FilteredIterator that = (FilteredIterator) obj;
+ FilteredIterator<?> that = (FilteredIterator<?>) obj;
return predicate.equals(that.predicate) && iterator.equals(that.iterator);
}
@@ -136,8 +131,8 @@
* @param pred to apply
* @return Iterator
*/
- public static Iterator filter(Iterator iter, UnaryPredicate pred) {
- return null == pred ? iter : (null == iter ? null : new FilteredIterator(iter, pred));
+ public static <T> Iterator<T> filter(Iterator<? extends T> iter, UnaryPredicate<? super T> pred) {
+ return null == pred ? (Iterator<T>) iter : (null == iter ? null : new FilteredIterator<T>(iter, pred));
}
// private
@@ -149,7 +144,7 @@
private boolean setNext() {
while (iterator.hasNext()) {
canRemove = false;
- Object obj = iterator.next();
+ T obj = iterator.next();
if (predicate.test(obj)) {
nextSet = true;
next = obj;
@@ -165,8 +160,8 @@
* Get the next element.
* @return next element.
*/
- private Object returnNext() {
- Object temp = next;
+ private T returnNext() {
+ T temp = next;
canRemove = true;
next = null;
nextSet = false;
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/IsElementOf.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/IsElementOf.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/IsElementOf.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/IsElementOf.java Mon Jun 9 10:17:39 2008
@@ -34,11 +34,11 @@
* @author Jason Horman (jason@jhorman.org)
* @author Rodney Waldhoff
*/
-public final class IsElementOf implements BinaryPredicate, Serializable {
+public final class IsElementOf<L, R> implements BinaryPredicate<L, R>, Serializable {
// static members
//---------------------------------------------------------------
- private static IsElementOf INSTANCE = new IsElementOf();
+ private static IsElementOf<Object, Object> INSTANCE = new IsElementOf<Object, Object>();
// constructors
//---------------------------------------------------------------
@@ -53,16 +53,17 @@
/**
* {@inheritDoc}
*/
- public boolean test(Object obj, Object col) {
+ public boolean test(L obj, R col) {
if (col instanceof Collection) {
- return testCollection(obj, (Collection) col);
- } else if (null != col && col.getClass().isArray()) {
+ return testCollection(obj, (Collection<?>) col);
+ }
+ if (null != col && col.getClass().isArray()) {
return testArray(obj, col);
- } else if (null == col) {
- throw new NullPointerException("Right side argument must not be null.");
- } else {
- throw new IllegalArgumentException("Expected Collection or Array, found " + col.getClass());
}
+ if (null == col) {
+ throw new IllegalArgumentException("Right side argument must not be null.");
+ }
+ throw new IllegalArgumentException("Expected Collection or Array, found " + col.getClass());
}
/**
@@ -92,7 +93,7 @@
* @param col to search
* @return boolean
*/
- private boolean testCollection(Object obj, Collection col) {
+ private boolean testCollection(Object obj, Collection<?> col) {
return col.contains(obj);
}
@@ -121,7 +122,7 @@
* Get an IsElementOf instance.
* @return IsElementOf
*/
- public static IsElementOf instance() {
+ public static IsElementOf<Object, Object> instance() {
return INSTANCE;
}
@@ -130,13 +131,13 @@
* @param obj collection/array to search
* @return UnaryPredicate
*/
- public static UnaryPredicate instance(Object obj) {
+ public static <A> UnaryPredicate<A> instance(Object obj) {
if (null == obj) {
throw new NullPointerException("Argument must not be null");
} else if (obj instanceof Collection) {
- return new RightBoundPredicate(instance(), obj);
+ return new RightBoundPredicate<A, Object>(new IsElementOf<A, Object>(), obj);
} else if (obj.getClass().isArray()) {
- return new RightBoundPredicate(instance(), obj);
+ return new RightBoundPredicate<A, Object>(new IsElementOf<A, Object>(), obj);
} else {
throw new IllegalArgumentException("Expected Collection or Array, found " + obj.getClass());
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/IsEmpty.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/IsEmpty.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/IsEmpty.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/IsEmpty.java Mon Jun 9 10:17:39 2008
@@ -27,39 +27,43 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class IsEmpty implements UnaryPredicate, Serializable {
+public final class IsEmpty<A> implements UnaryPredicate<A>, Serializable {
// class variables
// ------------------------------------------------------------------------
- private static final IsEmpty INSTANCE = new IsEmpty();
+ private static final IsEmpty<Object> INSTANCE = new IsEmpty<Object>();
// constructor
// ------------------------------------------------------------------------
/**
* Create a new IsEmpty.
*/
- public IsEmpty() { }
+ public IsEmpty() {
+ }
// instance methods
// ------------------------------------------------------------------------
/**
* {@inheritDoc}
*/
- public boolean test(Object obj) {
+ public boolean test(A obj) {
if (obj instanceof Collection) {
- return testCollection((Collection) obj);
- } else if (obj instanceof Map) {
- return testMap((Map) obj);
- } else if (obj instanceof String) {
+ return testCollection((Collection<?>) obj);
+ }
+ if (obj instanceof Map) {
+ return testMap((Map<?, ?>) obj);
+ }
+ if (obj instanceof String) {
return testString((String) obj);
- } else if (null != obj && obj.getClass().isArray()) {
+ }
+ if (null != obj && obj.getClass().isArray()) {
return testArray(obj);
- } else if (null == obj) {
- throw new NullPointerException("Argument must not be null");
- } else {
- throw new IllegalArgumentException("Expected Collection, Map, String or Array, found " + obj.getClass());
}
+ if (null == obj) {
+ throw new IllegalArgumentException("Argument must not be null");
+ }
+ throw new IllegalArgumentException("Expected Collection, Map, String or Array, found " + obj.getClass());
}
/**
@@ -88,7 +92,7 @@
* @param col to test
* @return boolean
*/
- private boolean testCollection(Collection col) {
+ private boolean testCollection(Collection<?> col) {
return col.isEmpty();
}
@@ -97,7 +101,7 @@
* @param map to test
* @return boolean
*/
- private boolean testMap(Map map) {
+ private boolean testMap(Map<?, ?> map) {
return map.isEmpty();
}
@@ -125,7 +129,7 @@
* Get an IsEmpty instance.
* @return IsEmpty
*/
- public static final IsEmpty instance() {
+ public static final IsEmpty<Object> instance() {
return INSTANCE;
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/Size.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/Size.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/Size.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/Size.java Mon Jun 9 10:17:39 2008
@@ -28,9 +28,9 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class Size implements UnaryFunction, Serializable {
+public final class Size<A> implements UnaryFunction<A, Integer>, Serializable {
- private static final Size INSTANCE = new Size();
+ private static final Size<Object> INSTANCE = new Size<Object>();
// constructor
// ------------------------------------------------------------------------
@@ -42,18 +42,20 @@
/**
* {@inheritDoc}
*/
- public Object evaluate(Object obj) {
+ public Integer evaluate(Object obj) {
if (obj instanceof Collection) {
- return evaluate((Collection) obj);
- } else if (obj instanceof String) {
+ return evaluate((Collection<?>) obj);
+ }
+ if (obj instanceof String) {
return evaluate((String) obj);
- } else if (null != obj && obj.getClass().isArray()) {
+ }
+ if (null != obj && obj.getClass().isArray()) {
return evaluateArray(obj);
- } else if (null == obj) {
- throw new NullPointerException("Argument must not be null");
- } else {
- throw new ClassCastException("Expected Collection, String or Array, found " + obj);
}
+ if (null == obj) {
+ throw new IllegalArgumentException("Argument must not be null");
+ }
+ throw new IllegalArgumentException("Expected Collection, String or Array, found " + obj);
}
/**
@@ -81,7 +83,7 @@
* Get a Size instance.
* @return Size
*/
- public static final Size instance() {
+ public static final Size<Object> instance() {
return INSTANCE;
}
@@ -90,8 +92,8 @@
* @param col to evaluate
* @return Integer
*/
- private Object evaluate(Collection col) {
- return new Integer(col.size());
+ private int evaluate(Collection<?> col) {
+ return col.size();
}
/**
@@ -99,8 +101,8 @@
* @param str to evaluate
* @return Integer
*/
- private Object evaluate(String str) {
- return new Integer(str.length());
+ private int evaluate(String str) {
+ return str.length();
}
/**
@@ -108,8 +110,8 @@
* @param array to evaluate
* @return Integer
*/
- private Object evaluateArray(Object array) {
- return new Integer(Array.getLength(array));
+ private int evaluateArray(Object array) {
+ return Array.getLength(array);
}
}
Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/TransformedIterator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/TransformedIterator.java?rev=665786&r1=665785&r2=665786&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/TransformedIterator.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/collection/TransformedIterator.java Mon Jun 9 10:17:39 2008
@@ -25,13 +25,13 @@
* @version $Revision$ $Date$
* @author Rodney Waldhoff
*/
-public final class TransformedIterator implements Iterator {
+public final class TransformedIterator<E, T> implements Iterator<T> {
// attributes
// ------------------------------------------------------------------------
- private UnaryFunction function = null;
- private Iterator iterator = null;
+ private UnaryFunction<? super E, ? extends T> function = null;
+ private Iterator<? extends E> iterator = null;
// constructor
// ------------------------------------------------------------------------
@@ -40,7 +40,7 @@
* @param iterator Iterator to decorate
* @param function to apply
*/
- public TransformedIterator(Iterator iterator, UnaryFunction function) {
+ public TransformedIterator(Iterator<? extends E> iterator, UnaryFunction<? super E, ? extends T> function) {
if (null == iterator) {
throw new IllegalArgumentException("Iterator argument was null");
}
@@ -66,7 +66,7 @@
* {@inheritDoc}
* @see java.util.Iterator#next()
*/
- public Object next() {
+ public T next() {
return function.evaluate(iterator.next());
}
@@ -88,7 +88,7 @@
if (obj instanceof TransformedIterator == false) {
return false;
}
- TransformedIterator that = (TransformedIterator) obj;
+ TransformedIterator<?, ?> that = (TransformedIterator<?, ?>) obj;
return function.equals(that.function) && iterator.equals(that.iterator);
}
@@ -114,13 +114,26 @@
// class methods
// ------------------------------------------------------------------------
/**
- * Get a TransformedIterator instance.
- * @param iter to decorate
- * @param func transforming function
- * @return Iterator
- */
- public static Iterator transform(Iterator iter, UnaryFunction func) {
- return null == func ? iter : (null == iter ? null : new TransformedIterator(iter, func));
+ * Get a Transformed Iterator instance.
+ * @param iter to decorate, if null result is null
+ * @param func transforming function, cannot be null
+ * @return Iterator<T>
+ */
+ public static <E, T> Iterator<T> transform(Iterator<? extends E> iter, UnaryFunction<? super E, ? extends T> func) {
+ if (null == iter) {
+ return null;
+ }
+ return new TransformedIterator<E, T>(iter, func);
}
+ /**
+ * Get an Iterator instance that may be transformed.
+ * @param iter to decorate, if null result is null
+ * @param func transforming function, if null result is iter
+ * @return Iterator<?>
+ */
+ public static <E> Iterator<?> maybeTransform(Iterator<? extends E> iter, UnaryFunction<? super E, ?> func) {
+ return null == func ? (null == iter ? null : iter) : new TransformedIterator<E, Object>(iter, func);
+ }
+
}