View Javadoc

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.collections;
18  
19  import java.util.Collection;
20  
21  import org.apache.commons.collections.functors.AllPredicate;
22  import org.apache.commons.collections.functors.AndPredicate;
23  import org.apache.commons.collections.functors.AnyPredicate;
24  import org.apache.commons.collections.functors.EqualPredicate;
25  import org.apache.commons.collections.functors.ExceptionPredicate;
26  import org.apache.commons.collections.functors.FalsePredicate;
27  import org.apache.commons.collections.functors.IdentityPredicate;
28  import org.apache.commons.collections.functors.InstanceofPredicate;
29  import org.apache.commons.collections.functors.InvokerTransformer;
30  import org.apache.commons.collections.functors.NonePredicate;
31  import org.apache.commons.collections.functors.NotNullPredicate;
32  import org.apache.commons.collections.functors.NotPredicate;
33  import org.apache.commons.collections.functors.NullIsExceptionPredicate;
34  import org.apache.commons.collections.functors.NullIsFalsePredicate;
35  import org.apache.commons.collections.functors.NullIsTruePredicate;
36  import org.apache.commons.collections.functors.NullPredicate;
37  import org.apache.commons.collections.functors.OnePredicate;
38  import org.apache.commons.collections.functors.OrPredicate;
39  import org.apache.commons.collections.functors.TransformedPredicate;
40  import org.apache.commons.collections.functors.TransformerPredicate;
41  import org.apache.commons.collections.functors.TruePredicate;
42  import org.apache.commons.collections.functors.UniquePredicate;
43  
44  /**
45   * <code>PredicateUtils</code> provides reference implementations and utilities
46   * for the Predicate functor interface. The supplied predicates are:
47   * <ul>
48   * <li>Invoker - returns the result of a method call on the input object
49   * <li>InstanceOf - true if the object is an instanceof a class
50   * <li>Equal - true if the object equals() a specified object
51   * <li>Identity - true if the object == a specified object
52   * <li>Null - true if the object is null
53   * <li>NotNull - true if the object is not null
54   * <li>Unique - true if the object has not already been evaluated
55   * <li>And/All - true if all of the predicates are true
56   * <li>Or/Any - true if any of the predicates is true
57   * <li>Either/One - true if only one of the predicate is true
58   * <li>Neither/None - true if none of the predicates are true
59   * <li>Not - true if the predicate is false, and vice versa
60   * <li>Transformer - wraps a Transformer as a Predicate
61   * <li>True - always return true
62   * <li>False - always return false
63   * <li>Exception - always throws an exception
64   * <li>NullIsException/NullIsFalse/NullIsTrue - check for null input
65   * <li>Transformed - transforms the input before calling the predicate
66   * </ul>
67   * All the supplied predicates are Serializable.
68   *
69   * @since 3.0
70   * @version $Id: PredicateUtils.java 1436345 2013-01-21 13:49:53Z tn $
71   */
72  public class PredicateUtils {
73  
74      /**
75       * This class is not normally instantiated.
76       */
77      public PredicateUtils() {
78          super();
79      }
80  
81      // Simple predicates
82      //-----------------------------------------------------------------------------
83  
84      /**
85       * Gets a Predicate that always throws an exception.
86       * This could be useful during testing as a placeholder.
87       *
88       * @param <T>  the type that the predicate queries
89       * @return the predicate
90       * @see org.apache.commons.collections.functors.ExceptionPredicate
91       */
92      public static <T> Predicate<T> exceptionPredicate() {
93          return ExceptionPredicate.<T>exceptionPredicate();
94      }
95  
96      /**
97       * Gets a Predicate that always returns true.
98       *
99       * @param <T>  the type that the predicate queries
100      * @return the predicate
101      * @see org.apache.commons.collections.functors.TruePredicate
102      */
103     public static <T> Predicate<T> truePredicate() {
104         return TruePredicate.truePredicate();
105     }
106 
107     /**
108      * Gets a Predicate that always returns false.
109      *
110      * @param <T>  the type that the predicate queries
111      * @return the predicate
112      * @see org.apache.commons.collections.functors.FalsePredicate
113      */
114     public static <T> Predicate<T> falsePredicate() {
115         return FalsePredicate.<T> falsePredicate();
116     }
117 
118     /**
119      * Gets a Predicate that checks if the input object passed in is null.
120      *
121      * @param <T>  the type that the predicate queries
122      * @return the predicate
123      * @see org.apache.commons.collections.functors.NullPredicate
124      */
125     public static <T> Predicate<T> nullPredicate() {
126         return NullPredicate.nullPredicate();
127     }
128 
129     /**
130      * Gets a Predicate that checks if the input object passed in is not null.
131      *
132      * @param <T>  the type that the predicate queries
133      * @return the predicate
134      * @see org.apache.commons.collections.functors.NotNullPredicate
135      */
136     public static <T> Predicate<T> notNullPredicate() {
137         return NotNullPredicate.<T>notNullPredicate();
138     }
139 
140     /**
141      * Creates a Predicate that checks if the input object is equal to the
142      * specified object using equals().
143      *
144      * @param <T>  the type that the predicate queries
145      * @param value  the value to compare against
146      * @return the predicate
147      * @see org.apache.commons.collections.functors.EqualPredicate
148      */
149     public static <T> Predicate<T> equalPredicate(final T value) {
150         return EqualPredicate.equalPredicate(value);
151     }
152 
153     /**
154      * Creates a Predicate that checks if the input object is equal to the
155      * specified object by identity.
156      *
157      * @param <T>  the type that the predicate queries
158      * @param value  the value to compare against
159      * @return the predicate
160      * @see org.apache.commons.collections.functors.IdentityPredicate
161      */
162     public static <T> Predicate<T> identityPredicate(final T value) {
163         return IdentityPredicate.<T>identityPredicate(value);
164     }
165 
166     /**
167      * Creates a Predicate that checks if the object passed in is of
168      * a particular type, using instanceof. A <code>null</code> input
169      * object will return <code>false</code>.
170      *
171      * @param type  the type to check for, may not be null
172      * @return the predicate
173      * @throws IllegalArgumentException if the class is null
174      * @see org.apache.commons.collections.functors.InstanceofPredicate
175      */
176     public static Predicate<Object> instanceofPredicate(final Class<?> type) {
177         return InstanceofPredicate.instanceOfPredicate(type);
178     }
179 
180     /**
181      * Creates a Predicate that returns true the first time an object is
182      * encountered, and false if the same object is received
183      * again. The comparison is by equals(). A <code>null</code> input object
184      * is accepted and will return true the first time, and false subsequently
185      * as well.
186      *
187      * @param <T>  the type that the predicate queries
188      * @return the predicate
189      * @see org.apache.commons.collections.functors.UniquePredicate
190      */
191     public static <T> Predicate<T> uniquePredicate() {
192         // must return new instance each time
193         return UniquePredicate.<T>uniquePredicate();
194     }
195 
196     /**
197      * Creates a Predicate that invokes a method on the input object.
198      * The method must return either a boolean or a non-null Boolean,
199      * and have no parameters. If the input object is null, a
200      * PredicateException is thrown.
201      * <p>
202      * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code>
203      * will call the <code>isEmpty</code> method on the input object to
204      * determine the predicate result.
205      *
206      * @param <T>  the type that the predicate queries
207      * @param methodName  the method name to call on the input object, may not be null
208      * @return the predicate
209      * @throws IllegalArgumentException if the methodName is null.
210      * @see org.apache.commons.collections.functors.InvokerTransformer
211      * @see org.apache.commons.collections.functors.TransformerPredicate
212      */
213     public static <T> Predicate<T> invokerPredicate(final String methodName) {
214         // reuse transformer as it has caching - this is lazy really, should have inner class here
215         return asPredicate(InvokerTransformer.<Object, Boolean>invokerTransformer(methodName));
216     }
217 
218     /**
219      * Creates a Predicate that invokes a method on the input object.
220      * The method must return either a boolean or a non-null Boolean,
221      * and have no parameters. If the input object is null, a
222      * PredicateException is thrown.
223      * <p>
224      * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code>
225      * will call the <code>isEmpty</code> method on the input object to
226      * determine the predicate result.
227      *
228      * @param <T>  the type that the predicate queries
229      * @param methodName  the method name to call on the input object, may not be null
230      * @param paramTypes  the parameter types
231      * @param args  the arguments
232      * @return the predicate
233      * @throws IllegalArgumentException if the method name is null
234      * @throws IllegalArgumentException if the paramTypes and args don't match
235      * @see org.apache.commons.collections.functors.InvokerTransformer
236      * @see org.apache.commons.collections.functors.TransformerPredicate
237      */
238     public static <T> Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes,
239                                                     final Object[] args) {
240         // reuse transformer as it has caching - this is lazy really, should have inner class here
241         return asPredicate(InvokerTransformer.<Object, Boolean>invokerTransformer(methodName, paramTypes, args));
242     }
243 
244     // Boolean combinations
245     //-----------------------------------------------------------------------------
246 
247     /**
248      * Create a new Predicate that returns true only if both of the specified
249      * predicates are true.
250      *
251      * @param <T>  the type that the predicate queries
252      * @param predicate1  the first predicate, may not be null
253      * @param predicate2  the second predicate, may not be null
254      * @return the <code>and</code> predicate
255      * @throws IllegalArgumentException if either predicate is null
256      * @see org.apache.commons.collections.functors.AndPredicate
257      */
258     public static <T> Predicate<T> andPredicate(final Predicate<? super T> predicate1,
259                                                 final Predicate<? super T> predicate2) {
260         return AndPredicate.<T>andPredicate(predicate1, predicate2);
261     }
262 
263     /**
264      * Create a new Predicate that returns true only if all of the specified
265      * predicates are true.
266      * If the array of predicates is empty, then this predicate returns true.
267      *
268      * @param <T>  the type that the predicate queries
269      * @param predicates  an array of predicates to check, may not be null
270      * @return the <code>all</code> predicate
271      * @throws IllegalArgumentException if the predicates array is null
272      * @throws IllegalArgumentException if any predicate in the array is null
273      * @see org.apache.commons.collections.functors.AllPredicate
274      */
275     public static <T> Predicate<T> allPredicate(final Predicate<? super T>... predicates) {
276         return AllPredicate.allPredicate(predicates);
277     }
278 
279     /**
280      * Create a new Predicate that returns true only if all of the specified
281      * predicates are true. The predicates are checked in iterator order.
282      * If the collection of predicates is empty, then this predicate returns true.
283      *
284      * @param <T>  the type that the predicate queries
285      * @param predicates  a collection of predicates to check, may not be null
286      * @return the <code>all</code> predicate
287      * @throws IllegalArgumentException if the predicates collection is null
288      * @throws IllegalArgumentException if any predicate in the collection is null
289      * @see org.apache.commons.collections.functors.AllPredicate
290      */
291     public static <T> Predicate<T> allPredicate(final Collection<? extends Predicate<T>> predicates) {
292         return AllPredicate.allPredicate(predicates);
293     }
294 
295     /**
296      * Create a new Predicate that returns true if either of the specified
297      * predicates are true.
298      *
299      * @param <T>  the type that the predicate queries
300      * @param predicate1  the first predicate, may not be null
301      * @param predicate2  the second predicate, may not be null
302      * @return the <code>or</code> predicate
303      * @throws IllegalArgumentException if either predicate is null
304      * @see org.apache.commons.collections.functors.OrPredicate
305      */
306     public static <T> Predicate<T> orPredicate(final Predicate<? super T> predicate1,
307                                                final Predicate<? super T> predicate2) {
308         return OrPredicate.<T>orPredicate(predicate1, predicate2);
309     }
310 
311     /**
312      * Create a new Predicate that returns true if any of the specified
313      * predicates are true.
314      * If the array of predicates is empty, then this predicate returns false.
315      *
316      * @param <T>  the type that the predicate queries
317      * @param predicates  an array of predicates to check, may not be null
318      * @return the <code>any</code> predicate
319      * @throws IllegalArgumentException if the predicates array is null
320      * @throws IllegalArgumentException if any predicate in the array is null
321      * @see org.apache.commons.collections.functors.AnyPredicate
322      */
323     public static <T> Predicate<T> anyPredicate(final Predicate<? super T>... predicates) {
324         return AnyPredicate.anyPredicate(predicates);
325     }
326 
327     /**
328      * Create a new Predicate that returns true if any of the specified
329      * predicates are true. The predicates are checked in iterator order.
330      * If the collection of predicates is empty, then this predicate returns false.
331      *
332      * @param <T>  the type that the predicate queries
333      * @param predicates  a collection of predicates to check, may not be null
334      * @return the <code>any</code> predicate
335      * @throws IllegalArgumentException if the predicates collection is null
336      * @throws IllegalArgumentException if any predicate in the collection is null
337      * @see org.apache.commons.collections.functors.AnyPredicate
338      */
339     public static <T> Predicate<T> anyPredicate(final Collection<? extends Predicate<T>> predicates) {
340         return AnyPredicate.anyPredicate(predicates);
341     }
342 
343     /**
344      * Create a new Predicate that returns true if one, but not both, of the
345      * specified predicates are true. XOR
346      *
347      * @param <T>  the type that the predicate queries
348      * @param predicate1  the first predicate, may not be null
349      * @param predicate2  the second predicate, may not be null
350      * @return the <code>either</code> predicate
351      * @throws IllegalArgumentException if either predicate is null
352      * @see org.apache.commons.collections.functors.OnePredicate
353      */
354     public static <T> Predicate<T> eitherPredicate(final Predicate<? super T> predicate1,
355                                                    final Predicate<? super T> predicate2) {
356         @SuppressWarnings("unchecked")
357         final Predicate<T> onePredicate = PredicateUtils.<T>onePredicate(predicate1, predicate2);
358         return onePredicate;
359     }
360 
361     /**
362      * Create a new Predicate that returns true if only one of the specified
363      * predicates are true.
364      * If the array of predicates is empty, then this predicate returns false.
365      *
366      * @param <T>  the type that the predicate queries
367      * @param predicates  an array of predicates to check, may not be null
368      * @return the <code>one</code> predicate
369      * @throws IllegalArgumentException if the predicates array is null
370      * @throws IllegalArgumentException if any predicate in the array is null
371      * @see org.apache.commons.collections.functors.OnePredicate
372      */
373     public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates) {
374         return OnePredicate.onePredicate(predicates);
375     }
376 
377     /**
378      * Create a new Predicate that returns true if only one of the specified
379      * predicates are true. The predicates are checked in iterator order.
380      * If the collection of predicates is empty, then this predicate returns false.
381      *
382      * @param <T>  the type that the predicate queries
383      * @param predicates  a collection of predicates to check, may not be null
384      * @return the <code>one</code> predicate
385      * @throws IllegalArgumentException if the predicates collection is null
386      * @throws IllegalArgumentException if any predicate in the collection is null
387      * @see org.apache.commons.collections.functors.OnePredicate
388      */
389     public static <T> Predicate<T> onePredicate(final Collection<Predicate<T>> predicates) {
390         return OnePredicate.onePredicate(predicates);
391     }
392 
393     /**
394      * Create a new Predicate that returns true if neither of the specified
395      * predicates are true.
396      *
397      * @param <T>  the type that the predicate queries
398      * @param predicate1  the first predicate, may not be null
399      * @param predicate2  the second predicate, may not be null
400      * @return the <code>neither</code> predicate
401      * @throws IllegalArgumentException if either predicate is null
402      * @see org.apache.commons.collections.functors.NonePredicate
403      */
404     public static <T> Predicate<T> neitherPredicate(final Predicate<? super T> predicate1,
405                                                     final Predicate<? super T> predicate2) {
406         @SuppressWarnings("unchecked")
407         final Predicate<T> nonePredicate = PredicateUtils.<T>nonePredicate(predicate1, predicate2);
408         return nonePredicate;
409     }
410 
411     /**
412      * Create a new Predicate that returns true if none of the specified
413      * predicates are true.
414      * If the array of predicates is empty, then this predicate returns true.
415      *
416      * @param <T>  the type that the predicate queries
417      * @param predicates  an array of predicates to check, may not be null
418      * @return the <code>none</code> predicate
419      * @throws IllegalArgumentException if the predicates array is null
420      * @throws IllegalArgumentException if any predicate in the array is null
421      * @see org.apache.commons.collections.functors.NonePredicate
422      */
423     public static <T> Predicate<T> nonePredicate(final Predicate<? super T>... predicates) {
424         return NonePredicate.nonePredicate(predicates);
425     }
426 
427     /**
428      * Create a new Predicate that returns true if none of the specified
429      * predicates are true. The predicates are checked in iterator order.
430      * If the collection of predicates is empty, then this predicate returns true.
431      *
432      * @param <T>  the type that the predicate queries
433      * @param predicates  a collection of predicates to check, may not be null
434      * @return the <code>none</code> predicate
435      * @throws IllegalArgumentException if the predicates collection is null
436      * @throws IllegalArgumentException if any predicate in the collection is null
437      * @see org.apache.commons.collections.functors.NonePredicate
438      */
439     public static <T> Predicate<T> nonePredicate(final Collection<? extends Predicate<T>> predicates) {
440         return NonePredicate.nonePredicate(predicates);
441     }
442 
443     /**
444      * Create a new Predicate that returns true if the specified predicate
445      * returns false and vice versa.
446      *
447      * @param <T>  the type that the predicate queries
448      * @param predicate  the predicate to not
449      * @return the <code>not</code> predicate
450      * @throws IllegalArgumentException if the predicate is null
451      * @see org.apache.commons.collections.functors.NotPredicate
452      */
453     public static <T> Predicate<T> notPredicate(final Predicate<? super T> predicate) {
454         return NotPredicate.notPredicate(predicate);
455     }
456 
457     // Adaptors
458     //-----------------------------------------------------------------------------
459 
460     /**
461      * Create a new Predicate that wraps a Transformer. The Transformer must
462      * return either Boolean.TRUE or Boolean.FALSE otherwise a PredicateException
463      * will be thrown.
464      *
465      * @param <T>  the type that the predicate queries
466      * @param transformer  the transformer to wrap, may not be null
467      * @return the transformer wrapping predicate
468      * @throws IllegalArgumentException if the transformer is null
469      * @see org.apache.commons.collections.functors.TransformerPredicate
470      */
471     public static <T> Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer) {
472         return TransformerPredicate.transformerPredicate(transformer);
473     }
474 
475     // Null handlers
476     //-----------------------------------------------------------------------------
477 
478     /**
479      * Gets a Predicate that throws an exception if the input object is null,
480      * otherwise it calls the specified Predicate. This allows null handling
481      * behaviour to be added to Predicates that don't support nulls.
482      *
483      * @param <T>  the type that the predicate queries
484      * @param predicate  the predicate to wrap, may not be null
485      * @return the predicate
486      * @throws IllegalArgumentException if the predicate is null.
487      * @see org.apache.commons.collections.functors.NullIsExceptionPredicate
488      */
489     public static <T> Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate){
490         return NullIsExceptionPredicate.nullIsExceptionPredicate(predicate);
491     }
492 
493     /**
494      * Gets a Predicate that returns false if the input object is null, otherwise
495      * it calls the specified Predicate. This allows null handling behaviour to
496      * be added to Predicates that don't support nulls.
497      *
498      * @param <T>  the type that the predicate queries
499      * @param predicate  the predicate to wrap, may not be null
500      * @return the predicate
501      * @throws IllegalArgumentException if the predicate is null.
502      * @see org.apache.commons.collections.functors.NullIsFalsePredicate
503      */
504     public static <T> Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate){
505         return NullIsFalsePredicate.nullIsFalsePredicate(predicate);
506     }
507 
508     /**
509      * Gets a Predicate that returns true if the input object is null, otherwise
510      * it calls the specified Predicate. This allows null handling behaviour to
511      * be added to Predicates that don't support nulls.
512      *
513      * @param <T>  the type that the predicate queries
514      * @param predicate  the predicate to wrap, may not be null
515      * @return the predicate
516      * @throws IllegalArgumentException if the predicate is null.
517      * @see org.apache.commons.collections.functors.NullIsTruePredicate
518      */
519     public static <T> Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate){
520         return NullIsTruePredicate.nullIsTruePredicate(predicate);
521     }
522 
523     // Transformed
524     //-----------------------------------------------------------------------
525     /**
526      * Creates a predicate that transforms the input object before passing it
527      * to the predicate.
528      *
529      * @param <T>  the type that the predicate queries
530      * @param transformer  the transformer to call first
531      * @param predicate  the predicate to call with the result of the transform
532      * @return the predicate
533      * @throws IllegalArgumentException if the transformer or the predicate is null
534      * @see org.apache.commons.collections.functors.TransformedPredicate
535      * @since 3.1
536      */
537     public static <T> Predicate<T> transformedPredicate(
538             final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate) {
539         return TransformedPredicate.<T>transformedPredicate(transformer, predicate);
540     }
541 
542 }