You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2013/07/31 00:48:08 UTC
svn commit: r1508677 [4/9] - in /commons/proper/functor/trunk:
api/src/main/java/org/apache/commons/functor/
core/src/main/java/org/apache/commons/functor/adapter/
core/src/main/java/org/apache/commons/functor/aggregator/
core/src/main/java/org/apache/...
Modified: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/comparator/Min.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/comparator/Min.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/comparator/Min.java (original)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/comparator/Min.java Tue Jul 30 22:48:02 2013
@@ -20,7 +20,7 @@ import java.io.Serializable;
import java.util.Comparator;
import org.apache.commons.functor.BinaryFunction;
-import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.Function;
import org.apache.commons.functor.adapter.RightBoundFunction;
import org.apache.commons.lang3.Validate;
@@ -115,13 +115,13 @@ public final class Min<T> implements Bin
}
/**
- * Get a Min UnaryFunction.
+ * Get a Min Function.
*
* @param <T> the binary function arguments and return types.
* @param right the right side argument of the Min function
- * @return UnaryFunction<T, T>
+ * @return Function<T, T>
*/
- public static <T extends Comparable<?>> UnaryFunction<T, T> instance(T right) {
+ public static <T extends Comparable<?>> Function<T, T> instance(T right) {
return RightBoundFunction.bind(new Min<T>(), right);
}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/AbstractLoopNullaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/AbstractLoopNullaryProcedure.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/AbstractLoopNullaryProcedure.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/AbstractLoopNullaryProcedure.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.functor.NullaryProcedure;
+import org.apache.commons.lang3.Validate;
+
+import java.io.Serializable;
+
+/**
+ * Abstract base class for {@link WhileDoNullaryProcedure} and {@link DoWhileNullaryProcedure}
+ * used to implement loop procedures.
+ * <p>
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public abstract class AbstractLoopNullaryProcedure implements NullaryProcedure, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = -5903381842630236070L;
+
+ /** Base hash integer used to shift hash. */
+ private static final int HASH_SHIFT = 4;
+
+ /**
+ * The condition has to be verified that while true,
+ * forces the action repetition.
+ */
+ private final NullaryPredicate condition;
+
+ /**
+ * The action to be repeated until the condition is true.
+ */
+ private final NullaryProcedure action;
+
+ /**
+ * Create a new AbstractLoopNullaryProcedure.
+ * @param condition while true, repeat
+ * @param action loop body
+ */
+ protected AbstractLoopNullaryProcedure(NullaryPredicate condition, NullaryProcedure action) {
+ this.condition = Validate.notNull(condition, "NullaryProcedure argument must not be null");
+ this.action = Validate.notNull(action, "NullaryProcedure argument must not be null");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public final boolean equals(Object object) {
+ if (object == this) {
+ return true;
+ }
+ if (!(object instanceof AbstractLoopNullaryProcedure)) {
+ return false;
+ }
+ AbstractLoopNullaryProcedure that = (AbstractLoopNullaryProcedure) object;
+ return (getCondition().equals(that.getCondition())
+ && (getAction().equals(that.getAction())));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ return hashCode("AbstractLoopNullaryProcedure".hashCode());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return getClass().getName() + "<" + getCondition() + "," + getAction() + ">";
+ }
+
+ /**
+ * Create a hashCode by manipulating an input hashCode and factoring in instance state.
+ * @param hash incoming hashCode
+ * @return int
+ */
+ protected int hashCode(int hash) {
+ hash <<= HASH_SHIFT;
+ hash ^= getAction().hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= getCondition().hashCode();
+ return hash;
+ }
+
+ /**
+ * Get the condition.
+ * @return NullaryPredicate
+ */
+ protected final NullaryPredicate getCondition() {
+ return condition;
+ }
+
+ /**
+ * Get the action.
+ * @return NullaryProcedure
+ */
+ protected final NullaryProcedure getAction() {
+ return action;
+ }
+
+}
Modified: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/And.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/And.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/And.java (original)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/And.java Tue Jul 30 22:48:02 2013
@@ -31,18 +31,18 @@ import org.apache.commons.functor.Predic
* an instance whose delegates are not all
* <code>Serializable</code> will result in an exception.
* </p>
+ * @param <A> the predicate argument type.
* @version $Revision$ $Date$
*/
-public final class And extends BasePredicateList {
-
- // constructor
- // ------------------------------------------------------------------------
+public final class And<A> extends BasePredicateList<A> {
/**
* serialVersionUID declaration.
*/
- private static final long serialVersionUID = -6053343095016685571L;
+ private static final long serialVersionUID = 8324861737107307302L;
+ // constructor
+ // ------------------------------------------------------------------------
/**
* Create a new And.
*/
@@ -53,29 +53,29 @@ public final class And extends BasePredi
/**
* Create a new And instance.
*
- * @param predicates the predicates to add
+ * @param predicates the predicates to put in and.
*/
- public And(Iterable<Predicate> predicates) {
+ public And(Iterable<Predicate<? super A>> predicates) {
super(predicates);
}
/**
* Create a new And instance.
*
- * @param predicates the predicates to add
+ * @param predicates the predicates to put in and.
*/
- public And(Predicate... predicates) {
+ public And(Predicate<? super A>... predicates) {
super(predicates);
}
// modifiers
// ------------------------------------------------------------------------
/**
- * Add a Predicate.
+ * Fluently add a Predicate.
* @param p Predicate to add
* @return this
*/
- public And and(Predicate p) {
+ public And<A> and(Predicate<? super A> p) {
super.addPredicate(p);
return this;
}
@@ -85,9 +85,9 @@ public final class And extends BasePredi
/**
* {@inheritDoc}
*/
- public boolean test() {
- for (Predicate p : getPredicateList()) {
- if (!p.test()) {
+ public boolean test(A obj) {
+ for (Predicate<? super A> p : getPredicateList()) {
+ if (!p.test(obj)) {
return false;
}
}
@@ -99,15 +99,15 @@ public final class And extends BasePredi
*/
@Override
public boolean equals(Object that) {
- return that == this || (that instanceof And && equals((And) that));
+ return that == this || (that instanceof And<?> && equals((And<?>) that));
}
/**
- * Learn whether a given And is equal to this.
- * @param that the And to test
+ * Learn whether another And is equal to this.
+ * @param that And to test
* @return boolean
*/
- public boolean equals(And that) {
+ public boolean equals(And<?> that) {
return getPredicateListEquals(that);
}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/BaseNullaryPredicateList.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/BaseNullaryPredicateList.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/BaseNullaryPredicateList.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/BaseNullaryPredicateList.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.functor.NullaryPredicate;
+
+/**
+ * Abstract base class for {@link NullaryPredicate NullaryPredicates}
+ * composed of a list of {@link NullaryPredicate NullaryPredicates}.
+ * <p>
+ * Note that although this class implements
+ * {@link Serializable}, a given instance will
+ * only be truly <code>Serializable</code> if all the
+ * underlying functors are. Attempts to serialize
+ * an instance whose delegates are not all
+ * <code>Serializable</code> will result in an exception.
+ * </p>
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+abstract class BaseNullaryPredicateList implements NullaryPredicate, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = 7860902316994888181L;
+ // attributes
+ // ------------------------------------------------------------------------
+ /**
+ * A list where storing the adapted predicates.
+ */
+ private final List<NullaryPredicate> list = new ArrayList<NullaryPredicate>();
+
+ // constructor
+ // ------------------------------------------------------------------------
+ /**
+ * Create a new BaseNullaryPredicateList instance.
+ */
+ protected BaseNullaryPredicateList() {
+ super();
+ }
+
+ /**
+ * Create a new BaseNullaryPredicateList instance.
+ *
+ * @param predicates to add
+ */
+ protected BaseNullaryPredicateList(NullaryPredicate... predicates) {
+ this();
+ if (predicates != null) {
+ for (NullaryPredicate p : predicates) {
+ addNullaryPredicate(p);
+ }
+ }
+ }
+
+ /**
+ * Create a new BaseNullaryPredicateList instance.
+ *
+ * @param predicates to add
+ */
+ protected BaseNullaryPredicateList(Iterable<NullaryPredicate> predicates) {
+ this();
+ if (predicates != null) {
+ for (NullaryPredicate p : predicates) {
+ addNullaryPredicate(p);
+ }
+ }
+ }
+
+ // abstract
+ // ------------------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public abstract boolean equals(Object that);
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public abstract int hashCode();
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public abstract String toString();
+
+ // modifiers
+ // ------------------------------------------------------------------------
+ /**
+ * Add a NullaryPredicate to the list.
+ * @param p NullaryPredicate to add
+ */
+ protected void addNullaryPredicate(NullaryPredicate p) {
+ if (p != null) {
+ list.add(p);
+ }
+ }
+
+ // protected
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get the "live" list of {@link NullaryPredicate}s.
+ * @return List<NullaryPredicate>
+ */
+ protected List<NullaryPredicate> getNullaryPredicateList() {
+ return list;
+ }
+
+ /**
+ * Learn whether the list of another BaseNullaryPredicateList is equal to my list.
+ * @param that BaseNullaryPredicateList to test
+ * @return boolean
+ */
+ protected boolean getNullaryPredicateListEquals(BaseNullaryPredicateList that) {
+ return (null != that && this.list.equals(that.list));
+ }
+
+ /**
+ * Get a hashCode for my list.
+ * @return int
+ */
+ protected int getNullaryPredicateListHashCode() {
+ return list.hashCode();
+ }
+
+ /**
+ * Get a toString for my list.
+ * @return String
+ */
+ protected String getNullaryPredicateListToString() {
+ return String.valueOf(list);
+ }
+
+}
Modified: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/BasePredicateList.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/BasePredicateList.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/BasePredicateList.java (original)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/BasePredicateList.java Tue Jul 30 22:48:02 2013
@@ -33,24 +33,26 @@ import org.apache.commons.functor.Predic
* an instance whose delegates are not all
* <code>Serializable</code> will result in an exception.
* </p>
+ * @param <A> the predicate argument type.
* @version $Revision$ $Date$
*/
-abstract class BasePredicateList implements Predicate, Serializable {
+abstract class BasePredicateList<A> implements Predicate<A>, Serializable {
+
/**
* serialVersionUID declaration.
*/
- private static final long serialVersionUID = 7860902316994888181L;
+ private static final long serialVersionUID = 1467575113401282954L;
// attributes
// ------------------------------------------------------------------------
/**
* A list where storing the adapted predicates.
*/
- private final List<Predicate> list = new ArrayList<Predicate>();
+ private final List<Predicate<? super A>> list = new ArrayList<Predicate<? super A>>();
// constructor
// ------------------------------------------------------------------------
/**
- * Create a new BasePredicateList instance.
+ * Create a new BasePredicateList.
*/
protected BasePredicateList() {
super();
@@ -61,10 +63,10 @@ abstract class BasePredicateList impleme
*
* @param predicates to add
*/
- protected BasePredicateList(Predicate... predicates) {
+ protected BasePredicateList(Predicate<? super A>... predicates) {
this();
if (predicates != null) {
- for (Predicate p : predicates) {
+ for (Predicate<? super A> p : predicates) {
addPredicate(p);
}
}
@@ -75,10 +77,10 @@ abstract class BasePredicateList impleme
*
* @param predicates to add
*/
- protected BasePredicateList(Iterable<Predicate> predicates) {
+ protected BasePredicateList(Iterable<Predicate<? super A>> predicates) {
this();
if (predicates != null) {
- for (Predicate p : predicates) {
+ for (Predicate<? super A> p : predicates) {
addPredicate(p);
}
}
@@ -110,7 +112,7 @@ abstract class BasePredicateList impleme
* Add a Predicate to the list.
* @param p Predicate to add
*/
- protected void addPredicate(Predicate p) {
+ protected void addPredicate(Predicate<? super A> p) {
if (p != null) {
list.add(p);
}
@@ -120,24 +122,24 @@ abstract class BasePredicateList impleme
// ------------------------------------------------------------------------
/**
- * Get the "live" list of {@link Predicate}s.
- * @return List<Predicate>
+ * Get the "live" list of contained {@link Predicate}s.
+ * @return List
*/
- protected List<Predicate> getPredicateList() {
+ protected List<Predicate<? super A>> getPredicateList() {
return list;
}
/**
- * Learn whether the list of another BasePredicateList is equal to my list.
- * @param that BasePredicateList to test
+ * Learn whether another BasePredicateList has content equal to this.
+ * @param that the BasePredicateList to test
* @return boolean
*/
- protected boolean getPredicateListEquals(BasePredicateList that) {
+ protected boolean getPredicateListEquals(BasePredicateList<?> that) {
return (null != that && this.list.equals(that.list));
}
/**
- * Get a hashCode for my list.
+ * Get a hashCode for the list.
* @return int
*/
protected int getPredicateListHashCode() {
@@ -145,7 +147,7 @@ abstract class BasePredicateList impleme
}
/**
- * Get a toString for my list.
+ * Get a toString for the list.
* @return String
*/
protected String getPredicateListToString() {
Modified: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/Composite.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/Composite.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/Composite.java (original)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/Composite.java Tue Jul 30 22:48:02 2013
@@ -18,9 +18,9 @@ package org.apache.commons.functor.core.
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.BinaryPredicate;
-import org.apache.commons.functor.UnaryFunction;
-import org.apache.commons.functor.UnaryPredicate;
-import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.Function;
+import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.Procedure;
/**
* Utility/fluent methods for creating composite functors.
@@ -40,49 +40,49 @@ public final class Composite {
public Composite() { }
/**
- * Create a composite UnaryProcedure.
+ * Create a composite Procedure.
* @param <A> the procedure argument type.
- * @param procedure UnaryProcedure to execute against output of <code>f</code>
- * @return CompositeUnaryProcedure<A>
+ * @param procedure Procedure to execute against output of <code>f</code>
+ * @return CompositeProcedure<A>
*/
- public static <A> CompositeUnaryProcedure<A> procedure(UnaryProcedure<? super A> procedure) {
- return new CompositeUnaryProcedure<A>(procedure);
+ public static <A> CompositeProcedure<A> procedure(Procedure<? super A> procedure) {
+ return new CompositeProcedure<A>(procedure);
}
/**
- * Create a composite UnaryProcedure.
+ * Create a composite Procedure.
* @param <A> the function argument type.
* @param <T> the the procedure argument type and function returned value type.
- * @param procedure UnaryProcedure to execute against output of <code>f</code>
- * @param function UnaryFunction to apply
- * @return CompositeUnaryProcedure<A>
+ * @param procedure Procedure to execute against output of <code>f</code>
+ * @param function Function to apply
+ * @return CompositeProcedure<A>
*/
- public static <A, T> CompositeUnaryProcedure<A> procedure(UnaryProcedure<? super T> procedure,
- UnaryFunction<? super A, ? extends T> function) {
- return new CompositeUnaryProcedure<T>(procedure).of(function);
+ public static <A, T> CompositeProcedure<A> procedure(Procedure<? super T> procedure,
+ Function<? super A, ? extends T> function) {
+ return new CompositeProcedure<T>(procedure).of(function);
}
/**
- * Create a composite UnaryPredicate.
+ * Create a composite Predicate.
* @param <A> the predicate argument type.
- * @param pred UnaryPredicate to test the output of <code>f</code>
- * @return CompositeUnaryPredicate<A>
+ * @param pred Predicate to test the output of <code>f</code>
+ * @return CompositePredicate<A>
*/
- public static <A> CompositeUnaryPredicate<A> predicate(UnaryPredicate<? super A> pred) {
- return new CompositeUnaryPredicate<A>(pred);
+ public static <A> CompositePredicate<A> predicate(Predicate<? super A> pred) {
+ return new CompositePredicate<A>(pred);
}
/**
- * Create a composite UnaryPredicate.
+ * Create a composite Predicate.
* @param <A> the function argument type.
* @param <T> the predicate argument type and the function returned value type.
- * @param predicate UnaryPredicate to test the output of <code>f</code>
- * @param function UnaryFunction to apply
- * @return CompositeUnaryPredicate<A>
- */
- public static <A, T> CompositeUnaryPredicate<A> predicate(UnaryPredicate<? super T> predicate,
- UnaryFunction<? super A, ? extends T> function) {
- return new CompositeUnaryPredicate<T>(predicate).of(function);
+ * @param predicate Predicate to test the output of <code>f</code>
+ * @param function Function to apply
+ * @return CompositePredicate<A>
+ */
+ public static <A, T> CompositePredicate<A> predicate(Predicate<? super T> predicate,
+ Function<? super A, ? extends T> function) {
+ return new CompositePredicate<T>(predicate).of(function);
}
/**
@@ -92,73 +92,73 @@ public final class Composite {
* @param <G> the input functions left argument type.
* @param <H> the input functions right argument type.
* @param p BinaryPredicate to test <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i>
- * @param g left UnaryFunction
- * @param h right UnaryFunction
+ * @param g left Function
+ * @param h right Function
* @return BinaryPredicate
*/
- public static <L, R, G, H> UnaryCompositeBinaryPredicate<L, R> predicate(
- BinaryPredicate<? super G, ? super H> p, UnaryFunction<? super L, ? extends G> g,
- UnaryFunction<? super R, ? extends H> h) {
- return new UnaryCompositeBinaryPredicate<L, R>(p, g, h);
+ public static <L, R, G, H> CompositeBinaryPredicate<L, R> predicate(
+ BinaryPredicate<? super G, ? super H> p, Function<? super L, ? extends G> g,
+ Function<? super R, ? extends H> h) {
+ return new CompositeBinaryPredicate<L, R>(p, g, h);
}
/**
- * Create a composite UnaryFunction.
+ * Create a composite Function.
* @param <A> the function argument type.
* @param <T> the function returned value type.
- * @param f UnaryFunction to apply to the output of <code>g</code>
- * @return UnaryFunction
+ * @param f Function to apply to the output of <code>g</code>
+ * @return Function
*/
- public static <A, T> CompositeUnaryFunction<A, T> function(UnaryFunction<? super A, ? extends T> f) {
- return new CompositeUnaryFunction<A, T>(f);
+ public static <A, T> CompositeFunction<A, T> function(Function<? super A, ? extends T> f) {
+ return new CompositeFunction<A, T>(f);
}
/**
- * Create a composite UnaryFunction.
+ * Create a composite Function.
* @param <A> the function argument type.
* @param <X> the function argument type.
* @param <T> the function returned value type.
- * @param f UnaryFunction to apply to the output of <code>g</code>
- * @param g UnaryFunction to apply first
- * @return UnaryFunction
- */
- public static <A, X, T> CompositeUnaryFunction<A, T> function(UnaryFunction<? super X, ? extends T> f,
- UnaryFunction<? super A, ? extends X> g) {
- return new CompositeUnaryFunction<X, T>(f).of(g);
+ * @param f Function to apply to the output of <code>g</code>
+ * @param g Function to apply first
+ * @return Function
+ */
+ public static <A, X, T> CompositeFunction<A, T> function(Function<? super X, ? extends T> f,
+ Function<? super A, ? extends X> g) {
+ return new CompositeFunction<X, T>(f).of(g);
}
// /**
-// * Chain a BinaryFunction to a UnaryFunction.
+// * Chain a BinaryFunction to a Function.
// * @param <L>
// * @param <R>
// * @param <X>
// * @param <T>
-// * @param f UnaryFunction to apply to the output of <code>g</code>
+// * @param f Function to apply to the output of <code>g</code>
// * @param g BinaryFunction to apply first
// * @return BinaryFunction<L, R, T>
// */
-// public static <L, R, X, T> BinaryFunction<L, R, T> function(UnaryFunction<? super X, ? extends T> f,
+// public static <L, R, X, T> BinaryFunction<L, R, T> function(Function<? super X, ? extends T> f,
// BinaryFunction<? super L,
// ? super R, ? extends X> g) {
-// return new CompositeUnaryFunction<X, T>(f).of(g);
+// return new CompositeFunction<X, T>(f).of(g);
// }
/**
- * Create a composite<UnaryFunction> BinaryFunction.
+ * Create a composite<Function> BinaryFunction.
* @param <L> the output predicate left argument type.
* @param <R> the output predicate right argument type.
* @param <G> the input functions left argument type.
* @param <H> the input functions right argument type.
* @param <T> the function returned value type.
* @param f BinaryFunction to apply to <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i>
- * @param g left UnaryFunction
- * @param h right UnaryFunction
+ * @param g left Function
+ * @param h right Function
* @return BinaryFunction
*/
- public static <L, R, G, H, T> UnaryCompositeBinaryFunction<L, R, T> function(
- BinaryFunction<? super G, ? super H, ? extends T> f, UnaryFunction<? super L, ? extends G> g,
- UnaryFunction<? super R, ? extends H> h) {
- return new UnaryCompositeBinaryFunction<L, R, T>(f, g, h);
+ public static <L, R, G, H, T> CompositeBinaryFunction<L, R, T> function(
+ BinaryFunction<? super G, ? super H, ? extends T> f, Function<? super L, ? extends G> g,
+ Function<? super R, ? extends H> h) {
+ return new CompositeBinaryFunction<L, R, T>(f, g, h);
}
/**
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeBinaryFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeBinaryFunction.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeBinaryFunction.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeBinaryFunction.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.Function;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A {@link BinaryFunction BinaryFunction} composed of
+ * one binary function, <i>f</i>, and two
+ * functions, <i>g</i> and <i>h</i>,
+ * evaluating the ordered parameters <i>x</i>, <i>y</i>
+ * to <code><i>f</i>(<i>g</i>(<i>x</i>),<i>h</i>(<i>y</i>))</code>.
+ * <p>
+ * Note that although this class implements
+ * {@link Serializable}, a given instance will
+ * only be truly <code>Serializable</code> if all the
+ * underlying functors are. Attempts to serialize
+ * an instance whose delegates are not all
+ * <code>Serializable</code> will result in an exception.
+ * </p>
+ * @param <L> the left argument type.
+ * @param <R> the right argument type.
+ * @param <T> the returned value type.
+ * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
+ */
+public class CompositeBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
+
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = 264219357293822629L;
+
+ /** Base hash integer used to shift hash. */
+ private static final int HASH_SHIFT = 4;
+
+ /**
+ *
+ *
+ * @param <G> the adapted function left argument type.
+ * @param <H> the adapted function right argument type.
+ * @param <L> the left argument type.
+ * @param <R> the right argument type.
+ * @param <T> the returned value type.
+ */
+ private static class Helper<G, H, L, R, T> implements BinaryFunction<L, R, T>, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = 4513309646430305164L;
+ /**
+ * The adapted function to receive <code>(output(g), output(h))</code>.
+ */
+ private BinaryFunction<? super G, ? super H, ? extends T> f;
+ /**
+ * The adapted left function.
+ */
+ private Function<? super L, ? extends G> g;
+ /**
+ * The adapted right function.
+ */
+ private Function<? super R, ? extends H> h;
+
+ /**
+ * Create a new Helper.
+ * @param f BinaryFunction to receive <code>(output(g), output(h))</code>
+ * @param g left Function
+ * @param h right Function
+ */
+ public Helper(BinaryFunction<? super G, ? super H, ? extends T> f, Function<? super L, ? extends G> g,
+ Function<? super R, ? extends H> h) {
+ this.f = f;
+ this.g = g;
+ this.h = h;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public T evaluate(L left, R right) {
+ return f.evaluate(g.evaluate(left), h.evaluate(right));
+ }
+ }
+
+ /**
+ * The adapted helper.
+ */
+ private final Helper<?, ?, L, R, T> helper;
+
+ // constructor
+ // ------------------------------------------------------------------------
+ /**
+ * Create a new CompositeBinaryFunction.
+ *
+ * @param <G> the adapted function left argument type.
+ * @param <H> the adapted function right argument type.
+ * @param f BinaryFunction to receive <code>(output(g), output(h))</code>
+ * @param g left Function
+ * @param h right Function
+ */
+ public <G, H> CompositeBinaryFunction(BinaryFunction<? super G, ? super H, ? extends T> f,
+ Function<? super L, ? extends G> g, Function<? super R, ? extends H> h) {
+ this.helper = new Helper<G, H, L, R, T>(
+ Validate.notNull(f, "BinaryFunction must not be null"),
+ Validate.notNull(g, "left Function must not be null"),
+ Validate.notNull(h, "right Function must not be null")
+ );
+ }
+
+ // function interface
+ // ------------------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public T evaluate(L left, R right) {
+ return helper.evaluate(left, right);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object that) {
+ return that == this || (that instanceof CompositeBinaryFunction<?, ?, ?>
+ && equals((CompositeBinaryFunction<?, ?, ?>) that));
+ }
+
+ /**
+ * Learn whether a given CompositeBinaryFunction is equal to this.
+ * @param that CompositeBinaryFunction to test
+ * @return boolean
+ */
+ public boolean equals(CompositeBinaryFunction<?, ?, ?> that) {
+ return null != that
+ && helper.f.equals(that.helper.f)
+ && helper.g.equals(that.helper.g)
+ && helper.h.equals(that.helper.h);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int hash = "CompositeBinaryFunction".hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= helper.f.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= helper.g.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= helper.h.hashCode();
+ return hash;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "CompositeBinaryFunction<" + helper.f + ";" + helper.g + ";" + helper.h + ">";
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeBinaryPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeBinaryPredicate.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeBinaryPredicate.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeBinaryPredicate.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.BinaryPredicate;
+import org.apache.commons.functor.Function;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A {@link BinaryPredicate BinaryPredicate} composed of
+ * one binary predicate, <i>p</i>, and two
+ * functions, <i>f</i> and <i>g</i>,
+ * evaluating the ordered parameters <i>x</i>, <i>y</i>
+ * to <code><i>p</i>(<i>f</i>(<i>x</i>),<i>g</i>(<i>y</i>))</code>.
+ * <p>
+ * Note that although this class implements
+ * {@link Serializable}, a given instance will
+ * only be truly <code>Serializable</code> if all the
+ * underlying functors are. Attempts to serialize
+ * an instance whose delegates are not all
+ * <code>Serializable</code> will result in an exception.
+ * </p>
+ * @param <L> the left argument type.
+ * @param <R> the right argument type.
+ * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
+ */
+public class CompositeBinaryPredicate<L, R> implements BinaryPredicate<L, R>, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = 3841123079006929493L;
+
+ /** Base hash integer used to shift hash. */
+ private static final int HASH_SHIFT = 4;
+
+ /**
+ * Internal helper.
+ *
+ * @param <G> right function type.
+ * @param <H> right function type.
+ * @param <L> left function type.
+ * @param <R> left function type.
+ */
+ private static class Helper<G, H, L, R> implements BinaryPredicate<L, R>, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = -3463108273324567825L;
+ /**
+ * BinaryPredicate to test <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i>.
+ */
+ private BinaryPredicate<? super G, ? super H> f;
+ /**
+ * left Function.
+ */
+ private Function<? super L, ? extends G> g;
+ /**
+ * right Function.
+ */
+ private Function<? super R, ? extends H> h;
+
+ /**
+ * Create a new Helper.
+ * @param f BinaryPredicate to test <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i>
+ * @param g left Function
+ * @param h right Function
+ */
+ public Helper(BinaryPredicate<? super G, ? super H> f, Function<? super L, ? extends G> g,
+ Function<? super R, ? extends H> h) {
+ this.f = f;
+ this.g = g;
+ this.h = h;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean test(L left, R right) {
+ return f.test(g.evaluate(left), h.evaluate(right));
+ }
+ }
+
+ // attributes
+ // ------------------------------------------------------------------------
+ /**
+ * The adapted helper.
+ */
+ private final Helper<?, ?, L, R> helper;
+
+ // constructor
+ // ------------------------------------------------------------------------
+ /**
+ * Create a new CompositeBinaryPredicate.
+ *
+ * @param <G> right function type.
+ * @param <H> right function type.
+ * @param f BinaryPredicate to test <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i>
+ * @param g left Function
+ * @param h right Function
+ */
+ public <G, H> CompositeBinaryPredicate(final BinaryPredicate<? super G, ? super H> f,
+ final Function<? super L, ? extends G> g, final Function<? super R, ? extends H> h) {
+ helper = new Helper<G, H, L, R>(
+ Validate.notNull(f, "BinaryPredicate must not be null"),
+ Validate.notNull(g, "left Function must not be null"),
+ Validate.notNull(h, "right Function must not be null")
+ );
+ }
+
+ // function interface
+ // ------------------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public boolean test(L left, R right) {
+ return helper.test(left, right);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object that) {
+ return that == this || (that instanceof CompositeBinaryPredicate<?, ?>
+ && equals((CompositeBinaryPredicate<?, ?>) that));
+ }
+
+ /**
+ * Learn whether another CompositeBinaryPredicate is equal to this.
+ * @param that CompositeBinaryPredicate to test
+ * @return boolean
+ */
+ public boolean equals(CompositeBinaryPredicate<?, ?> that) {
+ return null != that && helper.f.equals(that.helper.f) && helper.g.equals(that.helper.g)
+ && helper.h.equals(that.helper.h);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int hash = "CompositeBinaryPredicate".hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= helper.f.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= helper.g.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= helper.h.hashCode();
+ return hash;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "CompositeBinaryPredicate<" + helper.f + ";" + helper.g + ";" + helper.h + ">";
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeFunction.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeFunction.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeFunction.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.Function;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A {@link Function Function}
+ * representing the composition of
+ * {@link Function Functions},
+ * "chaining" the output of one to the input
+ * of another. For example,
+ * <pre>new CompositeFunction(f).of(g)</pre>
+ * {@link #evaluate evaluates} to
+ * <code>f.evaluate(g.evaluate(obj))</code>, and
+ * <pre>new CompositeFunction(f).of(g).of(h)</pre>
+ * {@link #evaluate evaluates} to
+ * <code>f.evaluate(g.evaluate(h.evaluate(obj)))</code>.
+ * <p>
+ * When the collection is empty, this function is
+ * an identity function.
+ * </p>
+ * <p>
+ * Note that although this class implements
+ * {@link Serializable}, a given instance will
+ * only be truly <code>Serializable</code> if all the
+ * underlying functors are. Attempts to serialize
+ * an instance whose delegates are not all
+ * <code>Serializable</code> will result in an exception.
+ * </p>
+ * @param <A> the argument type.
+ * @param <T> the returned value type.
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public class CompositeFunction<A, T> implements Function<A, T>, Serializable {
+
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = 4945193629275757281L;
+
+ /** Base hash integer used to shift hash. */
+ private static final int HASH_SHIFT = 4;
+
+ /**
+ * Encapsulates a double function evaluation.
+ * @param <A> argument type
+ * @param <X> intermediate type
+ * @param <T> return type
+ */
+ private static class Helper<X, A, T> implements Function<A, T>, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = 8167255331321876718L;
+ /**
+ * The last evaluator function.
+ */
+ private Function<? super X, ? extends T> following;
+ /**
+ * The first evaluator function.
+ */
+ private Function<? super A, ? extends X> preceding;
+
+ /**
+ * Create a new Helper.
+ * @param following Function<X, Y>
+ * @param preceding Function<Y, Z>
+ */
+ public Helper(Function<? super X, ? extends T> following,
+ Function<? super A, ? extends X> preceding) {
+ this.following = Validate.notNull(following, "Function argument must not be null");
+ this.preceding = Validate.notNull(preceding, "Function argument must not be null");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public T evaluate(A obj) {
+ return following.evaluate(preceding.evaluate(obj));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return obj == this || obj instanceof Helper<?, ?, ?> && equals((Helper<?, ?, ?>) obj);
+ }
+
+ /**
+ * Checks if input helper is equals to this instance.
+ *
+ * @param helper the helper to check
+ * @return true, if helpers are equals, false otherwise
+ */
+ private boolean equals(Helper<?, ?, ?> helper) {
+ return helper.following.equals(following) && helper.preceding.equals(preceding);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int result = "CompositeFunction$Helper".hashCode();
+ result <<= 2;
+ result ^= following.hashCode();
+ result <<= 2;
+ result ^= preceding.hashCode();
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return following.toString() + " of " + preceding.toString();
+ }
+ }
+
+ /**
+ * The adapted function.
+ */
+ private final Function<? super A, ? extends T> function;
+
+ /**
+ * Create a new CompositeFunction.
+ * @param function Function to call
+ */
+ public CompositeFunction(Function<? super A, ? extends T> function) {
+ this.function = Validate.notNull(function, "function must not be null");
+ }
+
+ /**
+ * Creates a new {@link CompositeFunction} instance given the input functions.
+ *
+ * @param <X> the argument type.
+ * @param following The first evaluator function.
+ * @param preceding The last evaluator function.
+ */
+ private <X> CompositeFunction(Function<? super X, ? extends T> following,
+ Function<? super A, ? extends X> preceding) {
+ this.function = new Helper<X, A, T>(following, preceding);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public final T evaluate(A obj) {
+ return function.evaluate(obj);
+ }
+
+ /**
+ * Fluently obtain a CompositeFunction that is "this function" applied to the specified preceding function.
+ * @param <P> argument type of the resulting function.
+ * @param preceding Function
+ * @return CompositeFunction<P, T>
+ */
+ public final <P> CompositeFunction<P, T> of(Function<? super P, ? extends A> preceding) {
+ Validate.notNull(preceding, "preceding function was null");
+ return new CompositeFunction<P, T>(function, preceding);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public final boolean equals(Object that) {
+ return that == this
+ || (that instanceof CompositeFunction<?, ?> && equals((CompositeFunction<?, ?>) that));
+ }
+
+ /**
+ * Learn whether another CompositeFunction is equal to this.
+ * @param that CompositeFunction to test
+ * @return boolean
+ */
+ public final boolean equals(CompositeFunction<?, ?> that) {
+ // by construction, list is never null
+ return null != that && function.equals(that.function);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ // by construction, list is never null
+ return ("CompositeFunction".hashCode() << HASH_SHIFT) ^ function.hashCode();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "CompositeFunction<" + function + ">";
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositePredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositePredicate.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositePredicate.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositePredicate.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.Function;
+import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.adapter.PredicateFunction;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A {@link Predicate Predicate}
+ * representing the composition of
+ * {@link Function Functions},
+ * "chaining" the output of one to the input
+ * of another. For example,
+ * <pre>new CompositePredicate(p).of(f)</pre>
+ * {@link #test tests} to
+ * <code>p.test(f.evaluate(obj))</code>, and
+ * <pre>new CompositePredicate(p).of(f).of(g)</pre>
+ * {@link #test tests} to
+ * <code>p.test(f.evaluate(g.evaluate(obj)))</code>.
+ * <p>
+ * Note that although this class implements
+ * {@link Serializable}, a given instance will
+ * only be truly <code>Serializable</code> if all the
+ * underlying functors are. Attempts to serialize
+ * an instance whose delegates are not all
+ * <code>Serializable</code> will result in an exception.
+ * </p>
+ * @param <A> the predicate argument type.
+ * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
+ */
+public final class CompositePredicate<A> implements Predicate<A>, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = 4880363949059265252L;
+ // attributes
+ // ------------------------------------------------------------------------
+ /**
+ * The adapted composite function.
+ */
+ private final CompositeFunction<? super A, Boolean> function;
+
+ // constructor
+ // ------------------------------------------------------------------------
+ /**
+ * Create a new CompositePredicate.
+ * @param predicate Predicate against which the composite functions' output will be tested
+ */
+ public CompositePredicate(Predicate<? super A> predicate) {
+ this.function =
+ new CompositeFunction<A, Boolean>(
+ new PredicateFunction<A>(Validate.notNull(predicate,
+ "predicate must not be null")));
+ }
+
+ /**
+ * Create a new CompositePredicate.
+ * @param function delegate
+ */
+ private CompositePredicate(CompositeFunction<? super A, Boolean> function) {
+ this.function = function;
+ }
+
+ // modifiers
+ // ------------------------------------------------------------------------
+ /**
+ * Fluently obtain a CompositePredicate that applies our predicate to the result of the preceding function.
+ * @param <P> the input function left argument and output predicate argument types
+ * @param preceding Function
+ * @return CompositePredicate<P>
+ */
+ public <P> CompositePredicate<P> of(Function<? super P, ? extends A> preceding) {
+ return new CompositePredicate<P>(function.of(preceding));
+ }
+
+ // predicate interface
+ // ------------------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public boolean test(A obj) {
+ return function.evaluate(obj).booleanValue();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object that) {
+ return that == this || (that instanceof CompositePredicate<?>
+ && equals((CompositePredicate<?>) that));
+ }
+
+ /**
+ * Learn whether another CompositePredicate is equal to this.
+ * @param that CompositePredicate to test
+ * @return boolean
+ */
+ public boolean equals(CompositePredicate<?> that) {
+ return null != that && function.equals(that.function);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int hash = "CompositePredicate".hashCode();
+ hash <<= 2;
+ hash ^= function.hashCode();
+ return hash;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "CompositeFunction<" + function + ">";
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeProcedure.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeProcedure.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeProcedure.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/CompositeProcedure.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.Function;
+import org.apache.commons.functor.Procedure;
+import org.apache.commons.functor.adapter.ProcedureFunction;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A {@link Procedure Procedure}
+ * representing the composition of
+ * {@link Function Functions},
+ * "chaining" the output of one to the input
+ * of another. For example,
+ * <pre>new CompositeProcedure(p).of(f)</pre>
+ * {@link #run runs} to
+ * <code>p.run(f.evaluate(obj))</code>, and
+ * <pre>new CompositeProcedure(p).of(f).of(g)</pre>
+ * {@link #run runs}
+ * <code>p.run(f.evaluate(g.evaluate(obj)))</code>.
+ * <p>
+ * Note that although this class implements
+ * {@link Serializable}, a given instance will
+ * only be truly <code>Serializable</code> if all the
+ * underlying functors are. Attempts to serialize
+ * an instance whose delegates are not all
+ * <code>Serializable</code> will result in an exception.
+ * </p>
+ * @param <A> the procedure argument type.
+ * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
+ */
+public final class CompositeProcedure<A> implements Procedure<A>, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = -7496282561355676509L;
+ // attributes
+ // ------------------------------------------------------------------------
+ /**
+ * The adapted composite procedure.
+ */
+ private final CompositeFunction<? super A, Object> function;
+
+ // constructor
+ // ------------------------------------------------------------------------
+ /**
+ * Create a new CompositeProcedure.
+ * @param procedure final Procedure to run
+ */
+ public CompositeProcedure(Procedure<? super A> procedure) {
+ this.function =
+ new CompositeFunction<A, Object>(
+ new ProcedureFunction<A, Object>(Validate.notNull(
+ procedure, "procedure must not be null")));
+ }
+
+ /**
+ * Create a new CompositeProcedure.
+ * @param function final CompositeFunction to run
+ */
+ private CompositeProcedure(CompositeFunction<? super A, Object> function) {
+ this.function = function;
+ }
+
+ // modifiers
+ // ------------------------------------------------------------------------
+ /**
+ * Fluently obtain a CompositeProcedure that runs our procedure against the result of the preceding function.
+ * @param <T> the input function left argument and output procedure argument type
+ * @param preceding Function
+ * @return CompositeProcedure<P>
+ */
+ public <T> CompositeProcedure<T> of(Function<? super T, ? extends A> preceding) {
+ return new CompositeProcedure<T>(function.of(preceding));
+ }
+
+ // predicate interface
+ // ------------------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public void run(A obj) {
+ function.evaluate(obj);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object that) {
+ return that == this || (that instanceof CompositeProcedure<?>
+ && equals((CompositeProcedure<?>) that));
+ }
+
+ /**
+ * Learn whether another CompositeProcedure is equal to this.
+ * @param that CompositeProcedure to test
+ * @return boolean
+ */
+ public boolean equals(CompositeProcedure<?> that) {
+ return null != that && function.equals(that.function);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int hash = "CompositeProcedure".hashCode();
+ hash <<= 2;
+ hash ^= function.hashCode();
+ return hash;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "CompositeProcedure<" + function + ">";
+ }
+
+}
Modified: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/Conditional.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/Conditional.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/Conditional.java (original)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/Conditional.java Tue Jul 30 22:48:02 2013
@@ -19,12 +19,12 @@ package org.apache.commons.functor.core.
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.BinaryPredicate;
import org.apache.commons.functor.BinaryProcedure;
+import org.apache.commons.functor.NullaryFunction;
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.functor.NullaryProcedure;
import org.apache.commons.functor.Function;
import org.apache.commons.functor.Predicate;
import org.apache.commons.functor.Procedure;
-import org.apache.commons.functor.UnaryFunction;
-import org.apache.commons.functor.UnaryPredicate;
-import org.apache.commons.functor.UnaryProcedure;
/**
* Utility methods for creating conditional functors.
@@ -48,100 +48,101 @@ public final class Conditional {
// ------------------------------------------------------------------------
/**
- * Create a guarded Procedure.
+ * Create a guarded NullaryProcedure.
* @param q if
* @param r then
- * @return Procedure
+ * @return NullaryProcedure
*/
- public static Procedure procedure(Predicate q, Procedure r) {
- return new ConditionalProcedure(q, r);
+ public static NullaryProcedure procedure(NullaryPredicate q, NullaryProcedure r) {
+ return new ConditionalNullaryProcedure(q, r);
}
/**
- * Create a conditional Procedure.
+ * Create a conditional NullaryProcedure.
* @param q if
* @param r then
* @param s else
- * @return Procedure
+ * @return NullaryProcedure
*/
- public static Procedure procedure(Predicate q, Procedure r, Procedure s) {
- return new ConditionalProcedure(q, r, s);
+ public static NullaryProcedure procedure(NullaryPredicate q, NullaryProcedure r, NullaryProcedure s) {
+ return new ConditionalNullaryProcedure(q, r, s);
}
/**
- * Create a conditional Function.
+ * Create a conditional NullaryFunction.
* @param <T> the input functions parameter type
* @param q if
* @param r then
* @param s else
- * @return Function<T>
+ * @return NullaryFunction<T>
*/
- public static <T> Function<T> function(Predicate q, Function<? extends T> r, Function<? extends T> s) {
- return new ConditionalFunction<T>(q, r, s);
+ public static <T> NullaryFunction<T> function(NullaryPredicate q,
+ NullaryFunction<? extends T> r, NullaryFunction<? extends T> s) {
+ return new ConditionalNullaryFunction<T>(q, r, s);
}
/**
- * Create a conditional Predicate.
+ * Create a conditional NullaryPredicate.
* @param q if
* @param r then
* @param s else
- * @return Predicate
+ * @return NullaryPredicate
*/
- public static Predicate predicate(Predicate q, Predicate r, Predicate s) {
- return new ConditionalPredicate(q, r, s);
+ public static NullaryPredicate predicate(NullaryPredicate q, NullaryPredicate r, NullaryPredicate s) {
+ return new ConditionalNullaryPredicate(q, r, s);
}
/**
- * Create a guarded UnaryProcedure.
+ * Create a guarded Procedure.
*
* @param <A> the predicates argument type.
* @param q if
* @param r then
- * @return UnaryProcedure<A>
+ * @return Procedure<A>
*/
- public static <A> UnaryProcedure<A> procedure(UnaryPredicate<? super A> q, UnaryProcedure<? super A> r) {
- return new ConditionalUnaryProcedure<A>(q, r);
+ public static <A> Procedure<A> procedure(Predicate<? super A> q, Procedure<? super A> r) {
+ return new ConditionalProcedure<A>(q, r);
}
/**
- * Create a conditional UnaryProcedure.
+ * Create a conditional Procedure.
*
* @param <A> the predicates argument type.
* @param q if
* @param r then
* @param s else
- * @return UnaryProcedure<A>
+ * @return Procedure<A>
*/
- public static <A> UnaryProcedure<A> procedure(UnaryPredicate<? super A> q, UnaryProcedure<? super A> r,
- UnaryProcedure<? super A> s) {
- return new ConditionalUnaryProcedure<A>(q, r, s);
+ public static <A> Procedure<A> procedure(Predicate<? super A> q, Procedure<? super A> r,
+ Procedure<? super A> s) {
+ return new ConditionalProcedure<A>(q, r, s);
}
/**
- * Create a conditional UnaryFunction.
+ * Create a conditional Function.
* @param <A> the predicates argument type.
* @param <T> the output function returned value type.
* @param q if
* @param r then
* @param s else
- * @return UnaryFunction<A, T>
+ * @return Function<A, T>
*/
- public static <A, T> UnaryFunction<A, T> function(UnaryPredicate<? super A> q,
- UnaryFunction<? super A, ? extends T> r, UnaryFunction<? super A, ? extends T> s) {
- return new ConditionalUnaryFunction<A, T>(q, r, s);
+ public static <A, T> Function<A, T> function(Predicate<? super A> q,
+ Function<? super A, ? extends T> r, Function<? super A, ? extends T> s) {
+ return new ConditionalFunction<A, T>(q, r, s);
}
/**
- * Create a conditional UnaryPredicate.
+ * Create a conditional Predicate.
* @param <A> the predicates argument type.
* @param q if
* @param r then
* @param s else
- * @return UnaryPredicate<A>
+ * @return Predicate<A>
*/
- public static <A> UnaryPredicate<A> predicate(UnaryPredicate<? super A> q, UnaryPredicate<? super A> r,
- UnaryPredicate<? super A> s) {
- return new ConditionalUnaryPredicate<A>(q, r, s);
+ public static <A> Predicate<A> predicate(Predicate<? super A> q, Predicate<? super A> r,
+ Predicate<? super A> s) {
+ return new ConditionalPredicate<A>(q, r, s);
}
/**
Modified: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalFunction.java?rev=1508677&r1=1508676&r2=1508677&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalFunction.java (original)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalFunction.java Tue Jul 30 22:48:02 2013
@@ -28,9 +28,9 @@ import org.apache.commons.lang3.Validate
* or "conditional" operator (<code>? :</code>).
* Given a {@link Predicate predicate}
* <i>p</i> and {@link Function functions}
- * <i>f</i> and <i>g</i>, {@link #evaluate evaluates}
+ * <i>f</i> and <i>g</i>, {@link #evaluate evalautes}
* to
- * <code>p.test() ? f.evaluate() : g.evaluate()</code>.
+ * <code>p.test(x) ? f.evaluate(x) : g.evaluate(x)</code>.
* <p>
* Note that although this class implements
* {@link Serializable}, a given instance will
@@ -39,14 +39,15 @@ import org.apache.commons.lang3.Validate
* an instance whose delegates are not all
* <code>Serializable</code> will result in an exception.
* </p>
+ * @param <A> the argument type.
* @param <T> the returned value type.
* @version $Revision$ $Date$
*/
-public final class ConditionalFunction<T> implements Function<T>, Serializable {
+public final class ConditionalFunction<A, T> implements Function<A, T>, Serializable {
/**
* serialVersionUID declaration.
*/
- private static final long serialVersionUID = 4214871352184887792L;
+ private static final long serialVersionUID = -8152490481969255068L;
/** Base hash integer used to shift hash. */
private static final int HASH_SHIFT = 4;
@@ -55,15 +56,15 @@ public final class ConditionalFunction<T
/**
* the condition to be evaluated.
*/
- private final Predicate ifPred;
+ private final Predicate<? super A> ifPred;
/**
* the function executed if the condition is satisfied.
*/
- private final Function<? extends T> thenFunc;
+ private final Function<? super A, ? extends T> thenFunc;
/**
* the function executed if the condition is not satisfied.
*/
- private final Function<? extends T> elseFunc;
+ private final Function<? super A, ? extends T> elseFunc;
// constructor
// ------------------------------------------------------------------------
@@ -73,7 +74,8 @@ public final class ConditionalFunction<T
* @param thenFunc then
* @param elseFunc else
*/
- public ConditionalFunction(Predicate ifPred, Function<? extends T> thenFunc, Function<? extends T> elseFunc) {
+ public ConditionalFunction(Predicate<? super A> ifPred, Function<? super A, ? extends T> thenFunc,
+ Function<? super A, ? extends T> elseFunc) {
this.ifPred = Validate.notNull(ifPred, "Predicate argument was null");
this.thenFunc = Validate.notNull(thenFunc, "'then' Function argument was null");
this.elseFunc = Validate.notNull(elseFunc, "'else' Function argument was null");
@@ -84,11 +86,11 @@ public final class ConditionalFunction<T
/**
* {@inheritDoc}
*/
- public T evaluate() {
- if (ifPred.test()) {
- return thenFunc.evaluate();
+ public T evaluate(A obj) {
+ if (ifPred.test(obj)) {
+ return thenFunc.evaluate(obj);
} else {
- return elseFunc.evaluate();
+ return elseFunc.evaluate(obj);
}
}
@@ -97,7 +99,8 @@ public final class ConditionalFunction<T
*/
@Override
public boolean equals(Object that) {
- return that == this || (that instanceof ConditionalFunction<?> && equals((ConditionalFunction<?>) that));
+ return that == this || (that instanceof ConditionalFunction<?, ?>
+ && equals((ConditionalFunction<?, ?>) that));
}
/**
@@ -105,7 +108,7 @@ public final class ConditionalFunction<T
* @param that ConditionalFunction to test
* @return boolean
*/
- public boolean equals(ConditionalFunction<?> that) {
+ public boolean equals(ConditionalFunction<?, ?> that) {
return null != that
&& ifPred.equals(that.ifPred)
&& thenFunc.equals(that.thenFunc)
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryFunction.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryFunction.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryFunction.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.NullaryFunction;
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A {@link NullaryFunction NullaryFunction}
+ * similiar to Java's "ternary"
+ * or "conditional" operator (<code>? :</code>).
+ * Given a {@link NullaryPredicate predicate}
+ * <i>p</i> and {@link NullaryFunction functions}
+ * <i>f</i> and <i>g</i>, {@link #evaluate evaluates}
+ * to
+ * <code>p.test() ? f.evaluate() : g.evaluate()</code>.
+ * <p>
+ * Note that although this class implements
+ * {@link Serializable}, a given instance will
+ * only be truly <code>Serializable</code> if all the
+ * underlying functors are. Attempts to serialize
+ * an instance whose delegates are not all
+ * <code>Serializable</code> will result in an exception.
+ * </p>
+ * @param <T> the returned value type.
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public final class ConditionalNullaryFunction<T> implements NullaryFunction<T>, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = 4214871352184887792L;
+
+ /** Base hash integer used to shift hash. */
+ private static final int HASH_SHIFT = 4;
+ // attributes
+ // ------------------------------------------------------------------------
+ /**
+ * the condition to be evaluated.
+ */
+ private final NullaryPredicate ifPred;
+ /**
+ * the function executed if the condition is satisfied.
+ */
+ private final NullaryFunction<? extends T> thenFunc;
+ /**
+ * the function executed if the condition is not satisfied.
+ */
+ private final NullaryFunction<? extends T> elseFunc;
+
+ // constructor
+ // ------------------------------------------------------------------------
+ /**
+ * Create a new ConditionalNullaryFunction.
+ * @param ifPred if
+ * @param thenFunc then
+ * @param elseFunc else
+ */
+ public ConditionalNullaryFunction(NullaryPredicate ifPred, NullaryFunction<? extends T> thenFunc,
+ NullaryFunction<? extends T> elseFunc) {
+ this.ifPred = Validate.notNull(ifPred, "NullaryPredicate argument was null");
+ this.thenFunc = Validate.notNull(thenFunc, "'then' NullaryFunction argument was null");
+ this.elseFunc = Validate.notNull(elseFunc, "'else' NullaryFunction argument was null");
+ }
+
+ // predicate interface
+ // ------------------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public T evaluate() {
+ if (ifPred.test()) {
+ return thenFunc.evaluate();
+ } else {
+ return elseFunc.evaluate();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object that) {
+ return that == this || (that instanceof ConditionalNullaryFunction<?>
+ && equals((ConditionalNullaryFunction<?>) that));
+ }
+
+ /**
+ * Learn whether another ConditionalNullaryFunction is equal to this.
+ * @param that ConditionalNullaryFunction to test
+ * @return boolean
+ */
+ public boolean equals(ConditionalNullaryFunction<?> that) {
+ return null != that
+ && ifPred.equals(that.ifPred)
+ && thenFunc.equals(that.thenFunc)
+ && elseFunc.equals(that.elseFunc);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int hash = "ConditionalNullaryFunction".hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= ifPred.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= thenFunc.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= elseFunc.hashCode();
+ return hash;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "ConditionalNullaryFunction<" + ifPred + "?" + thenFunc + ":" + elseFunc + ">";
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryPredicate.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryPredicate.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryPredicate.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryPredicate.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A {@link NullaryPredicate NullaryPredicate}
+ * similiar to Java's "ternary"
+ * or "conditional" operator (<code>? :</code>).
+ * Given three {@link NullaryPredicate predicates}
+ * <i>p</i>, <i>q</i>, <i>r</i>,
+ * {@link #test tests}
+ * to
+ * <code>p.test() ? q.test() : r.test()</code>.
+ * <p>
+ * Note that although this class implements
+ * {@link Serializable}, a given instance will
+ * only be truly <code>Serializable</code> if all the
+ * underlying functors are. Attempts to serialize
+ * an instance whose delegates are not all
+ * <code>Serializable</code> will result in an exception.
+ * </p>
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public final class ConditionalNullaryPredicate implements NullaryPredicate, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = 7333505000745854098L;
+
+ /** Base hash integer used to shift hash. */
+ private static final int HASH_SHIFT = 4;
+ // attributes
+ // ------------------------------------------------------------------------
+ /**
+ * the condition to be evaluated.
+ */
+ private final NullaryPredicate ifPred;
+ /**
+ * the predicate executed if the condition is satisfied.
+ */
+ private final NullaryPredicate thenPred;
+ /**
+ * the predicate executed if the condition is not satisfied.
+ */
+ private final NullaryPredicate elsePred;
+
+ // constructor
+ // ------------------------------------------------------------------------
+ /**
+ * Create a new ConditionalNullaryPredicate.
+ * @param ifPred if
+ * @param thenPred then
+ * @param elsePred else
+ */
+ public ConditionalNullaryPredicate(NullaryPredicate ifPred, NullaryPredicate thenPred, NullaryPredicate elsePred) {
+ this.ifPred = Validate.notNull(ifPred, "'if' NullaryPredicate argument was null");
+ this.thenPred = Validate.notNull(thenPred, "'then' NullaryPredicate argument was null");
+ this.elsePred = Validate.notNull(elsePred, "'else' NullaryPredicate argument was null");
+ }
+
+ // predicate interface
+ // ------------------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public boolean test() {
+ return ifPred.test() ? thenPred.test() : elsePred.test();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object that) {
+ return that == this || (that instanceof ConditionalNullaryPredicate
+ && equals((ConditionalNullaryPredicate) that));
+ }
+
+ /**
+ * Learn whether another ConditionalNullaryPredicate is equal to this.
+ * @param that ConditionalNullaryPredicate to test
+ * @return boolean
+ */
+ public boolean equals(ConditionalNullaryPredicate that) {
+ return null != that
+ && ifPred.equals(that.ifPred)
+ && thenPred.equals(that.thenPred)
+ && elsePred.equals(that.elsePred);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int hash = "ConditionalNullaryPredicate".hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= ifPred.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= thenPred.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= elsePred.hashCode();
+ return hash;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "ConditionalNullaryPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">";
+ }
+
+}
Added: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryProcedure.java?rev=1508677&view=auto
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryProcedure.java (added)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/composite/ConditionalNullaryProcedure.java Tue Jul 30 22:48:02 2013
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.NullaryPredicate;
+import org.apache.commons.functor.NullaryProcedure;
+import org.apache.commons.functor.core.NoOp;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * A {@link NullaryProcedure NullaryProcedure}
+ * similiar to Java's "ternary"
+ * or "conditional" operator (<code>? :</code>).
+ * Given a {@link NullaryPredicate predicate}
+ * <i>p</i> and {@link NullaryProcedure procedures}
+ * <i>q</i> and <i>r</i>, {@link #run runs}
+ * <code>if (p.test()) { q.run(); } else { r.run(); }</code>.
+ * <p>
+ * Note that although this class implements
+ * {@link Serializable}, a given instance will
+ * only be truly <code>Serializable</code> if all the
+ * underlying functors are. Attempts to serialize
+ * an instance whose delegates are not all
+ * <code>Serializable</code> will result in an exception.
+ * </p>
+ * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
+ */
+public final class ConditionalNullaryProcedure implements NullaryProcedure, Serializable {
+ /**
+ * serialVersionUID declaration.
+ */
+ private static final long serialVersionUID = -4228632798836328605L;
+
+ /** Base hash integer used to shift hash. */
+ private static final int HASH_SHIFT = 4;
+
+ // attributes
+ // ------------------------------------------------------------------------
+ /**
+ * the condition to be evaluated.
+ */
+ private final NullaryPredicate ifPred;
+ /**
+ * the procedure executed if the condition is satisfied.
+ */
+ private final NullaryProcedure thenProc;
+ /**
+ * the procedure executed if the condition is not satisfied.
+ */
+ private final NullaryProcedure elseProc;
+
+ // constructor
+ // ------------------------------------------------------------------------
+ /**
+ * Create a new ConditionalNullaryProcedure.
+ * @param ifPred if
+ * @param thenProc then
+ */
+ public ConditionalNullaryProcedure(NullaryPredicate ifPred, NullaryProcedure thenProc) {
+ this(ifPred, thenProc, NoOp.instance());
+ }
+
+ /**
+ * Create a new ConditionalNullaryProcedure.
+ * @param ifPred if
+ * @param thenProc then
+ * @param elseProc else
+ */
+ public ConditionalNullaryProcedure(NullaryPredicate ifPred, NullaryProcedure thenProc, NullaryProcedure elseProc) {
+ this.ifPred = Validate.notNull(ifPred, "NullaryPredicate argument was null");
+ this.thenProc = Validate.notNull(thenProc, "'then' NullaryProcedure argument was null");
+ this.elseProc = Validate.notNull(elseProc, "'else' NullaryProcedure argument was null");
+ }
+
+ // predicate interface
+ // ------------------------------------------------------------------------
+ /**
+ * {@inheritDoc}
+ */
+ public void run() {
+ if (ifPred.test()) {
+ thenProc.run();
+ } else {
+ elseProc.run();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object that) {
+ return that == this || (that instanceof ConditionalNullaryProcedure
+ && equals((ConditionalNullaryProcedure) that));
+ }
+
+ /**
+ * Learn whether another ConditionalProcecure is equal to this.
+ * @param that the ConditionalNullaryProcedure to test
+ * @return boolean
+ */
+ public boolean equals(ConditionalNullaryProcedure that) {
+ return null != that
+ && ifPred.equals(that.ifPred)
+ && thenProc.equals(that.thenProc)
+ && elseProc.equals(that.elseProc);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int hash = "ConditionalNullaryProcedure".hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= ifPred.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= thenProc.hashCode();
+ hash <<= HASH_SHIFT;
+ hash ^= elseProc.hashCode();
+ return hash;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "ConditionalNullaryProcedure<" + ifPred + "?" + thenProc + ":" + elseProc + ">";
+ }
+
+}