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