1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.core.composite;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.functor.UnaryPredicate;
22  import org.apache.commons.lang3.Validate;
23  
24  /**
25   * {@link #test Tests} to the logical inverse
26   * of some other predicate.
27   * <p>
28   * Note that although this class implements
29   * {@link Serializable}, a given instance will
30   * only be truly <code>Serializable</code> if the
31   * underlying functor is.  Attempts to serialize
32   * an instance whose delegate is not
33   * <code>Serializable</code> will result in an exception.
34   * </p>
35   * @param <A> the argument type.
36   * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
37   */
38  public final class UnaryNot<A> implements UnaryPredicate<A>, Serializable {
39      /**
40       * serialVersionUID declaration.
41       */
42      private static final long serialVersionUID = -97785102566566058L;
43      // attributes
44      // ------------------------------------------------------------------------
45      /**
46       * The adapted predicate.
47       */
48      private final UnaryPredicate<? super A> predicate;
49  
50      // constructor
51      // ------------------------------------------------------------------------
52      /**
53       * Create a new UnaryNot.
54       * @param predicate UnaryPredicate to negate
55       */
56      public UnaryNot(UnaryPredicate<? super A> predicate) {
57          this.predicate = Validate.notNull(predicate, "UnaryPredicate argument was null");
58      }
59  
60      // predicate interface
61      // ------------------------------------------------------------------------
62      /**
63       * {@inheritDoc}
64       */
65      public boolean test(A obj) {
66          return !(predicate.test(obj));
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public boolean equals(Object that) {
74          return that == this || (that instanceof UnaryNot<?> && equals((UnaryNot<?>) that));
75      }
76  
77      /**
78       * Learn whether another UnaryNot is equal to this.
79       * @param that UnaryNot to test
80       * @return boolean
81       */
82      public boolean equals(UnaryNot<?> that) {
83          return null != that && predicate.equals(that.predicate);
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public int hashCode() {
91          int hash = "UnaryNot".hashCode();
92          hash ^= predicate.hashCode();
93          return hash;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public String toString() {
101         return "UnaryNot<" + predicate + ">";
102     }
103 
104     // static
105     // ------------------------------------------------------------------------
106     /**
107      * Invert a UnaryPredicate.
108      * @param <A> the argument type.
109      * @param pred UnaryPredicate to invert
110      * @return UnaryPredicate<A>
111      */
112     public static <A> UnaryPredicate<A> not(UnaryPredicate<? super A> pred) {
113         return null == pred ? null : new UnaryNot<A>(pred);
114     }
115 
116 }