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
23 /**
24 * {@link #test Tests} to the logical inverse
25 * of some other predicate.
26 * <p>
27 * Note that although this class implements
28 * {@link Serializable}, a given instance will
29 * only be truly <code>Serializable</code> if the
30 * underlying functor is. Attempts to serialize
31 * an instance whose delegate is not
32 * <code>Serializable</code> will result in an exception.
33 * </p>
34 * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
35 * @author Rodney Waldhoff
36 */
37 public final class BinaryNot<L, R> implements BinaryPredicate<L, R>, Serializable {
38 /**
39 * serialVersionUID declaration.
40 */
41 private static final long serialVersionUID = -3488974286912054737L;
42 // attributes
43 // ------------------------------------------------------------------------
44 private final BinaryPredicate<? super L, ? super R> predicate;
45
46 // constructor
47 // ------------------------------------------------------------------------
48 /**
49 * Create a new BinaryNot.
50 * @param predicate BinaryPredicate to negate
51 */
52 public BinaryNot(BinaryPredicate<? super L, ? super R> predicate) {
53 if (predicate == null) {
54 throw new IllegalArgumentException("BinaryPredicate argument was null");
55 }
56 this.predicate = predicate;
57 }
58
59 // predicate interface
60 // ------------------------------------------------------------------------
61 /**
62 * {@inheritDoc}
63 */
64 public boolean test(L left, R right) {
65 return !(predicate.test(left, right));
66 }
67
68 /**
69 * {@inheritDoc}
70 */
71 public boolean equals(Object that) {
72 return that == this || (that instanceof BinaryNot<?, ?> && equals((BinaryNot<?, ?>) that));
73 }
74
75 /**
76 * Learn whether another BinaryNot is equal to this.
77 * @param that BinaryNot to test
78 * @return boolean
79 */
80 public boolean equals(BinaryNot<?, ?> that) {
81 return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate));
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 public int hashCode() {
88 int hash = "BinaryNot".hashCode();
89 if (null != predicate) {
90 hash ^= predicate.hashCode();
91 }
92 return hash;
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 public String toString() {
99 return "BinaryNot<" + predicate + ">";
100 }
101
102 // static
103 // ------------------------------------------------------------------------
104 /**
105 * Negate a BinaryPredicate.
106 * @param that BinaryPredicate to negate
107 * @return BinaryPredicate
108 */
109 public static <L, R> BinaryPredicate<L, R> not(BinaryPredicate<? super L, ? super R> that) {
110 return null == that ? null : new BinaryNot<L, R>(that);
111 }
112
113 }