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.comparator;
18  
19  import java.io.Serializable;
20  import java.util.Comparator;
21  
22  import org.apache.commons.functor.BinaryPredicate;
23  import org.apache.commons.functor.UnaryPredicate;
24  import org.apache.commons.functor.adapter.RightBoundPredicate;
25  import org.apache.commons.lang3.Validate;
26  
27  /**
28   * A {@link BinaryPredicate BinaryPredicate} that {@link #test tests}
29   * <code>true</code> iff the left argument is not equal to the
30   * right argument under the specified {@link Comparator}.
31   * When no (or a <code>null</code> <code>Comparator</code> is specified,
32   * a {@link Comparable Comparable} <code>Comparator</code> is used.
33   *
34   * @see org.apache.commons.functor.core.IsEqual
35   * @param <T> the binary predicate input types
36   * @version $Revision: 1365328 $ $Date: 2012-07-24 19:19:23 -0300 (Tue, 24 Jul 2012) $
37   */
38  public final class IsNotEquivalent<T> implements BinaryPredicate<T, T>, Serializable {
39  
40      /**
41       * Basic IsNotEquivalent instance.
42       */
43      public static final IsNotEquivalent<Comparable<?>> INSTANCE = IsNotEquivalent.<Comparable<?>>instance();
44  
45      /**
46       * serialVersionUID declaration.
47       */
48      private static final long serialVersionUID = 1021154684877529051L;
49  
50      /**
51       * The wrapped comparator.
52       */
53      private final Comparator<? super T> comparator;
54  
55      /**
56       * Create a new IsNotEquivalent.
57       */
58      @SuppressWarnings("unchecked")
59      public IsNotEquivalent() {
60          this((Comparator<? super T>) ComparableComparator.INSTANCE);
61      }
62  
63      /**
64       * Construct a <code>IsNotEquivalent</code> {@link BinaryPredicate predicate}
65       * for the given {@link Comparator Comparator}.
66       *
67       * @param comparator the {@link Comparator Comparator}, when <code>null</code>,
68       *        a <code>Comparator</code> for {@link Comparable Comparable}s will
69       *        be used.
70       */
71      public IsNotEquivalent(Comparator<? super T> comparator) {
72          this.comparator = Validate.notNull(comparator, "Comparator argument must not be null");
73      }
74  
75      /**
76       * {@inheritDoc}
77       * Return <code>true</code> iff the <i>left</i> parameter is
78       * not equal to the <i>right</i> parameter under my current
79       * {@link Comparator Comparator}.
80       */
81      public boolean test(T left, T right) {
82          return comparator.compare(left, right) != 0;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public boolean equals(Object that) {
90          return that == this || (that instanceof IsNotEquivalent<?> && equals((IsNotEquivalent<?>) that));
91      }
92  
93      /**
94       * Learn whether another IsNotEquivalent is equal to this.
95       * @param that IsNotEquivalent to test
96       * @return boolean
97       */
98      public boolean equals(IsNotEquivalent<?> that) {
99          if (null != that) {
100             return comparator.equals(that.comparator);
101         }
102         return false;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public int hashCode() {
110         int hash = "IsNotEquivalent".hashCode();
111         // by construction, comparator is never null
112         hash ^= comparator.hashCode();
113         return hash;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public String toString() {
121         return "IsNotEquivalent<" + comparator + ">";
122     }
123 
124     /**
125      * Get an IsNotEquivalent instance.
126      *
127      * @param <T> the binary predicate input types
128      * @return IsNotEquivalent
129      */
130     @SuppressWarnings("unchecked")
131     public static <T extends Comparable<?>> IsNotEquivalent<T> instance() {
132         return new IsNotEquivalent<T>((Comparator<? super T>) ComparableComparator.INSTANCE);
133     }
134 
135     /**
136      * Get an IsNotEquivalent UnaryPredicate.
137      *
138      * @param <T> the binary predicate input types
139      * @param right Comparable against which UnaryPredicate arguments will be compared.
140      * @return UnaryPredicate
141      */
142     public static <T extends Comparable<?>> UnaryPredicate<T> instance(T right) {
143         return RightBoundPredicate.bind(instance(), right);
144     }
145 
146 }