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
022import org.apache.commons.collections4.ComparatorUtils;
023
024/**
025 * Reverses the order of another comparator by reversing the arguments
026 * to its {@link #compare(Object, Object) compare} method.
027 *
028 * @param <E> the type of objects compared by this comparator
029 *
030 * @since 2.0
031 * @see java.util.Collections#reverseOrder()
032 */
033public class ReverseComparator<E> implements Comparator<E>, Serializable {
034
035    /** Serialization version from Collections 2.0. */
036    private static final long serialVersionUID = 2858887242028539265L;
037
038    /** The comparator being decorated. */
039    private final Comparator<? super E> comparator;
040
041    //-----------------------------------------------------------------------
042    /**
043     * Creates a comparator that compares objects based on the inverse of their
044     * natural ordering.  Using this Constructor will create a ReverseComparator
045     * that is functionally identical to the Comparator returned by
046     * java.util.Collections.<b>reverseOrder()</b>.
047     *
048     * @see java.util.Collections#reverseOrder()
049     */
050    public ReverseComparator() {
051        this(null);
052    }
053
054    /**
055     * Creates a comparator that inverts the comparison
056     * of the given comparator.  If you pass in <code>null</code>,
057     * the ReverseComparator defaults to reversing the
058     * natural order, as per {@link java.util.Collections#reverseOrder()}.
059     *
060     * @param comparator Comparator to reverse
061     */
062    @SuppressWarnings("unchecked")
063    public ReverseComparator(final Comparator<? super E> comparator) {
064        this.comparator = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator;
065    }
066
067    //-----------------------------------------------------------------------
068    /**
069     * Compares two objects in reverse order.
070     *
071     * @param obj1  the first object to compare
072     * @param obj2  the second object to compare
073     * @return negative if obj1 is less, positive if greater, zero if equal
074     */
075    @Override
076    public int compare(final E obj1, final E obj2) {
077        return comparator.compare(obj2, obj1);
078    }
079
080    //-----------------------------------------------------------------------
081    /**
082     * Implement a hash code for this comparator that is consistent with
083     * {@link #equals(Object) equals}.
084     *
085     * @return a suitable hash code
086     * @since 3.0
087     */
088    @Override
089    public int hashCode() {
090        return "ReverseComparator".hashCode() ^ comparator.hashCode();
091    }
092
093    /**
094     * Returns <code>true</code> iff <i>that</i> Object is
095     * is a {@link Comparator} whose ordering is known to be
096     * equivalent to mine.
097     * <p>
098     * This implementation returns <code>true</code>
099     * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code>
100     * equals <code>this.getClass()</code>, and the underlying
101     * comparators are equal.
102     * Subclasses may want to override this behavior to remain consistent
103     * with the {@link Comparator#equals(Object) equals} contract.
104     *
105     * @param object  the object to compare to
106     * @return true if equal
107     * @since 3.0
108     */
109    @Override
110    public boolean equals(final Object object) {
111        if (this == object) {
112            return true;
113        }
114        if (null == object) {
115            return false;
116        }
117        if (object.getClass().equals(this.getClass())) {
118            final ReverseComparator<?> thatrc = (ReverseComparator<?>) object;
119            return comparator.equals(thatrc.comparator);
120        }
121        return false;
122    }
123
124}