commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/predicate TestPredicateUtils.java
Date Sun, 25 Aug 2002 21:17:33 GMT
scolebourne    2002/08/25 14:17:33

  Modified:    pattern/src/java/org/apache/commons/pattern/command
                        CommandUtils.java
               pattern/src/test/org/apache/commons/pattern/command
                        TestCommandUtils.java
               pattern/src/java/org/apache/commons/pattern/factory
                        FactoryUtils.java
               pattern/src/java/org/apache/commons/pattern/transformer
                        TransformerUtils.java
               pattern/src/test/org/apache/commons/pattern/transformer
                        TestTransformerUtils.java
               pattern/src/java/org/apache/commons/pattern/predicate
                        PredicateUtils.java
               pattern/src/test/org/apache/commons/pattern/predicate
                        TestPredicateUtils.java
  Log:
  Ensure array arguments are cloned
  Add invokerXxx types
  Rename adaptors to asXxx naming
  Test
  
  Revision  Changes    Path
  1.2       +88 -60    jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/command/CommandUtils.java
  
  Index: CommandUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/command/CommandUtils.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CommandUtils.java	20 Jul 2002 18:33:10 -0000	1.1
  +++ CommandUtils.java	25 Aug 2002 21:17:32 -0000	1.2
  @@ -54,25 +54,27 @@
   package org.apache.commons.pattern.command;
   
   import java.io.Serializable;
  -import java.lang.reflect.InvocationTargetException;
  -import java.lang.reflect.Method;
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.Map;
   
   import org.apache.commons.pattern.predicate.Predicate;
   import org.apache.commons.pattern.predicate.PredicateUtils;
  +import org.apache.commons.pattern.transformer.Transformer;
  +import org.apache.commons.pattern.transformer.TransformerException;
  +import org.apache.commons.pattern.transformer.TransformerUtils;
   /**
    * <code>CommandUtils</code> provides reference implementations and utilities
    * for the Command pattern interface. The supplied commands are:
    * <ul>
  - * <li>Invoke - invokes a method on the input object
  + * <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>NOP - does nothing
    * <li>Exception - always throws an exception
    * </ul>
  @@ -120,6 +122,20 @@
       }
   
       /**
  +     * Creates a Command that calls a Factory each time the transformer is used.
  +     * The transformer will return the value returned by the factory.
  +     *
  +     * @param transformer  the transformer to run each time in the command
  +     * @return the command.
  +     */
  +    public static Command asCommand(Transformer transformer) {
  +        if (transformer == null) {
  +            throw new IllegalArgumentException("TransformerCommand: The transformer must not be null");
  +        }
  +        return new TransformerCommand(transformer);
  +    }
  +
  +    /**
        * Creates a Command that will call the command <code>count</code> times.
        *
        * @param count  the number of times to loop
  @@ -183,8 +199,8 @@
        * @return the command
        * @throws IllegalArgumentException if the method name is null
        */
  -    public static Command invokeCommand(String methodName) {
  -        return new InvokeCommand(methodName, null, null);
  +    public static Command invokerCommand(String methodName) {
  +        return asCommand(TransformerUtils.invokerTransformer(methodName, null, null));
       }
   
       /**
  @@ -198,8 +214,8 @@
        * @throws IllegalArgumentException if the method name is null
        * @throws IllegalArgumentException if the paramTypes and args don't match
        */
  -    public static Command invokeCommand(String methodName, Class[] paramTypes, Object[] args) {
  -        return new InvokeCommand(methodName, paramTypes, args);
  +    public static Command invokerCommand(String methodName, Class[] paramTypes, Object[] args) {
  +        return asCommand(TransformerUtils.invokerTransformer(methodName, paramTypes, args));
       }
   
       /**
  @@ -212,7 +228,9 @@
        * @throws IllegalArgumentException if either command is null
        */
       public static Command chainedCommand(Command command1, Command command2) {
  -        return chainedCommand(new Command[] { command1, command2 });
  +        Command[] commands = new Command[] { command1, command2 };
  +        validate(commands);
  +        return new ChainedCommand(commands);
       }
   
       /**
  @@ -226,6 +244,7 @@
        * @throws IllegalArgumentException if any command in the array is null
        */
       public static Command chainedCommand(Command[] commands) {
  +        commands = copy(commands);
           validate(commands);
           return new ChainedCommand(commands);
       }
  @@ -242,17 +261,17 @@
        * @throws IllegalArgumentException if any command in the collection is null
        */
       public static Command chainedCommand(Collection commands) {
  -        Command[] trs = null;
           if (commands == null) {
               throw new IllegalArgumentException("ChainedCommand: The command collection must not be null");
           }
           // convert to array like this to guarantee iterator() ordering
  -        trs = new Command[commands.size()];
  +        Command[] cmds = new Command[commands.size()];
           int i = 0;
           for (Iterator it = commands.iterator(); it.hasNext();) {
  -            trs[i++] = (Command) it.next();
  +            cmds[i++] = (Command) it.next();
           }
  -        return chainedCommand(trs);
  +        validate(cmds);
  +        return new ChainedCommand(cmds);
       }
   
       /**
  @@ -267,7 +286,7 @@
        * @throws IllegalArgumentException if either command is null
        */
       public static Command switchCommand(Predicate predicate, Command trueCommand, Command falseCommand) {
  -        return switchCommand(new Predicate[] { predicate }, new Command[] { trueCommand }, falseCommand);
  +        return switchCommandInternal(new Predicate[] { predicate }, new Command[] { trueCommand }, falseCommand);
       }
   
       /**
  @@ -285,7 +304,7 @@
        * @throws IllegalArgumentException if the arrays are different sizes
        */
       public static Command switchCommand(Predicate[] predicates, Command[] commands) {
  -        return switchCommand(predicates, commands, null);
  +        return switchCommandInternal(copy(predicates), copy(commands), null);
       }
   
       /**
  @@ -305,17 +324,9 @@
        * @throws IllegalArgumentException if the arrays are different sizes
        */
       public static Command switchCommand(Predicate[] predicates, Command[] commands, Command defaultCommand) {
  -        validate(predicates);
  -        validate(commands);
  -        if (predicates.length != commands.length) {
  -            throw new IllegalArgumentException("SwitchCommand: The predicate and command arrays must be the same size");
  -        }
  -        if (defaultCommand == null) {
  -            defaultCommand = nopCommand();
  -        }
  -        return new SwitchCommand(predicates, commands, defaultCommand);
  +        return switchCommandInternal(copy(predicates), copy(commands), defaultCommand);
       }
  -
  +    
       /**
        * Create a new Command that calls one of the commands depending 
        * on the predicates. 
  @@ -352,7 +363,22 @@
               trs[i] = (Command) entry.getValue();
               i++;
           }
  -        return switchCommand(preds, trs, def);
  +        return switchCommandInternal(preds, trs, def);
  +    }
  +
  +    /**
  +     * Validate input and create command
  +     */
  +    private static Command switchCommandInternal(Predicate[] predicates, Command[] commands, Command defaultCommand) {
  +        validate(predicates);
  +        validate(commands);
  +        if (predicates.length != commands.length) {
  +            throw new IllegalArgumentException("SwitchCommand: The predicate and command arrays must be the same size");
  +        }
  +        if (defaultCommand == null) {
  +            defaultCommand = nopCommand();
  +        }
  +        return new SwitchCommand(predicates, commands, defaultCommand);
       }
   
       /**
  @@ -391,6 +417,18 @@
       }
   
       /**
  +     * Copy method
  +     * 
  +     * @param predicates  the predicates to copy
  +     */
  +    private static Predicate[] copy(Predicate[] predicates) {
  +        if (predicates == null) {
  +            return null;
  +        }
  +        return (Predicate[]) predicates.clone();
  +    }
  +    
  +    /**
        * Validate method
        * 
        * @param predicates  the predicates to validate
  @@ -411,6 +449,18 @@
       }
   
       /**
  +     * Copy method
  +     * 
  +     * @param commands  the commands to copy
  +     */
  +    private static Command[] copy(Command[] commands) {
  +        if (commands == null) {
  +            return null;
  +        }
  +        return (Command[]) commands.clone();
  +    }
  +    
  +    /**
        * Validate method
        * 
        * @param commands  the commands to validate
  @@ -476,55 +526,33 @@
           }
       }
   
  -    // InvokeCommand
  +    // TransformerCommand
       //----------------------------------------------------------------------------------
   
       /**
  -     * InvokeCommand creates objects using reflection.
  +     * TransformerCommand returns the result of calling a Transformer.
        */
  -    private static class InvokeCommand implements Command, Serializable {
  +    private static class TransformerCommand implements Command, Serializable {
   
  -        private final String iMethodName;
  -        private final Class[] iParamTypes;
  -        private final Object[] iArgs;
  +        private final Transformer iTransformer;
   
           /**
  -         * Constructor
  +         * Constructor to store factory
            */
  -        public InvokeCommand(String methodName, Class[] paramTypes, Object[] args) {
  +        private TransformerCommand(Transformer transformer) {
               super();
  -            if (methodName == null) {
  -                throw new IllegalArgumentException("InvokeCommand: The method to invoke must not be null");
  -            }
  -            if (((paramTypes == null) && (args != null))
  -                || ((paramTypes != null) && (args == null))
  -                || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
  -                throw new IllegalArgumentException("InvokeCommand: The parameter types must match the arguments");
  -            }
  -
  -            iMethodName = methodName;
  -            iParamTypes = paramTypes;
  -            iArgs = args;
  +            iTransformer = transformer;
           }
   
           /**
  -         * Execute the command based on the input object.
  +         * Return the result of calling the factory
            */
           public void execute(Object input) {
  -            if (input == null) {
  -                return;
  -            }
               try {
  -                Class cls = input.getClass();
  -                Method method = cls.getMethod(iMethodName, iParamTypes);
  -                method.invoke(input, iArgs);
  -
  -            } catch (NoSuchMethodException ex) {
  -                throw new CommandException("InvokeCommand: The method must exist and be public ");
  -            } catch (IllegalAccessException ex) {
  -                throw new CommandException("InvokeCommand: Method must be public", ex);
  -            } catch (InvocationTargetException ex) {
  -                throw new CommandException("InvokeCommand: Method threw an exception", ex);
  +                iTransformer.transform(input);
  +
  +            } catch (TransformerException ex) {
  +                throw new CommandException("TransformerCommand: " + ex.getMessage(), ex);
               }
           }
       }
  
  
  
  1.2       +3 -3      jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/command/TestCommandUtils.java
  
  Index: TestCommandUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/command/TestCommandUtils.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestCommandUtils.java	20 Jul 2002 18:33:36 -0000	1.1
  +++ TestCommandUtils.java	25 Aug 2002 21:17:33 -0000	1.2
  @@ -148,10 +148,10 @@
   
       public void testInvokeCommand() {
           StringBuffer buf = new StringBuffer("Hello");
  -        CommandUtils.invokeCommand("reverse").execute(buf);
  +        CommandUtils.invokerCommand("reverse").execute(buf);
           assertEquals("olleH", buf.toString());
           buf = new StringBuffer("Hello");
  -        CommandUtils.invokeCommand("setLength", new Class[] {Integer.TYPE}, new Object[] {new Integer(2)}).execute(buf);
  +        CommandUtils.invokerCommand("setLength", new Class[] {Integer.TYPE}, new Object[] {new Integer(2)}).execute(buf);
           assertEquals("He", buf.toString());
       }
   
  
  
  
  1.8       +7 -2      jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/factory/FactoryUtils.java
  
  Index: FactoryUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/factory/FactoryUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FactoryUtils.java	20 Jul 2002 14:45:07 -0000	1.7
  +++ FactoryUtils.java	25 Aug 2002 21:17:33 -0000	1.8
  @@ -364,8 +364,13 @@
               }
   
               iClassToInstantiate = classToInstantiate;
  -            iParamTypes = paramTypes;
  -            iArgs = args;
  +            if ((paramTypes == null) && (args == null)) {
  +                iParamTypes = null;
  +                iArgs = null;
  +            } else {
  +                iParamTypes = (Class[]) paramTypes.clone();
  +                iArgs = (Object[]) args.clone();
  +            }
   
               findConstructor();
           }
  
  
  
  1.2       +161 -27   jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/transformer/TransformerUtils.java
  
  Index: TransformerUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/transformer/TransformerUtils.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TransformerUtils.java	20 Jul 2002 14:47:57 -0000	1.1
  +++ TransformerUtils.java	25 Aug 2002 21:17:33 -0000	1.2
  @@ -54,6 +54,8 @@
   package org.apache.commons.pattern.transformer;
   
   import java.io.Serializable;
  +import java.lang.reflect.InvocationTargetException;
  +import java.lang.reflect.Method;
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.Map;
  @@ -70,7 +72,7 @@
    * <code>TransformerUtils</code> provides reference implementations and 
    * utilities for the Transformer pattern interface. The supplied transformers are:
    * <ul>
  - * <li>NOP - returns the input object, which should be immutable
  + * <li>Invoker - returns the result of a method call on the input object
    * <li>Clone - returns a clone of the input object
    * <li>Constant - always returns the same object
    * <li>Command - performs a Command and returns the input object
  @@ -82,6 +84,7 @@
    * <li>Instantiate - the Class input object is instantiated
    * <li>Map - returns an object from a supplied Map
    * <li>Null - always returns null
  + * <li>NOP - returns the input object, which should be immutable
    * <li>Exception - always throws an exception
    * </ul>
    * All the supplied transformers are Serializable.
  @@ -183,7 +186,7 @@
        * @param command  the command to run each time in the transformer
        * @return the transformer.
        */
  -    public static Transformer commandTransformer(Command command) {
  +    public static Transformer asTransformer(Command command) {
           if (command == null) {
               throw new IllegalArgumentException("CommandTransformer: The command must not be null");
           }
  @@ -197,7 +200,7 @@
        * @param predicate  the predicate to run each time in the transformer
        * @return the transformer.
        */
  -    public static Transformer predicateTransformer(Predicate predicate) {
  +    public static Transformer asTransformer(Predicate predicate) {
           if (predicate == null) {
               throw new IllegalArgumentException("PredicateTransformer: The predicate must not be null");
           }
  @@ -211,7 +214,7 @@
        * @param factory  the factory to run each time in the transformer
        * @return the transformer.
        */
  -    public static Transformer factoryTransformer(Factory factory) {
  +    public static Transformer asTransformer(Factory factory) {
           if (factory == null) {
               throw new IllegalArgumentException("FactoryTransformer: The factory must not be null");
           }
  @@ -228,7 +231,9 @@
        * @throws IllegalArgumentException if either transformer is null
        */
       public static Transformer chainedTransformer(Transformer transformer1, Transformer transformer2) {
  -        return chainedTransformer(new Transformer[] { transformer1, transformer2 });
  +        Transformer[] trs = new Transformer[] {transformer1, transformer2};
  +        validate(trs);
  +        return new ChainedTransformer(trs);
       }
   
       /**
  @@ -242,8 +247,9 @@
        * @throws IllegalArgumentException if any transformer in the array is null
        */
       public static Transformer chainedTransformer(Transformer[] transformers) {
  -        validate(transformers);
  -        return new ChainedTransformer(transformers);
  +        Transformer[] trs = copy(transformers);
  +        validate(trs);
  +        return new ChainedTransformer(trs);
       }
   
       /**
  @@ -268,7 +274,8 @@
           for (Iterator it = transformers.iterator(); it.hasNext();) {
               trs[i++] = (Transformer) it.next();
           }
  -        return chainedTransformer(trs);
  +        validate(trs);
  +        return new ChainedTransformer(trs);
       }
   
       /**
  @@ -283,7 +290,7 @@
        * @throws IllegalArgumentException if either transformer is null
        */
       public static Transformer switchTransformer(Predicate predicate, Transformer trueTransformer, Transformer falseTransformer) {
  -        return switchTransformer(new Predicate[] { predicate }, new Transformer[] { trueTransformer }, falseTransformer);
  +        return switchTransformerInternal(new Predicate[] { predicate }, new Transformer[] { trueTransformer }, falseTransformer);
       }
   
       /**
  @@ -301,7 +308,7 @@
        * @throws IllegalArgumentException if the arrays are different sizes
        */
       public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers) {
  -        return switchTransformer(predicates, transformers, null);
  +        return switchTransformerInternal(copy(predicates), copy(transformers), null);
       }
   
       /**
  @@ -321,15 +328,7 @@
        * @throws IllegalArgumentException if the arrays are different sizes
        */
       public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
  -        validate(predicates);
  -        validate(transformers);
  -        if (predicates.length != transformers.length) {
  -            throw new IllegalArgumentException("SwitchTransformer: The predicate and transformer arrays must be the same size");
  -        }
  -        if (defaultTransformer == null) {
  -            defaultTransformer = nullTransformer();
  -        }
  -        return new SwitchTransformer(predicates, transformers, defaultTransformer);
  +        return switchTransformerInternal(copy(predicates), copy(transformers), defaultTransformer);
       }
   
       /**
  @@ -369,10 +368,25 @@
               trs[i] = (Transformer) entry.getValue();
               i++;
           }
  -        return switchTransformer(preds, trs, def);
  +        return switchTransformerInternal(preds, trs, def);
       }
   
       /**
  +     * Validate input and create transformer
  +     */
  +    private static Transformer switchTransformerInternal(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
  +        validate(predicates);
  +        validate(transformers);
  +        if (predicates.length != transformers.length) {
  +            throw new IllegalArgumentException("SwitchTransformer: The predicate and transformer arrays must be the same size");
  +        }
  +        if (defaultTransformer == null) {
  +            defaultTransformer = nullTransformer();
  +        }
  +        return new SwitchTransformer(predicates, transformers, defaultTransformer);
  +    }
  +    
  +    /**
        * Create a new Transformer that uses the input object as a key to find the
        * transformer to call. 
        * <p>
  @@ -408,7 +422,7 @@
       }
   
       /**
  -     * Gets a Transformer that expects an input Class object that it will instantiate
  +     * Gets a Transformer that expects an input Class object that it will instantiate.
        * 
        * @return the transformer
        */
  @@ -446,6 +460,51 @@
       }
   
       /**
  +     * Gets a Transformer that invokes a method on the input object.
  +     * The method must have no parameters. If the input object is null, 
  +     * null is returned.
  +     * <p>
  +     * For example, <code>TransformerUtils.invokerTransformer("getName");</code>
  +     * will call the <code>getName/code> method on the input object to 
  +     * determine the transformer result.
  +     * 
  +     * @param methodName  the method name to call on the input object, may not be null
  +     * @return the transformer
  +     * @throws IllegalArgumentException if the methodName is null.
  +     */
  +    public static Transformer invokerTransformer(String methodName){
  +        return new InvokerTransformer(methodName, null, null);
  +    }
  +
  +    /**
  +     * Gets a Transformer that invokes a method on the input object.
  +     * The method parameters are specified. If the input object is null, 
  +     * null is returned.
  +     * 
  +     * @param methodName  the name of the method
  +     * @param paramTypes  the parameter types
  +     * @param args  the arguments
  +     * @return the transformer
  +     * @throws IllegalArgumentException if the method name is null
  +     * @throws IllegalArgumentException if the paramTypes and args don't match
  +     */
  +    public static Transformer invokerTransformer(String methodName, Class[] paramTypes, Object[] args){
  +        return new InvokerTransformer(methodName, paramTypes, args);
  +    }
  +
  +    /**
  +     * Copy method
  +     * 
  +     * @param predicates  the predicates to copy
  +     */
  +    private static Predicate[] copy(Predicate[] predicates) {
  +        if (predicates == null) {
  +            return null;
  +        }
  +        return (Predicate[]) predicates.clone();
  +    }
  +    
  +    /**
        * Validate method
        * 
        * @param predicates  the predicates to validate
  @@ -467,6 +526,18 @@
       }
   
       /**
  +     * Copy method
  +     * 
  +     * @param transformers  the transformers to copy
  +     */
  +    private static Transformer[] copy(Transformer[] transformers) {
  +        if (transformers == null) {
  +            return null;
  +        }
  +        return (Transformer[]) transformers.clone();
  +    }
  +    
  +    /**
        * Validate method
        * 
        * @param transformers  the transformers to validate
  @@ -619,7 +690,7 @@
                   return input;
   
               } catch (CommandException ex) {
  -                throw new TransformerException("CommandTransformer", ex);
  +                throw new TransformerException("CommandTransformer: " + ex.getMessage(), ex);
               }
           }
       }
  @@ -650,7 +721,7 @@
                   return new Boolean(iPredicate.evaluate(input));
   
               } catch (PredicateException ex) {
  -                throw new TransformerException("PredicateTransformer", ex);
  +                throw new TransformerException("PredicateTransformer: " + ex.getMessage(), ex);
               }
           }
       }
  @@ -681,7 +752,7 @@
                   return iFactory.create();
   
               } catch (FactoryException ex) {
  -                throw new TransformerException("FactoryTransformer", ex);
  +                throw new TransformerException("FactoryTransformer: " + ex.getMessage(), ex);
               }
           }
       }
  @@ -771,8 +842,13 @@
                   || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
                   throw new IllegalArgumentException("InstantiateTransformer: The parameter types must match the arguments");
               }
  -            iParamTypes = paramTypes;
  -            iArgs = args;
  +            if ((paramTypes == null) && (args == null)) {
  +                iParamTypes = null;
  +                iArgs = null;
  +            } else {
  +                iParamTypes = (Class[]) paramTypes.clone();
  +                iArgs = (Object[]) args.clone();
  +            }
           }
   
           /**
  @@ -818,6 +894,64 @@
            */
           public Object transform(Object input) {
               return iMap.get(input);
  +        }
  +    }
  +
  +    // InvokerTransformer
  +    //----------------------------------------------------------------------------------
  +
  +    /**
  +     * InvokerTransformer returns the result of invoking the specified method on 
  +     * the input object.
  +     */
  +    private static class InvokerTransformer implements Transformer, Serializable {
  +        private final String iMethodName;
  +        private final Class[] iParamTypes;
  +        private final Object[] iArgs;
  +
  +        /**
  +         * Constructor
  +         */
  +        public InvokerTransformer(String methodName, Class[] paramTypes, Object[] args) {
  +            super();
  +            if (methodName == null) {
  +                throw new IllegalArgumentException("InvokerTransformer: The method to invoke must not be null");
  +            }
  +            if (((paramTypes == null) && (args != null))
  +                || ((paramTypes != null) && (args == null))
  +                || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) {
  +                throw new IllegalArgumentException("InvokerTransformer: The parameter types must match the arguments");
  +            }
  +
  +            iMethodName = methodName;
  +            if ((paramTypes == null) && (args == null)) {
  +                iParamTypes = null;
  +                iArgs = null;
  +            } else {
  +                iParamTypes = (Class[]) paramTypes.clone();
  +                iArgs = (Object[]) args.clone();
  +            }
  +        }
  +
  +        /**
  +         * Execute the command based on the input object.
  +         */
  +        public Object transform(Object input) {
  +            if (input == null) {
  +                return null;
  +            }
  +            try {
  +                Class cls = input.getClass();
  +                Method method = cls.getMethod(iMethodName, iParamTypes);
  +                return method.invoke(input, iArgs);
  +                
  +            } catch (NoSuchMethodException ex) {
  +                throw new TransformerException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' does not exist");
  +            } catch (IllegalAccessException ex) {
  +                throw new TransformerException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' cannot be accessed");
  +            } catch (InvocationTargetException ex) {
  +                throw new TransformerException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' threw an exception", ex);
  +            }
           }
       }
   
  
  
  
  1.3       +81 -16    jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/transformer/TestTransformerUtils.java
  
  Index: TestTransformerUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/transformer/TestTransformerUtils.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestTransformerUtils.java	20 Jul 2002 18:34:41 -0000	1.2
  +++ TestTransformerUtils.java	25 Aug 2002 21:17:33 -0000	1.3
  @@ -59,12 +59,16 @@
   import java.util.Collections;
   import java.util.Date;
   import java.util.HashMap;
  +import java.util.List;
   import java.util.Map;
   
   import junit.framework.Test;
   import junit.framework.TestSuite;
   
   import org.apache.commons.lang.SerializationException;
  +import org.apache.commons.pattern.command.Command;
  +import org.apache.commons.pattern.command.CommandUtils;
  +import org.apache.commons.pattern.factory.Factory;
   import org.apache.commons.pattern.factory.FactoryUtils;
   import org.apache.commons.pattern.predicate.Predicate;
   import org.apache.commons.pattern.predicate.PredicateUtils;
  @@ -191,12 +195,12 @@
       //------------------------------------------------------------------
   
       public void testCommandTransformer() {
  -//        assertEquals(cObject, TransformerUtils.commandTransformer().transform(null));
  -//        assertEquals(cObject, TransformerUtils.commandTransformer().transform(cObject));
  -//        assertEquals(cObject, TransformerUtils.commandTransformer().transform(cString));
  -//        assertEquals(cObject, TransformerUtils.commandTransformer().transform(cInteger));
  +        assertEquals(null, TransformerUtils.asTransformer(CommandUtils.nopCommand()).transform(null));
  +        assertEquals(cObject, TransformerUtils.asTransformer(CommandUtils.nopCommand()).transform(cObject));
  +        assertEquals(cString, TransformerUtils.asTransformer(CommandUtils.nopCommand()).transform(cString));
  +        assertEquals(cInteger, TransformerUtils.asTransformer(CommandUtils.nopCommand()).transform(cInteger));
           try {
  -            TransformerUtils.commandTransformer(null);
  +            TransformerUtils.asTransformer((Command) null);
           } catch (IllegalArgumentException ex) {
               return;
           }
  @@ -207,12 +211,12 @@
       //------------------------------------------------------------------
   
       public void testPredicateTransformer() {
  -        assertEquals(Boolean.TRUE, TransformerUtils.predicateTransformer(PredicateUtils.truePredicate()).transform(null));
  -        assertEquals(Boolean.TRUE, TransformerUtils.predicateTransformer(PredicateUtils.truePredicate()).transform(cObject));
  -        assertEquals(Boolean.TRUE, TransformerUtils.predicateTransformer(PredicateUtils.truePredicate()).transform(cString));
  -        assertEquals(Boolean.TRUE, TransformerUtils.predicateTransformer(PredicateUtils.truePredicate()).transform(cInteger));
  +        assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(null));
  +        assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(cObject));
  +        assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(cString));
  +        assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(cInteger));
           try {
  -            TransformerUtils.predicateTransformer(null);
  +            TransformerUtils.asTransformer((Predicate) null);
           } catch (IllegalArgumentException ex) {
               return;
           }
  @@ -223,12 +227,12 @@
       //------------------------------------------------------------------
   
       public void testFactoryTransformer() {
  -        assertEquals(null, TransformerUtils.factoryTransformer(FactoryUtils.nullFactory()).transform(null));
  -        assertEquals(null, TransformerUtils.factoryTransformer(FactoryUtils.nullFactory()).transform(cObject));
  -        assertEquals(null, TransformerUtils.factoryTransformer(FactoryUtils.nullFactory()).transform(cString));
  -        assertEquals(null, TransformerUtils.factoryTransformer(FactoryUtils.nullFactory()).transform(cInteger));
  +        assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(null));
  +        assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cObject));
  +        assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cString));
  +        assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cInteger));
           try {
  -            TransformerUtils.factoryTransformer(null);
  +            TransformerUtils.asTransformer((Factory) null);
           } catch (IllegalArgumentException ex) {
               return;
           }
  @@ -463,5 +467,66 @@
           fail();
       }
       
  +    // invokerTransformer
  +    //------------------------------------------------------------------
  +
  +    public void testInvokerTransformer() {
  +        List list = new ArrayList();
  +        assertEquals(new Integer(0), TransformerUtils.invokerTransformer("size").transform(list));
  +        list.add(new Object());
  +        assertEquals(new Integer(1), TransformerUtils.invokerTransformer("size").transform(list));
  +        assertEquals(null, TransformerUtils.invokerTransformer("size").transform(null));
  +    }
  +
  +    public void testInvokerTransformerEx1() {
  +        try {
  +            TransformerUtils.invokerTransformer(null);
  +        } catch (IllegalArgumentException ex) {
  +            return;
  +        }
  +        fail();
  +    }
  +    
  +    public void testInvokerTransformerEx3() {
  +        try {
  +            TransformerUtils.invokerTransformer("noSuchMethod").transform(new Object());
  +        } catch (TransformerException ex) {
  +            return;
  +        }
  +        fail();
  +    }
  +    
  +    // invokerTransformer2
  +    //------------------------------------------------------------------
  +
  +    public void testInvokerTransformer2() {
  +        List list = new ArrayList();
  +        assertEquals(Boolean.FALSE, TransformerUtils.invokerTransformer(
  +            "contains", new Class[] {Object.class}, new Object[] {cString}).transform(list));
  +        list.add(cString);
  +        assertEquals(Boolean.TRUE, TransformerUtils.invokerTransformer(
  +            "contains", new Class[] {Object.class}, new Object[] {cString}).transform(list));
  +        assertEquals(null, TransformerUtils.invokerTransformer(
  +            "contains", new Class[] {Object.class}, new Object[] {cString}).transform(null));
  +    }
  +
  +    public void testInvokerTransformer2Ex1() {
  +        try {
  +            TransformerUtils.invokerTransformer(null, null, null);
  +        } catch (IllegalArgumentException ex) {
  +            return;
  +        }
  +        fail();
  +    }
  +    
  +    public void testInvokerTransformer2Ex3() {
  +        try {
  +            TransformerUtils.invokerTransformer(
  +                "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).transform(new Object());
  +        } catch (TransformerException ex) {
  +            return;
  +        }
  +        fail();
  +    }
       
   }
  
  
  
  1.5       +160 -86   jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/predicate/PredicateUtils.java
  
  Index: PredicateUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/predicate/PredicateUtils.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PredicateUtils.java	9 Aug 2002 23:11:19 -0000	1.4
  +++ PredicateUtils.java	25 Aug 2002 21:17:33 -0000	1.5
  @@ -61,24 +61,26 @@
   
   import org.apache.commons.pattern.transformer.Transformer;
   import org.apache.commons.pattern.transformer.TransformerException;
  +import org.apache.commons.pattern.transformer.TransformerUtils;
   /**
    * <code>PredicateUtils</code> provides reference implementations and utilities
    * for the Predicate pattern interface. The supplied predicates are:
    * <ul>
  - * <li>True - always return true
  - * <li>False - always return false
  - * <li>And/All - true if all of the predicates are true
  - * <li>Or/Any - true if any of the predicates is true
  - * <li>Either/One - true if only one of the predicate is true
  - * <li>Neither/None - true if none of the predicates are true
  - * <li>Not - true if the predicate is false, and vice versa
  + * <li>Invoker - returns the result of a method call on the input object
    * <li>InstanceOf - true if the object is an instanceof a class
    * <li>Equal - true if the object equals() a specified object
    * <li>Identity - true if the object == a specified object
    * <li>Null - true if the object is null
    * <li>NotNull - true if the object is not null
    * <li>Unique - true if the object has not already been evaluated
  + * <li>And/All - true if all of the predicates are true
  + * <li>Or/Any - true if any of the predicates is true
  + * <li>Either/One - true if only one of the predicate is true
  + * <li>Neither/None - true if none of the predicates are true
  + * <li>Not - true if the predicate is false, and vice versa
    * <li>Transformer - wraps a Transformer as a Predicate
  + * <li>True - always return true
  + * <li>False - always return false
    * <li>Exception - always throws an exception
    * <li>NullIsException/NullIsFalse/NullIsTrue - check for null input
    * </ul>
  @@ -118,6 +120,9 @@
           super();
       }
   
  +    // Simple predicates
  +    //-----------------------------------------------------------------------------
  +
       /** 
        * Gets a Predicate that always throws an exception.
        * This could be useful during testing as a placeholder.
  @@ -147,7 +152,7 @@
       }
   
       /**
  -     * Gets a Predicate that checks if the object passed in is null.
  +     * Gets a Predicate that checks if the input object passed in is null.
        * 
        * @return the predicate
        */
  @@ -156,7 +161,7 @@
       }
   
       /**
  -     * Gets a Predicate that checks if the object passed in is not null.
  +     * Gets a Predicate that checks if the input object passed in is not null.
        * 
        * @return the predicate
        */
  @@ -165,7 +170,7 @@
       }
   
       /**
  -     * Create a Predicate that checks if the object passed in equals to the
  +     * Creates a Predicate that checks if the input object is equal to the
        * specified object using equals().
        * 
        * @param value  the value to compare against
  @@ -179,7 +184,7 @@
       }
   
       /**
  -     * Create a Predicate that checks if the object passed in equals to the
  +     * Creates a Predicate that checks if the input object is equal to the
        * specified object by identity.
        * 
        * @param value  the value to compare against
  @@ -191,13 +196,82 @@
           }
           return new IdentityPredicate(value);
       }
  +    
  +    /**
  +     * Creates a Predicate that checks if the object passed in is of
  +     * a particular type, using instanceof.
  +     * 
  +     * @param type  the type to check for, may not be null
  +     * @return the predicate
  +     * @throws IllegalArgumentException if the class is null
  +     */
  +    public static Predicate instanceofPredicate(Class type) {
  +        if (type == null) {
  +            throw new IllegalArgumentException("InstanceofPredicate: The type to check instanceof must not be null");
  +        }
  +        return new InstanceofPredicate(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().
  +     * 
  +     * @return the predicate
  +     */
  +    public static Predicate uniquePredicate() {
  +        // must return new instance each time
  +        return new UniquePredicate();
  +    }
  +
  +    /**
  +     * Creates a Predicate that invokes a method on the input object.
  +     * The method must return either a boolean or a non-null Boolean,
  +     * and have no parameters. If the input object is null, a 
  +     * PredicateException is thrown.
  +     * <p>
  +     * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code>
  +     * will call the <code>isEmpty</code> method on the input object to 
  +     * determine the predicate result.
  +     * 
  +     * @param methodName  the method name to call on the input object, may not be null
  +     * @return the predicate
  +     * @throws IllegalArgumentException if the methodName is null.
  +     */
  +    public static Predicate invokerPredicate(String methodName){
  +        return asPredicate(TransformerUtils.invokerTransformer(methodName));
  +    }
  +
  +    /**
  +     * Creates a Predicate that invokes a method on the input object.
  +     * The method must return either a boolean or a non-null Boolean,
  +     * and have no parameters. If the input object is null, a 
  +     * PredicateException is thrown.
  +     * <p>
  +     * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code>
  +     * will call the <code>isEmpty</code> method on the input object to 
  +     * determine the predicate result.
  +     * 
  +     * @param methodName  the method name to call on the input object, may not be null
  +     * @param paramTypes  the parameter types
  +     * @param args  the arguments
  +     * @return the predicate
  +     * @throws IllegalArgumentException if the method name is null
  +     * @throws IllegalArgumentException if the paramTypes and args don't match
  +     */
  +    public static Predicate invokerPredicate(String methodName, Class[] paramTypes, Object[] args){
  +        return asPredicate(TransformerUtils.invokerTransformer(methodName, paramTypes, args));
  +    }
  +
  +    // Boolean combinations
  +    //-----------------------------------------------------------------------------
   
       /**
        * Create a new Predicate that returns true only if both of the specified
        * predicates are true.
        * 
  -     * @param predicate1  the first predicate
  -     * @param predicate2  the second predicate
  +     * @param predicate1  the first predicate, may not be null
  +     * @param predicate2  the second predicate, may not be null
        * @return the predicate
        * @throws IllegalArgumentException if either predicate is null
        */
  @@ -209,37 +283,36 @@
        * Create a new Predicate that returns true only if all of the specified
        * predicates are true.
        * 
  -     * @param predicates  an array of predicates to check
  +     * @param predicates  an array of predicates to check, may not be null
        * @return the 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
        */
       public static Predicate allPredicate(Predicate[] predicates) {
  -        validate(predicates);
  -        return new AllPredicate(predicates);
  +        return new AllPredicate(validate(predicates));
       }
   
       /**
        * Create a new Predicate that returns true only if all of the specified
        * predicates are true. The predicates are checked in iterator order.
        * 
  -     * @param predicates  an array of predicates to check
  +     * @param predicates  a collection of predicates to check, may not be null
        * @return the 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
        */
       public static Predicate allPredicate(Collection predicates) {
  -        return allPredicate(convertCollection(predicates));
  +        return new AllPredicate(validate(predicates));
       }
   
       /**
        * Create a new Predicate that returns true if either of the specified
        * predicates are true.
        * 
  -     * @param predicate1  the first predicate
  -     * @param predicate2  the second predicate
  +     * @param predicate1  the first predicate, may not be null
  +     * @param predicate2  the second predicate, may not be null
        * @return the predicate
        * @throws IllegalArgumentException if either predicate is null
        */
  @@ -251,37 +324,36 @@
        * Create a new Predicate that returns true if any of the specified
        * predicates are true.
        * 
  -     * @param predicates  an array of predicates to check
  +     * @param predicates  an array of predicates to check, may not be null
        * @return the 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
        */
       public static Predicate anyPredicate(Predicate[] predicates) {
  -        validate(predicates);
  -        return new AnyPredicate(predicates);
  +        return new AnyPredicate(validate(predicates));
       }
   
       /**
        * Create a new Predicate that returns true if any of the specified
        * predicates are true. The predicates are checked in iterator order.
        * 
  -     * @param predicates  an array of predicates to check
  +     * @param predicates  a collection of predicates to check, may not be null
        * @return the 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
        */
       public static Predicate anyPredicate(Collection predicates) {
  -        return anyPredicate(convertCollection(predicates));
  +        return new AnyPredicate(validate(predicates));
       }
   
       /**
        * Create a new Predicate that returns true if one, but not both, of the
        * specified predicates are true.
        * 
  -     * @param predicate1  the first predicate
  -     * @param predicate2  the second predicate
  +     * @param predicate1  the first predicate, may not be null
  +     * @param predicate2  the second predicate, may not be null
        * @return the predicate
        * @throws IllegalArgumentException if either predicate is null
        */
  @@ -293,37 +365,36 @@
        * Create a new Predicate that returns true if only one of the specified
        * predicates are true.
        * 
  -     * @param predicates  an array of predicates to check
  +     * @param predicates  an array of predicates to check, may not be null
        * @return the 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
        */
       public static Predicate onePredicate(Predicate[] predicates) {
  -        validate(predicates);
  -        return new OnePredicate(predicates);
  +        return new OnePredicate(validate(predicates));
       }
   
       /**
        * Create a new Predicate that returns true if only one of the specified
        * predicates are true. The predicates are checked in iterator order.
        * 
  -     * @param predicates  an array of predicates to check
  +     * @param predicates  a collection of predicates to check, may not be null
        * @return the 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
        */
       public static Predicate onePredicate(Collection predicates) {
  -        return onePredicate(convertCollection(predicates));
  +        return new OnePredicate(validate(predicates));
       }
   
       /**
        * Create a new Predicate that returns true if neither of the specified 
        * predicates are true.
        * 
  -     * @param predicate1  the first predicate
  -     * @param predicate2  the second predicate
  +     * @param predicate1  the first predicate, may not be null
  +     * @param predicate2  the second predicate, may not be null
        * @return the predicate
        * @throws IllegalArgumentException if either predicate is null
        */
  @@ -335,32 +406,36 @@
        * Create a new Predicate that returns true if none of the specified
        * predicates are true.
        * 
  -     * @param predicates  an array of predicates to check
  +     * @param predicates  an array of predicates to check, may not be null
        * @return the 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
        */
       public static Predicate nonePredicate(Predicate[] predicates) {
  -        validate(predicates);
  -        for (int i = 0; i < predicates.length; i++) {
  -            predicates[i] = notPredicate(predicates[i]);
  +        Predicate[] preds = validate(predicates);
  +        for (int i = 0; i < preds.length; i++) {
  +            preds[i] = notPredicate(preds[i]);
           }
  -        return new AllPredicate(predicates);
  +        return new AllPredicate(preds);
       }
   
       /**
        * Create a new Predicate that returns true if none of the specified
        * predicates are true. The predicates are checked in iterator order.
        * 
  -     * @param predicates  an array of predicates to check
  +     * @param predicates  a collection of predicates to check, may not be null
        * @return the 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
        */
       public static Predicate nonePredicate(Collection predicates) {
  -        return nonePredicate(convertCollection(predicates));
  +        Predicate[] preds = validate(predicates);
  +        for (int i = 0; i < preds.length; i++) {
  +            preds[i] = notPredicate(preds[i]);
  +        }
  +        return new AllPredicate(preds);
       }
   
       /**
  @@ -378,52 +453,34 @@
           return new NotPredicate(predicate);
       }
   
  -    /**
  -     * Create a new Predicate that checks if the object passed in is of
  -     * a particular type, using instanceof.
  -     * 
  -     * @param type  the type to check for
  -     * @return the predicate
  -     * @throws IllegalArgumentException if the class is null
  -     */
  -    public static Predicate instanceofPredicate(Class type) {
  -        if (type == null) {
  -            throw new IllegalArgumentException("InstanceofPredicate: The type to check instanceof must not be null");
  -        }
  -        return new InstanceofPredicate(type);
  -    }
  -
  -    /**
  -     * Gets 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().
  -     * 
  -     * @return the predicate
  -     */
  -    public static Predicate uniquePredicate() {
  -        return new UniquePredicate();
  -    }
  +    // Adaptors
  +    //-----------------------------------------------------------------------------
   
       /**
  -     * Create a new Predicate that calls a Transformer. The Transformer must
  +     * Create a new Predicate that wraps a Transformer. The Transformer must
        * return either Boolean.TRUE or Boolean.FALSE otherwise a PredicateException
        * will be thrown.
        * 
  -     * @param transformer  the transformer to call
  +     * @param transformer  the transformer to wrap, may not be null
        * @return the predicate
        * @throws IllegalArgumentException if the transformer is null
        */
  -    public static Predicate transformerPredicate(Transformer transformer) {
  +    public static Predicate asPredicate(Transformer transformer) {
           if (transformer == null) {
               throw new IllegalArgumentException("TransformerPredicate: The transformer to call must not be null");
           }
           return new TransformerPredicate(transformer);
       }
   
  +    // Null handlers
  +    //-----------------------------------------------------------------------------
  +
       /**
  -     * Gets a Predicate that returns an exception if the input object is null
  +     * Gets a Predicate that throws an exception if the input object is null, 
  +     * otherwise it calls the specified Predicate. This allows null handling 
  +     * behaviour to be added to Predicates that don't support nulls.
        * 
  -     * @param predicate  a valid (non-null) Predicate
  +     * @param predicate  the predicate to wrap, may not be null
        * @return the predicate
        * @throws IllegalArgumentException if the predicate is null.
        */
  @@ -435,9 +492,11 @@
       }
   
       /**
  -     * Gets a Predicate that returns false if the input object is null
  +     * Gets a Predicate that returns false if the input object is null, otherwise
  +     * it calls the specified Predicate. This allows null handling behaviour to
  +     * be added to Predicates that don't support nulls.
        * 
  -     * @param predicate  a valid (non-null) Predicate
  +     * @param predicate  the predicate to wrap, may not be null
        * @return the predicate
        * @throws IllegalArgumentException if the predicate is null.
        */
  @@ -449,9 +508,11 @@
       }
   
       /**
  -     * Gets a Predicate that returns true if the input object is null
  +     * Gets a Predicate that returns true if the input object is null, otherwise
  +     * it calls the specified Predicate. This allows null handling behaviour to
  +     * be added to Predicates that don't support nulls.
        * 
  -     * @param predicate  a valid (non-null) Predicate
  +     * @param predicate  the predicate to wrap, may not be null
        * @return the predicate
        * @throws IllegalArgumentException if the predicate is null.
        */
  @@ -463,29 +524,39 @@
       }
   
       /**
  -     * Convert a collection to an array using the iterator
  +     * Convert a collection to an array using the iterator.
        * 
        * @param predicates  the predicates to validate
  +     * @return predicate array
        */
  -    private static Predicate[] convertCollection(Collection predicates) {
  +    private static Predicate[] validate(Collection predicates) {
           if (predicates == null) {
               throw new IllegalArgumentException("PredicateUtils: 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());
  +        }
           // convert to array like this to guarantee iterator() ordering
           Predicate[] preds = new Predicate[predicates.size()];
           int i = 0;
           for (Iterator it = predicates.iterator(); it.hasNext();) {
  -            preds[i++] = (Predicate) it.next();
  +            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");
  +            }
  +            i++;
           }
           return preds;
       }
       
       /**
  -     * Validate method shared amongst predicate implementations
  +     * Validate method shared amongst predicate implementations.
        * 
        * @param predicates  the predicates to validate
  +     * @return predicate array (copy)
        */
  -    private static void validate(Predicate[] predicates) {
  +    private static Predicate[] validate(Predicate[] predicates) {
           if (predicates == null) {
               throw new IllegalArgumentException("PredicateUtils: The predicate array must not be null");
           }
  @@ -493,11 +564,14 @@
               throw new IllegalArgumentException(
                   "PredicateUtils: 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");
               }
  +            preds[i] = predicates[i];
           }
  +        return preds;
       }
   
       // ExceptionPredicate
  @@ -549,11 +623,11 @@
           }
       }
   
  -    // AndPredicate
  +    // AllPredicate
       //----------------------------------------------------------------------------------
   
       /**
  -     * AndPredicate returns true if all predicates return true
  +     * AllPredicate returns true if all predicates return true
        */
       private static class AllPredicate implements Predicate, Serializable {
           private final Predicate[] iPredicates;
  @@ -579,11 +653,11 @@
           }
       }
   
  -    // OrPredicate
  +    // AnyPredicate
       //----------------------------------------------------------------------------------
   
       /**
  -     * OrPredicate returns true if one of the predicates return true
  +     * AnyPredicate returns true if one of the predicates return true
        */
       private static class AnyPredicate implements Predicate, Serializable {
           private final Predicate[] iPredicates;
  @@ -797,7 +871,7 @@
                   result = iTransformer.transform(object);
   
               } catch (TransformerException ex) {
  -                throw new PredicateException("", ex);
  +                throw new PredicateException("TransformerPredicate: " + ex.getMessage(), ex);
               }
               if (result instanceof Boolean == false) {
                   throw new PredicateException(
  
  
  
  1.4       +87 -9     jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/predicate/TestPredicateUtils.java
  
  Index: TestPredicateUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/predicate/TestPredicateUtils.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestPredicateUtils.java	9 Aug 2002 23:11:35 -0000	1.3
  +++ TestPredicateUtils.java	25 Aug 2002 21:17:33 -0000	1.4
  @@ -58,6 +58,7 @@
   import java.util.Collection;
   import java.util.Collections;
   import java.util.Date;
  +import java.util.List;
   
   import junit.framework.Test;
   import junit.framework.TestSuite;
  @@ -681,26 +682,103 @@
           assertEquals(false, p.evaluate(cString));
       }
       
  -    // transformerPredicate
  +    // asPredicate(Transformer)
       //------------------------------------------------------------------
   
  -    public void testTransformerPredicate() {
  -        assertEquals(false, PredicateUtils.transformerPredicate(TransformerUtils.nopTransformer()).evaluate(Boolean.FALSE));
  -        assertEquals(true, PredicateUtils.transformerPredicate(TransformerUtils.nopTransformer()).evaluate(Boolean.TRUE));
  +    public void testAsPredicateTransformer() {
  +        assertEquals(false, PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(Boolean.FALSE));
  +        assertEquals(true, PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(Boolean.TRUE));
       }
   
  -    public void testTransformerPredicateEx1() {
  +    public void testAsPredicateTransformerEx1() {
           try {
  -            PredicateUtils.transformerPredicate(null);
  +            PredicateUtils.asPredicate(null);
           } catch (IllegalArgumentException ex) {
               return;
           }
           fail();
       }
       
  -    public void testTransformerPredicateEx2() {
  +    public void testAsPredicateTransformerEx2() {
           try {
  -            PredicateUtils.transformerPredicate(TransformerUtils.nopTransformer()).evaluate(null);
  +            PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(null);
  +        } catch (PredicateException ex) {
  +            return;
  +        }
  +        fail();
  +    }
  +    
  +    // invokerPredicate
  +    //------------------------------------------------------------------
  +
  +    public void testInvokerPredicate() {
  +        List list = new ArrayList();
  +        assertEquals(true, PredicateUtils.invokerPredicate("isEmpty").evaluate(list));
  +        list.add(new Object());
  +        assertEquals(false, PredicateUtils.invokerPredicate("isEmpty").evaluate(list));
  +    }
  +
  +    public void testInvokerPredicateEx1() {
  +        try {
  +            PredicateUtils.invokerPredicate(null);
  +        } catch (IllegalArgumentException ex) {
  +            return;
  +        }
  +        fail();
  +    }
  +    
  +    public void testInvokerPredicateEx2() {
  +        try {
  +            PredicateUtils.invokerPredicate("isEmpty").evaluate(null);
  +        } catch (PredicateException ex) {
  +            return;
  +        }
  +        fail();
  +    }
  +    
  +    public void testInvokerPredicateEx3() {
  +        try {
  +            PredicateUtils.invokerPredicate("noSuchMethod").evaluate(new Object());
  +        } catch (PredicateException ex) {
  +            return;
  +        }
  +        fail();
  +    }
  +    
  +    // invokerPredicate2
  +    //------------------------------------------------------------------
  +
  +    public void testInvokerPredicate2() {
  +        List list = new ArrayList();
  +        assertEquals(false, PredicateUtils.invokerPredicate(
  +            "contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(list));
  +        list.add(cString);
  +        assertEquals(true, PredicateUtils.invokerPredicate(
  +            "contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(list));
  +    }
  +
  +    public void testInvokerPredicate2Ex1() {
  +        try {
  +            PredicateUtils.invokerPredicate(null, null, null);
  +        } catch (IllegalArgumentException ex) {
  +            return;
  +        }
  +        fail();
  +    }
  +    
  +    public void testInvokerPredicate2Ex2() {
  +        try {
  +            PredicateUtils.invokerPredicate("contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(null);
  +        } catch (PredicateException ex) {
  +            return;
  +        }
  +        fail();
  +    }
  +    
  +    public void testInvokerPredicate2Ex3() {
  +        try {
  +            PredicateUtils.invokerPredicate(
  +                "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).evaluate(new Object());
           } catch (PredicateException ex) {
               return;
           }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message