You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2003/01/13 23:34:57 UTC
cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/comparators BooleanComparator.java ComparableComparator.java ReverseComparator.java
rwaldhoff 2003/01/13 14:34:57
Modified: collections/src/java/org/apache/commons/collections/comparators
BooleanComparator.java ComparableComparator.java
ReverseComparator.java
Log:
* reduce ComparableComparator.compare to "return ((Comparable)o1).compareTo(o2)"
* some javadoc fixes
Revision Changes Path
1.4 +3 -3 jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/BooleanComparator.java
Index: BooleanComparator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/BooleanComparator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- BooleanComparator.java 11 Jan 2003 01:07:13 -0000 1.3
+++ BooleanComparator.java 13 Jan 2003 22:34:57 -0000 1.4
@@ -157,7 +157,7 @@
* I sort <code>true</code> values before
* <code>false</code> values. In other words,
* returns <code>true</code> iff
- * {@link #compare(Boolean,Boolean) compare(Boolean.TRUE,Boolean.FALSE)}
+ * {@link #compare(Boolean,Boolean) compare(Boolean.FALSE,Boolean.TRUE)}
* returns a positive value.
*/
public boolean sortsTrueFirst() {
1.9 +40 -65 jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ComparableComparator.java
Index: ComparableComparator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ComparableComparator.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ComparableComparator.java 10 Jan 2003 20:21:23 -0000 1.8
+++ ComparableComparator.java 13 Jan 2003 22:34:57 -0000 1.9
@@ -1,6 +1,6 @@
-package org.apache.commons.collections.comparators;
-
-/* ====================================================================
+/*
+ * $Header$
+ * ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
@@ -55,28 +55,35 @@
*
*/
+package org.apache.commons.collections.comparators;
+
import java.io.Serializable;
import java.util.Comparator;
/**
- * A Comparator that compares Comparable objects.
- * Throws ClassCastExceptions if the objects are not
- * Comparable, or if either is null.
- *
- * Throws ClassCastException if the compareTo of both
- * objects do not provide an inverse result of each other
- * as per the Comparable javadoc. This Comparator is useful, for example,
+ * A {@link Comparator Comparator} that compares
+ * {@link Comparable Comparable} objects.
+ * <p />
+ * This Comparator is useful, for example,
* for enforcing the natural order in custom implementations
* of SortedSet and SortedMap.
+ * <p />
+ * Note: In the 2.0 and 2.1 releases of Commons Collections,
+ * this class would throw a {@link ClassCastException} if
+ * either of the arguments to {@link #compare compare}
+ * were <code>null</code>, not {@link Comparable Comparable},
+ * or for which {@link Comparable#compareTo compareTo} gave
+ * inconsistent results. This is no longer the case. See
+ * {@link #compare} for details.
*
* @since Commons Collections 2.0
- * @author bayard@generationjava.com
* @version $Revision$ $Date$
+ *
+ * @author bayard@generationjava.com
+ *
+ * @see java.util.Collections#reverseOrder
*/
-public class ComparableComparator implements Comparator,Serializable {
-
- private static final ComparableComparator instance =
- new ComparableComparator();
+public class ComparableComparator implements Comparator, Serializable {
/**
* Return a shared instance of a ComparableComparator. Developers are
@@ -88,58 +95,20 @@
return instance;
}
- private static final long serialVersionUID=-291439688585137865L;
-
public ComparableComparator() {
}
+ /**
+ * Compare the two {@link Comparable Comparable} arguments.
+ * This method is equivalent to:
+ * <pre>(({@link Comparable Comparable})o1).{@link Comparable#compareTo compareTo}(o2)</pre>
+ * @throws NullPointerException when <i>o1</i> is <code>null</code>,
+ * or when <code>((Comparable)o1).compareTo(o2)</code> does
+ * @throws ClassCastException when <i>o1</i> is not a {@link Comparable Comparable},
+ * or when <code>((Comparable)o1).compareTo(o2)</code> does
+ */
public int compare(Object o1, Object o2) {
- if( (o1 == null) || (o2 == null) ) {
- throw new ClassCastException(
- "There were nulls in the arguments for this method: "+
- "compare("+o1 + ", " + o2 + ")"
- );
- }
-
- if(o1 instanceof Comparable) {
- if(o2 instanceof Comparable) {
- int result1 = ((Comparable)o1).compareTo(o2);
- int result2 = ((Comparable)o2).compareTo(o1);
-
- // enforce comparable contract
- if(result1 == 0 && result2 == 0) {
- return 0;
- } else
- if(result1 < 0 && result2 > 0) {
- return result1;
- } else
- if(result1 > 0 && result2 < 0) {
- return result1;
- } else {
- // results inconsistent
- throw new ClassCastException("o1 not comparable to o2");
- }
- } else {
- // o2 wasn't comparable
- throw new ClassCastException(
- "The first argument of this method was not a Comparable: " +
- o2.getClass().getName()
- );
- }
- } else
- if(o2 instanceof Comparable) {
- // o1 wasn't comparable
- throw new ClassCastException(
- "The second argument of this method was not a Comparable: " +
- o1.getClass().getName()
- );
- } else {
- // neither were comparable
- throw new ClassCastException(
- "Both arguments of this method were not Comparables: " +
- o1.getClass().getName() + " and " + o2.getClass().getName()
- );
- }
+ return ((Comparable)o1).compareTo(o2);
}
/**
@@ -155,8 +124,8 @@
/**
* Returns <code>true</code> iff <i>that</i> Object is
- * is a {@link Comparator} whose ordering is known to be
- * equivalent to mine.
+ * is a {@link Comparator 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>
@@ -169,4 +138,10 @@
return (this == that) ||
((null != that) && (that.getClass().equals(this.getClass())));
}
+
+ private static final ComparableComparator instance =
+ new ComparableComparator();
+
+ private static final long serialVersionUID=-291439688585137865L;
+
}
1.12 +12 -10 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.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- ReverseComparator.java 10 Jan 2003 20:21:25 -0000 1.11
+++ ReverseComparator.java 13 Jan 2003 22:34:57 -0000 1.12
@@ -61,7 +61,7 @@
/**
* Reverses the order of another comparator by
- * reversing the arguments to its {@link #compare}
+ * reversing the arguments to its {@link #compare compare}
* method.
*
* @since Commons Collections 2.0
@@ -69,29 +69,29 @@
*
* @author bayard@generationjava.com
* @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
+ *
+ * @see java.util.Collections#reverseOrder
*/
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()
+ * @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,
+ * Creates a comparator that inverts the comparison
+ * of the given comparator. If you pass in <code>null</code>,
* the ReverseComparator defaults to reversing the
* natural order, as per
- * java.util.Collections.<b>reverseOrder()</b>.
+ * {@link java.util.Collections#reverseOrder}</b>.
*
* @param comparator Comparator to reverse
*/
@@ -145,5 +145,7 @@
}
// use serialVersionUID from Collections 2.0 for interoperability
- private static final long serialVersionUID = 2858887242028539265L;;
+ private static final long serialVersionUID = 2858887242028539265L;
+
+ private Comparator comparator;
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>