1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.collections15.functors.predicate;
17
18
19 import net.sf.collections15.Predicate;
20
21 import java.util.Collection;
22
23 /***
24 * <code>Predicate</code> implementation that returns <code>true</code> only if
25 * all the <code>Predicate</code>s return true. i.e. it returns the logical AND
26 * of the wrapped <code>Predicates</code>.
27 * <p/>
28 * More specifically, an <code>AllPredicate</code> will evaluate to
29 * <code>true</code> unless any of its wrapped <code>Predicate</code>s evaluate
30 * to <code>false</code>. This implies that if its collection of wrapped
31 * <code>Predicate</code>s is empty, an <code>AllPredicate</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 AllPredicate <E> extends AbstractMultplePredicateDecorator<E>
39 {
40
41 static final long serialVersionUID = 5113897087152622644L;
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>false</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>AllPredicate</code>
58 * will always return <code>true</code>.
59 */
60 public static <T> AllPredicate<T> getInstance(Collection<Predicate<? super T>> predicates)
61 {
62 return new AllPredicate<T>(predicates);
63 }
64
65 /***
66 * Creates a new 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>AllPredicate</code>
76 * will always return <code>true</code>.
77 */
78 protected AllPredicate(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 of the wrapped <code>Predicate</code>s returns <code>false</code>.
86 *
87 * @param object The input object to evaluate.
88 *
89 * @return <code>true</code> unless any of the wrapped <code>Predicate</code>s
90 * evaluates to <code>false</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) == false) {
97 return false;
98 }
99 }
100 return true;
101 }
102 }