PredicateUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4;

  18. import java.util.Collection;

  19. import org.apache.commons.collections4.functors.AllPredicate;
  20. import org.apache.commons.collections4.functors.AndPredicate;
  21. import org.apache.commons.collections4.functors.AnyPredicate;
  22. import org.apache.commons.collections4.functors.EqualPredicate;
  23. import org.apache.commons.collections4.functors.ExceptionPredicate;
  24. import org.apache.commons.collections4.functors.FalsePredicate;
  25. import org.apache.commons.collections4.functors.IdentityPredicate;
  26. import org.apache.commons.collections4.functors.InstanceofPredicate;
  27. import org.apache.commons.collections4.functors.InvokerTransformer;
  28. import org.apache.commons.collections4.functors.NonePredicate;
  29. import org.apache.commons.collections4.functors.NotNullPredicate;
  30. import org.apache.commons.collections4.functors.NotPredicate;
  31. import org.apache.commons.collections4.functors.NullIsExceptionPredicate;
  32. import org.apache.commons.collections4.functors.NullIsFalsePredicate;
  33. import org.apache.commons.collections4.functors.NullIsTruePredicate;
  34. import org.apache.commons.collections4.functors.NullPredicate;
  35. import org.apache.commons.collections4.functors.OnePredicate;
  36. import org.apache.commons.collections4.functors.OrPredicate;
  37. import org.apache.commons.collections4.functors.TransformedPredicate;
  38. import org.apache.commons.collections4.functors.TransformerPredicate;
  39. import org.apache.commons.collections4.functors.TruePredicate;
  40. import org.apache.commons.collections4.functors.UniquePredicate;

  41. /**
  42.  * {@code PredicateUtils} provides reference implementations and utilities
  43.  * for the Predicate functor interface. The supplied predicates are:
  44.  * <ul>
  45.  * <li>Invoker - returns the result of a method call on the input object
  46.  * <li>InstanceOf - true if the object is an instanceof a class
  47.  * <li>Equal - true if the object equals() a specified object
  48.  * <li>Identity - true if the object == a specified object
  49.  * <li>Null - true if the object is null
  50.  * <li>NotNull - true if the object is not null
  51.  * <li>Unique - true if the object has not already been evaluated
  52.  * <li>And/All - true if all of the predicates are true
  53.  * <li>Or/Any - true if any of the predicates is true
  54.  * <li>Either/One - true if only one of the predicate is true
  55.  * <li>Neither/None - true if none of the predicates are true
  56.  * <li>Not - true if the predicate is false, and vice versa
  57.  * <li>Transformer - wraps a Transformer as a Predicate
  58.  * <li>True - always return true
  59.  * <li>False - always return false
  60.  * <li>Exception - always throws an exception
  61.  * <li>NullIsException/NullIsFalse/NullIsTrue - check for null input
  62.  * <li>Transformed - transforms the input before calling the predicate
  63.  * </ul>
  64.  * <p>
  65.  * All the supplied predicates are Serializable.
  66.  * </p>
  67.  *
  68.  * @since 3.0
  69.  */
  70. public class PredicateUtils {

  71.     /**
  72.      * Create a new Predicate that returns true only if all of the specified
  73.      * predicates are true. The predicates are checked in iterator order.
  74.      * If the collection of predicates is empty, then this predicate returns true.
  75.      *
  76.      * @param <T>  the type that the predicate queries
  77.      * @param predicates  a collection of predicates to check, may not be null
  78.      * @return the {@code all} predicate
  79.      * @throws NullPointerException if the predicates collection is null
  80.      * @throws NullPointerException if any predicate in the collection is null
  81.      * @see AllPredicate
  82.      */
  83.     public static <T> Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates) {
  84.         return AllPredicate.allPredicate(predicates);
  85.     }

  86.     /**
  87.      * Create a new Predicate that returns true only if all of the specified
  88.      * predicates are true.
  89.      * If the array of predicates is empty, then this predicate returns true.
  90.      *
  91.      * @param <T>  the type that the predicate queries
  92.      * @param predicates  an array of predicates to check, may not be null
  93.      * @return the {@code all} predicate
  94.      * @throws NullPointerException if the predicates array is null
  95.      * @throws NullPointerException if any predicate in the array is null
  96.      * @see AllPredicate
  97.      */
  98.     public static <T> Predicate<T> allPredicate(final Predicate<? super T>... predicates) {
  99.         return AllPredicate.allPredicate(predicates);
  100.     }

  101.     /**
  102.      * Create a new Predicate that returns true only if both of the specified
  103.      * predicates are true.
  104.      *
  105.      * @param <T>  the type that the predicate queries
  106.      * @param predicate1  the first predicate, may not be null
  107.      * @param predicate2  the second predicate, may not be null
  108.      * @return the {@code and} predicate
  109.      * @throws NullPointerException if either predicate is null
  110.      * @see AndPredicate
  111.      */
  112.     public static <T> Predicate<T> andPredicate(final Predicate<? super T> predicate1,
  113.                                                 final Predicate<? super T> predicate2) {
  114.         return AndPredicate.andPredicate(predicate1, predicate2);
  115.     }

  116.     /**
  117.      * Create a new Predicate that returns true if any of the specified
  118.      * predicates are true. The predicates are checked in iterator order.
  119.      * If the collection of predicates is empty, then this predicate returns false.
  120.      *
  121.      * @param <T>  the type that the predicate queries
  122.      * @param predicates  a collection of predicates to check, may not be null
  123.      * @return the {@code any} predicate
  124.      * @throws NullPointerException if the predicates collection is null
  125.      * @throws NullPointerException if any predicate in the collection is null
  126.      * @see AnyPredicate
  127.      */
  128.     public static <T> Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates) {
  129.         return AnyPredicate.anyPredicate(predicates);
  130.     }

  131.     /**
  132.      * Create a new Predicate that returns true if any of the specified
  133.      * predicates are true.
  134.      * If the array of predicates is empty, then this predicate returns false.
  135.      *
  136.      * @param <T>  the type that the predicate queries
  137.      * @param predicates  an array of predicates to check, may not be null
  138.      * @return the {@code any} predicate
  139.      * @throws NullPointerException if the predicates array is null
  140.      * @throws NullPointerException if any predicate in the array is null
  141.      * @see AnyPredicate
  142.      */
  143.     public static <T> Predicate<T> anyPredicate(final Predicate<? super T>... predicates) {
  144.         return AnyPredicate.anyPredicate(predicates);
  145.     }

  146.     /**
  147.      * Create a new Predicate that wraps a Transformer. The Transformer must
  148.      * return either {@link Boolean#TRUE} or {@link Boolean#FALSE} otherwise a
  149.      * PredicateException will be thrown.
  150.      *
  151.      * @param <T>  the type that the predicate queries
  152.      * @param transformer  the transformer to wrap, may not be null
  153.      * @return the transformer wrapping predicate
  154.      * @throws NullPointerException if the transformer is null
  155.      * @see TransformerPredicate
  156.      */
  157.     public static <T> Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer) {
  158.         return TransformerPredicate.transformerPredicate(transformer);
  159.     }

  160.     /**
  161.      * Create a new Predicate that returns true if one, but not both, of the
  162.      * specified predicates are true. XOR
  163.      *
  164.      * @param <T>  the type that the predicate queries
  165.      * @param predicate1  the first predicate, may not be null
  166.      * @param predicate2  the second predicate, may not be null
  167.      * @return the {@code either} predicate
  168.      * @throws NullPointerException if either predicate is null
  169.      * @see OnePredicate
  170.      */
  171.     public static <T> Predicate<T> eitherPredicate(final Predicate<? super T> predicate1,
  172.                                                    final Predicate<? super T> predicate2) {
  173.         @SuppressWarnings("unchecked")
  174.         final Predicate<T> onePredicate = onePredicate(predicate1, predicate2);
  175.         return onePredicate;
  176.     }

  177.     /**
  178.      * Creates a Predicate that checks if the input object is equal to the
  179.      * specified object using equals().
  180.      *
  181.      * @param <T>  the type that the predicate queries
  182.      * @param value  the value to compare against
  183.      * @return the predicate
  184.      * @see EqualPredicate
  185.      */
  186.     public static <T> Predicate<T> equalPredicate(final T value) {
  187.         return EqualPredicate.equalPredicate(value);
  188.     }

  189.     /**
  190.      * Gets a Predicate that always throws an exception.
  191.      * This could be useful during testing as a placeholder.
  192.      *
  193.      * @param <T>  the type that the predicate queries
  194.      * @return the predicate
  195.      * @see ExceptionPredicate
  196.      */
  197.     public static <T> Predicate<T> exceptionPredicate() {
  198.         return ExceptionPredicate.exceptionPredicate();
  199.     }

  200.     /**
  201.      * Gets a Predicate that always returns false.
  202.      *
  203.      * @param <T>  the type that the predicate queries
  204.      * @return the predicate
  205.      * @see FalsePredicate
  206.      */
  207.     public static <T> Predicate<T> falsePredicate() {
  208.         return FalsePredicate.falsePredicate();
  209.     }

  210.     /**
  211.      * Creates a Predicate that checks if the input object is equal to the
  212.      * specified object by identity.
  213.      *
  214.      * @param <T>  the type that the predicate queries
  215.      * @param value  the value to compare against
  216.      * @return the predicate
  217.      * @see IdentityPredicate
  218.      */
  219.     public static <T> Predicate<T> identityPredicate(final T value) {
  220.         return IdentityPredicate.identityPredicate(value);
  221.     }

  222.     /**
  223.      * Creates a Predicate that checks if the object passed in is of
  224.      * a particular type, using instanceof. A {@code null} input
  225.      * object will return {@code false}.
  226.      *
  227.      * @param type  the type to check for, may not be null
  228.      * @return the predicate
  229.      * @throws NullPointerException if the class is null
  230.      * @see InstanceofPredicate
  231.      */
  232.     public static Predicate<Object> instanceofPredicate(final Class<?> type) {
  233.         return InstanceofPredicate.instanceOfPredicate(type);
  234.     }

  235.     /**
  236.      * Creates a Predicate that invokes a method on the input object.
  237.      * The method must return either a boolean or a non-null Boolean,
  238.      * and have no parameters. If the input object is null, a
  239.      * PredicateException is thrown.
  240.      * <p>
  241.      * For ePredicateUtils.invokerPredicate("isEmpty");}
  242.      * will call the {@code isEmpty} method on the input object to
  243.      * determine the predicate result.
  244.      *
  245.      * @param <T>  the type that the predicate queries
  246.      * @param methodName  the method name to call on the input object, may not be null
  247.      * @return the predicate
  248.      * @throws NullPointerException if the methodName is null.
  249.      * @see InvokerTransformer
  250.      * @see TransformerPredicate
  251.      */
  252.     public static <T> Predicate<T> invokerPredicate(final String methodName) {
  253.         // reuse transformer as it has caching - this is lazy really, should have inner class here
  254.         return asPredicate(InvokerTransformer.<Object, Boolean>invokerTransformer(methodName));
  255.     }

  256.     /**
  257.      * Creates a Predicate that invokes a method on the input object.
  258.      * The method must return either a boolean or a non-null Boolean,
  259.      * and have no parameters. If the input object is null, a
  260.      * PredicateException is thrown.
  261.      * <p>
  262.      * For example, {@code PredicateUtils.invokerPredicate("isEmpty");}
  263.      * will call the {@code isEmpty} method on the input object to
  264.      * determine the predicate result.
  265.      * </p>
  266.      *
  267.      * @param <T>  the type that the predicate queries
  268.      * @param methodName  the method name to call on the input object, may not be null
  269.      * @param paramTypes  the parameter types
  270.      * @param args  the arguments
  271.      * @return the predicate
  272.      * @throws NullPointerException if the method name is null
  273.      * @throws IllegalArgumentException if the paramTypes and args don't match
  274.      * @see InvokerTransformer
  275.      * @see TransformerPredicate
  276.      */
  277.     public static <T> Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes,
  278.                                                     final Object[] args) {
  279.         // reuse transformer as it has caching - this is lazy really, should have inner class here
  280.         return asPredicate(InvokerTransformer.<Object, Boolean>invokerTransformer(methodName, paramTypes, args));
  281.     }

  282.     /**
  283.      * Create a new Predicate that returns true if neither of the specified
  284.      * predicates are true.
  285.      *
  286.      * @param <T>  the type that the predicate queries
  287.      * @param predicate1  the first predicate, may not be null
  288.      * @param predicate2  the second predicate, may not be null
  289.      * @return the {@code neither} predicate
  290.      * @throws NullPointerException if either predicate is null
  291.      * @see NonePredicate
  292.      */
  293.     public static <T> Predicate<T> neitherPredicate(final Predicate<? super T> predicate1,
  294.                                                     final Predicate<? super T> predicate2) {
  295.         @SuppressWarnings("unchecked")
  296.         final Predicate<T> nonePredicate = nonePredicate(predicate1, predicate2);
  297.         return nonePredicate;
  298.     }

  299.     /**
  300.      * Create a new Predicate that returns true if none of the specified
  301.      * predicates are true. The predicates are checked in iterator order.
  302.      * If the collection of predicates is empty, then this predicate returns true.
  303.      *
  304.      * @param <T>  the type that the predicate queries
  305.      * @param predicates  a collection of predicates to check, may not be null
  306.      * @return the {@code none} predicate
  307.      * @throws NullPointerException if the predicates collection is null
  308.      * @throws NullPointerException if any predicate in the collection is null
  309.      * @see NonePredicate
  310.      */
  311.     public static <T> Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates) {
  312.         return NonePredicate.nonePredicate(predicates);
  313.     }

  314.     /**
  315.      * Create a new Predicate that returns true if none of the specified
  316.      * predicates are true.
  317.      * If the array of predicates is empty, then this predicate returns true.
  318.      *
  319.      * @param <T>  the type that the predicate queries
  320.      * @param predicates  an array of predicates to check, may not be null
  321.      * @return the {@code none} predicate
  322.      * @throws NullPointerException if the predicates array is null
  323.      * @throws NullPointerException if any predicate in the array is null
  324.      * @see NonePredicate
  325.      */
  326.     public static <T> Predicate<T> nonePredicate(final Predicate<? super T>... predicates) {
  327.         return NonePredicate.nonePredicate(predicates);
  328.     }

  329.     /**
  330.      * Gets a Predicate that checks if the input object passed in is not null.
  331.      *
  332.      * @param <T>  the type that the predicate queries
  333.      * @return the predicate
  334.      * @see NotNullPredicate
  335.      */
  336.     public static <T> Predicate<T> notNullPredicate() {
  337.         return NotNullPredicate.notNullPredicate();
  338.     }

  339.     /**
  340.      * Create a new Predicate that returns true if the specified predicate
  341.      * returns false and vice versa.
  342.      *
  343.      * @param <T>  the type that the predicate queries
  344.      * @param predicate  the predicate to not
  345.      * @return the {@code not} predicate
  346.      * @throws NullPointerException if the predicate is null
  347.      * @see NotPredicate
  348.      */
  349.     public static <T> Predicate<T> notPredicate(final Predicate<? super T> predicate) {
  350.         return NotPredicate.notPredicate(predicate);
  351.     }

  352.     /**
  353.      * Gets a Predicate that throws an exception if the input object is null,
  354.      * otherwise it calls the specified Predicate. This allows null handling
  355.      * behavior to be added to Predicates that don't support nulls.
  356.      *
  357.      * @param <T>  the type that the predicate queries
  358.      * @param predicate  the predicate to wrap, may not be null
  359.      * @return the predicate
  360.      * @throws NullPointerException if the predicate is null.
  361.      * @see NullIsExceptionPredicate
  362.      */
  363.     public static <T> Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate) {
  364.         return NullIsExceptionPredicate.nullIsExceptionPredicate(predicate);
  365.     }

  366.     /**
  367.      * Gets a Predicate that returns false if the input object is null, otherwise
  368.      * it calls the specified Predicate. This allows null handling behavior to
  369.      * be added to Predicates that don't support nulls.
  370.      *
  371.      * @param <T>  the type that the predicate queries
  372.      * @param predicate  the predicate to wrap, may not be null
  373.      * @return the predicate
  374.      * @throws NullPointerException if the predicate is null.
  375.      * @see NullIsFalsePredicate
  376.      */
  377.     public static <T> Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate) {
  378.         return NullIsFalsePredicate.nullIsFalsePredicate(predicate);
  379.     }

  380.     /**
  381.      * Gets a Predicate that returns true if the input object is null, otherwise
  382.      * it calls the specified Predicate. This allows null handling behavior to
  383.      * be added to Predicates that don't support nulls.
  384.      *
  385.      * @param <T>  the type that the predicate queries
  386.      * @param predicate  the predicate to wrap, may not be null
  387.      * @return the predicate
  388.      * @throws NullPointerException if the predicate is null.
  389.      * @see NullIsTruePredicate
  390.      */
  391.     public static <T> Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate) {
  392.         return NullIsTruePredicate.nullIsTruePredicate(predicate);
  393.     }

  394.     /**
  395.      * Gets a Predicate that checks if the input object passed in is null.
  396.      *
  397.      * @param <T>  the type that the predicate queries
  398.      * @return the predicate
  399.      * @see NullPredicate
  400.      */
  401.     public static <T> Predicate<T> nullPredicate() {
  402.         return NullPredicate.nullPredicate();
  403.     }

  404.     /**
  405.      * Create a new Predicate that returns true if only one of the specified
  406.      * predicates are true. The predicates are checked in iterator order.
  407.      * If the collection of predicates is empty, then this predicate returns false.
  408.      *
  409.      * @param <T>  the type that the predicate queries
  410.      * @param predicates  a collection of predicates to check, may not be null
  411.      * @return the {@code one} predicate
  412.      * @throws NullPointerException if the predicates collection is null
  413.      * @throws NullPointerException if any predicate in the collection is null
  414.      * @see OnePredicate
  415.      */
  416.     public static <T> Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates) {
  417.         return OnePredicate.onePredicate(predicates);
  418.     }

  419.     /**
  420.      * Create a new Predicate that returns true if only one of the specified
  421.      * predicates are true.
  422.      * If the array of predicates is empty, then this predicate returns false.
  423.      *
  424.      * @param <T>  the type that the predicate queries
  425.      * @param predicates  an array of predicates to check, may not be null
  426.      * @return the {@code one} predicate
  427.      * @throws NullPointerException if the predicates array is null
  428.      * @throws NullPointerException if any predicate in the array is null
  429.      * @see OnePredicate
  430.      */
  431.     public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates) {
  432.         return OnePredicate.onePredicate(predicates);
  433.     }

  434.     /**
  435.      * Create a new Predicate that returns true if either of the specified
  436.      * predicates are true.
  437.      *
  438.      * @param <T>  the type that the predicate queries
  439.      * @param predicate1  the first predicate, may not be null
  440.      * @param predicate2  the second predicate, may not be null
  441.      * @return the {@code or} predicate
  442.      * @throws NullPointerException if either predicate is null
  443.      * @see OrPredicate
  444.      */
  445.     public static <T> Predicate<T> orPredicate(final Predicate<? super T> predicate1,
  446.                                                final Predicate<? super T> predicate2) {
  447.         return OrPredicate.orPredicate(predicate1, predicate2);
  448.     }

  449.     /**
  450.      * Creates a predicate that transforms the input object before passing it
  451.      * to the predicate.
  452.      *
  453.      * @param <T>  the type that the predicate queries
  454.      * @param transformer  the transformer to call first
  455.      * @param predicate  the predicate to call with the result of the transform
  456.      * @return the predicate
  457.      * @throws NullPointerException if the transformer or the predicate is null
  458.      * @see TransformedPredicate
  459.      * @since 3.1
  460.      */
  461.     public static <T> Predicate<T> transformedPredicate(final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate) {
  462.         return TransformedPredicate.transformedPredicate(transformer, predicate);
  463.     }

  464.     /**
  465.      * Gets a Predicate that always returns true.
  466.      *
  467.      * @param <T>  the type that the predicate queries
  468.      * @return the predicate
  469.      * @see TruePredicate
  470.      */
  471.     public static <T> Predicate<T> truePredicate() {
  472.         return TruePredicate.truePredicate();
  473.     }

  474.     /**
  475.      * Creates a Predicate that returns true the first time an object is
  476.      * encountered, and false if the same object is received
  477.      * again. The comparison is by equals(). A {@code null} input object
  478.      * is accepted and will return true the first time, and false subsequently
  479.      * as well.
  480.      *
  481.      * @param <T>  the type that the predicate queries
  482.      * @return the predicate
  483.      * @see UniquePredicate
  484.      */
  485.     public static <T> Predicate<T> uniquePredicate() {
  486.         // must return new instance each time
  487.         return UniquePredicate.uniquePredicate();
  488.     }

  489.     /**
  490.      * Don't allow instances.
  491.      */
  492.     private PredicateUtils() {
  493.         // empty
  494.     }

  495. }