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 import java.util.Collection;
21
22
23 /***
24 * <code>Predicate</code> implementation that returns <code>true</code> unless
25 * any of the wrapped <code>Predicate</code>s return <code>true</code>. i.e. it
26 * returns the logical NOR of the wrapped <code>Predicate</code>s.
27 * <p/>
28 * More specifically, an <code>NonePredicate</code> will evaluate to
29 * <code>true</code> unless any of its wrapped <code>Predicate</code>s evaluates
30 * to <code>true</code>. This implies that if its collection of wrapped
31 * <code>Predicate</code>s is empty, an <code>NonePredicate</code> will always
32 * evaluate to <code>true</code>.
33 *
34 * @author Stephen Colebourne
35 * @author Chris Lambrou (port to Java 5.0)
36 * @since Collections15 1.0
37 */
38 public final class NonePredicate <E> extends AbstractMultplePredicateDecorator<E>
39 {
40
41 static final long serialVersionUID = -2938835189720150174L;
42
43 /***
44 * Returns an instance that wraps the specified collection of
45 * <code>Predicate</code>s.
46 *
47 * @param predicates The <cod>Predicate</code>s to wrap. The contents of the
48 * collection are defensively copied by the new instance.
49 *
50 * @return A <code>Predicate</code> instance that will evaluate to
51 * <code>true</code> unless any of the wrapped <code>Predicate</code>s
52 * evaluate to <code>true</code>.
53 *
54 * @throws IllegalArgumentException Thrown if the collection, or any of its
55 * elements are <code>null</code>. The
56 * collection may be empty, however, in
57 * which case the resulting <code>AnyPredicate</code>
58 * will always return <code>true</code>.
59 */
60 public static <T> NonePredicate<T> getInstance(Collection<Predicate<? super T>> predicates)
61 {
62 return new NonePredicate<T>(predicates);
63 }
64
65 /***
66 * Creates an instance that wraps the specified collection of
67 * <code>Predicate</code>s.
68 *
69 * @param predicates The <cod>Predicate</code>s to wrap. The contents of the
70 * collection are defensively copied by the new instance.
71 *
72 * @throws IllegalArgumentException Thrown if the collection, or any of its
73 * elements are <code>null</code>. The
74 * collection may be empty, however, in
75 * which case the resulting <code>AnyPredicate</code>
76 * will always return <code>true</code>.
77 */
78 protected NonePredicate(Collection<Predicate<? super E>> predicates)
79 {
80 super(predicates);
81 }
82
83 /***
84 * Evaluates the <code>Predicate</code>, returning <code>true</code> unless
85 * any <code>Predicate</code> returns <code>true</code>.
86 *
87 * @param object The input object to evaluate.
88 *
89 * @return <code>true</code> unless any decorated <code>Predicate</code>
90 * returns <code>true</code>.
91 */
92 public boolean evaluate(E object)
93 {
94 int size = decoratedPredicates.size();
95 for (int i = 0; i < size; i++) {
96 if (decoratedPredicates.get(i).evaluate(object)) {
97 return false;
98 }
99 }
100 return true;
101 }
102 }