ComparableComparator.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. /**
  21.  * A {@link Comparator Comparator} that compares {@link Comparable Comparable}
  22.  * objects.
  23.  * <p>
  24.  * This Comparator is useful, for example, for enforcing the natural order in
  25.  * custom implementations of {@link java.util.SortedSet SortedSet} and
  26.  * {@link java.util.SortedMap SortedMap}.
  27.  * </p>
  28.  * <p>
  29.  * Note: In the 2.0 and 2.1 releases of Commons Collections, this class would
  30.  * throw a {@link ClassCastException} if either of the arguments to
  31.  * {@link #compare(Comparable, Comparable)} compare} were {@code null}, not
  32.  * {@link Comparable Comparable}, or for which
  33.  * {@link Comparable#compareTo(Object) compareTo} gave inconsistent results.
  34.  * This is no longer the case. See {@link #compare(Comparable, Comparable)} compare} for
  35.  * details.
  36.  * </p>
  37.  *
  38.  * @param <E> the type of objects compared by this comparator
  39.  * @since 2.0
  40.  * @see java.util.Collections#reverseOrder()
  41.  */
  42. public class ComparableComparator<E extends Comparable<? super E>> implements Comparator<E>, Serializable {

  43.     /** Serialization version. */
  44.     private static final long serialVersionUID = -291439688585137865L;

  45.     /** The singleton instance. */
  46.     @SuppressWarnings("rawtypes")
  47.     public static final ComparableComparator INSTANCE = new ComparableComparator();

  48.     /**
  49.      * Gets the singleton instance of a ComparableComparator.
  50.      * <p>
  51.      * Developers are encouraged to use the comparator returned from this method
  52.      * instead of constructing a new instance to reduce allocation and GC overhead
  53.      * when multiple comparable comparators may be used in the same VM.
  54.      *
  55.      * @param <E>  the element type
  56.      * @return the singleton ComparableComparator
  57.      * @since 4.0
  58.      */
  59.     public static <E extends Comparable<? super E>> ComparableComparator<E> comparableComparator() {
  60.         return INSTANCE;
  61.     }

  62.     /**
  63.      * Constructor whose use should be avoided.
  64.      * <p>
  65.      * Please use the {@link #comparableComparator()} method whenever possible.
  66.      */
  67.     public ComparableComparator() {
  68.     }

  69.     /**
  70.      * Compare the two {@link Comparable Comparable} arguments.
  71.      * This method is equivalent to:
  72.      * <pre>((Comparable)obj1).compareTo(obj2)</pre>
  73.      *
  74.      * @param obj1  the first object to compare
  75.      * @param obj2  the second object to compare
  76.      * @return negative if obj1 is less, positive if greater, zero if equal
  77.      * @throws NullPointerException if <em>obj1</em> is {@code null},
  78.      *         or when {@code ((Comparable)obj1).compareTo(obj2)} does
  79.      * @throws ClassCastException if <em>obj1</em> is not a {@code Comparable},
  80.      *         or when {@code ((Comparable)obj1).compareTo(obj2)} does
  81.      */
  82.     @Override
  83.     public int compare(final E obj1, final E obj2) {
  84.         return obj1.compareTo(obj2);
  85.     }

  86.     /**
  87.      * Returns {@code true} iff <em>that</em> Object is a {@link Comparator Comparator}
  88.      * whose ordering is known to be equivalent to mine.
  89.      * <p>
  90.      * This implementation returns {@code true} iff
  91.      * {@code <em>object</em>.{@link Object#getClass() getClass()}} equals
  92.      * {@code this.getClass()}. Subclasses may want to override this behavior to remain
  93.      * consistent with the {@link Comparator#equals(Object)} contract.
  94.      *
  95.      * @param object  the object to compare with
  96.      * @return {@code true} if equal
  97.      * @since 3.0
  98.      */
  99.     @Override
  100.     public boolean equals(final Object object) {
  101.         return this == object ||
  102.                null != object && object.getClass().equals(this.getClass());
  103.     }

  104.     /**
  105.      * Implement a hash code for this comparator that is consistent with
  106.      * {@link #equals(Object) equals}.
  107.      *
  108.      * @return a hash code for this comparator.
  109.      * @since 3.0
  110.      */
  111.     @Override
  112.     public int hashCode() {
  113.         return "ComparableComparator".hashCode();
  114.     }

  115. }