rwaldhoff 2003/01/07 15:05:33
Modified: collections/src/java/org/apache/commons/collections/comparators
ReverseComparator.java
Log:
support equals and hashCode per Comparator contract
Revision Changes Path
1.9 +40 -1
jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ReverseComparator.java
Index: ReverseComparator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ReverseComparator.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ReverseComparator.java 12 Jun 2002 03:59:17 -0000 1.8
+++ ReverseComparator.java 7 Jan 2003 23:05:33 -0000 1.9
@@ -102,4 +102,43 @@
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 <code>true</code> iff <i>that</i> Object is
+ * is a {@link Comparator} whose ordering is known to be
+ * equivalent to mine.
+ * <p>
+ * This implementation returns <code>true</code>
+ * iff <code><i>that</i>.{@link Object#getClass getClass()}</code>
+ * equals <code>this.getClass()</code>, 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;;
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>