Add support for calling superclass compareTo

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137177 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2002-12-08 21:43:34 +00:00
parent 19248809e7
commit 5003e9c501
2 changed files with 34 additions and 3 deletions

View File

@ -83,9 +83,10 @@ import org.apache.commons.lang.enum.Enum;
* public int comapareTo(Object o) {
* MyClass rhs = (MyClass) o;
* return new CompareToBuilder()
* .appendSuper(super.compareTo(o)
* .append(field1, rhs.field1)
* .append(field2, rhs.field2)
* .appendb(field3, rhs.field3)
* .append(field3, rhs.field3)
* .toComparison();
* }
* </pre>
@ -106,7 +107,7 @@ import org.apache.commons.lang.enum.Enum;
*
* @author <a href="mailto:steve.downey@netfolio.com">Steve Downey</a>
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: CompareToBuilder.java,v 1.6 2002/12/08 21:38:19 scolebourne Exp $
* @version $Id: CompareToBuilder.java,v 1.7 2002/12/08 21:43:34 scolebourne Exp $
*/
public class CompareToBuilder {
@ -214,6 +215,22 @@ public class CompareToBuilder {
//-------------------------------------------------------------------------
/**
* <p>Adds the result of super.hashCode() to this builder.</p>
*
* @param superHashCode the result of calling <code>super.equals()</code>
* @return CompareToBuilder - used to chain calls.
*/
public CompareToBuilder appendSuper(int superHashCode) {
if (comparison != 0) {
return this;
}
comparison = superHashCode;
return this;
}
//-------------------------------------------------------------------------
/**
* <p>Comparison of two Object references.</p>
* <ol>

View File

@ -64,7 +64,7 @@ import junit.textui.TestRunner;
*
* @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: CompareToBuilderTest.java,v 1.2 2002/12/08 21:37:25 scolebourne Exp $
* @version $Id: CompareToBuilderTest.java,v 1.3 2002/12/08 21:43:34 scolebourne Exp $
*/
public class CompareToBuilderTest extends TestCase {
@ -149,6 +149,20 @@ public class CompareToBuilderTest extends TestCase {
} catch (ClassCastException ex) {}
}
public void testAppendSuper() {
TestObject o1 = new TestObject(4);
TestObject o2 = new TestObject(5);
assertTrue(new CompareToBuilder().appendSuper(0).append(o1, o1).toComparison() == 0);
assertTrue(new CompareToBuilder().appendSuper(0).append(o1, o2).toComparison() < 0);
assertTrue(new CompareToBuilder().appendSuper(0).append(o2, o1).toComparison() > 0);
assertTrue(new CompareToBuilder().appendSuper(-1).append(o1, o1).toComparison() < 0);
assertTrue(new CompareToBuilder().appendSuper(-1).append(o1, o2).toComparison() < 0);
assertTrue(new CompareToBuilder().appendSuper(1).append(o1, o1).toComparison() > 0);
assertTrue(new CompareToBuilder().appendSuper(1).append(o1, o2).toComparison() > 0);
}
public void testObject() {
TestObject o1 = new TestObject(4);
TestObject o2 = new TestObject(4);