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     * Creates a comparator that compares objects based on the inverse of their
043     * natural ordering.  Using this Constructor will create a ReverseComparator
044     * that is functionally identical to the Comparator returned by
045     * java.util.Collections.<b>reverseOrder()</b>.
046     *
047     * @see java.util.Collections#reverseOrder()
048     */
049    public ReverseComparator() {
050        this(null);
051    }
052
053    /**
054     * Creates a comparator that inverts the comparison
055     * of the given comparator.  If you pass in {@code null},
056     * the ReverseComparator defaults to reversing the
057     * natural order, as per {@link java.util.Collections#reverseOrder()}.
058     *
059     * @param comparator Comparator to reverse
060     */
061    public ReverseComparator(final Comparator<? super E> comparator) {
062        this.comparator = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator;
063    }
064
065    /**
066     * Compares two objects in reverse order.
067     *
068     * @param obj1  the first object to compare
069     * @param obj2  the second object to compare
070     * @return negative if obj1 is less, positive if greater, zero if equal
071     */
072    @Override
073    public int compare(final E obj1, final E obj2) {
074        return comparator.compare(obj2, obj1);
075    }
076
077    /**
078     * Returns {@code true} iff <i>that</i> Object is
079     * a {@link Comparator} whose ordering is known to be
080     * equivalent to mine.
081     * <p>
082     * This implementation returns {@code true}
083     * iff {@code <i>object</i>.{@link Object#getClass() getClass()}}
084     * equals {@code this.getClass()}, and the underlying
085     * comparators are equal.
086     * Subclasses may want to override this behavior to remain consistent
087     * with the {@link Comparator#equals(Object) equals} contract.
088     *
089     * @param object  the object to compare to
090     * @return true if equal
091     * @since 3.0
092     */
093    @Override
094    public boolean equals(final Object object) {
095        if (this == object) {
096            return true;
097        }
098        if (null == object) {
099            return false;
100        }
101        if (object.getClass().equals(this.getClass())) {
102            final ReverseComparator<?> thatrc = (ReverseComparator<?>) object;
103            return comparator.equals(thatrc.comparator);
104        }
105        return false;
106    }
107
108    /**
109     * Implement a hash code for this comparator that is consistent with
110     * {@link #equals(Object) equals}.
111     *
112     * @return a suitable hash code
113     * @since 3.0
114     */
115    @Override
116    public int hashCode() {
117        return "ReverseComparator".hashCode() ^ comparator.hashCode();
118    }
119
120}