001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4;
018
019import java.util.Collection;
020
021import org.apache.commons.collections4.functors.AllPredicate;
022import org.apache.commons.collections4.functors.AndPredicate;
023import org.apache.commons.collections4.functors.AnyPredicate;
024import org.apache.commons.collections4.functors.EqualPredicate;
025import org.apache.commons.collections4.functors.ExceptionPredicate;
026import org.apache.commons.collections4.functors.FalsePredicate;
027import org.apache.commons.collections4.functors.IdentityPredicate;
028import org.apache.commons.collections4.functors.InstanceofPredicate;
029import org.apache.commons.collections4.functors.InvokerTransformer;
030import org.apache.commons.collections4.functors.NonePredicate;
031import org.apache.commons.collections4.functors.NotNullPredicate;
032import org.apache.commons.collections4.functors.NotPredicate;
033import org.apache.commons.collections4.functors.NullIsExceptionPredicate;
034import org.apache.commons.collections4.functors.NullIsFalsePredicate;
035import org.apache.commons.collections4.functors.NullIsTruePredicate;
036import org.apache.commons.collections4.functors.NullPredicate;
037import org.apache.commons.collections4.functors.OnePredicate;
038import org.apache.commons.collections4.functors.OrPredicate;
039import org.apache.commons.collections4.functors.TransformedPredicate;
040import org.apache.commons.collections4.functors.TransformerPredicate;
041import org.apache.commons.collections4.functors.TruePredicate;
042import org.apache.commons.collections4.functors.UniquePredicate;
043
044/**
045 * <code>PredicateUtils</code> provides reference implementations and utilities
046 * for the Predicate functor interface. The supplied predicates are:
047 * <ul>
048 * <li>Invoker - returns the result of a method call on the input object
049 * <li>InstanceOf - true if the object is an instanceof a class
050 * <li>Equal - true if the object equals() a specified object
051 * <li>Identity - true if the object == a specified object
052 * <li>Null - true if the object is null
053 * <li>NotNull - true if the object is not null
054 * <li>Unique - true if the object has not already been evaluated
055 * <li>And/All - true if all of the predicates are true
056 * <li>Or/Any - true if any of the predicates is true
057 * <li>Either/One - true if only one of the predicate is true
058 * <li>Neither/None - true if none of the predicates are true
059 * <li>Not - true if the predicate is false, and vice versa
060 * <li>Transformer - wraps a Transformer as a Predicate
061 * <li>True - always return true
062 * <li>False - always return false
063 * <li>Exception - always throws an exception
064 * <li>NullIsException/NullIsFalse/NullIsTrue - check for null input
065 * <li>Transformed - transforms the input before calling the predicate
066 * </ul>
067 * All the supplied predicates are Serializable.
068 *
069 * @since 3.0
070 * @version $Id: PredicateUtils.html 972421 2015-11-14 20:00:04Z tn $
071 */
072public class PredicateUtils {
073
074    /**
075     * This class is not normally instantiated.
076     */
077    private PredicateUtils() {}
078
079    // Simple predicates
080    //-----------------------------------------------------------------------------
081
082    /**
083     * Gets a Predicate that always throws an exception.
084     * This could be useful during testing as a placeholder.
085     *
086     * @param <T>  the type that the predicate queries
087     * @return the predicate
088     * @see org.apache.commons.collections4.functors.ExceptionPredicate
089     */
090    public static <T> Predicate<T> exceptionPredicate() {
091        return ExceptionPredicate.<T>exceptionPredicate();
092    }
093
094    /**
095     * Gets a Predicate that always returns true.
096     *
097     * @param <T>  the type that the predicate queries
098     * @return the predicate
099     * @see org.apache.commons.collections4.functors.TruePredicate
100     */
101    public static <T> Predicate<T> truePredicate() {
102        return TruePredicate.truePredicate();
103    }
104
105    /**
106     * Gets a Predicate that always returns false.
107     *
108     * @param <T>  the type that the predicate queries
109     * @return the predicate
110     * @see org.apache.commons.collections4.functors.FalsePredicate
111     */
112    public static <T> Predicate<T> falsePredicate() {
113        return FalsePredicate.<T> falsePredicate();
114    }
115
116    /**
117     * Gets a Predicate that checks if the input object passed in is null.
118     *
119     * @param <T>  the type that the predicate queries
120     * @return the predicate
121     * @see org.apache.commons.collections4.functors.NullPredicate
122     */
123    public static <T> Predicate<T> nullPredicate() {
124        return NullPredicate.nullPredicate();
125    }
126
127    /**
128     * Gets a Predicate that checks if the input object passed in is not null.
129     *
130     * @param <T>  the type that the predicate queries
131     * @return the predicate
132     * @see org.apache.commons.collections4.functors.NotNullPredicate
133     */
134    public static <T> Predicate<T> notNullPredicate() {
135        return NotNullPredicate.<T>notNullPredicate();
136    }
137
138    /**
139     * Creates a Predicate that checks if the input object is equal to the
140     * specified object using equals().
141     *
142     * @param <T>  the type that the predicate queries
143     * @param value  the value to compare against
144     * @return the predicate
145     * @see org.apache.commons.collections4.functors.EqualPredicate
146     */
147    public static <T> Predicate<T> equalPredicate(final T value) {
148        return EqualPredicate.equalPredicate(value);
149    }
150
151    /**
152     * Creates a Predicate that checks if the input object is equal to the
153     * specified object by identity.
154     *
155     * @param <T>  the type that the predicate queries
156     * @param value  the value to compare against
157     * @return the predicate
158     * @see org.apache.commons.collections4.functors.IdentityPredicate
159     */
160    public static <T> Predicate<T> identityPredicate(final T value) {
161        return IdentityPredicate.<T>identityPredicate(value);
162    }
163
164    /**
165     * Creates a Predicate that checks if the object passed in is of
166     * a particular type, using instanceof. A <code>null</code> input
167     * object will return <code>false</code>.
168     *
169     * @param type  the type to check for, may not be null
170     * @return the predicate
171     * @throws IllegalArgumentException if the class is null
172     * @see org.apache.commons.collections4.functors.InstanceofPredicate
173     */
174    public static Predicate<Object> instanceofPredicate(final Class<?> type) {
175        return InstanceofPredicate.instanceOfPredicate(type);
176    }
177
178    /**
179     * Creates a Predicate that returns true the first time an object is
180     * encountered, and false if the same object is received
181     * again. The comparison is by equals(). A <code>null</code> input object
182     * is accepted and will return true the first time, and false subsequently
183     * as well.
184     *
185     * @param <T>  the type that the predicate queries
186     * @return the predicate
187     * @see org.apache.commons.collections4.functors.UniquePredicate
188     */
189    public static <T> Predicate<T> uniquePredicate() {
190        // must return new instance each time
191        return UniquePredicate.<T>uniquePredicate();
192    }
193
194    /**
195     * Creates a Predicate that invokes a method on the input object.
196     * The method must return either a boolean or a non-null Boolean,
197     * and have no parameters. If the input object is null, a
198     * PredicateException is thrown.
199     * <p>
200     * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code>
201     * will call the <code>isEmpty</code> method on the input object to
202     * determine the predicate result.
203     *
204     * @param <T>  the type that the predicate queries
205     * @param methodName  the method name to call on the input object, may not be null
206     * @return the predicate
207     * @throws IllegalArgumentException if the methodName is null.
208     * @see org.apache.commons.collections4.functors.InvokerTransformer
209     * @see org.apache.commons.collections4.functors.TransformerPredicate
210     */
211    public static <T> Predicate<T> invokerPredicate(final String methodName) {
212        // reuse transformer as it has caching - this is lazy really, should have inner class here
213        return asPredicate(InvokerTransformer.<Object, Boolean>invokerTransformer(methodName));
214    }
215
216    /**
217     * Creates a Predicate that invokes a method on the input object.
218     * The method must return either a boolean or a non-null Boolean,
219     * and have no parameters. If the input object is null, a
220     * PredicateException is thrown.
221     * <p>
222     * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code>
223     * will call the <code>isEmpty</code> method on the input object to
224     * determine the predicate result.
225     *
226     * @param <T>  the type that the predicate queries
227     * @param methodName  the method name to call on the input object, may not be null
228     * @param paramTypes  the parameter types
229     * @param args  the arguments
230     * @return the predicate
231     * @throws IllegalArgumentException if the method name is null
232     * @throws IllegalArgumentException if the paramTypes and args don't match
233     * @see org.apache.commons.collections4.functors.InvokerTransformer
234     * @see org.apache.commons.collections4.functors.TransformerPredicate
235     */
236    public static <T> Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes,
237                                                    final Object[] args) {
238        // reuse transformer as it has caching - this is lazy really, should have inner class here
239        return asPredicate(InvokerTransformer.<Object, Boolean>invokerTransformer(methodName, paramTypes, args));
240    }
241
242    // Boolean combinations
243    //-----------------------------------------------------------------------------
244
245    /**
246     * Create a new Predicate that returns true only if both of the specified
247     * predicates are true.
248     *
249     * @param <T>  the type that the predicate queries
250     * @param predicate1  the first predicate, may not be null
251     * @param predicate2  the second predicate, may not be null
252     * @return the <code>and</code> predicate
253     * @throws IllegalArgumentException if either predicate is null
254     * @see org.apache.commons.collections4.functors.AndPredicate
255     */
256    public static <T> Predicate<T> andPredicate(final Predicate<? super T> predicate1,
257                                                final Predicate<? super T> predicate2) {
258        return AndPredicate.<T>andPredicate(predicate1, predicate2);
259    }
260
261    /**
262     * Create a new Predicate that returns true only if all of the specified
263     * predicates are true.
264     * If the array of predicates is empty, then this predicate returns true.
265     *
266     * @param <T>  the type that the predicate queries
267     * @param predicates  an array of predicates to check, may not be null
268     * @return the <code>all</code> predicate
269     * @throws IllegalArgumentException if the predicates array is null
270     * @throws IllegalArgumentException if any predicate in the array is null
271     * @see org.apache.commons.collections4.functors.AllPredicate
272     */
273    public static <T> Predicate<T> allPredicate(final Predicate<? super T>... predicates) {
274        return AllPredicate.allPredicate(predicates);
275    }
276
277    /**
278     * Create a new Predicate that returns true only if all of the specified
279     * predicates are true. The predicates are checked in iterator order.
280     * If the collection of predicates is empty, then this predicate returns true.
281     *
282     * @param <T>  the type that the predicate queries
283     * @param predicates  a collection of predicates to check, may not be null
284     * @return the <code>all</code> predicate
285     * @throws IllegalArgumentException if the predicates collection is null
286     * @throws IllegalArgumentException if any predicate in the collection is null
287     * @see org.apache.commons.collections4.functors.AllPredicate
288     */
289    public static <T> Predicate<T> allPredicate(final Collection<? extends Predicate<T>> predicates) {
290        return AllPredicate.allPredicate(predicates);
291    }
292
293    /**
294     * Create a new Predicate that returns true if either of the specified
295     * predicates are true.
296     *
297     * @param <T>  the type that the predicate queries
298     * @param predicate1  the first predicate, may not be null
299     * @param predicate2  the second predicate, may not be null
300     * @return the <code>or</code> predicate
301     * @throws IllegalArgumentException if either predicate is null
302     * @see org.apache.commons.collections4.functors.OrPredicate
303     */
304    public static <T> Predicate<T> orPredicate(final Predicate<? super T> predicate1,
305                                               final Predicate<? super T> predicate2) {
306        return OrPredicate.<T>orPredicate(predicate1, predicate2);
307    }
308
309    /**
310     * Create a new Predicate that returns true if any of the specified
311     * predicates are true.
312     * If the array of predicates is empty, then this predicate returns false.
313     *
314     * @param <T>  the type that the predicate queries
315     * @param predicates  an array of predicates to check, may not be null
316     * @return the <code>any</code> predicate
317     * @throws IllegalArgumentException if the predicates array is null
318     * @throws IllegalArgumentException if any predicate in the array is null
319     * @see org.apache.commons.collections4.functors.AnyPredicate
320     */
321    public static <T> Predicate<T> anyPredicate(final Predicate<? super T>... predicates) {
322        return AnyPredicate.anyPredicate(predicates);
323    }
324
325    /**
326     * Create a new Predicate that returns true if any of the specified
327     * predicates are true. The predicates are checked in iterator order.
328     * If the collection of predicates is empty, then this predicate returns false.
329     *
330     * @param <T>  the type that the predicate queries
331     * @param predicates  a collection of predicates to check, may not be null
332     * @return the <code>any</code> predicate
333     * @throws IllegalArgumentException if the predicates collection is null
334     * @throws IllegalArgumentException if any predicate in the collection is null
335     * @see org.apache.commons.collections4.functors.AnyPredicate
336     */
337    public static <T> Predicate<T> anyPredicate(final Collection<? extends Predicate<T>> predicates) {
338        return AnyPredicate.anyPredicate(predicates);
339    }
340
341    /**
342     * Create a new Predicate that returns true if one, but not both, of the
343     * specified predicates are true. XOR
344     *
345     * @param <T>  the type that the predicate queries
346     * @param predicate1  the first predicate, may not be null
347     * @param predicate2  the second predicate, may not be null
348     * @return the <code>either</code> predicate
349     * @throws IllegalArgumentException if either predicate is null
350     * @see org.apache.commons.collections4.functors.OnePredicate
351     */
352    public static <T> Predicate<T> eitherPredicate(final Predicate<? super T> predicate1,
353                                                   final Predicate<? super T> predicate2) {
354        @SuppressWarnings("unchecked")
355        final Predicate<T> onePredicate = PredicateUtils.<T>onePredicate(predicate1, predicate2);
356        return onePredicate;
357    }
358
359    /**
360     * Create a new Predicate that returns true if only one of the specified
361     * predicates are true.
362     * If the array of predicates is empty, then this predicate returns false.
363     *
364     * @param <T>  the type that the predicate queries
365     * @param predicates  an array of predicates to check, may not be null
366     * @return the <code>one</code> predicate
367     * @throws IllegalArgumentException if the predicates array is null
368     * @throws IllegalArgumentException if any predicate in the array is null
369     * @see org.apache.commons.collections4.functors.OnePredicate
370     */
371    public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates) {
372        return OnePredicate.onePredicate(predicates);
373    }
374
375    /**
376     * Create a new Predicate that returns true if only one of the specified
377     * predicates are true. The predicates are checked in iterator order.
378     * If the collection of predicates is empty, then this predicate returns false.
379     *
380     * @param <T>  the type that the predicate queries
381     * @param predicates  a collection of predicates to check, may not be null
382     * @return the <code>one</code> predicate
383     * @throws IllegalArgumentException if the predicates collection is null
384     * @throws IllegalArgumentException if any predicate in the collection is null
385     * @see org.apache.commons.collections4.functors.OnePredicate
386     */
387    public static <T> Predicate<T> onePredicate(final Collection<Predicate<T>> predicates) {
388        return OnePredicate.onePredicate(predicates);
389    }
390
391    /**
392     * Create a new Predicate that returns true if neither of the specified
393     * predicates are true.
394     *
395     * @param <T>  the type that the predicate queries
396     * @param predicate1  the first predicate, may not be null
397     * @param predicate2  the second predicate, may not be null
398     * @return the <code>neither</code> predicate
399     * @throws IllegalArgumentException if either predicate is null
400     * @see org.apache.commons.collections4.functors.NonePredicate
401     */
402    public static <T> Predicate<T> neitherPredicate(final Predicate<? super T> predicate1,
403                                                    final Predicate<? super T> predicate2) {
404        @SuppressWarnings("unchecked")
405        final Predicate<T> nonePredicate = PredicateUtils.<T>nonePredicate(predicate1, predicate2);
406        return nonePredicate;
407    }
408
409    /**
410     * Create a new Predicate that returns true if none of the specified
411     * predicates are true.
412     * If the array of predicates is empty, then this predicate returns true.
413     *
414     * @param <T>  the type that the predicate queries
415     * @param predicates  an array of predicates to check, may not be null
416     * @return the <code>none</code> predicate
417     * @throws IllegalArgumentException if the predicates array is null
418     * @throws IllegalArgumentException if any predicate in the array is null
419     * @see org.apache.commons.collections4.functors.NonePredicate
420     */
421    public static <T> Predicate<T> nonePredicate(final Predicate<? super T>... predicates) {
422        return NonePredicate.nonePredicate(predicates);
423    }
424
425    /**
426     * Create a new Predicate that returns true if none of the specified
427     * predicates are true. The predicates are checked in iterator order.
428     * If the collection of predicates is empty, then this predicate returns true.
429     *
430     * @param <T>  the type that the predicate queries
431     * @param predicates  a collection of predicates to check, may not be null
432     * @return the <code>none</code> predicate
433     * @throws IllegalArgumentException if the predicates collection is null
434     * @throws IllegalArgumentException if any predicate in the collection is null
435     * @see org.apache.commons.collections4.functors.NonePredicate
436     */
437    public static <T> Predicate<T> nonePredicate(final Collection<? extends Predicate<T>> predicates) {
438        return NonePredicate.nonePredicate(predicates);
439    }
440
441    /**
442     * Create a new Predicate that returns true if the specified predicate
443     * returns false and vice versa.
444     *
445     * @param <T>  the type that the predicate queries
446     * @param predicate  the predicate to not
447     * @return the <code>not</code> predicate
448     * @throws IllegalArgumentException if the predicate is null
449     * @see org.apache.commons.collections4.functors.NotPredicate
450     */
451    public static <T> Predicate<T> notPredicate(final Predicate<? super T> predicate) {
452        return NotPredicate.notPredicate(predicate);
453    }
454
455    // Adaptors
456    //-----------------------------------------------------------------------------
457
458    /**
459     * Create a new Predicate that wraps a Transformer. The Transformer must
460     * return either Boolean.TRUE or Boolean.FALSE otherwise a PredicateException
461     * will be thrown.
462     *
463     * @param <T>  the type that the predicate queries
464     * @param transformer  the transformer to wrap, may not be null
465     * @return the transformer wrapping predicate
466     * @throws IllegalArgumentException if the transformer is null
467     * @see org.apache.commons.collections4.functors.TransformerPredicate
468     */
469    public static <T> Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer) {
470        return TransformerPredicate.transformerPredicate(transformer);
471    }
472
473    // Null handlers
474    //-----------------------------------------------------------------------------
475
476    /**
477     * Gets a Predicate that throws an exception if the input object is null,
478     * otherwise it calls the specified Predicate. This allows null handling
479     * behaviour to be added to Predicates that don't support nulls.
480     *
481     * @param <T>  the type that the predicate queries
482     * @param predicate  the predicate to wrap, may not be null
483     * @return the predicate
484     * @throws IllegalArgumentException if the predicate is null.
485     * @see org.apache.commons.collections4.functors.NullIsExceptionPredicate
486     */
487    public static <T> Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate){
488        return NullIsExceptionPredicate.nullIsExceptionPredicate(predicate);
489    }
490
491    /**
492     * Gets a Predicate that returns false if the input object is null, otherwise
493     * it calls the specified Predicate. This allows null handling behaviour to
494     * be added to Predicates that don't support nulls.
495     *
496     * @param <T>  the type that the predicate queries
497     * @param predicate  the predicate to wrap, may not be null
498     * @return the predicate
499     * @throws IllegalArgumentException if the predicate is null.
500     * @see org.apache.commons.collections4.functors.NullIsFalsePredicate
501     */
502    public static <T> Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate){
503        return NullIsFalsePredicate.nullIsFalsePredicate(predicate);
504    }
505
506    /**
507     * Gets a Predicate that returns true if the input object is null, otherwise
508     * it calls the specified Predicate. This allows null handling behaviour to
509     * be added to Predicates that don't support nulls.
510     *
511     * @param <T>  the type that the predicate queries
512     * @param predicate  the predicate to wrap, may not be null
513     * @return the predicate
514     * @throws IllegalArgumentException if the predicate is null.
515     * @see org.apache.commons.collections4.functors.NullIsTruePredicate
516     */
517    public static <T> Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate){
518        return NullIsTruePredicate.nullIsTruePredicate(predicate);
519    }
520
521    // Transformed
522    //-----------------------------------------------------------------------
523    /**
524     * Creates a predicate that transforms the input object before passing it
525     * to the predicate.
526     *
527     * @param <T>  the type that the predicate queries
528     * @param transformer  the transformer to call first
529     * @param predicate  the predicate to call with the result of the transform
530     * @return the predicate
531     * @throws IllegalArgumentException if the transformer or the predicate is null
532     * @see org.apache.commons.collections4.functors.TransformedPredicate
533     * @since 3.1
534     */
535    public static <T> Predicate<T> transformedPredicate(
536            final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate) {
537        return TransformedPredicate.<T>transformedPredicate(transformer, predicate);
538    }
539
540}