ReverseComparator.java

  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. import java.io.Serializable;
  19. import java.util.Comparator;

  20. import org.apache.commons.collections4.ComparatorUtils;

  21. /**
  22.  * Reverses the order of another comparator by reversing the arguments
  23.  * to its {@link #compare(Object, Object) compare} method.
  24.  *
  25.  * @param <E> the type of objects compared by this comparator
  26.  * @since 2.0
  27.  * @see java.util.Collections#reverseOrder()
  28.  */
  29. public class ReverseComparator<E> implements Comparator<E>, Serializable {

  30.     /** Serialization version from Collections 2.0. */
  31.     private static final long serialVersionUID = 2858887242028539265L;

  32.     /** The comparator being decorated. */
  33.     private final Comparator<? super E> comparator;

  34.     /**
  35.      * Creates a comparator that compares objects based on the inverse of their
  36.      * natural ordering.  Using this Constructor will create a ReverseComparator
  37.      * that is functionally identical to the Comparator returned by
  38.      * java.util.Collections.<strong>reverseOrder()</strong>.
  39.      *
  40.      * @see java.util.Collections#reverseOrder()
  41.      */
  42.     public ReverseComparator() {
  43.         this(null);
  44.     }

  45.     /**
  46.      * Creates a comparator that inverts the comparison
  47.      * of the given comparator.  If you pass in {@code null},
  48.      * the ReverseComparator defaults to reversing the
  49.      * natural order, as per {@link java.util.Collections#reverseOrder()}.
  50.      *
  51.      * @param comparator Comparator to reverse
  52.      */
  53.     public ReverseComparator(final Comparator<? super E> comparator) {
  54.         this.comparator = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator;
  55.     }

  56.     /**
  57.      * Compares two objects in reverse order.
  58.      *
  59.      * @param obj1  the first object to compare
  60.      * @param obj2  the second object to compare
  61.      * @return negative if obj1 is less, positive if greater, zero if equal
  62.      */
  63.     @Override
  64.     public int compare(final E obj1, final E obj2) {
  65.         return comparator.compare(obj2, obj1);
  66.     }

  67.     /**
  68.      * Returns {@code true} iff <em>that</em> Object is
  69.      * a {@link Comparator} whose ordering is known to be
  70.      * equivalent to mine.
  71.      * <p>
  72.      * This implementation returns {@code true}
  73.      * iff {@code <em>object</em>.{@link Object#getClass() getClass()}}
  74.      * equals {@code this.getClass()}, and the underlying
  75.      * comparators are equal.
  76.      * Subclasses may want to override this behavior to remain consistent
  77.      * with the {@link Comparator#equals(Object) equals} contract.
  78.      *
  79.      * @param object  the object to compare to
  80.      * @return true if equal
  81.      * @since 3.0
  82.      */
  83.     @Override
  84.     public boolean equals(final Object object) {
  85.         if (this == object) {
  86.             return true;
  87.         }
  88.         if (null == object) {
  89.             return false;
  90.         }
  91.         if (object.getClass().equals(this.getClass())) {
  92.             final ReverseComparator<?> thatrc = (ReverseComparator<?>) object;
  93.             return comparator.equals(thatrc.comparator);
  94.         }
  95.         return false;
  96.     }

  97.     /**
  98.      * Implement a hash code for this comparator that is consistent with
  99.      * {@link #equals(Object) equals}.
  100.      *
  101.      * @return a suitable hash code
  102.      * @since 3.0
  103.      */
  104.     @Override
  105.     public int hashCode() {
  106.         return "ReverseComparator".hashCode() ^ comparator.hashCode();
  107.     }

  108. }