This commit is contained in:
Gary Gregory 2024-07-11 11:20:02 -04:00
parent bc8ad80ec6
commit 7ec4e0c3e9
10 changed files with 67 additions and 58 deletions

View File

@ -29,7 +29,7 @@ import java.util.function.Consumer;
* {@link ClosureUtils}. These include method invocation and for/while loops. * {@link ClosureUtils}. These include method invocation and for/while loops.
* </p> * </p>
* *
* @param <T> the type that the closure acts on * @param <T> the type of the input to the operation.
* @since 1.0 * @since 1.0
* @deprecated Use {@link Consumer}. * @deprecated Use {@link Consumer}.
*/ */

View File

@ -43,9 +43,10 @@ import org.apache.commons.collections4.FunctorException;
* } * }
* </pre> * </pre>
* *
* @param <T> the type of the input to the operation.
* @since 4.0 * @since 4.0
*/ */
public abstract class CatchAndRethrowClosure<E> implements Closure<E> { public abstract class CatchAndRethrowClosure<T> implements Closure<T> {
/** /**
* Execute this closure on the specified input object. * Execute this closure on the specified input object.
@ -55,7 +56,7 @@ public abstract class CatchAndRethrowClosure<E> implements Closure<E> {
* checked exception. * checked exception.
*/ */
@Override @Override
public void execute(final E input) { public void execute(final T input) {
try { try {
executeAndThrow(input); executeAndThrow(input);
} catch (final RuntimeException ex) { } catch (final RuntimeException ex) {
@ -72,5 +73,5 @@ public abstract class CatchAndRethrowClosure<E> implements Closure<E> {
* @throws Throwable if the closure execution resulted in a checked * @throws Throwable if the closure execution resulted in a checked
* exception. * exception.
*/ */
protected abstract void executeAndThrow(E input) throws Throwable; protected abstract void executeAndThrow(T input) throws Throwable;
} }

View File

@ -25,9 +25,10 @@ import org.apache.commons.collections4.Closure;
/** /**
* Closure implementation that chains the specified closures together. * Closure implementation that chains the specified closures together.
* *
* @param <T> the type of the input to the operation.
* @since 3.0 * @since 3.0
*/ */
public class ChainedClosure<E> implements Closure<E>, Serializable { public class ChainedClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = -3520677225766901240L; private static final long serialVersionUID = -3520677225766901240L;
@ -77,7 +78,7 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
} }
/** The closures to call in turn */ /** The closures to call in turn */
private final Closure<? super E>[] iClosures; private final Closure<? super T>[] iClosures;
/** /**
* Hidden constructor for the use by the static factory methods. * Hidden constructor for the use by the static factory methods.
@ -85,7 +86,7 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
* @param clone if {@code true} the input argument will be cloned * @param clone if {@code true} the input argument will be cloned
* @param closures the closures to chain, no nulls * @param closures the closures to chain, no nulls
*/ */
private ChainedClosure(final boolean clone, final Closure<? super E>... closures) { private ChainedClosure(final boolean clone, final Closure<? super T>... closures) {
iClosures = clone ? FunctorUtils.copy(closures) : closures; iClosures = clone ? FunctorUtils.copy(closures) : closures;
} }
@ -95,7 +96,7 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
* *
* @param closures the closures to chain, copied, no nulls * @param closures the closures to chain, copied, no nulls
*/ */
public ChainedClosure(final Closure<? super E>... closures) { public ChainedClosure(final Closure<? super T>... closures) {
this(true, closures); this(true, closures);
} }
@ -105,8 +106,8 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
* @param input the input object passed to each closure * @param input the input object passed to each closure
*/ */
@Override @Override
public void execute(final E input) { public void execute(final T input) {
for (final Closure<? super E> iClosure : iClosures) { for (final Closure<? super T> iClosure : iClosures) {
iClosure.accept(input); iClosure.accept(input);
} }
} }
@ -117,7 +118,7 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
* @return a copy of the closures * @return a copy of the closures
* @since 3.1 * @since 3.1
*/ */
public Closure<? super E>[] getClosures() { public Closure<? super T>[] getClosures() {
return FunctorUtils.copy(iClosures); return FunctorUtils.copy(iClosures);
} }

View File

@ -24,9 +24,10 @@ import org.apache.commons.collections4.FunctorException;
/** /**
* Closure implementation that always throws an exception. * Closure implementation that always throws an exception.
* *
* @param <T> the type of the input to the operation.
* @since 3.0 * @since 3.0
*/ */
public final class ExceptionClosure<E> implements Closure<E>, Serializable { public final class ExceptionClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 7179106032121985545L; private static final long serialVersionUID = 7179106032121985545L;
@ -38,11 +39,11 @@ public final class ExceptionClosure<E> implements Closure<E>, Serializable {
/** /**
* Factory returning the singleton instance. * Factory returning the singleton instance.
* *
* @param <E> the type that the closure acts on * @param <T> the type of the input to the operation.
* @return the singleton instance * @return the singleton instance
* @since 3.1 * @since 3.1
*/ */
public static <E> Closure<E> exceptionClosure() { public static <T> Closure<T> exceptionClosure() {
return INSTANCE; return INSTANCE;
} }
@ -59,7 +60,7 @@ public final class ExceptionClosure<E> implements Closure<E>, Serializable {
* @throws FunctorException always * @throws FunctorException always
*/ */
@Override @Override
public void execute(final E input) { public void execute(final T input) {
throw new FunctorException("ExceptionClosure invoked"); throw new FunctorException("ExceptionClosure invoked");
} }

View File

@ -27,9 +27,10 @@ import org.apache.commons.collections4.Closure;
* for more details. * for more details.
* </p> * </p>
* *
* @param <T> the type of the input to the operation.
* @since 3.0 * @since 3.0
*/ */
public class ForClosure<E> implements Closure<E> { public class ForClosure<T> implements Closure<T> {
/** /**
* Factory method that performs validation. * Factory method that performs validation.
@ -56,7 +57,7 @@ public class ForClosure<E> implements Closure<E> {
private final int iCount; private final int iCount;
/** The closure to call */ /** The closure to call */
private final Closure<? super E> iClosure; private final Closure<? super T> iClosure;
/** /**
* Constructor that performs no validation. * Constructor that performs no validation.
@ -65,7 +66,7 @@ public class ForClosure<E> implements Closure<E> {
* @param count the number of times to execute the closure * @param count the number of times to execute the closure
* @param closure the closure to execute, not null * @param closure the closure to execute, not null
*/ */
public ForClosure(final int count, final Closure<? super E> closure) { public ForClosure(final int count, final Closure<? super T> closure) {
iCount = count; iCount = count;
iClosure = closure; iClosure = closure;
} }
@ -76,7 +77,7 @@ public class ForClosure<E> implements Closure<E> {
* @param input the input object * @param input the input object
*/ */
@Override @Override
public void execute(final E input) { public void execute(final T input) {
for (int i = 0; i < iCount; i++) { for (int i = 0; i < iCount; i++) {
iClosure.accept(input); iClosure.accept(input);
} }
@ -88,7 +89,7 @@ public class ForClosure<E> implements Closure<E> {
* @return the closure * @return the closure
* @since 3.1 * @since 3.1
*/ */
public Closure<? super E> getClosure() { public Closure<? super T> getClosure() {
return iClosure; return iClosure;
} }

View File

@ -26,9 +26,10 @@ import org.apache.commons.collections4.Predicate;
* Closure implementation acts as an if statement calling one or other closure * Closure implementation acts as an if statement calling one or other closure
* based on a predicate. * based on a predicate.
* *
* @param <T> the type of the input to the operation.
* @since 3.0 * @since 3.0
*/ */
public class IfClosure<E> implements Closure<E>, Serializable { public class IfClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 3518477308466486130L; private static final long serialVersionUID = 3518477308466486130L;
@ -67,13 +68,13 @@ public class IfClosure<E> implements Closure<E>, Serializable {
Objects.requireNonNull(falseClosure, "falseClosure")); Objects.requireNonNull(falseClosure, "falseClosure"));
} }
/** The test */ /** The test */
private final Predicate<? super E> iPredicate; private final Predicate<? super T> iPredicate;
/** The closure to use if true */ /** The closure to use if true */
private final Closure<? super E> iTrueClosure; private final Closure<? super T> iTrueClosure;
/** The closure to use if false */ /** The closure to use if false */
private final Closure<? super E> iFalseClosure; private final Closure<? super T> iFalseClosure;
/** /**
* Constructor that performs no validation. * Constructor that performs no validation.
@ -86,7 +87,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @param trueClosure closure used if true, not null * @param trueClosure closure used if true, not null
* @since 3.2 * @since 3.2
*/ */
public IfClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure) { public IfClosure(final Predicate<? super T> predicate, final Closure<? super T> trueClosure) {
this(predicate, trueClosure, NOPClosure.nopClosure()); this(predicate, trueClosure, NOPClosure.nopClosure());
} }
@ -98,8 +99,8 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @param trueClosure closure used if true, not null * @param trueClosure closure used if true, not null
* @param falseClosure closure used if false, not null * @param falseClosure closure used if false, not null
*/ */
public IfClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure, public IfClosure(final Predicate<? super T> predicate, final Closure<? super T> trueClosure,
final Closure<? super E> falseClosure) { final Closure<? super T> falseClosure) {
iPredicate = predicate; iPredicate = predicate;
iTrueClosure = trueClosure; iTrueClosure = trueClosure;
iFalseClosure = falseClosure; iFalseClosure = falseClosure;
@ -111,7 +112,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @param input the input object * @param input the input object
*/ */
@Override @Override
public void execute(final E input) { public void execute(final T input) {
if (iPredicate.test(input)) { if (iPredicate.test(input)) {
iTrueClosure.accept(input); iTrueClosure.accept(input);
} else { } else {
@ -125,7 +126,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @return the closure * @return the closure
* @since 3.1 * @since 3.1
*/ */
public Closure<? super E> getFalseClosure() { public Closure<? super T> getFalseClosure() {
return iFalseClosure; return iFalseClosure;
} }
@ -135,7 +136,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @return the predicate * @return the predicate
* @since 3.1 * @since 3.1
*/ */
public Predicate<? super E> getPredicate() { public Predicate<? super T> getPredicate() {
return iPredicate; return iPredicate;
} }
@ -145,7 +146,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @return the closure * @return the closure
* @since 3.1 * @since 3.1
*/ */
public Closure<? super E> getTrueClosure() { public Closure<? super T> getTrueClosure() {
return iTrueClosure; return iTrueClosure;
} }

View File

@ -23,9 +23,10 @@ import org.apache.commons.collections4.Closure;
/** /**
* Closure implementation that does nothing. * Closure implementation that does nothing.
* *
* @param <T> the type of the input to the operation.
* @since 3.0 * @since 3.0
*/ */
public final class NOPClosure<E> implements Closure<E>, Serializable { public final class NOPClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 3518477308466486130L; private static final long serialVersionUID = 3518477308466486130L;
@ -57,7 +58,7 @@ public final class NOPClosure<E> implements Closure<E>, Serializable {
* @param input the input object * @param input the input object
*/ */
@Override @Override
public void execute(final E input) { public void execute(final T input) {
// do nothing // do nothing
} }

View File

@ -27,9 +27,10 @@ import org.apache.commons.collections4.Predicate;
* Closure implementation calls the closure whose predicate returns true, * Closure implementation calls the closure whose predicate returns true,
* like a switch statement. * like a switch statement.
* *
* @param <T> the type of the input to the operation.
* @since 3.0 * @since 3.0
*/ */
public class SwitchClosure<E> implements Closure<E>, Serializable { public class SwitchClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = 3518477308466486130L; private static final long serialVersionUID = 3518477308466486130L;
@ -98,13 +99,13 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
return new SwitchClosure<>(predicates, closures, defaultClosure); return new SwitchClosure<>(predicates, closures, defaultClosure);
} }
/** The tests to consider */ /** The tests to consider */
private final Predicate<? super E>[] iPredicates; private final Predicate<? super T>[] iPredicates;
/** The matching closures to call */ /** The matching closures to call */
private final Closure<? super E>[] iClosures; private final Closure<? super T>[] iClosures;
/** The default closure to call if no tests match */ /** The default closure to call if no tests match */
private final Closure<? super E> iDefault; private final Closure<? super T> iDefault;
/** /**
* Hidden constructor for the use by the static factory methods. * Hidden constructor for the use by the static factory methods.
@ -114,11 +115,11 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @param closures matching array of closures, no nulls * @param closures matching array of closures, no nulls
* @param defaultClosure the closure to use if no match, null means nop * @param defaultClosure the closure to use if no match, null means nop
*/ */
private SwitchClosure(final boolean clone, final Predicate<? super E>[] predicates, private SwitchClosure(final boolean clone, final Predicate<? super T>[] predicates,
final Closure<? super E>[] closures, final Closure<? super E> defaultClosure) { final Closure<? super T>[] closures, final Closure<? super T> defaultClosure) {
iPredicates = clone ? FunctorUtils.copy(predicates) : predicates; iPredicates = clone ? FunctorUtils.copy(predicates) : predicates;
iClosures = clone ? FunctorUtils.copy(closures) : closures; iClosures = clone ? FunctorUtils.copy(closures) : closures;
iDefault = defaultClosure == null ? NOPClosure.<E>nopClosure() : defaultClosure; iDefault = defaultClosure == null ? NOPClosure.<T>nopClosure() : defaultClosure;
} }
/** /**
@ -129,8 +130,8 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @param closures matching array of closures, cloned, no nulls * @param closures matching array of closures, cloned, no nulls
* @param defaultClosure the closure to use if no match, null means nop * @param defaultClosure the closure to use if no match, null means nop
*/ */
public SwitchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures, public SwitchClosure(final Predicate<? super T>[] predicates, final Closure<? super T>[] closures,
final Closure<? super E> defaultClosure) { final Closure<? super T> defaultClosure) {
this(true, predicates, closures, defaultClosure); this(true, predicates, closures, defaultClosure);
} }
@ -140,7 +141,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @param input the input object * @param input the input object
*/ */
@Override @Override
public void execute(final E input) { public void execute(final T input) {
for (int i = 0; i < iPredicates.length; i++) { for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].test(input)) { if (iPredicates[i].test(input)) {
iClosures[i].accept(input); iClosures[i].accept(input);
@ -156,7 +157,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @return a copy of the closures * @return a copy of the closures
* @since 3.1 * @since 3.1
*/ */
public Closure<? super E>[] getClosures() { public Closure<? super T>[] getClosures() {
return FunctorUtils.copy(iClosures); return FunctorUtils.copy(iClosures);
} }
@ -166,7 +167,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @return the default closure * @return the default closure
* @since 3.1 * @since 3.1
*/ */
public Closure<? super E> getDefaultClosure() { public Closure<? super T> getDefaultClosure() {
return iDefault; return iDefault;
} }
@ -176,7 +177,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @return a copy of the predicates * @return a copy of the predicates
* @since 3.1 * @since 3.1
*/ */
public Predicate<? super E>[] getPredicates() { public Predicate<? super T>[] getPredicates() {
return FunctorUtils.copy(iPredicates); return FunctorUtils.copy(iPredicates);
} }

View File

@ -25,9 +25,10 @@ import org.apache.commons.collections4.Transformer;
* Closure implementation that calls a Transformer using the input object * Closure implementation that calls a Transformer using the input object
* and ignore the result. * and ignore the result.
* *
* @param <T> the type of the input to the operation.
* @since 3.0 * @since 3.0
*/ */
public class TransformerClosure<E> implements Closure<E>, Serializable { public class TransformerClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */ /** Serial version UID */
private static final long serialVersionUID = -5194992589193388969L; private static final long serialVersionUID = -5194992589193388969L;
@ -49,7 +50,7 @@ public class TransformerClosure<E> implements Closure<E>, Serializable {
} }
/** The transformer to wrap */ /** The transformer to wrap */
private final Transformer<? super E, ?> iTransformer; private final Transformer<? super T, ?> iTransformer;
/** /**
* Constructor that performs no validation. * Constructor that performs no validation.
@ -57,7 +58,7 @@ public class TransformerClosure<E> implements Closure<E>, Serializable {
* *
* @param transformer the transformer to call, not null * @param transformer the transformer to call, not null
*/ */
public TransformerClosure(final Transformer<? super E, ?> transformer) { public TransformerClosure(final Transformer<? super T, ?> transformer) {
iTransformer = transformer; iTransformer = transformer;
} }
@ -67,7 +68,7 @@ public class TransformerClosure<E> implements Closure<E>, Serializable {
* @param input the input object * @param input the input object
*/ */
@Override @Override
public void execute(final E input) { public void execute(final T input) {
iTransformer.transform(input); iTransformer.transform(input);
} }
@ -77,7 +78,7 @@ public class TransformerClosure<E> implements Closure<E>, Serializable {
* @return the transformer * @return the transformer
* @since 3.1 * @since 3.1
*/ */
public Transformer<? super E, ?> getTransformer() { public Transformer<? super T, ?> getTransformer() {
return iTransformer; return iTransformer;
} }

View File

@ -31,9 +31,10 @@ import org.apache.commons.collections4.Predicate;
* for more details. * for more details.
* </p> * </p>
* *
* @param <T> the type of the input to the operation.
* @since 3.0 * @since 3.0
*/ */
public class WhileClosure<E> implements Closure<E> { public class WhileClosure<T> implements Closure<T> {
/** /**
* Factory method that performs validation. * Factory method that performs validation.
@ -51,9 +52,9 @@ public class WhileClosure<E> implements Closure<E> {
Objects.requireNonNull(closure, "closure"), doLoop); Objects.requireNonNull(closure, "closure"), doLoop);
} }
/** The test condition */ /** The test condition */
private final Predicate<? super E> iPredicate; private final Predicate<? super T> iPredicate;
/** The closure to call */ /** The closure to call */
private final Closure<? super E> iClosure; private final Closure<? super T> iClosure;
/** The flag, true is a do loop, false is a while */ /** The flag, true is a do loop, false is a while */
private final boolean iDoLoop; private final boolean iDoLoop;
@ -66,7 +67,7 @@ public class WhileClosure<E> implements Closure<E> {
* @param closure the closure to execute, not null * @param closure the closure to execute, not null
* @param doLoop true to act as a do-while loop, always executing the closure once * @param doLoop true to act as a do-while loop, always executing the closure once
*/ */
public WhileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure, final boolean doLoop) { public WhileClosure(final Predicate<? super T> predicate, final Closure<? super T> closure, final boolean doLoop) {
iPredicate = predicate; iPredicate = predicate;
iClosure = closure; iClosure = closure;
iDoLoop = doLoop; iDoLoop = doLoop;
@ -78,7 +79,7 @@ public class WhileClosure<E> implements Closure<E> {
* @param input the input object * @param input the input object
*/ */
@Override @Override
public void execute(final E input) { public void execute(final T input) {
if (iDoLoop) { if (iDoLoop) {
iClosure.accept(input); iClosure.accept(input);
} }
@ -93,7 +94,7 @@ public class WhileClosure<E> implements Closure<E> {
* @return the closure * @return the closure
* @since 3.1 * @since 3.1
*/ */
public Closure<? super E> getClosure() { public Closure<? super T> getClosure() {
return iClosure; return iClosure;
} }
@ -103,7 +104,7 @@ public class WhileClosure<E> implements Closure<E> {
* @return the predicate * @return the predicate
* @since 3.1 * @since 3.1
*/ */
public Predicate<? super E> getPredicate() { public Predicate<? super T> getPredicate() {
return iPredicate; return iPredicate;
} }