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.BinaryPredicate;
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 <L> the left argument type.
36   * @param <R> the right argument type.
37   * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
38   */
39  public final class BinaryNot<L, R> implements BinaryPredicate<L, R>, Serializable {
40      /**
41       * serialVersionUID declaration.
42       */
43      private static final long serialVersionUID = -3488974286912054737L;
44      // attributes
45      // ------------------------------------------------------------------------
46      /**
47       * The adapted predicate.
48       */
49      private final BinaryPredicate<? super L, ? super R> predicate;
50  
51      // constructor
52      // ------------------------------------------------------------------------
53      /**
54       * Create a new BinaryNot.
55       * @param predicate BinaryPredicate to negate
56       */
57      public BinaryNot(BinaryPredicate<? super L, ? super R> predicate) {
58          this.predicate = Validate.notNull(predicate, "BinaryPredicate argument was null");
59      }
60  
61      // predicate interface
62      // ------------------------------------------------------------------------
63      /**
64       * {@inheritDoc}
65       */
66      public boolean test(L left, R right) {
67          return !(predicate.test(left, right));
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public boolean equals(Object that) {
75          return that == this || (that instanceof BinaryNot<?, ?> && equals((BinaryNot<?, ?>) that));
76      }
77  
78      /**
79       * Learn whether another BinaryNot is equal to this.
80       * @param that BinaryNot to test
81       * @return boolean
82       */
83      public boolean equals(BinaryNot<?, ?> that) {
84          return null != that && predicate.equals(that.predicate);
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public int hashCode() {
92          int hash = "BinaryNot".hashCode();
93          hash ^= predicate.hashCode();
94          return hash;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public String toString() {
102         return "BinaryNot<" + predicate + ">";
103     }
104 
105     // static
106     // ------------------------------------------------------------------------
107     /**
108      * Negate a BinaryPredicate.
109      * @param <L> the left argument type.
110      * @param <R> the right argument type.
111      * @param that BinaryPredicate to negate
112      * @return BinaryPredicate
113      */
114     public static <L, R> BinaryPredicate<L, R> not(BinaryPredicate<? super L, ? super R> that) {
115         return null == that ? null : new BinaryNot<L, R>(that);
116     }
117 
118 }