From 3264047c67ea87caeb2186ba37ab00603f69b8e3 Mon Sep 17 00:00:00 2001 From: Rodney Waldhoff Date: Tue, 7 Jan 2003 23:05:33 +0000 Subject: [PATCH] support equals and hashCode per Comparator contract git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130919 13f79535-47bb-0310-9956-ffa450edef68 --- .../comparators/ReverseComparator.java | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/commons/collections/comparators/ReverseComparator.java b/src/java/org/apache/commons/collections/comparators/ReverseComparator.java index a6c834fa2..9c4249541 100644 --- a/src/java/org/apache/commons/collections/comparators/ReverseComparator.java +++ b/src/java/org/apache/commons/collections/comparators/ReverseComparator.java @@ -63,7 +63,7 @@ import java.util.Comparator; * @since 2.0 * @author bayard@generationjava.com * @author Michael A. Smith - * @version $Id: ReverseComparator.java,v 1.8 2002/06/12 03:59:17 mas Exp $ + * @version $Id: ReverseComparator.java,v 1.9 2003/01/07 23:05:33 rwaldhoff Exp $ */ public class ReverseComparator implements Comparator,Serializable { @@ -102,4 +102,43 @@ public class ReverseComparator implements Comparator,Serializable { return comparator.compare(o2, o1); } + /** + * Implement a hash code for this comparator that is consistent with + * {@link #equals}. + * + * @since Collections 2.2 + */ + public int hashCode() { + return "ReverseComparator".hashCode() ^ comparator.hashCode(); + } + + /** + * Returns true iff that Object is + * is a {@link Comparator} whose ordering is known to be + * equivalent to mine. + *

+ * This implementation returns true + * iff that.{@link Object#getClass getClass()} + * equals this.getClass(), and the underlying + * comparators are equal. Subclasses may want to override + * this behavior to remain consistent with the + * {@link Comparator.equals} contract. + * + * @since Collections 2.2 + */ + public boolean equals(Object that) { + if(this == that) { + return true; + } else if(null == that) { + return false; + } else if(that.getClass().equals(this.getClass())) { + ReverseComparator thatrc = (ReverseComparator)that; + return comparator.equals(thatrc.comparator); + } else { + return false; + } + } + + // use serialVersionUID from Collections 2.0 for interoperability + private static final long serialVersionUID = 2858887242028539265L;; }