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  
21  /***
22   * <code>Predicate</code> implementation that decorates an existing
23   * <code>Predicate</code>, returning the opposite of the decorated
24   * <code>Predicate</code>'s return value.
25   * <p/>
26   * Note that the generic type of the decorated <code>Predicate</code>,
27   * <code>D</code>, and the generic type of the decorating
28   * <code>Predicate</code>, <code>E</code> are not required to be the same, but
29   * an instance of type <code>E</code> must be assignable to a reference of type
30   * <code>D</code>.
31   *
32   * @author Stephen Colebourne
33   * @author Chris Lambrou (port to Java 5.0)
34   * @since Collections15 1.0
35   */
36  public class NotPredicate <E> extends AbstractPredicateDecorator<E>
37  {
38  
39      static final long serialVersionUID = 5237151612134812288L;
40  
41      /***
42       * Returns an instance whose {@link #evaluate} method returns the inverse
43       * of the decorated <code>Predicate</code>'s <code>evaluate</code> method.
44       *
45       * @param predicate The decorated <code>Predicate</code>.
46       *
47       * @throws IllegalArgumentException Thrown if the decorated <code>Predicate</code>
48       *                                  is <code>null</code>.
49       */
50      public static <E> NotPredicate<E> getInstance(Predicate<? super E> predicate)
51      {
52          return new NotPredicate<E>(predicate);
53      }
54  
55      /***
56       * Creates a new instance whose {@link #evaluate} method returns the inverse
57       * of the decorated <code>Predicate</code>'s <code>evaluate</code> method.
58       *
59       * @param predicate The decorated <code>Predicate</code>.
60       *
61       * @throws IllegalArgumentException Thrown if the decorated <code>Predicate</code>
62       *                                  is <code>null</code>.
63       */
64      protected NotPredicate(Predicate<? super E> predicate)
65      {
66          super(predicate);
67      }
68  
69      /***
70       * Evaluates the <code>Predicate</code>, returning the opposite value to the
71       * decorated <code>Predicate</code>'s <code>evaluate</code> method.
72       *
73       * @param object The input object to evaluate.
74       *
75       * @return <code>true</code> if the decorated <code>Predicate</code> returns
76       *         <code>false</code>. <code>false</code> if the decorated
77       *         <code>Predicate</code> returns <code>true</code>.
78       */
79      public boolean evaluate(E object)
80      {
81          return !(decoratedPredicate.evaluate(object));
82      }
83  
84  }