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.
* </p>
*
* @param <T> the type that the closure acts on
* @param <T> the type of the input to the operation.
* @since 1.0
* @deprecated Use {@link Consumer}.
*/

View File

@ -43,9 +43,10 @@ import org.apache.commons.collections4.FunctorException;
* }
* </pre>
*
* @param <T> the type of the input to the operation.
* @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.
@ -55,7 +56,7 @@ public abstract class CatchAndRethrowClosure<E> implements Closure<E> {
* checked exception.
*/
@Override
public void execute(final E input) {
public void execute(final T input) {
try {
executeAndThrow(input);
} 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
* 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.
*
* @param <T> the type of the input to the operation.
* @since 3.0
*/
public class ChainedClosure<E> implements Closure<E>, Serializable {
public class ChainedClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -3520677225766901240L;
@ -77,7 +78,7 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
}
/** 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.
@ -85,7 +86,7 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
* @param clone if {@code true} the input argument will be cloned
* @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;
}
@ -95,7 +96,7 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
*
* @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);
}
@ -105,8 +106,8 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
* @param input the input object passed to each closure
*/
@Override
public void execute(final E input) {
for (final Closure<? super E> iClosure : iClosures) {
public void execute(final T input) {
for (final Closure<? super T> iClosure : iClosures) {
iClosure.accept(input);
}
}
@ -117,7 +118,7 @@ public class ChainedClosure<E> implements Closure<E>, Serializable {
* @return a copy of the closures
* @since 3.1
*/
public Closure<? super E>[] getClosures() {
public Closure<? super T>[] getClosures() {
return FunctorUtils.copy(iClosures);
}

View File

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

View File

@ -27,9 +27,10 @@ import org.apache.commons.collections4.Closure;
* for more details.
* </p>
*
* @param <T> the type of the input to the operation.
* @since 3.0
*/
public class ForClosure<E> implements Closure<E> {
public class ForClosure<T> implements Closure<T> {
/**
* Factory method that performs validation.
@ -56,7 +57,7 @@ public class ForClosure<E> implements Closure<E> {
private final int iCount;
/** The closure to call */
private final Closure<? super E> iClosure;
private final Closure<? super T> iClosure;
/**
* 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 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;
iClosure = closure;
}
@ -76,7 +77,7 @@ public class ForClosure<E> implements Closure<E> {
* @param input the input object
*/
@Override
public void execute(final E input) {
public void execute(final T input) {
for (int i = 0; i < iCount; i++) {
iClosure.accept(input);
}
@ -88,7 +89,7 @@ public class ForClosure<E> implements Closure<E> {
* @return the closure
* @since 3.1
*/
public Closure<? super E> getClosure() {
public Closure<? super T> getClosure() {
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
* based on a predicate.
*
* @param <T> the type of the input to the operation.
* @since 3.0
*/
public class IfClosure<E> implements Closure<E>, Serializable {
public class IfClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = 3518477308466486130L;
@ -67,13 +68,13 @@ public class IfClosure<E> implements Closure<E>, Serializable {
Objects.requireNonNull(falseClosure, "falseClosure"));
}
/** The test */
private final Predicate<? super E> iPredicate;
private final Predicate<? super T> iPredicate;
/** The closure to use if true */
private final Closure<? super E> iTrueClosure;
private final Closure<? super T> iTrueClosure;
/** The closure to use if false */
private final Closure<? super E> iFalseClosure;
private final Closure<? super T> iFalseClosure;
/**
* 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
* @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());
}
@ -98,8 +99,8 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @param trueClosure closure used if true, not null
* @param falseClosure closure used if false, not null
*/
public IfClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure,
final Closure<? super E> falseClosure) {
public IfClosure(final Predicate<? super T> predicate, final Closure<? super T> trueClosure,
final Closure<? super T> falseClosure) {
iPredicate = predicate;
iTrueClosure = trueClosure;
iFalseClosure = falseClosure;
@ -111,7 +112,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @param input the input object
*/
@Override
public void execute(final E input) {
public void execute(final T input) {
if (iPredicate.test(input)) {
iTrueClosure.accept(input);
} else {
@ -125,7 +126,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @return the closure
* @since 3.1
*/
public Closure<? super E> getFalseClosure() {
public Closure<? super T> getFalseClosure() {
return iFalseClosure;
}
@ -135,7 +136,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @return the predicate
* @since 3.1
*/
public Predicate<? super E> getPredicate() {
public Predicate<? super T> getPredicate() {
return iPredicate;
}
@ -145,7 +146,7 @@ public class IfClosure<E> implements Closure<E>, Serializable {
* @return the closure
* @since 3.1
*/
public Closure<? super E> getTrueClosure() {
public Closure<? super T> getTrueClosure() {
return iTrueClosure;
}

View File

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

View File

@ -27,9 +27,10 @@ import org.apache.commons.collections4.Predicate;
* Closure implementation calls the closure whose predicate returns true,
* like a switch statement.
*
* @param <T> the type of the input to the operation.
* @since 3.0
*/
public class SwitchClosure<E> implements Closure<E>, Serializable {
public class SwitchClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = 3518477308466486130L;
@ -98,13 +99,13 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
return new SwitchClosure<>(predicates, closures, defaultClosure);
}
/** The tests to consider */
private final Predicate<? super E>[] iPredicates;
private final Predicate<? super T>[] iPredicates;
/** 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 */
private final Closure<? super E> iDefault;
private final Closure<? super T> iDefault;
/**
* 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 defaultClosure the closure to use if no match, null means nop
*/
private SwitchClosure(final boolean clone, final Predicate<? super E>[] predicates,
final Closure<? super E>[] closures, final Closure<? super E> defaultClosure) {
private SwitchClosure(final boolean clone, final Predicate<? super T>[] predicates,
final Closure<? super T>[] closures, final Closure<? super T> defaultClosure) {
iPredicates = clone ? FunctorUtils.copy(predicates) : predicates;
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 defaultClosure the closure to use if no match, null means nop
*/
public SwitchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures,
final Closure<? super E> defaultClosure) {
public SwitchClosure(final Predicate<? super T>[] predicates, final Closure<? super T>[] closures,
final Closure<? super T> defaultClosure) {
this(true, predicates, closures, defaultClosure);
}
@ -140,7 +141,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @param input the input object
*/
@Override
public void execute(final E input) {
public void execute(final T input) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].test(input)) {
iClosures[i].accept(input);
@ -156,7 +157,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @return a copy of the closures
* @since 3.1
*/
public Closure<? super E>[] getClosures() {
public Closure<? super T>[] getClosures() {
return FunctorUtils.copy(iClosures);
}
@ -166,7 +167,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @return the default closure
* @since 3.1
*/
public Closure<? super E> getDefaultClosure() {
public Closure<? super T> getDefaultClosure() {
return iDefault;
}
@ -176,7 +177,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
* @return a copy of the predicates
* @since 3.1
*/
public Predicate<? super E>[] getPredicates() {
public Predicate<? super T>[] getPredicates() {
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
* and ignore the result.
*
* @param <T> the type of the input to the operation.
* @since 3.0
*/
public class TransformerClosure<E> implements Closure<E>, Serializable {
public class TransformerClosure<T> implements Closure<T>, Serializable {
/** Serial version UID */
private static final long serialVersionUID = -5194992589193388969L;
@ -49,7 +50,7 @@ public class TransformerClosure<E> implements Closure<E>, Serializable {
}
/** The transformer to wrap */
private final Transformer<? super E, ?> iTransformer;
private final Transformer<? super T, ?> iTransformer;
/**
* 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
*/
public TransformerClosure(final Transformer<? super E, ?> transformer) {
public TransformerClosure(final Transformer<? super T, ?> transformer) {
iTransformer = transformer;
}
@ -67,7 +68,7 @@ public class TransformerClosure<E> implements Closure<E>, Serializable {
* @param input the input object
*/
@Override
public void execute(final E input) {
public void execute(final T input) {
iTransformer.transform(input);
}
@ -77,7 +78,7 @@ public class TransformerClosure<E> implements Closure<E>, Serializable {
* @return the transformer
* @since 3.1
*/
public Transformer<? super E, ?> getTransformer() {
public Transformer<? super T, ?> getTransformer() {
return iTransformer;
}

View File

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