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