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.collections4.comparators;
18  
19  import java.io.Serializable;
20  import java.util.Comparator;
21  
22  import org.apache.commons.collections4.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   * @param <E> the type of objects compared by this comparator
29   *
30   * @since 2.0
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<? super 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 {@link java.util.Collections#reverseOrder()}.
59       *
60       * @param comparator Comparator to reverse
61       */
62      public ReverseComparator(final Comparator<? super E> comparator) {
63          this.comparator = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator;
64      }
65  
66      //-----------------------------------------------------------------------
67      /**
68       * Compares two objects in reverse order.
69       *
70       * @param obj1  the first object to compare
71       * @param obj2  the second object to compare
72       * @return negative if obj1 is less, positive if greater, zero if equal
73       */
74      @Override
75      public int compare(final E obj1, final E obj2) {
76          return comparator.compare(obj2, obj1);
77      }
78  
79      //-----------------------------------------------------------------------
80      /**
81       * Implement a hash code for this comparator that is consistent with
82       * {@link #equals(Object) equals}.
83       *
84       * @return a suitable hash code
85       * @since 3.0
86       */
87      @Override
88      public int hashCode() {
89          return "ReverseComparator".hashCode() ^ comparator.hashCode();
90      }
91  
92      /**
93       * Returns <code>true</code> iff <i>that</i> Object is
94       * is a {@link Comparator} whose ordering is known to be
95       * equivalent to mine.
96       * <p>
97       * This implementation returns <code>true</code>
98       * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code>
99       * equals <code>this.getClass()</code>, and the underlying
100      * comparators are equal.
101      * Subclasses may want to override this behavior to remain consistent
102      * with the {@link Comparator#equals(Object) equals} contract.
103      *
104      * @param object  the object to compare to
105      * @return true if equal
106      * @since 3.0
107      */
108     @Override
109     public boolean equals(final Object object) {
110         if (this == object) {
111             return true;
112         }
113         if (null == object) {
114             return false;
115         }
116         if (object.getClass().equals(this.getClass())) {
117             final ReverseComparator<?> thatrc = (ReverseComparator<?>) object;
118             return comparator.equals(thatrc.comparator);
119         }
120         return false;
121     }
122 
123 }