rwaldhoff 2003/01/07 15:26:48
Modified: collections/src/java/org/apache/commons/collections/comparators
ComparatorChain.java ReverseComparator.java
Log:
override equals and hashCode to match Comparator contract
Revision Changes Path
1.8 +52 -4
jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ComparatorChain.java
Index: ComparatorChain.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ComparatorChain.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ComparatorChain.java 12 Jun 2002 03:59:17 -0000 1.7
+++ ComparatorChain.java 7 Jan 2003 23:26:47 -0000 1.8
@@ -7,7 +7,7 @@
*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
+ * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -96,6 +96,7 @@
*
* @since 2.0
* @author Morgan Delagrange
+ * @version $Revision$ $Date$
*/
public class ComparatorChain implements Comparator,Serializable {
@@ -329,4 +330,51 @@
return 0;
}
+ /**
+ * Implement a hash code for this comparator that is consistent with
+ * {@link #equals}.
+ *
+ * @since Collections 2.2
+ */
+ public int hashCode() {
+ int hash = 0;
+ if(null != comparatorChain) {
+ hash ^= comparatorChain.hashCode();
+ }
+ if(null != orderingBits) {
+ hash ^= orderingBits.hashCode();
+ }
+ return hash;
+ }
+
+ /**
+ * 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 and order bits 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())) {
+ ComparatorChain chain = (ComparatorChain)that;
+ return ( (null == orderingBits ? null == chain.orderingBits :
orderingBits.equals(chain.orderingBits))
+ && (null == comparatorChain ? null == chain.comparatorChain :
comparatorChain.equals(chain.comparatorChain)) );
+ } else {
+ return false;
+ }
+ }
+
+ // use serialVersionUID from Collections 2.0 for interoperability
+ private static final long serialVersionUID = -721644942746081630L;
}
1.10 +2 -2
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.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- ReverseComparator.java 7 Jan 2003 23:05:33 -0000 1.9
+++ ReverseComparator.java 7 Jan 2003 23:26:47 -0000 1.10
@@ -3,7 +3,7 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>