    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.collections4.comparators;
    018
    019import java.io.Serializable;
    020import java.util.Comparator;
    021import java.util.SortedMap;
    022import java.util.SortedSet;
    023
    024/**
    025 * A {@link Comparator Comparator} that compares {@link Comparable Comparable}
    026 * objects.
    027 * <p>
    028 * This Comparator is useful, for example, for enforcing the natural order in
    029 * custom implementations of {@link SortedSet SortedSet} and
    030 * {@link SortedMap SortedMap}.
    031 * </p>
    032 * <p>
    033 * Note: In the 2.0 and 2.1 releases of Commons Collections, this class would
    034 * throw a {@link ClassCastException} if either of the arguments to
    035 * {@link #compare(Comparable, Comparable)} compare} were {@code null}, not
    036 * {@link Comparable Comparable}, or for which
    037 * {@link Comparable#compareTo(Object) compareTo} gave inconsistent results.
    038 * This is no longer the case. See {@link #compare(Comparable, Comparable)} compare} for
    039 * details.
    040 * </p>
    041 *
    042 * @param <E> The type of objects compared by this comparator
    043 * @since 2.0
    044 * @see java.util.Collections#reverseOrder()
    045 */
    046public class ComparableComparator<E extends Comparable<? super E>> implements Comparator<E>, Serializable {
    047
    048    /** Serialization version. */
    049    private static final long serialVersionUID = -291439688585137865L;
    050
    051    /** The singleton instance. */
    052    @SuppressWarnings("rawtypes")
    053    public static final ComparableComparator INSTANCE = new ComparableComparator();
    054
    055    /**
    056     * Gets the singleton instance of a ComparableComparator.
    057     * <p>
    058     * Developers are encouraged to use the comparator returned from this method
    059     * instead of constructing a new instance to reduce allocation and GC overhead
    060     * when multiple comparable comparators may be used in the same VM.
    061     *
    062     * @param <E>  the element type
    063     * @return The singleton ComparableComparator
    064     * @since 4.0
    065     */
    066    public static <E extends Comparable<? super E>> ComparableComparator<E> comparableComparator() {
    067        return INSTANCE;
    068    }
    069
    070    /**
    071     * Constructor whose use should be avoided.
    072     * <p>
    073     * Please use the {@link #comparableComparator()} method whenever possible.
    074     */
    075    public ComparableComparator() {
    076    }
    077
    078    /**
    079     * Compare the two {@link Comparable Comparable} arguments.
    080     * This method is equivalent to:
    081     * <pre>((Comparable)obj1).compareTo(obj2)</pre>
    082     *
    083     * @param obj1  The first object to compare
    084     * @param obj2  The second object to compare
    085     * @return negative if obj1 is less, positive if greater, zero if equal
    086     * @throws NullPointerException if <em>obj1</em> is {@code null},
    087     *         or when {@code ((Comparable)obj1).compareTo(obj2)} does
    088     * @throws ClassCastException if <em>obj1</em> is not a {@code Comparable},
    089     *         or when {@code ((Comparable)obj1).compareTo(obj2)} does
    090     */
    091    @Override
    092    public int compare(final E obj1, final E obj2) {
    093        return obj1.compareTo(obj2);
    094    }
    095
    096    /**
    097     * Returns {@code true} iff <em>that</em> Object is a {@link Comparator Comparator}
    098     * whose ordering is known to be equivalent to mine.
    099     * <p>
    100     * This implementation returns {@code true} iff
    101     * {@code <em>object</em>.{@link Object#getClass() getClass()}} equals
    102     * {@code this.getClass()}. Subclasses may want to override this behavior to remain
    103     * consistent with the {@link Comparator#equals(Object)} contract.
    104     *
    105     * @param object  The object to compare with
    106     * @return {@code true} if equal
    107     * @since 3.0
    108     */
    109    @Override
    110    public boolean equals(final Object object) {
    111        return this == object ||
    112               object != null && object.getClass().equals(this.getClass());
    113    }
    114
    115    /**
    116     * Implement a hash code for this comparator that is consistent with
    117     * {@link #equals(Object) equals}.
    118     *
    119     * @return A hash code for this comparator.
    120     * @since 3.0
    121     */
    122    @Override
    123    public int hashCode() {
    124        return "ComparableComparator".hashCode();
    125    }
    126
    127}