View Javadoc

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