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;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.functor.BinaryPredicate;
22 import org.apache.commons.functor.UnaryPredicate;
23 import org.apache.commons.functor.adapter.RightBoundPredicate;
24
25 /**
26 * {@link #test Tests}
27 * <code>true</code> iff its arguments are
28 * not {@link Object#equals equal} or both
29 * <code>null</code>.
30 * <p>
31 * This relation is symmetric but irreflexive
32 * and not transitive.
33 * </p>
34 * @param <L> the left argument type.
35 * @param <R> the right argument type.
36 * @version $Revision: 1160397 $ $Date: 2011-08-22 21:42:42 +0200 (Mon, 22 Aug 2011) $
37 * @author Rodney Waldhoff
38 */
39 public final class IsNotEqual<L, R> implements BinaryPredicate<L, R>, Serializable {
40 // static attributes
41 // ------------------------------------------------------------------------
42 /**
43 * Basic IsNotEqual<Object, Object> instance.
44 */
45 public static final IsNotEqual<Object, Object> INSTANCE = IsNotEqual.<Object, Object>instance();
46 /**
47 * serialVersionUID declaration.
48 */
49 private static final long serialVersionUID = -7303588338955281317L;
50
51 // constructor
52 // ------------------------------------------------------------------------
53 /**
54 * Create a new IsNotEqual.
55 */
56 public IsNotEqual() {
57 }
58
59 // predicate interface
60 // ------------------------------------------------------------------------
61 /**
62 * {@inheritDoc}
63 */
64 public boolean test(L left, R right) {
65 return (null == left ? null != right : !left.equals(right));
66 }
67
68 /**
69 * {@inheritDoc}
70 */
71 public boolean equals(Object that) {
72 return that instanceof IsNotEqual<?, ?>;
73 }
74
75 /**
76 * {@inheritDoc}
77 */
78 public int hashCode() {
79 return "IsNotEqual".hashCode();
80 }
81
82 /**
83 * {@inheritDoc}
84 */
85 public String toString() {
86 return "IsNotEqual";
87 }
88
89 // static methods
90 // ------------------------------------------------------------------------
91
92 /**
93 * Get an IsNotEqual instance.
94 * @param <L> the left argument type.
95 * @param <R> the right argument type.
96 * @return IsNotEqual<L, R>
97 */
98 public static <L, R> IsNotEqual<L, R> instance() {
99 return new IsNotEqual<L, R>();
100 }
101
102 /**
103 * Get an IsNotEqual UnaryPredicate.
104 * @param <L> the left argument type.
105 * @param <R> the right argument type.
106 * @param object bound comparison object
107 * @return UnaryPredicate<L>
108 */
109 public static <L, R> UnaryPredicate<L> to(R object) {
110 return new RightBoundPredicate<L>(new IsNotEqual<L, R>(), object);
111 }
112 }