You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/08/10 01:11:19 UTC

cvs commit: jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/predicate PredicateUtils.java

scolebourne    2002/08/09 16:11:19

  Modified:    pattern/src/java/org/apache/commons/pattern/predicate
                        PredicateUtils.java
  Log:
  Added three new decorators, from Ola Berg
  
  Revision  Changes    Path
  1.4       +129 -1    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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PredicateUtils.java	20 Jul 2002 14:47:22 -0000	1.3
  +++ PredicateUtils.java	9 Aug 2002 23:11:19 -0000	1.4
  @@ -80,10 +80,12 @@
    * <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
  + * <li>NullIsException/NullIsFalse/NullIsTrue - check for null input
    * </ul>
    * All the supplied predicates are Serializable.
    *
    * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
  + * @author Ola Berg
    * @version $Id$
    */
   public class PredicateUtils {
  @@ -419,6 +421,48 @@
       }
   
       /**
  +     * Gets a Predicate that returns an exception if the input object is null
  +     * 
  +     * @param predicate  a valid (non-null) Predicate
  +     * @return the predicate
  +     * @throws IllegalArgumentException if the predicate is null.
  +     */
  +    public static Predicate nullIsExceptionPredicate(Predicate predicate){
  +        if (predicate == null) {
  +            throw new IllegalArgumentException("The predicate must not be null");
  +        }
  +        return new NullIsExceptionPredicate( predicate);
  +    }
  +
  +    /**
  +     * Gets a Predicate that returns false if the input object is null
  +     * 
  +     * @param predicate  a valid (non-null) Predicate
  +     * @return the predicate
  +     * @throws IllegalArgumentException if the predicate is null.
  +     */
  +    public static Predicate nullIsFalsePredicate(Predicate predicate){
  +        if (predicate == null) {
  +            throw new IllegalArgumentException("The predicate must not be null");
  +        }
  +        return new NullIsFalsePredicate(predicate);
  +    }
  +
  +    /**
  +     * Gets a Predicate that returns true if the input object is null
  +     * 
  +     * @param predicate  a valid (non-null) Predicate
  +     * @return the predicate
  +     * @throws IllegalArgumentException if the predicate is null.
  +     */
  +    public static Predicate nullIsTruePredicate(Predicate predicate){
  +        if (predicate == null) {
  +            throw new IllegalArgumentException("The predicate must not be null");
  +        }
  +        return new NullIsTruePredicate(predicate);
  +    }
  +
  +    /**
        * Convert a collection to an array using the iterator
        * 
        * @param predicates  the predicates to validate
  @@ -763,5 +807,89 @@
               return ((Boolean) result).booleanValue();
           }
       }
  +
  +    // NullIsExceptionPredicate
  +    //----------------------------------------------------------------------------------
  +
  +    /**
  +     * NullIsExceptionPredicate returns an exception if null is passed in.
  +     */
  +    private static class NullIsExceptionPredicate implements Predicate, Serializable {
  +        private final Predicate iPredicate;
  +        
  +        /**
  +         * Constructor
  +         */
  +        private NullIsExceptionPredicate(Predicate predicate){
  +            super();
  +            iPredicate = predicate;
  +        }
  +        
  +        /**
  +         * Return an exception if null
  +         */
  +        public boolean evaluate(Object object){
  +            if (object == null) {
  +                throw new PredicateException("NullIsExceptionPredicate: Input Object must not be null");
  +            }
  +            return iPredicate.evaluate(object);
  +        }
  +    };
  +
  +    // NullIsFalsePredicate
  +    //----------------------------------------------------------------------------------
  +
  +    /**
  +     * NullIsFalsePredicate returns false if null is passed in.
  +     */
  +    private static class NullIsFalsePredicate implements Predicate, Serializable {
  +        private final Predicate iPredicate;
  +        
  +        /**
  +         * Constructor
  +         */
  +        private NullIsFalsePredicate(Predicate predicate){
  +            super();
  +            iPredicate = predicate;
  +        }
  +        
  +        /**
  +         * Return false if null
  +         */
  +        public boolean evaluate(Object object){
  +            if (object == null) {
  +                return false;
  +            }
  +            return iPredicate.evaluate(object);
  +        }
  +    };
  +
  +    // NullIsTruePredicate
  +    //----------------------------------------------------------------------------------
  +
  +    /**
  +     * NullIsTruePredicate returns true if null is passed in.
  +     */
  +    private static class NullIsTruePredicate implements Predicate, Serializable {
  +        private final Predicate iPredicate;
  +        
  +        /**
  +         * Constructor
  +         */
  +        private NullIsTruePredicate(Predicate predicate){
  +            super();
  +            iPredicate = predicate;
  +        }
  +        
  +        /**
  +         * Return true if null
  +         */
  +        public boolean evaluate(Object object){
  +            if (object == null) {
  +                return true;
  +            }
  +            return iPredicate.evaluate(object);
  +        }
  +    };
   
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>