    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 *      http://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;
    021
    022/**
    023 * A {@link Comparator Comparator} that compares {@link Comparable Comparable}
    024 * objects.
    025 * <p>
    026 * This Comparator is useful, for example, for enforcing the natural order in
    027 * custom implementations of {@link java.util.SortedSet SortedSet} and
    028 * {@link java.util.SortedMap SortedMap}.
    029 * </p>
    030 * <p>
    031 * Note: In the 2.0 and 2.1 releases of Commons Collections, this class would
    032 * throw a {@link ClassCastException} if either of the arguments to
    033 * {@link #compare(Comparable, Comparable)} compare} were {@code null}, not
    034 * {@link Comparable Comparable}, or for which
    035 * {@link Comparable#compareTo(Object) compareTo} gave inconsistent results.
    036 * This is no longer the case. See {@link #compare(Comparable, Comparable)} compare} for
    037 * details.
    038 * </p>
    039 *
    040 * @param <E> the type of objects compared by this comparator
    041 * @since 2.0
    042 * @see java.util.Collections#reverseOrder()
    043 */
    044public class ComparableComparator<E extends Comparable<? super E>> implements Comparator<E>, Serializable {
    045
    046    /** Serialization version. */
    047    private static final long serialVersionUID = -291439688585137865L;
    048
    049    /** The singleton instance. */
    050    @SuppressWarnings("rawtypes")
    051    public static final ComparableComparator INSTANCE = new ComparableComparator();
    052
    053    /**
    054     * Gets the singleton instance of a ComparableComparator.
    055     * <p>
    056     * Developers are encouraged to use the comparator returned from this method
    057     * instead of constructing a new instance to reduce allocation and GC overhead
    058     * when multiple comparable comparators may be used in the same VM.
    059     *
    060     * @param <E>  the element type
    061     * @return the singleton ComparableComparator
    062     * @since 4.0
    063     */
    064    public static <E extends Comparable<? super E>> ComparableComparator<E> comparableComparator() {
    065        return INSTANCE;
    066    }
    067
    068    /**
    069     * Constructor whose use should be avoided.
    070     * <p>
    071     * Please use the {@link #comparableComparator()} method whenever possible.
    072     */
    073    public ComparableComparator() {
    074    }
    075
    076    /**
    077     * Compare the two {@link Comparable Comparable} arguments.
    078     * This method is equivalent to:
    079     * <pre>((Comparable)obj1).compareTo(obj2)</pre>
    080     *
    081     * @param obj1  the first object to compare
    082     * @param obj2  the second object to compare
    083     * @return negative if obj1 is less, positive if greater, zero if equal
    084     * @throws NullPointerException if <em>obj1</em> is {@code null},
    085     *         or when {@code ((Comparable)obj1).compareTo(obj2)} does
    086     * @throws ClassCastException if <em>obj1</em> is not a {@code Comparable},
    087     *         or when {@code ((Comparable)obj1).compareTo(obj2)} does
    088     */
    089    @Override
    090    public int compare(final E obj1, final E obj2) {
    091        return obj1.compareTo(obj2);
    092    }
    093
    094    /**
    095     * Returns {@code true} iff <em>that</em> Object is a {@link Comparator Comparator}
    096     * whose ordering is known to be equivalent to mine.
    097     * <p>
    098     * This implementation returns {@code true} iff
    099     * {@code <em>object</em>.{@link Object#getClass() getClass()}} equals
    100     * {@code this.getClass()}. Subclasses may want to override this behavior to remain
    101     * consistent with the {@link Comparator#equals(Object)} contract.
    102     *
    103     * @param object  the object to compare with
    104     * @return {@code true} if equal
    105     * @since 3.0
    106     */
    107    @Override
    108    public boolean equals(final Object object) {
    109        return this == object ||
    110               null != object && object.getClass().equals(this.getClass());
    111    }
    112
    113    /**
    114     * Implement a hash code for this comparator that is consistent with
    115     * {@link #equals(Object) equals}.
    116     *
    117     * @return a hash code for this comparator.
    118     * @since 3.0
    119     */
    120    @Override
    121    public int hashCode() {
    122        return "ComparableComparator".hashCode();
    123    }
    124
    125}