package org.apache.commons.collections.comparators;

/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 *    "Apache Commons" must not be used to endorse or promote products
 *    derived from this software without prior written permission. For
 *    written permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    "Apache Turbine", nor may "Apache" appear in their name, without
 *    prior written permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

import java.io.Serializable;
import java.util.Comparator;

/**
 * Reverses the order of another comparator.
 * 
 * @since 2.0
 * @author bayard@generationjava.com
 * @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
 * @version $Id: ReverseComparator.java,v 1.8 2002/06/12 03:59:17 mas Exp $
 */
public class ReverseComparator implements Comparator,Serializable {

    private Comparator comparator;

    /**
     * Creates a comparator that compares objects based on the inverse of their
     * natural ordering.  Using this Constructor will create a ReverseComparator
     * that is functionaly identical to the Comparator returned by
     * java.util.Collections.<b>reverseOrder()</b>.
     * 
     * @see java.util.Collections#reverseOrder()
     */
    public ReverseComparator() {
        this(null);
    }

    /**
     * Creates a reverse comparator that inverts the comparison
     * of the passed in comparator.  If you pass in a null,
     * the ReverseComparator defaults to reversing the
     * natural order, as per 
     * java.util.Collections.<b>reverseOrder()</b>.
     * 
     * @param comparator Comparator to reverse
     */
    public ReverseComparator(Comparator comparator) {
        if(comparator != null) {
            this.comparator = comparator;
        } else {
            this.comparator = ComparableComparator.getInstance();
        }
    }

    public int compare(Object o1, Object o2) {
        return comparator.compare(o2, o1);
    }

}

@@ -2994,33 +2994,39 @@
 v 1.
-8
+9
  200
-2/06/12 03:59:17 mas
+3/01/07 23:05:33 rwaldhoff
  Exp
@@ -4208,10 +4208,1364 @@
 %0A    %7D%0A%0A
+    /**%0A     * Implement a hash code for this comparator that is consistent with%0A     * %7B@link #equals%7D.%0A     * %0A     * @since Collections 2.2%0A     */%0A    public int hashCode() %7B%0A        return %22ReverseComparator%22.hashCode() %5E comparator.hashCode();%0A    %7D%0A%0A    /**%0A     * Returns %3Ccode%3Etrue%3C/code%3E iff %3Ci%3Ethat%3C/i%3E Object is %0A     * is a %7B@link Comparator%7D whose ordering is known to be %0A     * equivalent to mine.%0A     * %3Cp%3E%0A     * This implementation returns %3Ccode%3Etrue%3C/code%3E%0A     * iff %3Ccode%3E%3Ci%3Ethat%3C/i%3E.%7B@link Object#getClass getClass()%7D%3C/code%3E%0A     * equals %3Ccode%3Ethis.getClass()%3C/code%3E, and the underlying %0A     * comparators are equal.  Subclasses may want to override%0A     * this behavior to remain consistent with the %0A     * %7B@link Comparator.equals%7D contract.%0A     * %0A     * @since Collections 2.2%0A     */%0A    public boolean equals(Object that) %7B%0A        if(this == that) %7B%0A            return true;%0A        %7D else if(null == that) %7B%0A            return false;%0A        %7D else if(that.getClass().equals(this.getClass())) %7B%0A            ReverseComparator thatrc = (ReverseComparator)that;%0A            return comparator.equals(thatrc.comparator);%0A        %7D else %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A    // use serialVersionUID from Collections 2.0 for interoperability%0A    private static final long serialVersionUID = 2858887242028539265L;;%0A
 %7D%0A

support equals and hashCode per Comparator contract