View Javadoc

1   /*
2    *  Copyright 2001-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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 }