1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.collections15.functors.predicate;
17
18 import net.sf.collections15.Predicate;
19
20 /***
21 * <code>Predicate</code> implementation that decorates an existing
22 * <code>Predicate</code>, returning <code>false</code> if the input is
23 * <code>null</code>, and otherwise delegating to the decorated
24 * <code>Predicate</code>.
25 *
26 * @author Stephen Colebourne
27 * @author Chris Lambrou (port to Java 5.0)
28 * @since Collections15 1.0
29 */
30 public final class NullIsFalsePredicate <E> extends AbstractPredicateDecorator<E>
31 {
32
33 static final long serialVersionUID = -1618729285165144040L;
34
35 /***
36 * Returns an instance whose {@link #evaluate} method evaluates to
37 * <code>false</code> if the input is <code>null</code>, and otherwise
38 * delegates to the decorated <code>Predicate</code>.
39 *
40 * @param predicate The decorated <code>Predicate</code>.
41 *
42 * @return A <code>Predicate</code> that evaluates to <code>false</code> for
43 * a <code>null</code> input object, or delegates to the specified
44 * <code>Predicate</code> for non-<code>null</code> input.
45 *
46 * @throws IllegalArgumentException Thrown if the <code>Predicate</code> is
47 * <code>null</code>.
48 */
49 public static <E> NullIsFalsePredicate<E> getInstance(Predicate<? super E> predicate)
50 {
51 return new NullIsFalsePredicate<E>(predicate);
52 }
53
54 /***
55 * Creates a new instance whose {@link #evaluate} method evaluates to
56 * <code>false</code> if the input is <code>null</code>, and otherwise
57 * delegates to the decorated <code>Predicate</code>.
58 *
59 * @param predicate The decorated <code>Predicate</code>.
60 *
61 * @throws IllegalArgumentException Thrown if the <code>Predicate</code> is
62 * <code>null</code>.
63 */
64 protected NullIsFalsePredicate(Predicate<? super E> predicate)
65 {
66 super(predicate);
67 }
68
69 /***
70 * Evaluates the <code>Predicate</code>, returning <code>false</code> if the
71 * input object is <code>null</code>, or delegating to the decorated
72 * <code>Predicate</code> if the input is non-<code>null</code>.
73 *
74 * @param object The input object to evaluate.
75 *
76 * @return <code>false</code> if the input if <code>null</code>, or the
77 * result of the decorated <code>Predicate</code> if the input is
78 * non-<code>null</code>.
79 */
80 public boolean evaluate(E object)
81 {
82 if (object == null) {
83 return false;
84 }
85 return decoratedPredicate.evaluate(object);
86 }
87
88 }