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.collections.comparators;
18  
19  import java.io.Serializable;
20  import java.util.Comparator;
21  
22  import org.apache.commons.collections.ComparatorUtils;
23  
24  /**
25   * Reverses the order of another comparator by reversing the arguments
26   * to its {@link #compare(Object, Object) compare} method.
27   *
28   * @since 2.0
29   * @version $Id: ReverseComparator.java 1429905 2013-01-07 17:15:14Z ggregory $
30   *
31   * @see java.util.Collections#reverseOrder()
32   */
33  public class ReverseComparator<E> implements Comparator<E>, Serializable {
34  
35      /** Serialization version from Collections 2.0. */
36      private static final long serialVersionUID = 2858887242028539265L;
37  
38      /** The comparator being decorated. */
39      private final Comparator<E> comparator;
40  
41      //-----------------------------------------------------------------------
42      /**
43       * Creates a comparator that compares objects based on the inverse of their
44       * natural ordering.  Using this Constructor will create a ReverseComparator
45       * that is functionally identical to the Comparator returned by
46       * java.util.Collections.<b>reverseOrder()</b>.
47       * 
48       * @see java.util.Collections#reverseOrder()
49       */
50      public ReverseComparator() {
51          this(null);
52      }
53  
54      /**
55       * Creates a comparator that inverts the comparison
56       * of the given comparator.  If you pass in <code>null</code>,
57       * the ReverseComparator defaults to reversing the
58       * natural order, as per 
59       * {@link java.util.Collections#reverseOrder()}</b>.
60       * 
61       * @param comparator Comparator to reverse
62       */
63      @SuppressWarnings("unchecked")
64      public ReverseComparator(final Comparator<E> comparator) {
65          this.comparator = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator;
66      }
67  
68      //-----------------------------------------------------------------------
69      /**
70       * Compares two objects in reverse order.
71       * 
72       * @param obj1  the first object to compare
73       * @param obj2  the second object to compare
74       * @return negative if obj1 is less, positive if greater, zero if equal
75       */
76      public int compare(final E obj1, final E obj2) {
77          return comparator.compare(obj2, obj1);
78      }
79  
80      //-----------------------------------------------------------------------
81      /**
82       * Implement a hash code for this comparator that is consistent with
83       * {@link #equals(Object) equals}.
84       * 
85       * @return a suitable hash code
86       * @since 3.0
87       */
88      @Override
89      public int hashCode() {
90          return "ReverseComparator".hashCode() ^ comparator.hashCode();
91      }
92  
93      /**
94       * Returns <code>true</code> iff <i>that</i> Object is 
95       * is a {@link Comparator} whose ordering is known to be 
96       * equivalent to mine.
97       * <p>
98       * This implementation returns <code>true</code>
99       * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code>
100      * equals <code>this.getClass()</code>, and the underlying 
101      * comparators are equal.
102      * Subclasses may want to override this behavior to remain consistent
103      * with the {@link Comparator#equals(Object) equals} contract.
104      * 
105      * @param object  the object to compare to
106      * @return true if equal
107      * @since 3.0
108      */
109     @Override
110     public boolean equals(final Object object) {
111         if (this == object) {
112             return true;
113         }
114         if (null == object) {
115             return false;
116         }
117         if (object.getClass().equals(this.getClass())) {
118             final ReverseComparator<?> thatrc = (ReverseComparator<?>) object;
119             return comparator.equals(thatrc.comparator);
120         }
121         return false;
122     }
123 
124 }