Javadoc improvements.
Finish renaming of Command to Executor. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137127 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f2e2fdbb21
commit
94318c7e60
|
@ -65,34 +65,34 @@
|
|||
import org.apache.commons.lang.functor.TransformerUtils;
|
||||
/**
|
||||
* <code>ExecutorUtils</code> provides reference implementations and utilities
|
||||
* for the Executor pattern interface. The supplied commands are:
|
||||
* for the Executor functor interface. The supplied executors are:
|
||||
* <ul>
|
||||
* <li>Invoker - invokes a method on the input object
|
||||
* <li>For - repeatedly calls a command for a fixed number of times
|
||||
* <li>While - repeatedly calls a command while a predicate is true
|
||||
* <li>DoWhile - repeatedly calls a command while a predicate is true
|
||||
* <li>Chained - chains two or more commands together
|
||||
* <li>Switch - calls one command based on one or more predicates
|
||||
* <li>SwitchMap - calls one command looked up from a Map
|
||||
* <li>Transformer - wraps a Transformer as a Predicate
|
||||
* <li>For - repeatedly calls an executor for a fixed number of times
|
||||
* <li>While - repeatedly calls an executor while a predicate is true
|
||||
* <li>DoWhile - repeatedly calls an executor while a predicate is true
|
||||
* <li>Chained - chains two or more executors together
|
||||
* <li>Switch - calls one executor based on one or more predicates
|
||||
* <li>SwitchMap - calls one executor looked up from a Map
|
||||
* <li>Transformer - wraps a Transformer as an Executor
|
||||
* <li>NOP - does nothing
|
||||
* <li>Exception - always throws an exception
|
||||
* </ul>
|
||||
* All the supplied commands are Serializable.
|
||||
* All the supplied executors are Serializable.
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: ExecutorUtils.java,v 1.1 2002/11/06 19:15:40 bayard Exp $
|
||||
* @version $Id: ExecutorUtils.java,v 1.2 2002/11/14 21:54:49 scolebourne Exp $
|
||||
*/
|
||||
public class ExecutorUtils {
|
||||
|
||||
/**
|
||||
* A Executor that always throws an exception
|
||||
*/
|
||||
private static final Executor EXCEPTION_COMMAND = new ExceptionExecutor();
|
||||
private static final Executor EXCEPTION_EXECUTOR = new ExceptionExecutor();
|
||||
/**
|
||||
* A Executor that does nothing
|
||||
*/
|
||||
private static final Executor NOP_COMMAND = new NOPExecutor();
|
||||
private static final Executor NOP_EXECUTOR = new NOPExecutor();
|
||||
|
||||
/**
|
||||
* Restrictive constructor
|
||||
|
@ -105,116 +105,119 @@ protected ExecutorUtils() {
|
|||
* Gets a Executor that always throws an exception.
|
||||
* This could be useful during testing as a placeholder.
|
||||
*
|
||||
* @return the command
|
||||
* @return the executor
|
||||
*/
|
||||
public static Executor exceptionExecutor() {
|
||||
return EXCEPTION_COMMAND;
|
||||
return EXCEPTION_EXECUTOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Executor that will do nothing.
|
||||
* This could be useful during testing as a placeholder.
|
||||
*
|
||||
* @return the command
|
||||
* @return the executor
|
||||
*/
|
||||
public static Executor nopExecutor() {
|
||||
return NOP_COMMAND;
|
||||
return NOP_EXECUTOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Executor that calls a Factory each time the transformer is used.
|
||||
* The transformer will return the value returned by the factory.
|
||||
* Creates a Executor that calls a Transformer each time it is called.
|
||||
* The transformer will be called using the executor's input object.
|
||||
* The transformer's result will be ignored.
|
||||
*
|
||||
* @param transformer the transformer to run each time in the command
|
||||
* @return the command.
|
||||
* @param transformer the transformer to run each time in the executor
|
||||
* @return the executor.
|
||||
*/
|
||||
public static Executor asExecutor(Transformer transformer) {
|
||||
if (transformer == null) {
|
||||
throw new IllegalArgumentException("TransformerExecutor: The transformer must not be null");
|
||||
throw new IllegalArgumentException("The transformer must not be null");
|
||||
}
|
||||
return new TransformerExecutor(transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Executor that will call the command <code>count</code> times.
|
||||
* Creates a Executor that will call the executor <code>count</code> times.
|
||||
*
|
||||
* @param count the number of times to loop
|
||||
* @param command the command to call repeatedly
|
||||
* @return the command
|
||||
* @param executor the executor to call repeatedly
|
||||
* @return the <code>for</code> executor
|
||||
* @throws IllegalArgumentException if either argument is null
|
||||
*/
|
||||
public static Executor forExecutor(int count, Executor command) {
|
||||
public static Executor forExecutor(int count, Executor executor) {
|
||||
if (count < 0) {
|
||||
throw new IllegalArgumentException("ForExecutor: The loop count must not be less than zero, it was " + count);
|
||||
throw new IllegalArgumentException("The loop count must not be less than zero, it was " + count);
|
||||
}
|
||||
if (command == null) {
|
||||
throw new IllegalArgumentException("ForExecutor: The command must not be null");
|
||||
if (executor == null) {
|
||||
throw new IllegalArgumentException("The executor must not be null");
|
||||
}
|
||||
return new ForExecutor(count, command);
|
||||
return new ForExecutor(count, executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Executor that will call the command repeatedly until the
|
||||
* Creates a Executor that will call the executor repeatedly until the
|
||||
* predicate returns false.
|
||||
*
|
||||
* @param predicate the predicate to use as an end of loop test
|
||||
* @param command the command to call repeatedly
|
||||
* @return the command
|
||||
* @param executor the executor to call repeatedly
|
||||
* @return the <code>while</code> executor
|
||||
* @throws IllegalArgumentException if either argument is null
|
||||
*/
|
||||
public static Executor whileExecutor(Predicate predicate, Executor command) {
|
||||
public static Executor whileExecutor(Predicate predicate, Executor executor) {
|
||||
if (predicate == null) {
|
||||
throw new IllegalArgumentException("WhileExecutor: The predicate must not be null");
|
||||
throw new IllegalArgumentException("The predicate must not be null");
|
||||
}
|
||||
if (command == null) {
|
||||
throw new IllegalArgumentException("WhileExecutor: The command must not be null");
|
||||
if (executor == null) {
|
||||
throw new IllegalArgumentException("The executor must not be null");
|
||||
}
|
||||
return new WhileExecutor(predicate, command, false);
|
||||
return new WhileExecutor(predicate, executor, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Executor that will call the command once and then repeatedly
|
||||
* Creates a Executor that will call the executor once and then repeatedly
|
||||
* until the predicate returns false.
|
||||
*
|
||||
* @param command the command to call repeatedly
|
||||
* @param executor the executor to call repeatedly
|
||||
* @param predicate the predicate to use as an end of loop test
|
||||
* @return the command
|
||||
* @return the <code>do-while</code> executor
|
||||
* @throws IllegalArgumentException if either argument is null
|
||||
*/
|
||||
public static Executor doWhileExecutor(Executor command, Predicate predicate) {
|
||||
if (command == null) {
|
||||
throw new IllegalArgumentException("DoWhileExecutor: The command must not be null");
|
||||
public static Executor doWhileExecutor(Executor executor, Predicate predicate) {
|
||||
if (executor == null) {
|
||||
throw new IllegalArgumentException("The executor must not be null");
|
||||
}
|
||||
if (predicate == null) {
|
||||
throw new IllegalArgumentException("DoWhileExecutor: The predicate must not be null");
|
||||
throw new IllegalArgumentException("The predicate must not be null");
|
||||
}
|
||||
return new WhileExecutor(predicate, command, true);
|
||||
return new WhileExecutor(predicate, executor, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Executor that will invoke a specific method on the command's
|
||||
* Creates a Executor that will invoke a specific method on the executor's
|
||||
* input object by reflection.
|
||||
*
|
||||
* @param methodName the name of the method
|
||||
* @return the command
|
||||
* @return the <code>invoker</code> executor
|
||||
* @throws IllegalArgumentException if the method name is null
|
||||
*/
|
||||
public static Executor invokerExecutor(String methodName) {
|
||||
// reuse transformer as it has caching - this is lazy really, should have inner class here
|
||||
return asExecutor(TransformerUtils.invokerTransformer(methodName, null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Executor that will invoke a specific method on the command's
|
||||
* Creates a Executor that will invoke a specific method on the executor's
|
||||
* input object by reflection.
|
||||
*
|
||||
* @param methodName the name of the method
|
||||
* @param paramTypes the parameter types
|
||||
* @param args the arguments
|
||||
* @return the command
|
||||
* @return the <code>invoker</code> executor
|
||||
* @throws IllegalArgumentException if the method name is null
|
||||
* @throws IllegalArgumentException if the paramTypes and args don't match
|
||||
*/
|
||||
public static Executor invokerExecutor(String methodName, Class[] paramTypes, Object[] args) {
|
||||
// reuse transformer as it has caching - this is lazy really, should have inner class here
|
||||
return asExecutor(TransformerUtils.invokerTransformer(methodName, paramTypes, args));
|
||||
}
|
||||
|
||||
|
@ -222,52 +225,52 @@ public static Executor invokerExecutor(String methodName, Class[] paramTypes, Ob
|
|||
* Create a new Executor that calls two Executors, passing the result of
|
||||
* the first into the second.
|
||||
*
|
||||
* @param command1 the first command
|
||||
* @param command2 the second command
|
||||
* @return the command
|
||||
* @throws IllegalArgumentException if either command is null
|
||||
* @param executor1 the first executor
|
||||
* @param executor2 the second executor
|
||||
* @return the <code>chained</code> executor
|
||||
* @throws IllegalArgumentException if either executor is null
|
||||
*/
|
||||
public static Executor chainedExecutor(Executor command1, Executor command2) {
|
||||
Executor[] commands = new Executor[] { command1, command2 };
|
||||
validate(commands);
|
||||
return new ChainedExecutor(commands);
|
||||
public static Executor chainedExecutor(Executor executor1, Executor executor2) {
|
||||
Executor[] executors = new Executor[] { executor1, executor2 };
|
||||
validate(executors);
|
||||
return new ChainedExecutor(executors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Executor that calls each command in turn, passing the
|
||||
* result into the next command.
|
||||
* Create a new Executor that calls each executor in turn, passing the
|
||||
* result into the next executor.
|
||||
*
|
||||
* @param commands an array of commands to chain
|
||||
* @return the command
|
||||
* @throws IllegalArgumentException if the commands array is null
|
||||
* @throws IllegalArgumentException if the commands array has 0 elements
|
||||
* @throws IllegalArgumentException if any command in the array is null
|
||||
* @param executors an array of executors to chain
|
||||
* @return the <code>chained</code> executor
|
||||
* @throws IllegalArgumentException if the executors array is null
|
||||
* @throws IllegalArgumentException if the executors array has 0 elements
|
||||
* @throws IllegalArgumentException if any executor in the array is null
|
||||
*/
|
||||
public static Executor chainedExecutor(Executor[] commands) {
|
||||
commands = copy(commands);
|
||||
validate(commands);
|
||||
return new ChainedExecutor(commands);
|
||||
public static Executor chainedExecutor(Executor[] executors) {
|
||||
executors = copy(executors);
|
||||
validate(executors);
|
||||
return new ChainedExecutor(executors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Executor that calls each command in turn, passing the
|
||||
* result into the next command. The ordering is that of the iterator()
|
||||
* Create a new Executor that calls each executor in turn, passing the
|
||||
* result into the next executor. The ordering is that of the iterator()
|
||||
* method on the collection.
|
||||
*
|
||||
* @param commands a collection of commands to chain
|
||||
* @return the command
|
||||
* @throws IllegalArgumentException if the commands collection is null
|
||||
* @throws IllegalArgumentException if the commands collection is empty
|
||||
* @throws IllegalArgumentException if any command in the collection is null
|
||||
* @param executors a collection of executors to chain
|
||||
* @return the <code>chained</code> executor
|
||||
* @throws IllegalArgumentException if the executors collection is null
|
||||
* @throws IllegalArgumentException if the executors collection is empty
|
||||
* @throws IllegalArgumentException if any executor in the collection is null
|
||||
*/
|
||||
public static Executor chainedExecutor(Collection commands) {
|
||||
if (commands == null) {
|
||||
throw new IllegalArgumentException("ChainedExecutor: The command collection must not be null");
|
||||
public static Executor chainedExecutor(Collection executors) {
|
||||
if (executors == null) {
|
||||
throw new IllegalArgumentException("The executor collection must not be null");
|
||||
}
|
||||
// convert to array like this to guarantee iterator() ordering
|
||||
Executor[] cmds = new Executor[commands.size()];
|
||||
Executor[] cmds = new Executor[executors.size()];
|
||||
int i = 0;
|
||||
for (Iterator it = commands.iterator(); it.hasNext();) {
|
||||
for (Iterator it = executors.iterator(); it.hasNext();) {
|
||||
cmds[i++] = (Executor) it.next();
|
||||
}
|
||||
validate(cmds);
|
||||
|
@ -275,81 +278,85 @@ public static Executor chainedExecutor(Collection commands) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a new Executor that calls one of two commands depending
|
||||
* Create a new Executor that calls one of two executors depending
|
||||
* on the specified predicate.
|
||||
*
|
||||
* @param predicate the predicate to switch on
|
||||
* @param trueExecutor the command called if the predicate is true
|
||||
* @param falseExecutor the command called if the predicate is false
|
||||
* @return the command
|
||||
* @param trueExecutor the executor called if the predicate is true
|
||||
* @param falseExecutor the executor called if the predicate is false
|
||||
* @return the <code>switch</code> executor
|
||||
* @throws IllegalArgumentException if the predicate is null
|
||||
* @throws IllegalArgumentException if either command is null
|
||||
* @throws IllegalArgumentException if either executor is null
|
||||
*/
|
||||
public static Executor switchExecutor(Predicate predicate, Executor trueExecutor, Executor falseExecutor) {
|
||||
return switchExecutorInternal(new Predicate[] { predicate }, new Executor[] { trueExecutor }, falseExecutor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Executor that calls one of the commands depending
|
||||
* on the predicates. The command at array location 0 is called if the
|
||||
* predicate at array location 0 returned true. Each predicate is evaluated
|
||||
* Create a new Executor that calls one of the executors depending
|
||||
* on the predicates.
|
||||
* <p>
|
||||
* The executor at array location 0 is called if the predicate at array
|
||||
* location 0 returned true. Each predicate is evaluated
|
||||
* until one returns true.
|
||||
*
|
||||
* @param predicates an array of predicates to check
|
||||
* @param commands an array of commands to call
|
||||
* @return the command
|
||||
* @param executors an array of executors to call
|
||||
* @return the <code>switch</code> executor
|
||||
* @throws IllegalArgumentException if the either array is null
|
||||
* @throws IllegalArgumentException if the either array has 0 elements
|
||||
* @throws IllegalArgumentException if any element in the arrays is null
|
||||
* @throws IllegalArgumentException if the arrays are different sizes
|
||||
*/
|
||||
public static Executor switchExecutor(Predicate[] predicates, Executor[] commands) {
|
||||
return switchExecutorInternal(copy(predicates), copy(commands), null);
|
||||
public static Executor switchExecutor(Predicate[] predicates, Executor[] executors) {
|
||||
return switchExecutorInternal(copy(predicates), copy(executors), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Executor that calls one of the commands depending
|
||||
* on the predicates. The command at array location 0 is called if the
|
||||
* predicate at array location 0 returned true. Each predicate is evaluated
|
||||
* Create a new Executor that calls one of the executors depending
|
||||
* on the predicates.
|
||||
* <p>
|
||||
* The executor at array location 0 is called if the predicate at array
|
||||
* location 0 returned true. Each predicate is evaluated
|
||||
* until one returns true. If no predicates evaluate to true, the default
|
||||
* command is called.
|
||||
* executor is called.
|
||||
*
|
||||
* @param predicates an array of predicates to check
|
||||
* @param commands an array of commands to call
|
||||
* @param executors an array of executors to call
|
||||
* @param defaultExecutor the default to call if no predicate matches
|
||||
* @return the command
|
||||
* @return the <code>switch</code> executor
|
||||
* @throws IllegalArgumentException if the either array is null
|
||||
* @throws IllegalArgumentException if the either array has 0 elements
|
||||
* @throws IllegalArgumentException if any element in the arrays is null
|
||||
* @throws IllegalArgumentException if the arrays are different sizes
|
||||
*/
|
||||
public static Executor switchExecutor(Predicate[] predicates, Executor[] commands, Executor defaultExecutor) {
|
||||
return switchExecutorInternal(copy(predicates), copy(commands), defaultExecutor);
|
||||
public static Executor switchExecutor(Predicate[] predicates, Executor[] executors, Executor defaultExecutor) {
|
||||
return switchExecutorInternal(copy(predicates), copy(executors), defaultExecutor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Executor that calls one of the commands depending
|
||||
* Create a new Executor that calls one of the executors depending
|
||||
* on the predicates.
|
||||
* <p>
|
||||
* The Map consists of Predicate keys and Executor values. A command
|
||||
* The Map consists of Predicate keys and Executor values. A executor
|
||||
* is called if its matching predicate returns true. Each predicate is evaluated
|
||||
* until one returns true. If no predicates evaluate to true, the default
|
||||
* command is called. The default command is set in the map with a
|
||||
* executor is called. The default executor is set in the map with a
|
||||
* null key. The ordering is that of the iterator() method on the entryset
|
||||
* collection of the map.
|
||||
*
|
||||
* @param predicatesAndExecutors a map of predicates to commands
|
||||
* @return the command
|
||||
* @param predicatesAndExecutors a map of predicates to executors
|
||||
* @return the <code>switch</code> executor
|
||||
* @throws IllegalArgumentException if the map is null
|
||||
* @throws IllegalArgumentException if the map is empty
|
||||
* @throws IllegalArgumentException if any command in the map is null
|
||||
* @throws IllegalArgumentException if any executor in the map is null
|
||||
* @throws ClassCastException if the map elements are of the wrong type
|
||||
*/
|
||||
public static Executor switchExecutor(Map predicatesAndExecutors) {
|
||||
Executor[] trs = null;
|
||||
Predicate[] preds = null;
|
||||
if (predicatesAndExecutors == null) {
|
||||
throw new IllegalArgumentException("SwitchExecutor: The predicate and command map must not be null");
|
||||
throw new IllegalArgumentException("The predicate and executor map must not be null");
|
||||
}
|
||||
// convert to array like this to guarantee iterator() ordering
|
||||
Executor def = (Executor) predicatesAndExecutors.remove(null);
|
||||
|
@ -367,40 +374,49 @@ public static Executor switchExecutor(Map predicatesAndExecutors) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Validate input and create command
|
||||
* Validate input and create executor.
|
||||
*
|
||||
* @param predicates an array of predicates to check
|
||||
* @param executors an array of executors to call
|
||||
* @param defaultExecutor the default to call if no predicate matches
|
||||
* @return the <code>switch</code> executor
|
||||
* @throws IllegalArgumentException if the either array is null
|
||||
* @throws IllegalArgumentException if the either array has 0 elements
|
||||
* @throws IllegalArgumentException if any element in the arrays is null
|
||||
* @throws IllegalArgumentException if the arrays are different sizes
|
||||
*/
|
||||
private static Executor switchExecutorInternal(Predicate[] predicates, Executor[] commands, Executor defaultExecutor) {
|
||||
private static Executor switchExecutorInternal(Predicate[] predicates, Executor[] executors, Executor defaultExecutor) {
|
||||
validate(predicates);
|
||||
validate(commands);
|
||||
if (predicates.length != commands.length) {
|
||||
throw new IllegalArgumentException("SwitchExecutor: The predicate and command arrays must be the same size");
|
||||
validate(executors);
|
||||
if (predicates.length != executors.length) {
|
||||
throw new IllegalArgumentException("The predicate and executor arrays must be the same size");
|
||||
}
|
||||
if (defaultExecutor == null) {
|
||||
defaultExecutor = nopExecutor();
|
||||
}
|
||||
return new SwitchExecutor(predicates, commands, defaultExecutor);
|
||||
return new SwitchExecutor(predicates, executors, defaultExecutor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Executor that uses the input object as a key to find the
|
||||
* command to call.
|
||||
* executor to call.
|
||||
* <p>
|
||||
* The Map consists of object keys and Executor values. A command
|
||||
* The Map consists of object keys and Executor values. A executor
|
||||
* is called if the input object equals the key. If there is no match, the
|
||||
* default command is called. The default command is set in the map
|
||||
* default executor is called. The default executor is set in the map
|
||||
* using a null key.
|
||||
*
|
||||
* @param objectsAndExecutors a map of objects to commands
|
||||
* @return the command
|
||||
* @param objectsAndExecutors a map of objects to executors
|
||||
* @return the executor
|
||||
* @throws IllegalArgumentException if the map is null
|
||||
* @throws IllegalArgumentException if the map is empty
|
||||
* @throws IllegalArgumentException if any command in the map is null
|
||||
* @throws IllegalArgumentException if any executor in the map is null
|
||||
*/
|
||||
public static Executor switchMapExecutor(Map objectsAndExecutors) {
|
||||
Executor[] trs = null;
|
||||
Predicate[] preds = null;
|
||||
if (objectsAndExecutors == null) {
|
||||
throw new IllegalArgumentException("SwitchEqualsExecutor: The obejct and command map must not be null");
|
||||
throw new IllegalArgumentException("The obejct and executor map must not be null");
|
||||
}
|
||||
Executor def = (Executor) objectsAndExecutors.remove(null);
|
||||
int size = objectsAndExecutors.size();
|
||||
|
@ -417,9 +433,10 @@ public static Executor switchMapExecutor(Map objectsAndExecutors) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Copy method
|
||||
* Clone the predicates to ensure that the internal reference can't be messed with.
|
||||
*
|
||||
* @param predicates the predicates to copy
|
||||
* @return the cloned predicates
|
||||
*/
|
||||
private static Predicate[] copy(Predicate[] predicates) {
|
||||
if (predicates == null) {
|
||||
|
@ -429,53 +446,56 @@ private static Predicate[] copy(Predicate[] predicates) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Validate method
|
||||
* Validate the predicates to ensure that all is well.
|
||||
*
|
||||
* @param predicates the predicates to validate
|
||||
* @return the validated predicates
|
||||
*/
|
||||
private static void validate(Predicate[] predicates) {
|
||||
if (predicates == null) {
|
||||
throw new IllegalArgumentException("ExecutorUtils: The predicate array must not be null");
|
||||
throw new IllegalArgumentException("The predicate array must not be null");
|
||||
}
|
||||
if (predicates.length < 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"ExecutorUtils: At least 1 predicate must be specified in the predicate array, size was " + predicates.length);
|
||||
"At least 1 predicate must be specified in the predicate array, size was " + predicates.length);
|
||||
}
|
||||
for (int i = 0; i < predicates.length; i++) {
|
||||
if (predicates[i] == null) {
|
||||
throw new IllegalArgumentException("ExecutorUtils: The predicate array must not contain a null predicate, index " + i + " was null");
|
||||
throw new IllegalArgumentException("The predicate array must not contain a null predicate, index " + i + " was null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy method
|
||||
* Clone the executors to ensure that the internal reference can't be messed with.
|
||||
*
|
||||
* @param commands the commands to copy
|
||||
* @param executors the executors to copy
|
||||
* @return the cloned executors
|
||||
*/
|
||||
private static Executor[] copy(Executor[] commands) {
|
||||
if (commands == null) {
|
||||
private static Executor[] copy(Executor[] executors) {
|
||||
if (executors == null) {
|
||||
return null;
|
||||
}
|
||||
return (Executor[]) commands.clone();
|
||||
return (Executor[]) executors.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate method
|
||||
* Validate the executors to ensure that all is well.
|
||||
*
|
||||
* @param commands the commands to validate
|
||||
* @param executors the executors to validate
|
||||
* @return the validated executors
|
||||
*/
|
||||
private static void validate(Executor[] commands) {
|
||||
if (commands == null) {
|
||||
throw new IllegalArgumentException("ExecutorUtils: The command array must not be null");
|
||||
private static void validate(Executor[] executors) {
|
||||
if (executors == null) {
|
||||
throw new IllegalArgumentException("The executor array must not be null");
|
||||
}
|
||||
if (commands.length < 1) {
|
||||
if (executors.length < 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"ExecutorUtils: At least 1 command must be specified in the command array, size was " + commands.length);
|
||||
"At least 1 executor must be specified in the executor array, size was " + executors.length);
|
||||
}
|
||||
for (int i = 0; i < commands.length; i++) {
|
||||
if (commands[i] == null) {
|
||||
throw new IllegalArgumentException("ExecutorUtils: The command array must not contain a null command, index " + i + " was null");
|
||||
for (int i = 0; i < executors.length; i++) {
|
||||
if (executors[i] == null) {
|
||||
throw new IllegalArgumentException("The executor array must not contain a null executor, index " + i + " was null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -530,14 +550,14 @@ public void execute(Object input) {
|
|||
//----------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* TransformerExecutor returns the result of calling a Transformer.
|
||||
* TransformerExecutor calls a Transformer using the input object and ignore the result.
|
||||
*/
|
||||
private static class TransformerExecutor implements Executor, Serializable {
|
||||
|
||||
/** The transformer to wrap */
|
||||
private final Transformer iTransformer;
|
||||
|
||||
/**
|
||||
* Constructor to store factory
|
||||
* Constructor to store transformer
|
||||
*/
|
||||
private TransformerExecutor(Transformer transformer) {
|
||||
super();
|
||||
|
@ -545,7 +565,7 @@ private TransformerExecutor(Transformer transformer) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the result of calling the factory
|
||||
* Call the transformer
|
||||
*/
|
||||
public void execute(Object input) {
|
||||
try {
|
||||
|
@ -561,22 +581,22 @@ public void execute(Object input) {
|
|||
//----------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* ChainedExecutor calls a list of commands.
|
||||
* ChainedExecutor calls a list of executors.
|
||||
*/
|
||||
private static class ChainedExecutor implements Executor, Serializable {
|
||||
|
||||
/** The executors to call in turn */
|
||||
private final Executor[] iExecutors;
|
||||
|
||||
/**
|
||||
* Constructor to store params
|
||||
*/
|
||||
private ChainedExecutor(Executor[] commands) {
|
||||
private ChainedExecutor(Executor[] executors) {
|
||||
super();
|
||||
iExecutors = commands;
|
||||
iExecutors = executors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a list of commands
|
||||
* Execute a list of executors
|
||||
*/
|
||||
public void execute(Object input) {
|
||||
for (int i = 0; i < iExecutors.length; i++) {
|
||||
|
@ -589,26 +609,28 @@ public void execute(Object input) {
|
|||
//----------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* SwitchExecutor calls the command whose predicate returns true.
|
||||
* SwitchExecutor calls the executor whose predicate returns true.
|
||||
*/
|
||||
private static class SwitchExecutor implements Executor, Serializable {
|
||||
|
||||
/** The tests to consider */
|
||||
private final Predicate[] iPredicates;
|
||||
/** The matching executors to call */
|
||||
private final Executor[] iExecutors;
|
||||
/** The default executor to call if no tests match */
|
||||
private final Executor iDefault;
|
||||
|
||||
/**
|
||||
* Constructor to store params
|
||||
*/
|
||||
private SwitchExecutor(Predicate[] predicates, Executor[] commands, Executor defaultExecutor) {
|
||||
private SwitchExecutor(Predicate[] predicates, Executor[] executors, Executor defaultExecutor) {
|
||||
super();
|
||||
iPredicates = predicates;
|
||||
iExecutors = commands;
|
||||
iExecutors = executors;
|
||||
iDefault = defaultExecutor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the command whose predicate returns true
|
||||
* Execute the executor whose predicate returns true
|
||||
*/
|
||||
public void execute(Object input) {
|
||||
for (int i = 0; i < iPredicates.length; i++) {
|
||||
|
@ -625,24 +647,25 @@ public void execute(Object input) {
|
|||
//----------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* ForExecutor calls the command a fixed nunmber of times.
|
||||
* ForExecutor calls the executor a fixed nunmber of times.
|
||||
*/
|
||||
private static class ForExecutor implements Executor, Serializable {
|
||||
|
||||
/** The number of times to loop */
|
||||
private final int iCount;
|
||||
/** The executor to call */
|
||||
private final Executor iExecutor;
|
||||
|
||||
/**
|
||||
* Constructor to store params
|
||||
*/
|
||||
private ForExecutor(int count, Executor command) {
|
||||
private ForExecutor(int count, Executor executor) {
|
||||
super();
|
||||
iCount = count;
|
||||
iExecutor = command;
|
||||
iExecutor = executor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the command count times
|
||||
* Execute the executor count times
|
||||
*/
|
||||
public void execute(Object input) {
|
||||
for (int i = 0; i < iCount; i++) {
|
||||
|
@ -655,26 +678,28 @@ public void execute(Object input) {
|
|||
//----------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* WhileExecutor calls the command until the predicate is false.
|
||||
* WhileExecutor calls the executor until the predicate is false.
|
||||
*/
|
||||
private static class WhileExecutor implements Executor, Serializable {
|
||||
|
||||
/** The test condition */
|
||||
private final Predicate iPredicate;
|
||||
/** The executor to call */
|
||||
private final Executor iExecutor;
|
||||
/** The flag, true is a do loop, false is a while */
|
||||
private final boolean iDoLoop;
|
||||
|
||||
/**
|
||||
* Constructor to store params
|
||||
*/
|
||||
private WhileExecutor(Predicate predicate, Executor command, boolean doLoop) {
|
||||
private WhileExecutor(Predicate predicate, Executor executor, boolean doLoop) {
|
||||
super();
|
||||
iPredicate = predicate;
|
||||
iExecutor = command;
|
||||
iExecutor = executor;
|
||||
iDoLoop = doLoop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the command until the predicate is false
|
||||
* Execute the executor until the predicate is false
|
||||
*/
|
||||
public void execute(Object input) {
|
||||
if (iDoLoop) {
|
||||
|
|
|
@ -62,7 +62,7 @@
|
|||
import org.apache.commons.lang.SerializationUtils;
|
||||
/**
|
||||
* <code>FactoryUtils</code> provides reference implementations and utilities
|
||||
* for the Factory pattern interface. The supplied factories are:
|
||||
* for the Factory functor interface. The supplied factories are:
|
||||
* <ul>
|
||||
* <li>Prototype - clones a specified object
|
||||
* <li>Reflection - creates objects using reflection
|
||||
|
@ -73,7 +73,7 @@
|
|||
* All the supplied factories are Serializable.
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: FactoryUtils.java,v 1.1 2002/11/05 16:44:28 bayard Exp $
|
||||
* @version $Id: FactoryUtils.java,v 1.2 2002/11/14 21:54:49 scolebourne Exp $
|
||||
*/
|
||||
public class FactoryUtils {
|
||||
|
||||
|
@ -105,6 +105,7 @@ public static Factory exceptionFactory() {
|
|||
|
||||
/**
|
||||
* Gets a Factory that will return null each time the factory is used.
|
||||
* This could be useful during testing as a placeholder.
|
||||
*
|
||||
* @return the factory
|
||||
*/
|
||||
|
@ -119,7 +120,7 @@ public static Factory nullFactory() {
|
|||
* use the prototype factory.
|
||||
*
|
||||
* @param constantToReturn the constant object to return each time in the factory
|
||||
* @return the factory.
|
||||
* @return the <code>constant</code> factory.
|
||||
*/
|
||||
public static Factory constantFactory(Object constantToReturn) {
|
||||
return new ConstantFactory(constantToReturn);
|
||||
|
@ -136,15 +137,14 @@ public static Factory constantFactory(Object constantToReturn) {
|
|||
* <ul>
|
||||
*
|
||||
* @param prototype the object to clone each time in the factory
|
||||
* @return the factory
|
||||
* @return the <code>prototype</code> factory
|
||||
* @throws IllegalArgumentException if the prototype is null
|
||||
* @throws IllegalArgumentException if the prototype cannot be cloned
|
||||
*/
|
||||
public static Factory prototypeFactory(Object prototype) {
|
||||
if (prototype == null) {
|
||||
throw new IllegalArgumentException("PrototypeFactory: The prototype must not be null");
|
||||
throw new IllegalArgumentException("The prototype must not be null");
|
||||
}
|
||||
// TODO: move to cloneable pattern
|
||||
try {
|
||||
prototype.getClass().getMethod("clone", null);
|
||||
return new PrototypeCloneFactory(prototype);
|
||||
|
@ -160,7 +160,7 @@ public static Factory prototypeFactory(Object prototype) {
|
|||
}
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("PrototypeFactory: The prototype must be cloneable");
|
||||
throw new IllegalArgumentException("The prototype must be cloneable via a public clone method");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -168,7 +168,7 @@ public static Factory prototypeFactory(Object prototype) {
|
|||
* a no-args constructor.
|
||||
*
|
||||
* @param classToInstantiate the Class to instantiate each time in the factory
|
||||
* @return the factory
|
||||
* @return the <code>reflection</code> factory
|
||||
* @throws IllegalArgumentException if the classToInstantiate is null
|
||||
*/
|
||||
public static Factory reflectionFactory(Class classToInstantiate) {
|
||||
|
@ -182,7 +182,7 @@ public static Factory reflectionFactory(Class classToInstantiate) {
|
|||
* @param classToInstantiate the Class to instantiate each time in the factory
|
||||
* @param paramTypes parameter types for the constructor, can be null
|
||||
* @param args the arguments to pass to the constructor, can be null
|
||||
* @return the factory
|
||||
* @return the <code>reflection</code> factory
|
||||
* @throws IllegalArgumentException if the classToInstantiate is null
|
||||
* @throws IllegalArgumentException if the paramTypes and args don't match
|
||||
* @throws IllegalArgumentException if the constructor doesn't exist
|
||||
|
@ -221,7 +221,7 @@ public Object create() {
|
|||
* ConstantFactory returns the same instance each time.
|
||||
*/
|
||||
private static class ConstantFactory implements Factory, Serializable {
|
||||
|
||||
/** The constant to return each time */
|
||||
private final Object iConstant;
|
||||
|
||||
/**
|
||||
|
@ -247,7 +247,9 @@ public Object create() {
|
|||
* PrototypeCloneFactory creates objects by copying a prototype using the clone method.
|
||||
*/
|
||||
private static class PrototypeCloneFactory implements Factory, Serializable {
|
||||
/** The object to clone each time */
|
||||
private final Object iPrototype;
|
||||
/** The method used to clone */
|
||||
private transient Method iCloneMethod;
|
||||
|
||||
/**
|
||||
|
@ -302,7 +304,7 @@ public Object create() {
|
|||
* PrototypeSerializationFactory creates objects by cloning a prototype using serialization.
|
||||
*/
|
||||
private static class PrototypeSerializationFactory implements Factory, Serializable {
|
||||
|
||||
/** The object to clone via serialization each time */
|
||||
private final Serializable iPrototype;
|
||||
|
||||
/**
|
||||
|
@ -336,10 +338,13 @@ public Object create() {
|
|||
* ReflectionFactory creates objects using reflection.
|
||||
*/
|
||||
private static class ReflectionFactory implements Factory, Serializable {
|
||||
|
||||
/** The class to create */
|
||||
private final Class iClassToInstantiate;
|
||||
/** The constructor parameter types */
|
||||
private final Class[] iParamTypes;
|
||||
/** The constructor arguments */
|
||||
private final Object[] iArgs;
|
||||
/** The constructor */
|
||||
private transient Constructor iConstructor = null;
|
||||
|
||||
/**
|
||||
|
|
|
@ -64,7 +64,7 @@
|
|||
import org.apache.commons.lang.functor.TransformerUtils;
|
||||
/**
|
||||
* <code>PredicateUtils</code> provides reference implementations and utilities
|
||||
* for the Predicate pattern interface. The supplied predicates are:
|
||||
* for the Predicate functor interface. The supplied predicates are:
|
||||
* <ul>
|
||||
* <li>Invoker - returns the result of a method call on the input object
|
||||
* <li>InstanceOf - true if the object is an instanceof a class
|
||||
|
@ -88,7 +88,7 @@
|
|||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @author Ola Berg
|
||||
* @version $Id: PredicateUtils.java,v 1.1 2002/11/05 16:44:28 bayard Exp $
|
||||
* @version $Id: PredicateUtils.java,v 1.2 2002/11/14 21:54:49 scolebourne Exp $
|
||||
*/
|
||||
public class PredicateUtils {
|
||||
|
||||
|
@ -127,7 +127,7 @@ protected PredicateUtils() {
|
|||
* Gets a Predicate that always throws an exception.
|
||||
* This could be useful during testing as a placeholder.
|
||||
*
|
||||
* @return the factory
|
||||
* @return the predicate
|
||||
*/
|
||||
public static Predicate exceptionPredicate() {
|
||||
return EXCEPTION_PREDICATE;
|
||||
|
@ -199,7 +199,8 @@ public static Predicate identityPredicate(Object value) {
|
|||
|
||||
/**
|
||||
* Creates a Predicate that checks if the object passed in is of
|
||||
* a particular type, using instanceof.
|
||||
* a particular type, using instanceof. A <code>null</code> input
|
||||
* object will return <code>false</code>.
|
||||
*
|
||||
* @param type the type to check for, may not be null
|
||||
* @return the predicate
|
||||
|
@ -207,7 +208,7 @@ public static Predicate identityPredicate(Object value) {
|
|||
*/
|
||||
public static Predicate instanceofPredicate(Class type) {
|
||||
if (type == null) {
|
||||
throw new IllegalArgumentException("InstanceofPredicate: The type to check instanceof must not be null");
|
||||
throw new IllegalArgumentException("The type to check instanceof must not be null");
|
||||
}
|
||||
return new InstanceofPredicate(type);
|
||||
}
|
||||
|
@ -215,7 +216,9 @@ public static Predicate instanceofPredicate(Class type) {
|
|||
/**
|
||||
* Creates a Predicate that returns true the first time an object is
|
||||
* encoutered, and false if the same object is received
|
||||
* again. The comparison is by equals().
|
||||
* again. The comparison is by equals(). A <code>null</code> input object
|
||||
* is accepted and will return true the first time, and false subsequently
|
||||
* as well.
|
||||
*
|
||||
* @return the predicate
|
||||
*/
|
||||
|
@ -239,6 +242,7 @@ public static Predicate uniquePredicate() {
|
|||
* @throws IllegalArgumentException if the methodName is null.
|
||||
*/
|
||||
public static Predicate invokerPredicate(String methodName){
|
||||
// reuse transformer as it has caching - this is lazy really, should have inner class here
|
||||
return asPredicate(TransformerUtils.invokerTransformer(methodName));
|
||||
}
|
||||
|
||||
|
@ -260,6 +264,7 @@ public static Predicate invokerPredicate(String methodName){
|
|||
* @throws IllegalArgumentException if the paramTypes and args don't match
|
||||
*/
|
||||
public static Predicate invokerPredicate(String methodName, Class[] paramTypes, Object[] args){
|
||||
// reuse transformer as it has caching - this is lazy really, should have inner class here
|
||||
return asPredicate(TransformerUtils.invokerTransformer(methodName, paramTypes, args));
|
||||
}
|
||||
|
||||
|
@ -272,7 +277,7 @@ public static Predicate invokerPredicate(String methodName, Class[] paramTypes,
|
|||
*
|
||||
* @param predicate1 the first predicate, may not be null
|
||||
* @param predicate2 the second predicate, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>and</code> predicate
|
||||
* @throws IllegalArgumentException if either predicate is null
|
||||
*/
|
||||
public static Predicate andPredicate(Predicate predicate1, Predicate predicate2) {
|
||||
|
@ -284,7 +289,7 @@ public static Predicate andPredicate(Predicate predicate1, Predicate predicate2)
|
|||
* predicates are true.
|
||||
*
|
||||
* @param predicates an array of predicates to check, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>all</code> predicate
|
||||
* @throws IllegalArgumentException if the predicates array is null
|
||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
||||
* @throws IllegalArgumentException if any predicate in the array is null
|
||||
|
@ -298,7 +303,7 @@ public static Predicate allPredicate(Predicate[] predicates) {
|
|||
* predicates are true. The predicates are checked in iterator order.
|
||||
*
|
||||
* @param predicates a collection of predicates to check, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>all</code> predicate
|
||||
* @throws IllegalArgumentException if the predicates collection is null
|
||||
* @throws IllegalArgumentException if the predicates collection has less than 2 elements
|
||||
* @throws IllegalArgumentException if any predicate in the collection is null
|
||||
|
@ -313,7 +318,7 @@ public static Predicate allPredicate(Collection predicates) {
|
|||
*
|
||||
* @param predicate1 the first predicate, may not be null
|
||||
* @param predicate2 the second predicate, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>or</code> predicate
|
||||
* @throws IllegalArgumentException if either predicate is null
|
||||
*/
|
||||
public static Predicate orPredicate(Predicate predicate1, Predicate predicate2) {
|
||||
|
@ -325,7 +330,7 @@ public static Predicate orPredicate(Predicate predicate1, Predicate predicate2)
|
|||
* predicates are true.
|
||||
*
|
||||
* @param predicates an array of predicates to check, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>any</code> predicate
|
||||
* @throws IllegalArgumentException if the predicates array is null
|
||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
||||
* @throws IllegalArgumentException if any predicate in the array is null
|
||||
|
@ -339,7 +344,7 @@ public static Predicate anyPredicate(Predicate[] predicates) {
|
|||
* predicates are true. The predicates are checked in iterator order.
|
||||
*
|
||||
* @param predicates a collection of predicates to check, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>any</code> predicate
|
||||
* @throws IllegalArgumentException if the predicates collection is null
|
||||
* @throws IllegalArgumentException if the predicates collection has less than 2 elements
|
||||
* @throws IllegalArgumentException if any predicate in the collection is null
|
||||
|
@ -354,7 +359,7 @@ public static Predicate anyPredicate(Collection predicates) {
|
|||
*
|
||||
* @param predicate1 the first predicate, may not be null
|
||||
* @param predicate2 the second predicate, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>either</code> predicate
|
||||
* @throws IllegalArgumentException if either predicate is null
|
||||
*/
|
||||
public static Predicate eitherPredicate(Predicate predicate1, Predicate predicate2) {
|
||||
|
@ -366,7 +371,7 @@ public static Predicate eitherPredicate(Predicate predicate1, Predicate predicat
|
|||
* predicates are true.
|
||||
*
|
||||
* @param predicates an array of predicates to check, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>one</code> predicate
|
||||
* @throws IllegalArgumentException if the predicates array is null
|
||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
||||
* @throws IllegalArgumentException if any predicate in the array is null
|
||||
|
@ -380,7 +385,7 @@ public static Predicate onePredicate(Predicate[] predicates) {
|
|||
* predicates are true. The predicates are checked in iterator order.
|
||||
*
|
||||
* @param predicates a collection of predicates to check, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>one</code> predicate
|
||||
* @throws IllegalArgumentException if the predicates collection is null
|
||||
* @throws IllegalArgumentException if the predicates collection has less than 2 elements
|
||||
* @throws IllegalArgumentException if any predicate in the collection is null
|
||||
|
@ -395,7 +400,7 @@ public static Predicate onePredicate(Collection predicates) {
|
|||
*
|
||||
* @param predicate1 the first predicate, may not be null
|
||||
* @param predicate2 the second predicate, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>neither</code> predicate
|
||||
* @throws IllegalArgumentException if either predicate is null
|
||||
*/
|
||||
public static Predicate neitherPredicate(Predicate predicate1, Predicate predicate2) {
|
||||
|
@ -407,7 +412,7 @@ public static Predicate neitherPredicate(Predicate predicate1, Predicate predica
|
|||
* predicates are true.
|
||||
*
|
||||
* @param predicates an array of predicates to check, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>none</code> predicate
|
||||
* @throws IllegalArgumentException if the predicates array is null
|
||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
||||
* @throws IllegalArgumentException if any predicate in the array is null
|
||||
|
@ -425,7 +430,7 @@ public static Predicate nonePredicate(Predicate[] predicates) {
|
|||
* predicates are true. The predicates are checked in iterator order.
|
||||
*
|
||||
* @param predicates a collection of predicates to check, may not be null
|
||||
* @return the predicate
|
||||
* @return the <code>none</code> predicate
|
||||
* @throws IllegalArgumentException if the predicates collection is null
|
||||
* @throws IllegalArgumentException if the predicates collection has less than 2 elements
|
||||
* @throws IllegalArgumentException if any predicate in the collection is null
|
||||
|
@ -443,12 +448,12 @@ public static Predicate nonePredicate(Collection predicates) {
|
|||
* returns false and vice versa.
|
||||
*
|
||||
* @param predicate the predicate to not
|
||||
* @return the predicate
|
||||
* @return the <code>not</code> predicate
|
||||
* @throws IllegalArgumentException if the predicate is null
|
||||
*/
|
||||
public static Predicate notPredicate(Predicate predicate) {
|
||||
if (predicate == null) {
|
||||
throw new IllegalArgumentException("NotPredicate: The predicate must not be null");
|
||||
throw new IllegalArgumentException("The predicate must not be null");
|
||||
}
|
||||
return new NotPredicate(predicate);
|
||||
}
|
||||
|
@ -462,12 +467,12 @@ public static Predicate notPredicate(Predicate predicate) {
|
|||
* will be thrown.
|
||||
*
|
||||
* @param transformer the transformer to wrap, may not be null
|
||||
* @return the predicate
|
||||
* @return the transformer wrapping predicate
|
||||
* @throws IllegalArgumentException if the transformer is null
|
||||
*/
|
||||
public static Predicate asPredicate(Transformer transformer) {
|
||||
if (transformer == null) {
|
||||
throw new IllegalArgumentException("TransformerPredicate: The transformer to call must not be null");
|
||||
throw new IllegalArgumentException("The transformer to call must not be null");
|
||||
}
|
||||
return new TransformerPredicate(transformer);
|
||||
}
|
||||
|
@ -531,11 +536,11 @@ public static Predicate nullIsTruePredicate(Predicate predicate){
|
|||
*/
|
||||
private static Predicate[] validate(Collection predicates) {
|
||||
if (predicates == null) {
|
||||
throw new IllegalArgumentException("PredicateUtils: The predicate collection must not be null");
|
||||
throw new IllegalArgumentException("The predicate collection must not be null");
|
||||
}
|
||||
if (predicates.size() < 2) {
|
||||
throw new IllegalArgumentException(
|
||||
"PredicateUtils: At least 2 predicates must be specified in the predicate collection, size was " + predicates.size());
|
||||
"At least 2 predicates must be specified in the predicate collection, size was " + predicates.size());
|
||||
}
|
||||
// convert to array like this to guarantee iterator() ordering
|
||||
Predicate[] preds = new Predicate[predicates.size()];
|
||||
|
@ -543,7 +548,7 @@ private static Predicate[] validate(Collection predicates) {
|
|||
for (Iterator it = predicates.iterator(); it.hasNext();) {
|
||||
preds[i] = (Predicate) it.next();
|
||||
if (preds[i] == null) {
|
||||
throw new IllegalArgumentException("PredicateUtils: The predicate collection must not contain a null predicate, index " + i + " was null");
|
||||
throw new IllegalArgumentException("The predicate collection must not contain a null predicate, index " + i + " was null");
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
@ -558,16 +563,16 @@ private static Predicate[] validate(Collection predicates) {
|
|||
*/
|
||||
private static Predicate[] validate(Predicate[] predicates) {
|
||||
if (predicates == null) {
|
||||
throw new IllegalArgumentException("PredicateUtils: The predicate array must not be null");
|
||||
throw new IllegalArgumentException("The predicate array must not be null");
|
||||
}
|
||||
if (predicates.length < 2) {
|
||||
throw new IllegalArgumentException(
|
||||
"PredicateUtils: At least 2 predicates must be specified in the predicate array, size was " + predicates.length);
|
||||
"At least 2 predicates must be specified in the predicate array, size was " + predicates.length);
|
||||
}
|
||||
Predicate[] preds = new Predicate[predicates.length];
|
||||
for (int i = 0; i < predicates.length; i++) {
|
||||
if (predicates[i] == null) {
|
||||
throw new IllegalArgumentException("PredicateUtils: The predicate array must not contain a null predicate, index " + i + " was null");
|
||||
throw new IllegalArgumentException("The predicate array must not contain a null predicate, index " + i + " was null");
|
||||
}
|
||||
preds[i] = predicates[i];
|
||||
}
|
||||
|
@ -604,7 +609,7 @@ public boolean evaluate(Object object) {
|
|||
* ConstantPredicate returns the same instance each time.
|
||||
*/
|
||||
private static class ConstantPredicate implements Predicate, Serializable {
|
||||
|
||||
/** The constant value to return each time */
|
||||
private final boolean iConstant;
|
||||
|
||||
/**
|
||||
|
@ -630,6 +635,7 @@ public boolean evaluate(Object object) {
|
|||
* AllPredicate returns true if all predicates return true
|
||||
*/
|
||||
private static class AllPredicate implements Predicate, Serializable {
|
||||
/** The array of predicates to call */
|
||||
private final Predicate[] iPredicates;
|
||||
|
||||
/**
|
||||
|
@ -660,6 +666,7 @@ public boolean evaluate(Object object) {
|
|||
* AnyPredicate returns true if one of the predicates return true
|
||||
*/
|
||||
private static class AnyPredicate implements Predicate, Serializable {
|
||||
/** The array of predicates to call */
|
||||
private final Predicate[] iPredicates;
|
||||
|
||||
/**
|
||||
|
@ -690,6 +697,7 @@ public boolean evaluate(Object object) {
|
|||
* OnePredicate returns true if only one of the predicates return true
|
||||
*/
|
||||
private static class OnePredicate implements Predicate, Serializable {
|
||||
/** The array of predicates to call */
|
||||
private final Predicate[] iPredicates;
|
||||
|
||||
/**
|
||||
|
@ -724,6 +732,7 @@ public boolean evaluate(Object object) {
|
|||
* NotPredicate returns the opposite of the wrapped predicate
|
||||
*/
|
||||
private static class NotPredicate implements Predicate, Serializable {
|
||||
/** The predicate to call */
|
||||
private final Predicate iPredicate;
|
||||
|
||||
/**
|
||||
|
@ -749,6 +758,7 @@ public boolean evaluate(Object object) {
|
|||
* InstanceofPredicate checks the type of an object
|
||||
*/
|
||||
private static class InstanceofPredicate implements Predicate, Serializable {
|
||||
/** The type to check for */
|
||||
private final Class iType;
|
||||
|
||||
/**
|
||||
|
@ -774,6 +784,7 @@ public boolean evaluate(Object object) {
|
|||
* EqualPredicate that checks if the object is a particular value by equals().
|
||||
*/
|
||||
private static class EqualPredicate implements Predicate, Serializable {
|
||||
/** The object to compare to */
|
||||
private final Object iValue;
|
||||
|
||||
/**
|
||||
|
@ -799,6 +810,7 @@ public boolean evaluate(Object object) {
|
|||
* IdentityPredicate that checks if the object is a particular value by identity.
|
||||
*/
|
||||
private static class IdentityPredicate implements Predicate, Serializable {
|
||||
/** The object to compare identity to */
|
||||
private final Object iValue;
|
||||
|
||||
/**
|
||||
|
@ -826,6 +838,7 @@ public boolean evaluate(Object object) {
|
|||
* again using equals().
|
||||
*/
|
||||
private static class UniquePredicate implements Predicate, Serializable {
|
||||
/** The set of previously seen objects */
|
||||
private final Set iSet = new HashSet();
|
||||
|
||||
/**
|
||||
|
@ -851,7 +864,7 @@ public boolean evaluate(Object object) {
|
|||
* TransformerPredicate returns the result of the Transformer as a boolean.
|
||||
*/
|
||||
private static class TransformerPredicate implements Predicate, Serializable {
|
||||
|
||||
/** The transformer to call */
|
||||
private final Transformer iTransformer;
|
||||
|
||||
/**
|
||||
|
@ -889,6 +902,7 @@ public boolean evaluate(Object object) {
|
|||
* NullIsExceptionPredicate returns an exception if null is passed in.
|
||||
*/
|
||||
private static class NullIsExceptionPredicate implements Predicate, Serializable {
|
||||
/** The predicate to call */
|
||||
private final Predicate iPredicate;
|
||||
|
||||
/**
|
||||
|
@ -917,6 +931,7 @@ public boolean evaluate(Object object){
|
|||
* NullIsFalsePredicate returns false if null is passed in.
|
||||
*/
|
||||
private static class NullIsFalsePredicate implements Predicate, Serializable {
|
||||
/** The predicate to call */
|
||||
private final Predicate iPredicate;
|
||||
|
||||
/**
|
||||
|
@ -945,6 +960,7 @@ public boolean evaluate(Object object){
|
|||
* NullIsTruePredicate returns true if null is passed in.
|
||||
*/
|
||||
private static class NullIsTruePredicate implements Predicate, Serializable {
|
||||
/** The predicate to call */
|
||||
private final Predicate iPredicate;
|
||||
|
||||
/**
|
||||
|
|
|
@ -59,10 +59,9 @@
|
|||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <code>TransformerUtils</code> provides reference implementations and
|
||||
* utilities for the Transformer pattern interface. The supplied transformers are:
|
||||
* utilities for the Transformer functor interface. The supplied transformers are:
|
||||
* <ul>
|
||||
* <li>Invoker - returns the result of a method call on the input object
|
||||
* <li>Clone - returns a clone of the input object
|
||||
|
@ -82,7 +81,7 @@
|
|||
* All the supplied transformers are Serializable.
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: TransformerUtils.java,v 1.2 2002/11/06 19:16:33 bayard Exp $
|
||||
* @version $Id: TransformerUtils.java,v 1.3 2002/11/14 21:54:49 scolebourne Exp $
|
||||
*/
|
||||
public class TransformerUtils {
|
||||
|
||||
|
@ -178,11 +177,11 @@ public static Transformer constantTransformer(Object constantToReturn) {
|
|||
* @param command the command to run each time in the transformer
|
||||
* @return the transformer.
|
||||
*/
|
||||
public static Transformer asTransformer(Executor command) {
|
||||
if (command == null) {
|
||||
throw new IllegalArgumentException("ExecutorTransformer: The command must not be null");
|
||||
public static Transformer asTransformer(Executor executor) {
|
||||
if (executor == null) {
|
||||
throw new IllegalArgumentException("The executor must not be null");
|
||||
}
|
||||
return new ExecutorTransformer(command);
|
||||
return new ExecutorTransformer(executor);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -194,7 +193,7 @@ public static Transformer asTransformer(Executor command) {
|
|||
*/
|
||||
public static Transformer asTransformer(Predicate predicate) {
|
||||
if (predicate == null) {
|
||||
throw new IllegalArgumentException("PredicateTransformer: The predicate must not be null");
|
||||
throw new IllegalArgumentException("The predicate must not be null");
|
||||
}
|
||||
return new PredicateTransformer(predicate);
|
||||
}
|
||||
|
@ -208,7 +207,7 @@ public static Transformer asTransformer(Predicate predicate) {
|
|||
*/
|
||||
public static Transformer asTransformer(Factory factory) {
|
||||
if (factory == null) {
|
||||
throw new IllegalArgumentException("FactoryTransformer: The factory must not be null");
|
||||
throw new IllegalArgumentException("The factory must not be null");
|
||||
}
|
||||
return new FactoryTransformer(factory);
|
||||
}
|
||||
|
@ -258,7 +257,7 @@ public static Transformer chainedTransformer(Transformer[] transformers) {
|
|||
public static Transformer chainedTransformer(Collection transformers) {
|
||||
Transformer[] trs = null;
|
||||
if (transformers == null) {
|
||||
throw new IllegalArgumentException("ChainedTransformer: The transformer collection must not be null");
|
||||
throw new IllegalArgumentException("The transformer collection must not be null");
|
||||
}
|
||||
// convert to array like this to guarantee iterator() ordering
|
||||
trs = new Transformer[transformers.size()];
|
||||
|
@ -346,7 +345,7 @@ public static Transformer switchTransformer(Map predicatesAndTransformers) {
|
|||
Transformer[] trs = null;
|
||||
Predicate[] preds = null;
|
||||
if (predicatesAndTransformers == null) {
|
||||
throw new IllegalArgumentException("SwitchTransformer: The predicate and transformer map must not be null");
|
||||
throw new IllegalArgumentException("The predicate and transformer map must not be null");
|
||||
}
|
||||
// convert to array like this to guarantee iterator() ordering
|
||||
Transformer def = (Transformer) predicatesAndTransformers.remove(null);
|
||||
|
@ -370,7 +369,7 @@ private static Transformer switchTransformerInternal(Predicate[] predicates, Tra
|
|||
validate(predicates);
|
||||
validate(transformers);
|
||||
if (predicates.length != transformers.length) {
|
||||
throw new IllegalArgumentException("SwitchTransformer: The predicate and transformer arrays must be the same size");
|
||||
throw new IllegalArgumentException("The predicate and transformer arrays must be the same size");
|
||||
}
|
||||
if (defaultTransformer == null) {
|
||||
defaultTransformer = nullTransformer();
|
||||
|
@ -397,7 +396,7 @@ public static Transformer switchMapTransformer(Map objectsAndTransformers) {
|
|||
Transformer[] trs = null;
|
||||
Predicate[] preds = null;
|
||||
if (objectsAndTransformers == null) {
|
||||
throw new IllegalArgumentException("SwitchEqualsTransformer: The obejct and transformer map must not be null");
|
||||
throw new IllegalArgumentException("The obejct and transformer map must not be null");
|
||||
}
|
||||
Transformer def = (Transformer) objectsAndTransformers.remove(null);
|
||||
int size = objectsAndTransformers.size();
|
||||
|
@ -446,7 +445,7 @@ public static Transformer instantiateTransformer(Class[] paramTypes, Object[] ar
|
|||
*/
|
||||
public static Transformer mapTransformer(Map map) {
|
||||
if (map == null) {
|
||||
throw new IllegalArgumentException("MapTransformer: The map must not be null");
|
||||
throw new IllegalArgumentException("The map must not be null");
|
||||
}
|
||||
return new MapTransformer(map);
|
||||
}
|
||||
|
@ -503,16 +502,16 @@ private static Predicate[] copy(Predicate[] predicates) {
|
|||
*/
|
||||
private static void validate(Predicate[] predicates) {
|
||||
if (predicates == null) {
|
||||
throw new IllegalArgumentException("TransformerUtils: The predicate array must not be null");
|
||||
throw new IllegalArgumentException("The predicate array must not be null");
|
||||
}
|
||||
if (predicates.length < 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"TransformerUtils: At least 1 predicate must be specified in the predicate array, size was " + predicates.length);
|
||||
"At least 1 predicate must be specified in the predicate array, size was " + predicates.length);
|
||||
}
|
||||
for (int i = 0; i < predicates.length; i++) {
|
||||
if (predicates[i] == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"TransformerUtils: The predicate array must not contain a null predicate, index " + i + " was null");
|
||||
"The predicate array must not contain a null predicate, index " + i + " was null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -536,16 +535,16 @@ private static Transformer[] copy(Transformer[] transformers) {
|
|||
*/
|
||||
private static void validate(Transformer[] transformers) {
|
||||
if (transformers == null) {
|
||||
throw new IllegalArgumentException("TransformerUtils: The transformer array must not be null");
|
||||
throw new IllegalArgumentException("The transformer array must not be null");
|
||||
}
|
||||
if (transformers.length < 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"TransformerUtils: At least 1 transformer must be specified in the transformer array, size was " + transformers.length);
|
||||
"At least 1 transformer must be specified in the transformer array, size was " + transformers.length);
|
||||
}
|
||||
for (int i = 0; i < transformers.length; i++) {
|
||||
if (transformers[i] == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"TransformerUtils: The transformer array must not contain a null transformer, index " + i + " was null");
|
||||
"The transformer array must not contain a null transformer, index " + i + " was null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -636,11 +635,11 @@ public Object transform(Object input) {
|
|||
* ConstantTransformer returns the same instance each time.
|
||||
*/
|
||||
private static class ConstantTransformer implements Transformer, Serializable {
|
||||
|
||||
/** The constant to return each time */
|
||||
private final Object iConstant;
|
||||
|
||||
/**
|
||||
* Constructor to store constant
|
||||
* Constructor to store constant.
|
||||
*/
|
||||
private ConstantTransformer(Object constant) {
|
||||
super();
|
||||
|
@ -648,7 +647,7 @@ private ConstantTransformer(Object constant) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Always return constant
|
||||
* Always return constant.
|
||||
*/
|
||||
public Object transform(Object input) {
|
||||
return iConstant;
|
||||
|
@ -662,19 +661,19 @@ public Object transform(Object input) {
|
|||
* ExecutorTransformer executes a Executor object.
|
||||
*/
|
||||
private static class ExecutorTransformer implements Transformer, Serializable {
|
||||
|
||||
/** The executor to call each time */
|
||||
private final Executor iExecutor;
|
||||
|
||||
/**
|
||||
* Constructor to store command
|
||||
* Constructor to store executor.
|
||||
*/
|
||||
private ExecutorTransformer(Executor command) {
|
||||
private ExecutorTransformer(Executor executor) {
|
||||
super();
|
||||
iExecutor = command;
|
||||
iExecutor = executor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exceute the command and return the input
|
||||
* Exceute the executor and return the input.
|
||||
*/
|
||||
public Object transform(Object input) {
|
||||
try {
|
||||
|
@ -694,11 +693,11 @@ public Object transform(Object input) {
|
|||
* PredicateTransformer evaluates a Predicate object.
|
||||
*/
|
||||
private static class PredicateTransformer implements Transformer, Serializable {
|
||||
|
||||
/** The predicate to call each time */
|
||||
private final Predicate iPredicate;
|
||||
|
||||
/**
|
||||
* Constructor to store predicate
|
||||
* Constructor to store predicate.
|
||||
*/
|
||||
private PredicateTransformer(Predicate predicate) {
|
||||
super();
|
||||
|
@ -706,7 +705,7 @@ private PredicateTransformer(Predicate predicate) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Evaluate the predicate and return the result as a Boolean
|
||||
* Evaluate the predicate and return the result as a Boolean.
|
||||
*/
|
||||
public Object transform(Object input) {
|
||||
try {
|
||||
|
@ -725,11 +724,11 @@ public Object transform(Object input) {
|
|||
* FactoryTransformer returns the result of calling a Factory.
|
||||
*/
|
||||
private static class FactoryTransformer implements Transformer, Serializable {
|
||||
|
||||
/** The factory to call each time */
|
||||
private final Factory iFactory;
|
||||
|
||||
/**
|
||||
* Constructor to store factory
|
||||
* Constructor to store factory.
|
||||
*/
|
||||
private FactoryTransformer(Factory factory) {
|
||||
super();
|
||||
|
@ -737,7 +736,7 @@ private FactoryTransformer(Factory factory) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the result of calling the factory
|
||||
* Return the result of calling the factory.
|
||||
*/
|
||||
public Object transform(Object input) {
|
||||
try {
|
||||
|
@ -756,11 +755,11 @@ public Object transform(Object input) {
|
|||
* ChainedTransformer returns the result of calling a list of transformers.
|
||||
*/
|
||||
private static class ChainedTransformer implements Transformer, Serializable {
|
||||
|
||||
/** The array of transformers to call */
|
||||
private final Transformer[] iTransformers;
|
||||
|
||||
/**
|
||||
* Constructor to store params
|
||||
* Constructor to store params.
|
||||
*/
|
||||
private ChainedTransformer(Transformer[] transformers) {
|
||||
super();
|
||||
|
@ -768,7 +767,7 @@ private ChainedTransformer(Transformer[] transformers) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the result of calling a list of transformers
|
||||
* Returns the result of calling a list of transformers.
|
||||
*/
|
||||
public Object transform(Object object) {
|
||||
for (int i = 0; i < iTransformers.length; i++) {
|
||||
|
@ -785,13 +784,15 @@ public Object transform(Object object) {
|
|||
* SwitchTransformer returns the result of the transformer whose predicate returns true.
|
||||
*/
|
||||
private static class SwitchTransformer implements Transformer, Serializable {
|
||||
|
||||
/** The array of predicates to switch on */
|
||||
private final Predicate[] iPredicates;
|
||||
/** The array of transformers to call */
|
||||
private final Transformer[] iTransformers;
|
||||
/** The default transformer called if no predicate matches */
|
||||
private final Transformer iDefault;
|
||||
|
||||
/**
|
||||
* Constructor to store params
|
||||
* Constructor to store params.
|
||||
*/
|
||||
private SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
|
||||
super();
|
||||
|
@ -801,7 +802,7 @@ private SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Tr
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the result of the transformer whose predicate returns true
|
||||
* Returns the result of the transformer whose predicate returns true.
|
||||
*/
|
||||
public Object transform(Object input) {
|
||||
for (int i = 0; i < iPredicates.length; i++) {
|
||||
|
@ -817,15 +818,16 @@ public Object transform(Object input) {
|
|||
//----------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* InstantiateTransformer returns the result of calling a Factory.
|
||||
* InstantiateTransformer returns the result of instantiating the input Class object.
|
||||
*/
|
||||
private static class InstantiateTransformer implements Transformer, Serializable {
|
||||
|
||||
/** The array of reflection parameter types */
|
||||
private final Class[] iParamTypes;
|
||||
/** The array of reflection arguments */
|
||||
private final Object[] iArgs;
|
||||
|
||||
/**
|
||||
* Constructor to store params
|
||||
* Constructor to store params.
|
||||
*/
|
||||
private InstantiateTransformer(Class[] paramTypes, Object[] args) {
|
||||
super();
|
||||
|
@ -844,7 +846,7 @@ private InstantiateTransformer(Class[] paramTypes, Object[] args) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the result of calling the factory
|
||||
* Return the result of instantiating the input Class object.
|
||||
*/
|
||||
public Object transform(Object input) {
|
||||
try {
|
||||
|
@ -870,11 +872,11 @@ public Object transform(Object input) {
|
|||
* MapTransformer returns the result by looking up in the map.
|
||||
*/
|
||||
private static class MapTransformer implements Transformer, Serializable {
|
||||
|
||||
/** The map of data to lookup in */
|
||||
private final Map iMap;
|
||||
|
||||
/**
|
||||
* Constructor to store map
|
||||
* Constructor to store map.
|
||||
*/
|
||||
private MapTransformer(Map map) {
|
||||
super();
|
||||
|
@ -882,7 +884,7 @@ private MapTransformer(Map map) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the result by looking up in the map
|
||||
* Returns the result by looking up in the map.
|
||||
*/
|
||||
public Object transform(Object input) {
|
||||
return iMap.get(input);
|
||||
|
@ -897,12 +899,15 @@ public Object transform(Object input) {
|
|||
* the input object.
|
||||
*/
|
||||
private static class InvokerTransformer implements Transformer, Serializable {
|
||||
/** The method name to call */
|
||||
private final String iMethodName;
|
||||
/** The array of reflection parameter types */
|
||||
private final Class[] iParamTypes;
|
||||
/** The array of reflection arguments */
|
||||
private final Object[] iArgs;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public InvokerTransformer(String methodName, Class[] paramTypes, Object[] args) {
|
||||
super();
|
||||
|
@ -926,7 +931,7 @@ public InvokerTransformer(String methodName, Class[] paramTypes, Object[] args)
|
|||
}
|
||||
|
||||
/**
|
||||
* Execute the command based on the input object.
|
||||
* Invoke the specified method on the input object.
|
||||
*/
|
||||
public Object transform(Object input) {
|
||||
if (input == null) {
|
||||
|
|
Loading…
Reference in New Issue