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