Javadoc and code tidy
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131721 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2bf26f1d36
commit
e55c203d54
|
@ -20,18 +20,82 @@ import java.util.Comparator;
|
|||
|
||||
/**
|
||||
* 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: 1.12 $ $Date: 2004/04/27 22:57:00 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/05/15 13:24:11 $
|
||||
*
|
||||
* @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 @@ public final class BooleanComparator implements Comparator, Serializable {
|
|||
/**
|
||||
* 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 @@ public final class BooleanComparator implements Comparator, Serializable {
|
|||
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 @@ public final class BooleanComparator implements Comparator, Serializable {
|
|||
* <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 @@ public final class BooleanComparator implements Comparator, Serializable {
|
|||
* 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);
|
||||
}
|
||||
|
|
|
@ -28,50 +28,73 @@ import java.util.Comparator;
|
|||
* <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: 1.14 $ $Date: 2004/02/18 00:59:06 $
|
||||
* @version $Revision: 1.15 $ $Date: 2004/05/15 13:24:11 $
|
||||
*
|
||||
* @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 @@ public class ComparableComparator implements Comparator, Serializable {
|
|||
* 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;
|
||||
|
||||
}
|
||||
|
|
|
@ -51,18 +51,21 @@ import java.util.List;
|
|||
*
|
||||
* @since Commons Collections 2.0
|
||||
* @author Morgan Delagrange
|
||||
* @version $Revision: 1.16 $ $Date: 2004/02/18 00:59:06 $
|
||||
* @version $Revision: 1.17 $ $Date: 2004/05/15 13:24:11 $
|
||||
*/
|
||||
public class ComparatorChain implements Comparator,Serializable {
|
||||
|
||||
protected List comparatorChain = null;
|
||||
// false (clear) = ascend; true (set) = descend
|
||||
protected BitSet orderingBits = null;
|
||||
/** Serialization version from Collections 2.0. */
|
||||
private static final long serialVersionUID = -721644942746081630L;
|
||||
|
||||
// ComparatorChain is "locked" after the first time
|
||||
// compare(Object,Object) is called
|
||||
/** The list of comparators in the chain. */
|
||||
protected List comparatorChain = null;
|
||||
/** Order - false (clear) = ascend; true (set) = descend. */
|
||||
protected BitSet orderingBits = null;
|
||||
/** 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 @@ public class ComparatorChain implements Comparator,Serializable {
|
|||
orderingBits = bits;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Add a Comparator to the end of the chain using the
|
||||
* forward sort order
|
||||
|
@ -247,12 +251,13 @@ public class ComparatorChain implements Comparator,Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* 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 @@ public class ComparatorChain implements Comparator,Serializable {
|
|||
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 @@ public class ComparatorChain implements Comparator,Serializable {
|
|||
* 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 @@ public class ComparatorChain implements Comparator,Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
// use serialVersionUID from Collections 2.0 for interoperability
|
||||
private static final long serialVersionUID = -721644942746081630L;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ import java.util.Map;
|
|||
/**
|
||||
* 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,15 +33,15 @@ import java.util.Map;
|
|||
* 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
|
||||
* multiple comparisons after all the setup operations are complete.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.9 $ $Date: 2004/02/18 00:59:06 $
|
||||
* @version $Revision: 1.10 $ $Date: 2004/05/15 13:24:11 $
|
||||
*
|
||||
* @author David Leppik
|
||||
* @author Stephen Colebourne
|
||||
|
@ -150,6 +150,9 @@ public class FixedOrderComparator implements Comparator {
|
|||
|
||||
/**
|
||||
* 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 @@ public class FixedOrderComparator implements Comparator {
|
|||
/**
|
||||
* 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 @@ public class FixedOrderComparator implements Comparator {
|
|||
*
|
||||
* @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)}.
|
||||
*/
|
||||
|
|
|
@ -23,12 +23,15 @@ import java.util.Comparator;
|
|||
* other objects.
|
||||
*
|
||||
* @since Commons Collections 2.0
|
||||
* @version $Revision: 1.12 $ $Date: 2004/02/18 00:59:06 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/05/15 13:24:11 $
|
||||
*
|
||||
* @author Michael A. Smith
|
||||
*/
|
||||
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,6 +43,7 @@ public class NullComparator implements Comparator, Serializable {
|
|||
**/
|
||||
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
|
||||
|
@ -111,6 +115,7 @@ public class NullComparator implements Comparator, Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Perform a comparison between two objects. If both objects are
|
||||
* <code>null</code>, a <code>0</code> value is returned. If one object
|
||||
|
@ -121,9 +126,7 @@ public class NullComparator implements Comparator, Serializable {
|
|||
* used to compare the non-<code>null</code> objects.
|
||||
*
|
||||
* @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 @@ public class NullComparator implements Comparator, Serializable {
|
|||
return this.nonNullComparator.compare(o1, o2);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Implement a hash code for this comparator that is consistent with
|
||||
* {@link #equals(Object)}.
|
||||
|
@ -168,5 +172,4 @@ public class NullComparator implements Comparator, Serializable {
|
|||
(this.nonNullComparator.equals(other.nonNullComparator)));
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -5820772575483504339L;
|
||||
}
|
||||
|
|
|
@ -19,27 +19,33 @@ import java.io.Serializable;
|
|||
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: 1.18 $ $Date: 2004/02/18 00:59:06 $
|
||||
* @version $Revision: 1.19 $ $Date: 2004/05/15 13:24:11 $
|
||||
*
|
||||
* @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 @@ public class ReverseComparator implements Comparator,Serializable {
|
|||
* 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 class ReverseComparator implements Comparator,Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
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 @@ public class ReverseComparator implements Comparator,Serializable {
|
|||
* 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;
|
||||
}
|
||||
|
|
|
@ -22,8 +22,7 @@ import org.apache.commons.collections.Transformer;
|
|||
/**
|
||||
* 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 @@ import org.apache.commons.collections.Transformer;
|
|||
* @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}
|
||||
* 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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue