You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/05/15 15:24:11 UTC
cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/comparators NullComparator.java FixedOrderComparator.java TransformingComparator.java ComparableComparator.java ComparatorChain.java BooleanComparator.java ReverseComparator.java
scolebourne 2004/05/15 06:24:11
Modified: collections/src/java/org/apache/commons/collections/comparators
NullComparator.java FixedOrderComparator.java
TransformingComparator.java
ComparableComparator.java ComparatorChain.java
BooleanComparator.java ReverseComparator.java
Log:
Javadoc and code tidy
Revision Changes Path
1.13 +10 -7 jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/NullComparator.java
Index: NullComparator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/NullComparator.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- NullComparator.java 18 Feb 2004 00:59:06 -0000 1.12
+++ NullComparator.java 15 May 2004 13:24:11 -0000 1.13
@@ -29,6 +29,9 @@
*/
public class NullComparator implements Comparator, Serializable {
+ /** Serialization version. */
+ private static final long serialVersionUID = -5820772575483504339L;
+
/**
* The comparator to use when comparing two non-<code>null</code> objects.
**/
@@ -40,7 +43,8 @@
**/
private boolean nullsAreHigh;
- /**
+ //-----------------------------------------------------------------------
+ /**
* Construct an instance that sorts <code>null</code> higher than any
* non-<code>null</code> object it is compared with. When comparing two
* non-<code>null</code> objects, the {@link ComparableComparator} is
@@ -111,6 +115,7 @@
}
}
+ //-----------------------------------------------------------------------
/**
* Perform a comparison between two objects. If both objects are
* <code>null</code>, a <code>0</code> value is returned. If one object
@@ -120,10 +125,8 @@
* underlying comparator specified in the constructor (or the default) is
* used to compare the non-<code>null</code> objects.
*
- * @param o1 the first object to compare
- *
- * @param o2 the object to compare it to.
- *
+ * @param o1 the first object to compare
+ * @param o2 the object to compare it to.
* @return <code>-1</code> if <code>o1</code> is "lower" than (less than,
* before, etc.) <code>o2</code>; <code>1</code> if <code>o1</code> is
* "higher" than (greater than, after, etc.) <code>o2</code>; or
@@ -136,6 +139,7 @@
return this.nonNullComparator.compare(o1, o2);
}
+ //-----------------------------------------------------------------------
/**
* Implement a hash code for this comparator that is consistent with
* {@link #equals(Object)}.
@@ -168,5 +172,4 @@
(this.nonNullComparator.equals(other.nonNullComparator)));
}
- private static final long serialVersionUID = -5820772575483504339L;
}
1.10 +11 -5 jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/FixedOrderComparator.java
Index: FixedOrderComparator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/FixedOrderComparator.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- FixedOrderComparator.java 18 Feb 2004 00:59:06 -0000 1.9
+++ FixedOrderComparator.java 15 May 2004 13:24:11 -0000 1.10
@@ -24,7 +24,7 @@
/**
* A Comparator which imposes a specific order on a specific set of Objects.
* Objects are presented to the FixedOrderComparator in a specified order and
- * subsequent calls to {@link #compare} yield that order.
+ * subsequent calls to {@link #compare(Object, Object) compare} yield that order.
* For example:
* <pre>
* String[] planets = {"Mercury", "Venus", "Earth", "Mars"};
@@ -33,8 +33,8 @@
* Arrays.sort(planets, distanceFromSun); // Back to original order
* </pre>
* <p>
- * Once {@link #compare} has been called, the FixedOrderComparator is locked and
- * attempts to modify it yield an UnsupportedOperationException.
+ * Once <code>compare</code> has been called, the FixedOrderComparator is locked
+ * and attempts to modify it yield an UnsupportedOperationException.
* <p>
* Instances of FixedOrderComparator are not synchronized. The class is not
* thread-safe at construction time, but it is thread-safe to perform
@@ -150,6 +150,9 @@
/**
* Gets the behavior for comparing unknown objects.
+ *
+ * @return the flag for unknown behaviour - UNKNOWN_AFTER,
+ * UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION
*/
public int getUnknownObjectBehavior() {
return unknownObjectBehavior;
@@ -158,6 +161,8 @@
/**
* Sets the behavior for comparing unknown objects.
*
+ * @param unknownObjectBehavior the flag for unknown behaviour -
+ * UNKNOWN_AFTER, UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION
* @throws UnsupportedOperationException if a comparison has been performed
* @throws IllegalArgumentException if the unknown flag is not valid
*/
@@ -223,7 +228,8 @@
*
* @param obj1 the first object to compare
* @param obj2 the second object to compare
- * @throws IllegalArgumentException if o1 or o2 are not known
+ * @return negative if obj1 is less, positive if greater, zero if equal
+ * @throws IllegalArgumentException if obj1 or obj2 are not known
* to this Comparator and an alternative behavior has not been set
* via {@link #setUnknownObjectBehavior(int)}.
*/
1.8 +22 -18 jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/TransformingComparator.java
Index: TransformingComparator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/TransformingComparator.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- TransformingComparator.java 18 Feb 2004 00:59:06 -0000 1.7
+++ TransformingComparator.java 15 May 2004 13:24:11 -0000 1.8
@@ -22,8 +22,7 @@
/**
* Decorates another Comparator with transformation behavior. That is, the
* return value from the transform operation will be passed to the decorated
- * {@link Comparator#compare compare} method.
- * <p />
+ * {@link Comparator#compare(Object,Object) compare} method.
*
* @since Commons Collections 2.0 (?)
* @version $Revision$ $Date$
@@ -31,41 +30,46 @@
* @see org.apache.commons.collections.Transformer
* @see org.apache.commons.collections.comparators.ComparableComparator
*/
-public class TransformingComparator implements Comparator
-{
+public class TransformingComparator implements Comparator {
+
+ /** The decorated comparator. */
protected Comparator decorated;
+ /** The transformer being used. */
protected Transformer transformer;
+ //-----------------------------------------------------------------------
/**
* Constructs an instance with the given Transformer and a
* {@link ComparableComparator ComparableComparator}.
- * @param transformer what will transform the arguments to
- * {@link #compare compare}
+ *
+ * @param transformer what will transform the arguments to <code>compare</code>
*/
- public TransformingComparator(Transformer transformer)
- {
+ public TransformingComparator(Transformer transformer) {
this(transformer, new ComparableComparator());
}
/**
- * Constructs an instance with the given Transformer and Comparator
- * @param transformer what will transform the arguments to {@link #compare compare}
- * @param decorated the decorated Comparator
+ * Constructs an instance with the given Transformer and Comparator.
+ *
+ * @param transformer what will transform the arguments to <code>compare</code>
+ * @param decorated the decorated Comparator
*/
- public TransformingComparator(Transformer transformer, Comparator decorated)
- {
+ public TransformingComparator(Transformer transformer, Comparator decorated) {
this.decorated = decorated;
this.transformer = transformer;
}
+ //-----------------------------------------------------------------------
/**
* Returns the result of comparing the values from the transform operation.
- * @return the result of comparing the values from the transform operation
+ *
+ * @param obj1 the first object to transform then compare
+ * @param obj2 the second object to transform then compare
+ * @return negative if obj1 is less, positive if greater, zero if equal
*/
- public int compare(Object o1, Object o2)
- {
- Object value1 = this.transformer.transform(o1);
- Object value2 = this.transformer.transform(o2);
+ public int compare(Object obj1, Object obj2) {
+ Object value1 = this.transformer.transform(obj1);
+ Object value2 = this.transformer.transform(obj2);
return this.decorated.compare(value1, value2);
}
1.15 +51 -30 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.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- ComparableComparator.java 18 Feb 2004 00:59:06 -0000 1.14
+++ ComparableComparator.java 15 May 2004 13:24:11 -0000 1.15
@@ -28,50 +28,73 @@
* <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}
+ * either of the arguments to {@link #compare(Object, Object) compare}
* were <code>null</code>, not {@link Comparable Comparable},
- * or for which {@link Comparable#compareTo compareTo} gave
+ * or for which {@link Comparable#compareTo(Object) compareTo} gave
* inconsistent results. This is no longer the case. See
- * {@link #compare} for details.
+ * {@link #compare(Object, Object) compare} for details.
*
* @since Commons Collections 2.0
* @version $Revision$ $Date$
*
* @author Henri Yandell
*
- * @see java.util.Collections#reverseOrder
+ * @see java.util.Collections#reverseOrder()
*/
public class ComparableComparator implements Comparator, Serializable {
+ /** Serialization version. */
+ private static final long serialVersionUID=-291439688585137865L;
+
+ /** The singleton instance. */
+ private static final ComparableComparator instance = new ComparableComparator();
+
+ //-----------------------------------------------------------------------
/**
- * Return a shared instance of a ComparableComparator. Developers are
- * encouraged to use the comparator returned from this method instead of
- * constructing a new instance to reduce allocation and GC overhead when
- * multiple comparable comparators may be used in the same VM.
- **/
+ * Gets the singleton instance of a ComparableComparator.
+ * <p>
+ * Developers are encouraged to use the comparator returned from this method
+ * instead of constructing a new instance to reduce allocation and GC overhead
+ * when multiple comparable comparators may be used in the same VM.
+ *
+ * @return the singleton ComparableComparator
+ */
public static ComparableComparator getInstance() {
return instance;
}
+ //-----------------------------------------------------------------------
+ /**
+ * Constructor whose use should be avoided.
+ * <p>
+ * Please use the {@link #getInstance()} method whenever possible.
+ */
public ComparableComparator() {
+ super();
}
+ //-----------------------------------------------------------------------
/**
* 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
+ * <pre>((Comparable)obj1).compareTo(obj2)</pre>
+ *
+ * @param obj1 the first object to compare
+ * @param obj2 the second object to compare
+ * @return negative if obj1 is less, positive if greater, zero if equal
+ * @throws NullPointerException when <i>obj1</i> is <code>null</code>,
+ * or when <code>((Comparable)obj1).compareTo(obj2)</code> does
+ * @throws ClassCastException when <i>obj1</i> is not a <code>Comparable</code>,
+ * or when <code>((Comparable)obj1).compareTo(obj2)</code> does
*/
- public int compare(Object o1, Object o2) {
- return ((Comparable)o1).compareTo(o2);
+ public int compare(Object obj1, Object obj2) {
+ return ((Comparable)obj1).compareTo(obj2);
}
+ //-----------------------------------------------------------------------
/**
* Implement a hash code for this comparator that is consistent with
- * {@link #equals}.
+ * {@link #equals(Object) equals}.
*
* @return a hash code for this comparator.
* @since Commons Collections 3.0
@@ -86,20 +109,18 @@
* 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>. Subclasses may want to override
- * this behavior to remain consistent with the {@link Comparator#equals}
- * contract.
+ * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code>
+ * equals <code>this.getClass()</code>.
+ * Subclasses may want to override this behavior to remain consistent
+ * with the {@link Comparator#equals(Object)} contract.
+ *
+ * @param object the object to compare with
+ * @return true if equal
* @since Commons Collections 3.0
*/
- public boolean equals(Object that) {
- return (this == that) ||
- ((null != that) && (that.getClass().equals(this.getClass())));
+ public boolean equals(Object object) {
+ return (this == object) ||
+ ((null != object) && (object.getClass().equals(this.getClass())));
}
-
- private static final ComparableComparator instance =
- new ComparableComparator();
-
- private static final long serialVersionUID=-291439688585137865L;
}
1.17 +26 -19 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.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- ComparatorChain.java 18 Feb 2004 00:59:06 -0000 1.16
+++ ComparatorChain.java 15 May 2004 13:24:11 -0000 1.17
@@ -55,14 +55,17 @@
*/
public class ComparatorChain implements Comparator,Serializable {
+ /** Serialization version from Collections 2.0. */
+ private static final long serialVersionUID = -721644942746081630L;
+
+ /** The list of comparators in the chain. */
protected List comparatorChain = null;
- // false (clear) = ascend; true (set) = descend
+ /** Order - false (clear) = ascend; true (set) = descend. */
protected BitSet orderingBits = null;
-
- // ComparatorChain is "locked" after the first time
- // compare(Object,Object) is called
+ /** Whether the chain has been "locked". */
protected boolean isLocked = false;
+ //-----------------------------------------------------------------------
/**
* Construct a ComparatorChain with no Comparators.
* You must add at least one Comparator before calling
@@ -131,6 +134,7 @@
orderingBits = bits;
}
+ //-----------------------------------------------------------------------
/**
* Add a Comparator to the end of the chain using the
* forward sort order
@@ -247,12 +251,13 @@
}
}
+ //-----------------------------------------------------------------------
/**
* Perform comparisons on the Objects as per
* Comparator.compare(o1,o2).
*
- * @param o1 object 1
- * @param o2 object 2
+ * @param o1 the first object to compare
+ * @param o2 the second object to compare
* @return -1, 0, or 1
* @exception UnsupportedOperationException
* if the ComparatorChain does not contain at least one
@@ -289,10 +294,12 @@
return 0;
}
+ //-----------------------------------------------------------------------
/**
* Implement a hash code for this comparator that is consistent with
- * {@link #equals}.
+ * {@link #equals(Object) equals}.
*
+ * @return a suitable hash code
* @since Commons Collections 3.0
*/
public int hashCode() {
@@ -312,21 +319,23 @@
* equivalent to mine.
* <p>
* This implementation returns <code>true</code>
- * iff <code><i>that</i>.{@link Object#getClass getClass()}</code>
+ * iff <code><i>object</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.
+ * comparators and order bits are equal.
+ * Subclasses may want to override this behavior to remain consistent
+ * with the {@link Comparator#equals(Object)} contract.
*
+ * @param object the object to compare with
+ * @return true if equal
* @since Commons Collections 3.0
*/
- public boolean equals(Object that) {
- if(this == that) {
+ public boolean equals(Object object) {
+ if(this == object) {
return true;
- } else if(null == that) {
+ } else if(null == object) {
return false;
- } else if(that.getClass().equals(this.getClass())) {
- ComparatorChain chain = (ComparatorChain)that;
+ } else if(object.getClass().equals(this.getClass())) {
+ ComparatorChain chain = (ComparatorChain)object;
return ( (null == orderingBits ? null == chain.orderingBits : orderingBits.equals(chain.orderingBits))
&& (null == comparatorChain ? null == chain.comparatorChain : comparatorChain.equals(chain.comparatorChain)) );
} else {
@@ -334,6 +343,4 @@
}
}
- // use serialVersionUID from Collections 2.0 for interoperability
- private static final long serialVersionUID = -721644942746081630L;
}
1.13 +92 -71 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.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- BooleanComparator.java 27 Apr 2004 22:57:00 -0000 1.12
+++ BooleanComparator.java 15 May 2004 13:24:11 -0000 1.13
@@ -20,10 +20,10 @@
/**
* A {@link Comparator} for {@link Boolean} objects.
- *
- * @see #getTrueFirstComparator
- * @see #getFalseFirstComparator
- * @see #getBooleanComparator
+ * <p>
+ * @see #getTrueFirstComparator()
+ * @see #getFalseFirstComparator()
+ * @see #getBooleanComparator(boolean)
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
@@ -31,7 +31,71 @@
* @author Rodney Waldhoff
*/
public final class BooleanComparator implements Comparator, Serializable {
+
+ // TODO: Serialization version
+
+ /** Constant "true first" reference. */
+ private static final BooleanComparator TRUE_FIRST = new BooleanComparator(true);
+
+ /** Constant "false first" reference. */
+ private static final BooleanComparator FALSE_FIRST = new BooleanComparator(false);
+
+ /** <code>true</code> iff <code>true</code> values sort before <code>false</code> values. */
+ private boolean trueFirst = false;
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns a BooleanComparator instance that sorts
+ * <code>true</code> values before <code>false</code> values.
+ * <p />
+ * Clients are encouraged to use the value returned from
+ * this method instead of constructing a new instance
+ * to reduce allocation and garbage collection overhead when
+ * multiple BooleanComparators may be used in the same
+ * virtual machine.
+ *
+ * @return the true first singleton BooleanComparator
+ */
+ public static BooleanComparator getTrueFirstComparator() {
+ return TRUE_FIRST;
+ }
+
+ /**
+ * Returns a BooleanComparator instance that sorts
+ * <code>false</code> values before <code>true</code> values.
+ * <p />
+ * Clients are encouraged to use the value returned from
+ * this method instead of constructing a new instance
+ * to reduce allocation and garbage collection overhead when
+ * multiple BooleanComparators may be used in the same
+ * virtual machine.
+ *
+ * @return the false first singleton BooleanComparator
+ */
+ public static BooleanComparator getFalseFirstComparator() {
+ return FALSE_FIRST;
+ }
+
+ /**
+ * Returns a BooleanComparator instance that sorts
+ * <code><i>trueFirst</i></code> values before
+ * <code>!<i>trueFirst</i></code> values.
+ * <p />
+ * Clients are encouraged to use the value returned from
+ * this method instead of constructing a new instance
+ * to reduce allocation and garbage collection overhead when
+ * multiple BooleanComparators may be used in the same
+ * virtual machine.
+ *
+ * @param trueFirst when <code>true</code>, sort
+ * <code>true</code> <code>Boolean</code>s before <code>false</code>
+ * @return a singleton BooleanComparator instance
+ */
+ public static BooleanComparator getBooleanComparator(boolean trueFirst) {
+ return trueFirst ? TRUE_FIRST : FALSE_FIRST;
+ }
+ //-----------------------------------------------------------------------
/**
* Creates a <code>BooleanComparator</code> that sorts
* <code>false</code> values before <code>true</code> values.
@@ -62,20 +126,26 @@
/**
* Compares two arbitrary Objects.
* When both arguments are <code>Boolean</code>, this method is equivalent to
- * {@link #compare(Boolean,Boolean) compare((Boolean)<i>o1</i>,(Boolean)<i>o2</i>)}.
+ * {@link #compare(Boolean,Boolean) compare((Boolean)<i>obj1</i>,(Boolean)<i>obj2</i>)}.
* When either argument is not a <code>Boolean</code>, this methods throws
* a {@link ClassCastException}.
*
+ * @param obj1 the first object to compare
+ * @param obj2 the second object to compare
+ * @return negative if obj1 is less, positive if greater, zero if equal
* @throws ClassCastException when either argument is not <code>Boolean</code>
*/
- public int compare(Object o1, Object o2) {
- return compare((Boolean)o1,(Boolean)o2);
+ public int compare(Object obj1, Object obj2) {
+ return compare((Boolean)obj1, (Boolean)obj2);
}
/**
* Compares two non-<code>null</code> <code>Boolean</code> objects
- * according to the value of {@link #sortsTrueFirst}.
+ * according to the value of {@link #trueFirst}.
*
+ * @param b1 the first boolean to compare
+ * @param b2 the second boolean to compare
+ * @return negative if obj1 is less, positive if greater, zero if equal
* @throws NullPointerException when either argument <code>null</code>
*/
public int compare(Boolean b1, Boolean b2) {
@@ -85,9 +155,10 @@
return (v1 ^ v2) ? ( (v1 ^ trueFirst) ? 1 : -1 ) : 0;
}
+ //-----------------------------------------------------------------------
/**
* Implement a hash code for this comparator that is consistent with
- * {@link #equals equals}.
+ * {@link #equals(Object) equals}.
*
* @return a hash code for this comparator.
*/
@@ -103,14 +174,18 @@
* <p>
* This implementation returns <code>true</code>
* iff <code><i>that</i></code> is a {@link BooleanComparator}
- * whose {@link #sortsTrueFirst} value is equal to mine.
+ * whose {@link #trueFirst} value is equal to mine.
+ *
+ * @param object the object to compare to
+ * @return true if equal
*/
- public boolean equals(Object that) {
- return (this == that) ||
- ((that instanceof BooleanComparator) &&
- (this.trueFirst == ((BooleanComparator)that).trueFirst));
+ public boolean equals(Object object) {
+ return (this == object) ||
+ ((object instanceof BooleanComparator) &&
+ (this.trueFirst == ((BooleanComparator)object).trueFirst));
}
+ //-----------------------------------------------------------------------
/**
* Returns <code>true</code> iff
* I sort <code>true</code> values before
@@ -118,65 +193,11 @@
* returns <code>true</code> iff
* {@link #compare(Boolean,Boolean) compare(Boolean.FALSE,Boolean.TRUE)}
* returns a positive value.
+ *
+ * @return the trueFirst flag
*/
public boolean sortsTrueFirst() {
return trueFirst;
}
-
- //-----------------------------------------------------------------------
- /**
- * Returns a BooleanComparator instance that sorts
- * <code>true</code> values before <code>false</code> values.
- * <p />
- * Clients are encouraged to use the value returned from
- * this method instead of constructing a new instance
- * to reduce allocation and garbage collection overhead when
- * multiple BooleanComparators may be used in the same
- * virtual machine.
- */
- public static BooleanComparator getTrueFirstComparator() {
- return TRUE_FIRST;
- }
-
- /**
- * Returns a BooleanComparator instance that sorts
- * <code>false</code> values before <code>true</code> values.
- * <p />
- * Clients are encouraged to use the value returned from
- * this method instead of constructing a new instance
- * to reduce allocation and garbage collection overhead when
- * multiple BooleanComparators may be used in the same
- * virtual machine.
- */
- public static BooleanComparator getFalseFirstComparator() {
- return FALSE_FIRST;
- }
-
- /**
- * Returns a BooleanComparator instance that sorts
- * <code><i>trueFirst</i></code> values before
- * <code>!<i>trueFirst</i></code> values.
- * <p />
- * Clients are encouraged to use the value returned from
- * this method instead of constructing a new instance
- * to reduce allocation and garbage collection overhead when
- * multiple BooleanComparators may be used in the same
- * virtual machine.
- *
- * @param trueFirst when <code>true</code>, sort
- * <code>true</code> <code>Boolean</code>s before <code>false</code>
- * @return a cached BooleanComparator instance
- */
- public static BooleanComparator getBooleanComparator(boolean trueFirst) {
- return trueFirst ? TRUE_FIRST : FALSE_FIRST;
- }
-
- /** <code>true</code> iff <code>true</code> values sort before <code>false</code> values. */
- private boolean trueFirst = false;
- /** My static "true first" reference. */
- private static final BooleanComparator TRUE_FIRST = new BooleanComparator(true);
-
- /** My static "false first" reference. */
- private static final BooleanComparator FALSE_FIRST = new BooleanComparator(false);
}
1.19 +37 -23 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.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- ReverseComparator.java 18 Feb 2004 00:59:06 -0000 1.18
+++ ReverseComparator.java 15 May 2004 13:24:11 -0000 1.19
@@ -19,9 +19,8 @@
import java.util.Comparator;
/**
- * Reverses the order of another comparator by
- * reversing the arguments to its {@link #compare compare}
- * method.
+ * Reverses the order of another comparator by reversing the arguments
+ * to its {@link #compare(Object, Object) compare} method.
*
* @since Commons Collections 2.0
* @version $Revision$ $Date$
@@ -29,17 +28,24 @@
* @author Henri Yandell
* @author Michael A. Smith
*
- * @see java.util.Collections#reverseOrder
+ * @see java.util.Collections#reverseOrder()
*/
public class ReverseComparator implements Comparator,Serializable {
+ /** Serialization version from Collections 2.0. */
+ private static final long serialVersionUID = 2858887242028539265L;
+
+ /** The comparator being decorated. */
+ 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 functionally 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);
@@ -50,7 +56,7 @@
* of the given comparator. If you pass in <code>null</code>,
* the ReverseComparator defaults to reversing the
* natural order, as per
- * {@link java.util.Collections#reverseOrder}</b>.
+ * {@link java.util.Collections#reverseOrder()}</b>.
*
* @param comparator Comparator to reverse
*/
@@ -62,14 +68,24 @@
}
}
- public int compare(Object o1, Object o2) {
- return comparator.compare(o2, o1);
+ //-----------------------------------------------------------------------
+ /**
+ * Compares two objects in reverse order.
+ *
+ * @param obj1 the first object to compare
+ * @param obj2 the second object to compare
+ * @return negative if obj1 is less, positive if greater, zero if equal
+ */
+ public int compare(Object obj1, Object obj2) {
+ return comparator.compare(obj2, obj1);
}
+ //-----------------------------------------------------------------------
/**
* Implement a hash code for this comparator that is consistent with
- * {@link #equals}.
+ * {@link #equals(Object) equals}.
*
+ * @return a suitable hash code
* @since Commons Collections 3.0
*/
public int hashCode() {
@@ -82,29 +98,27 @@
* equivalent to mine.
* <p>
* This implementation returns <code>true</code>
- * iff <code><i>that</i>.{@link Object#getClass getClass()}</code>
+ * iff <code><i>object</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.
+ * comparators are equal.
+ * Subclasses may want to override this behavior to remain consistent
+ * with the {@link Comparator#equals(Object) equals} contract.
*
+ * @param object the object to compare to
+ * @return true if equal
* @since Commons Collections 3.0
*/
- public boolean equals(Object that) {
- if(this == that) {
+ public boolean equals(Object object) {
+ if(this == object) {
return true;
- } else if(null == that) {
+ } else if(null == object) {
return false;
- } else if(that.getClass().equals(this.getClass())) {
- ReverseComparator thatrc = (ReverseComparator)that;
+ } else if(object.getClass().equals(this.getClass())) {
+ ReverseComparator thatrc = (ReverseComparator)object;
return comparator.equals(thatrc.comparator);
} else {
return false;
}
}
- // use serialVersionUID from Collections 2.0 for interoperability
- private static final long serialVersionUID = 2858887242028539265L;
-
- private Comparator comparator;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org