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/java/org/apache/commons/pattern/predicate PredicateUtils.java
Date Sat, 20 Jul 2002 14:47:22 GMT
scolebourne    2002/07/20 07:47:22

  Modified:    pattern/src/java/org/apache/commons/pattern/predicate
                        PredicateUtils.java
  Log:
  Add ExceptionPredicate
  Add TransformerPredicate
  Make static variables private
  Javadoc
  Tidy file
  
  Revision  Changes    Path
  1.3       +203 -35   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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PredicateUtils.java	29 Jun 2002 17:06:23 -0000	1.2
  +++ PredicateUtils.java	20 Jul 2002 14:47:22 -0000	1.3
  @@ -54,8 +54,13 @@
   package org.apache.commons.pattern.predicate;
   
   import java.io.Serializable;
  +import java.util.Collection;
   import java.util.HashSet;
  +import java.util.Iterator;
   import java.util.Set;
  +
  +import org.apache.commons.pattern.transformer.Transformer;
  +import org.apache.commons.pattern.transformer.TransformerException;
   /**
    * <code>PredicateUtils</code> provides reference implementations and utilities
    * for the Predicate pattern interface. The supplied predicates are:
  @@ -73,6 +78,8 @@
    * <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>Transformer - wraps a Transformer as a Predicate
  + * <li>Exception - always throws an exception
    * </ul>
    * All the supplied predicates are Serializable.
    *
  @@ -82,21 +89,25 @@
   public class PredicateUtils {
   
       /**
  +     * A predicate that always throws an exception
  +     */
  +    private static final Predicate EXCEPTION_PREDICATE = new ExceptionPredicate();
  +    /**
        * A predicate that always returns true
        */
  -    public static final Predicate TRUE_PREDICATE = new ConstantPredicate(true);
  +    private static final Predicate TRUE_PREDICATE = new ConstantPredicate(true);
       /**
        * A predicate that always returns false
        */
  -    public static final Predicate FALSE_PREDICATE = new ConstantPredicate(false);
  +    private static final Predicate FALSE_PREDICATE = new ConstantPredicate(false);
       /**
        * A predicate that returns true if the object is null
        */
  -    public static final Predicate NULL_PREDICATE = new IdentityPredicate(null);
  +    private static final Predicate NULL_PREDICATE = new IdentityPredicate(null);
       /**
        * A predicate that returns true if the object is not null
        */
  -    public static final Predicate NOT_NULL_PREDICATE = new NotPredicate(NULL_PREDICATE);
  +    private static final Predicate NOT_NULL_PREDICATE = new NotPredicate(NULL_PREDICATE);
   
       /**
        * Restructive constructor
  @@ -105,8 +116,18 @@
           super();
       }
   
  +    /** 
  +     * Gets a Predicate that always throws an exception.
  +     * This could be useful during testing as a placeholder.
  +     *
  +     * @return the factory
  +     */
  +    public static Predicate exceptionPredicate() {
  +        return EXCEPTION_PREDICATE;
  +    }
  +
       /**
  -     * Gets a predicate that always returns true.
  +     * Gets a Predicate that always returns true.
        * 
        * @return the predicate
        */
  @@ -115,7 +136,7 @@
       }
   
       /**
  -     * Gets a predicate that always returns false.
  +     * Gets a Predicate that always returns false.
        * 
        * @return the predicate
        */
  @@ -124,7 +145,7 @@
       }
   
       /**
  -     * Gets a predicate that checks if the object passed in is null.
  +     * Gets a Predicate that checks if the object passed in is null.
        * 
        * @return the predicate
        */
  @@ -133,7 +154,7 @@
       }
   
       /**
  -     * Gets a predicate that checks if the object passed in is not null.
  +     * Gets a Predicate that checks if the object passed in is not null.
        * 
        * @return the predicate
        */
  @@ -142,7 +163,7 @@
       }
   
       /**
  -     * Create a predicate that checks if the object passed in equals to the
  +     * Create a Predicate that checks if the object passed in equals to the
        * specified object using equals().
        * 
        * @param value  the value to compare against
  @@ -156,7 +177,7 @@
       }
   
       /**
  -     * Create a predicate that checks if the object passed in equals to the
  +     * Create a Predicate that checks if the object passed in equals to the
        * specified object by identity.
        * 
        * @param value  the value to compare against
  @@ -170,7 +191,7 @@
       }
   
       /**
  -     * Create a new predicate that returns true only if both of the specified
  +     * Create a new Predicate that returns true only if both of the specified
        * predicates are true.
        * 
        * @param predicate1  the first predicate
  @@ -183,7 +204,7 @@
       }
   
       /**
  -     * Create a new predicate that returns true only if all of the specified
  +     * Create a new Predicate that returns true only if all of the specified
        * predicates are true.
        * 
        * @param predicates  an array of predicates to check
  @@ -198,7 +219,21 @@
       }
   
       /**
  -     * Create a new predicate that returns true if either of the specified
  +     * 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
  +     * @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));
  +    }
  +
  +    /**
  +     * Create a new Predicate that returns true if either of the specified
        * predicates are true.
        * 
        * @param predicate1  the first predicate
  @@ -211,7 +246,7 @@
       }
   
       /**
  -     * Create a new predicate that returns true if any of the specified
  +     * Create a new Predicate that returns true if any of the specified
        * predicates are true.
        * 
        * @param predicates  an array of predicates to check
  @@ -226,7 +261,21 @@
       }
   
       /**
  -     * Create a new predicate that returns true if one, but not both, of the
  +     * 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
  +     * @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));
  +    }
  +
  +    /**
  +     * Create a new Predicate that returns true if one, but not both, of the
        * specified predicates are true.
        * 
        * @param predicate1  the first predicate
  @@ -239,7 +288,7 @@
       }
   
       /**
  -     * Create a new predicate that returns true if only one of the specified
  +     * Create a new Predicate that returns true if only one of the specified
        * predicates are true.
        * 
        * @param predicates  an array of predicates to check
  @@ -254,7 +303,21 @@
       }
   
       /**
  -     * Create a new predicate that returns true if neither of the specified 
  +     * 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
  +     * @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));
  +    }
  +
  +    /**
  +     * Create a new Predicate that returns true if neither of the specified 
        * predicates are true.
        * 
        * @param predicate1  the first predicate
  @@ -267,7 +330,7 @@
       }
   
       /**
  -     * Create a new predicate that returns true if none of the specified
  +     * Create a new Predicate that returns true if none of the specified
        * predicates are true.
        * 
        * @param predicates  an array of predicates to check
  @@ -285,7 +348,21 @@
       }
   
       /**
  -     * Create a new predicate that returns true if the specified predicate
  +     * 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
  +     * @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));
  +    }
  +
  +    /**
  +     * Create a new Predicate that returns true if the specified predicate
        * returns false and vice versa.
        * 
        * @param predicate  the predicate to not
  @@ -294,13 +371,13 @@
        */
       public static Predicate notPredicate(Predicate predicate) {
           if (predicate == null) {
  -            throw new IllegalArgumentException("Predicate: The predicate must not be null");
  +            throw new IllegalArgumentException("NotPredicate: The predicate must not be
null");
           }
           return new NotPredicate(predicate);
       }
   
       /**
  -     * Create a new predicate that checks if the object passed in is of
  +     * 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
  @@ -309,13 +386,13 @@
        */
       public static Predicate instanceofPredicate(Class type) {
           if (type == null) {
  -            throw new IllegalArgumentException("Predicate: The type to check instanceof
must not be 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
  +     * 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().
        * 
  @@ -326,29 +403,82 @@
       }
   
       /**
  +     * Create a new Predicate that calls a Transformer. The Transformer must
  +     * return either Boolean.TRUE or Boolean.FALSE otherwise a PredicateException
  +     * will be thrown.
  +     * 
  +     * @param transformer  the transformer to call
  +     * @return the predicate
  +     * @throws IllegalArgumentException if the transformer is null
  +     */
  +    public static Predicate transformerPredicate(Transformer transformer) {
  +        if (transformer == null) {
  +            throw new IllegalArgumentException("TransformerPredicate: The transformer to
call must not be null");
  +        }
  +        return new TransformerPredicate(transformer);
  +    }
  +
  +    /**
  +     * Convert a collection to an array using the iterator
  +     * 
  +     * @param predicates  the predicates to validate
  +     */
  +    private static Predicate[] convertCollection(Collection predicates) {
  +        if (predicates == null) {
  +            throw new IllegalArgumentException("PredicateUtils: The predicate collection
must not be null");
  +        }
  +        // 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();
  +        }
  +        return preds;
  +    }
  +    
  +    /**
        * Validate method shared amongst predicate implementations
        * 
        * @param predicates  the predicates to validate
        */
       private static void validate(Predicate[] predicates) {
           if (predicates == null) {
  -            throw new IllegalArgumentException("Predicate: The predicate array must not
be null");
  +            throw new IllegalArgumentException("PredicateUtils: The predicate array must
not be null");
           }
           if (predicates.length < 2) {
               throw new IllegalArgumentException(
  -                "Predicate: At least 2 predicates must be specified in the predicate array,
size was "
  -                    + predicates.length);
  +                "PredicateUtils: At least 2 predicates 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(
  -                    "Predicate: The predicate array must not contain a null predicate,
index "
  -                        + i
  -                        + " was null");
  +                throw new IllegalArgumentException("PredicateUtils: The predicate array
must not contain a null predicate, index " + i + " was null");
               }
           }
       }
   
  +    // ExceptionPredicate
  +    //----------------------------------------------------------------------------------
  +
  +    /**
  +     * ExceptionPredicate always throws an exception
  +     */
  +    private static class ExceptionPredicate implements Predicate, Serializable {
  +
  +        /**
  +         * Constructor
  +         */
  +        private ExceptionPredicate() {
  +            super();
  +        }
  +
  +        /**
  +         * Always throw an exception
  +         */
  +        public boolean evaluate(Object object) {
  +            throw new PredicateException("ExceptionPredicate invoked");
  +        }
  +    }
  +
       // ConstantPredicate
       //----------------------------------------------------------------------------------
   
  @@ -461,7 +591,7 @@
                   if (iPredicates[i].evaluate(object)) {
                       if (match) {
                           return false;
  -                    } 
  +                    }
                       match = true;
                   }
               }
  @@ -485,7 +615,7 @@
               super();
               iPredicate = predicate;
           }
  -        
  +
           /**
            * Return true if the wrapped predicate returns false, and vice versa
            */
  @@ -527,7 +657,7 @@
        */
       private static class EqualPredicate implements Predicate, Serializable {
           private final Object iValue;
  -        
  +
           /**
            * Constructor
            */
  @@ -552,7 +682,7 @@
        */
       private static class IdentityPredicate implements Predicate, Serializable {
           private final Object iValue;
  -        
  +
           /**
            * Constructor
            */
  @@ -593,6 +723,44 @@
            */
           public boolean evaluate(Object object) {
               return iSet.add(object);
  +        }
  +    }
  +
  +    // TransformerPredicate
  +    //----------------------------------------------------------------------------------
  +
  +    /**
  +     * TransformerPredicate returns the result of the Transformer as a boolean.
  +     */
  +    private static class TransformerPredicate implements Predicate, Serializable {
  +
  +        private final Transformer iTransformer;
  +
  +        /**
  +         * Constructor
  +         */
  +        public TransformerPredicate(Transformer transformer) {
  +            super();
  +            iTransformer = transformer;
  +        }
  +
  +        /**
  +         * Return the boolean result of a Transformer
  +         */
  +        public boolean evaluate(Object object) {
  +            Object result = null;
  +            try {
  +                result = iTransformer.transform(object);
  +
  +            } catch (TransformerException ex) {
  +                throw new PredicateException("", ex);
  +            }
  +            if (result instanceof Boolean == false) {
  +                throw new PredicateException(
  +                    "TransformerPredicate: Transformer must return an instanceof Boolean,
it was a "
  +                        + (result == null ? "null object" : result.getClass().getName()));
  +            }
  +            return ((Boolean) result).booleanValue();
           }
       }
   
  
  
  

--
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