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.
|
* A {@link Comparator} for {@link Boolean} objects.
|
||||||
*
|
* <p>
|
||||||
* @see #getTrueFirstComparator
|
* @see #getTrueFirstComparator()
|
||||||
* @see #getFalseFirstComparator
|
* @see #getFalseFirstComparator()
|
||||||
* @see #getBooleanComparator
|
* @see #getBooleanComparator(boolean)
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @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
|
* @author Rodney Waldhoff
|
||||||
*/
|
*/
|
||||||
public final class BooleanComparator implements Comparator, Serializable {
|
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
|
* Creates a <code>BooleanComparator</code> that sorts
|
||||||
* <code>false</code> values before <code>true</code> values.
|
* <code>false</code> values before <code>true</code> values.
|
||||||
|
@ -62,20 +126,26 @@ public final class BooleanComparator implements Comparator, Serializable {
|
||||||
/**
|
/**
|
||||||
* Compares two arbitrary Objects.
|
* Compares two arbitrary Objects.
|
||||||
* When both arguments are <code>Boolean</code>, this method is equivalent to
|
* 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
|
* When either argument is not a <code>Boolean</code>, this methods throws
|
||||||
* a {@link ClassCastException}.
|
* 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>
|
* @throws ClassCastException when either argument is not <code>Boolean</code>
|
||||||
*/
|
*/
|
||||||
public int compare(Object o1, Object o2) {
|
public int compare(Object obj1, Object obj2) {
|
||||||
return compare((Boolean)o1,(Boolean)o2);
|
return compare((Boolean)obj1, (Boolean)obj2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compares two non-<code>null</code> <code>Boolean</code> objects
|
* 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>
|
* @throws NullPointerException when either argument <code>null</code>
|
||||||
*/
|
*/
|
||||||
public int compare(Boolean b1, Boolean b2) {
|
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;
|
return (v1 ^ v2) ? ( (v1 ^ trueFirst) ? 1 : -1 ) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Implement a hash code for this comparator that is consistent with
|
* 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.
|
* @return a hash code for this comparator.
|
||||||
*/
|
*/
|
||||||
|
@ -103,14 +174,18 @@ public final class BooleanComparator implements Comparator, Serializable {
|
||||||
* <p>
|
* <p>
|
||||||
* This implementation returns <code>true</code>
|
* This implementation returns <code>true</code>
|
||||||
* iff <code><i>that</i></code> is a {@link BooleanComparator}
|
* 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) {
|
public boolean equals(Object object) {
|
||||||
return (this == that) ||
|
return (this == object) ||
|
||||||
((that instanceof BooleanComparator) &&
|
((object instanceof BooleanComparator) &&
|
||||||
(this.trueFirst == ((BooleanComparator)that).trueFirst));
|
(this.trueFirst == ((BooleanComparator)object).trueFirst));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Returns <code>true</code> iff
|
* Returns <code>true</code> iff
|
||||||
* I sort <code>true</code> values before
|
* I sort <code>true</code> values before
|
||||||
|
@ -118,65 +193,11 @@ public final class BooleanComparator implements Comparator, Serializable {
|
||||||
* returns <code>true</code> iff
|
* returns <code>true</code> iff
|
||||||
* {@link #compare(Boolean,Boolean) compare(Boolean.FALSE,Boolean.TRUE)}
|
* {@link #compare(Boolean,Boolean) compare(Boolean.FALSE,Boolean.TRUE)}
|
||||||
* returns a positive value.
|
* returns a positive value.
|
||||||
|
*
|
||||||
|
* @return the trueFirst flag
|
||||||
*/
|
*/
|
||||||
public boolean sortsTrueFirst() {
|
public boolean sortsTrueFirst() {
|
||||||
return trueFirst;
|
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 />
|
* <p />
|
||||||
* Note: In the 2.0 and 2.1 releases of Commons Collections,
|
* Note: In the 2.0 and 2.1 releases of Commons Collections,
|
||||||
* this class would throw a {@link ClassCastException} if
|
* 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},
|
* 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
|
* 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
|
* @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
|
* @author Henri Yandell
|
||||||
*
|
*
|
||||||
* @see java.util.Collections#reverseOrder
|
* @see java.util.Collections#reverseOrder()
|
||||||
*/
|
*/
|
||||||
public class ComparableComparator implements Comparator, Serializable {
|
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
|
* Gets the singleton instance of a ComparableComparator.
|
||||||
* encouraged to use the comparator returned from this method instead of
|
* <p>
|
||||||
* constructing a new instance to reduce allocation and GC overhead when
|
* Developers are encouraged to use the comparator returned from this method
|
||||||
* multiple comparable comparators may be used in the same VM.
|
* 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() {
|
public static ComparableComparator getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* Constructor whose use should be avoided.
|
||||||
|
* <p>
|
||||||
|
* Please use the {@link #getInstance()} method whenever possible.
|
||||||
|
*/
|
||||||
public ComparableComparator() {
|
public ComparableComparator() {
|
||||||
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Compare the two {@link Comparable Comparable} arguments.
|
* Compare the two {@link Comparable Comparable} arguments.
|
||||||
* This method is equivalent to:
|
* This method is equivalent to:
|
||||||
* <pre>(({@link Comparable Comparable})o1).{@link Comparable#compareTo compareTo}(o2)</pre>
|
* <pre>((Comparable)obj1).compareTo(obj2)</pre>
|
||||||
* @throws NullPointerException when <i>o1</i> is <code>null</code>,
|
*
|
||||||
* or when <code>((Comparable)o1).compareTo(o2)</code> does
|
* @param obj1 the first object to compare
|
||||||
* @throws ClassCastException when <i>o1</i> is not a {@link Comparable Comparable},
|
* @param obj2 the second object to compare
|
||||||
* or when <code>((Comparable)o1).compareTo(o2)</code> does
|
* @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) {
|
public int compare(Object obj1, Object obj2) {
|
||||||
return ((Comparable)o1).compareTo(o2);
|
return ((Comparable)obj1).compareTo(obj2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Implement a hash code for this comparator that is consistent with
|
* Implement a hash code for this comparator that is consistent with
|
||||||
* {@link #equals}.
|
* {@link #equals(Object) equals}.
|
||||||
*
|
*
|
||||||
* @return a hash code for this comparator.
|
* @return a hash code for this comparator.
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
|
@ -86,20 +109,18 @@ public class ComparableComparator implements Comparator, Serializable {
|
||||||
* known to be equivalent to mine.
|
* known to be equivalent to mine.
|
||||||
* <p>
|
* <p>
|
||||||
* This implementation returns <code>true</code>
|
* 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>. Subclasses may want to override
|
* equals <code>this.getClass()</code>.
|
||||||
* this behavior to remain consistent with the {@link Comparator#equals}
|
* Subclasses may want to override this behavior to remain consistent
|
||||||
* contract.
|
* with the {@link Comparator#equals(Object)} contract.
|
||||||
|
*
|
||||||
|
* @param object the object to compare with
|
||||||
|
* @return true if equal
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
*/
|
*/
|
||||||
public boolean equals(Object that) {
|
public boolean equals(Object object) {
|
||||||
return (this == that) ||
|
return (this == object) ||
|
||||||
((null != that) && (that.getClass().equals(this.getClass())));
|
((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
|
* @since Commons Collections 2.0
|
||||||
* @author Morgan Delagrange
|
* @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 {
|
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;
|
protected List comparatorChain = null;
|
||||||
// false (clear) = ascend; true (set) = descend
|
/** Order - false (clear) = ascend; true (set) = descend. */
|
||||||
protected BitSet orderingBits = null;
|
protected BitSet orderingBits = null;
|
||||||
|
/** Whether the chain has been "locked". */
|
||||||
// ComparatorChain is "locked" after the first time
|
|
||||||
// compare(Object,Object) is called
|
|
||||||
protected boolean isLocked = false;
|
protected boolean isLocked = false;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Construct a ComparatorChain with no Comparators.
|
* Construct a ComparatorChain with no Comparators.
|
||||||
* You must add at least one Comparator before calling
|
* You must add at least one Comparator before calling
|
||||||
|
@ -131,6 +134,7 @@ public class ComparatorChain implements Comparator,Serializable {
|
||||||
orderingBits = bits;
|
orderingBits = bits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Add a Comparator to the end of the chain using the
|
* Add a Comparator to the end of the chain using the
|
||||||
* forward sort order
|
* forward sort order
|
||||||
|
@ -247,12 +251,13 @@ public class ComparatorChain implements Comparator,Serializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Perform comparisons on the Objects as per
|
* Perform comparisons on the Objects as per
|
||||||
* Comparator.compare(o1,o2).
|
* Comparator.compare(o1,o2).
|
||||||
*
|
*
|
||||||
* @param o1 object 1
|
* @param o1 the first object to compare
|
||||||
* @param o2 object 2
|
* @param o2 the second object to compare
|
||||||
* @return -1, 0, or 1
|
* @return -1, 0, or 1
|
||||||
* @exception UnsupportedOperationException
|
* @exception UnsupportedOperationException
|
||||||
* if the ComparatorChain does not contain at least one
|
* if the ComparatorChain does not contain at least one
|
||||||
|
@ -289,10 +294,12 @@ public class ComparatorChain implements Comparator,Serializable {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Implement a hash code for this comparator that is consistent with
|
* 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
|
* @since Commons Collections 3.0
|
||||||
*/
|
*/
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
|
@ -312,21 +319,23 @@ public class ComparatorChain implements Comparator,Serializable {
|
||||||
* equivalent to mine.
|
* equivalent to mine.
|
||||||
* <p>
|
* <p>
|
||||||
* This implementation returns <code>true</code>
|
* 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
|
* equals <code>this.getClass()</code>, and the underlying
|
||||||
* comparators and order bits are equal. Subclasses may want
|
* comparators and order bits are equal.
|
||||||
* to override this behavior to remain consistent with the
|
* Subclasses may want to override this behavior to remain consistent
|
||||||
* {@link Comparator#equals} contract.
|
* with the {@link Comparator#equals(Object)} contract.
|
||||||
*
|
*
|
||||||
|
* @param object the object to compare with
|
||||||
|
* @return true if equal
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
*/
|
*/
|
||||||
public boolean equals(Object that) {
|
public boolean equals(Object object) {
|
||||||
if(this == that) {
|
if(this == object) {
|
||||||
return true;
|
return true;
|
||||||
} else if(null == that) {
|
} else if(null == object) {
|
||||||
return false;
|
return false;
|
||||||
} else if(that.getClass().equals(this.getClass())) {
|
} else if(object.getClass().equals(this.getClass())) {
|
||||||
ComparatorChain chain = (ComparatorChain)that;
|
ComparatorChain chain = (ComparatorChain)object;
|
||||||
return ( (null == orderingBits ? null == chain.orderingBits : orderingBits.equals(chain.orderingBits))
|
return ( (null == orderingBits ? null == chain.orderingBits : orderingBits.equals(chain.orderingBits))
|
||||||
&& (null == comparatorChain ? null == chain.comparatorChain : comparatorChain.equals(chain.comparatorChain)) );
|
&& (null == comparatorChain ? null == chain.comparatorChain : comparatorChain.equals(chain.comparatorChain)) );
|
||||||
} else {
|
} 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.
|
* A Comparator which imposes a specific order on a specific set of Objects.
|
||||||
* Objects are presented to the FixedOrderComparator in a specified order and
|
* 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:
|
* For example:
|
||||||
* <pre>
|
* <pre>
|
||||||
* String[] planets = {"Mercury", "Venus", "Earth", "Mars"};
|
* String[] planets = {"Mercury", "Venus", "Earth", "Mars"};
|
||||||
|
@ -33,15 +33,15 @@ import java.util.Map;
|
||||||
* Arrays.sort(planets, distanceFromSun); // Back to original order
|
* Arrays.sort(planets, distanceFromSun); // Back to original order
|
||||||
* </pre>
|
* </pre>
|
||||||
* <p>
|
* <p>
|
||||||
* Once {@link #compare} has been called, the FixedOrderComparator is locked and
|
* Once <code>compare</code> has been called, the FixedOrderComparator is locked
|
||||||
* attempts to modify it yield an UnsupportedOperationException.
|
* and attempts to modify it yield an UnsupportedOperationException.
|
||||||
* <p>
|
* <p>
|
||||||
* Instances of FixedOrderComparator are not synchronized. The class is not
|
* Instances of FixedOrderComparator are not synchronized. The class is not
|
||||||
* thread-safe at construction time, but it is thread-safe to perform
|
* thread-safe at construction time, but it is thread-safe to perform
|
||||||
* multiple comparisons after all the setup operations are complete.
|
* multiple comparisons after all the setup operations are complete.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @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 David Leppik
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
|
@ -150,6 +150,9 @@ public class FixedOrderComparator implements Comparator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the behavior for comparing unknown objects.
|
* Gets the behavior for comparing unknown objects.
|
||||||
|
*
|
||||||
|
* @return the flag for unknown behaviour - UNKNOWN_AFTER,
|
||||||
|
* UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION
|
||||||
*/
|
*/
|
||||||
public int getUnknownObjectBehavior() {
|
public int getUnknownObjectBehavior() {
|
||||||
return unknownObjectBehavior;
|
return unknownObjectBehavior;
|
||||||
|
@ -158,6 +161,8 @@ public class FixedOrderComparator implements Comparator {
|
||||||
/**
|
/**
|
||||||
* Sets the behavior for comparing unknown objects.
|
* 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 UnsupportedOperationException if a comparison has been performed
|
||||||
* @throws IllegalArgumentException if the unknown flag is not valid
|
* @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 obj1 the first object to compare
|
||||||
* @param obj2 the second 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
|
* to this Comparator and an alternative behavior has not been set
|
||||||
* via {@link #setUnknownObjectBehavior(int)}.
|
* via {@link #setUnknownObjectBehavior(int)}.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -23,12 +23,15 @@ import java.util.Comparator;
|
||||||
* other objects.
|
* other objects.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 2.0
|
* @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
|
* @author Michael A. Smith
|
||||||
*/
|
*/
|
||||||
public class NullComparator implements Comparator, Serializable {
|
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.
|
* The comparator to use when comparing two non-<code>null</code> objects.
|
||||||
**/
|
**/
|
||||||
|
@ -40,7 +43,8 @@ public class NullComparator implements Comparator, Serializable {
|
||||||
**/
|
**/
|
||||||
private boolean nullsAreHigh;
|
private boolean nullsAreHigh;
|
||||||
|
|
||||||
/**
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
* Construct an instance that sorts <code>null</code> higher than any
|
* 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> object it is compared with. When comparing two
|
||||||
* non-<code>null</code> objects, the {@link ComparableComparator} is
|
* non-<code>null</code> objects, the {@link ComparableComparator} is
|
||||||
|
@ -111,6 +115,7 @@ public class NullComparator implements Comparator, Serializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Perform a comparison between two objects. If both objects are
|
* Perform a comparison between two objects. If both objects are
|
||||||
* <code>null</code>, a <code>0</code> value is returned. If one object
|
* <code>null</code>, a <code>0</code> value is returned. If one object
|
||||||
|
@ -120,10 +125,8 @@ public class NullComparator implements Comparator, Serializable {
|
||||||
* underlying comparator specified in the constructor (or the default) is
|
* underlying comparator specified in the constructor (or the default) is
|
||||||
* used to compare the non-<code>null</code> objects.
|
* used to compare the non-<code>null</code> objects.
|
||||||
*
|
*
|
||||||
* @param o1 the first object to compare
|
* @param o1 the first object to compare
|
||||||
*
|
* @param o2 the object to compare it to.
|
||||||
* @param o2 the object to compare it to.
|
|
||||||
*
|
|
||||||
* @return <code>-1</code> if <code>o1</code> is "lower" than (less than,
|
* @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
|
* before, etc.) <code>o2</code>; <code>1</code> if <code>o1</code> is
|
||||||
* "higher" than (greater than, after, etc.) <code>o2</code>; or
|
* "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);
|
return this.nonNullComparator.compare(o1, o2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Implement a hash code for this comparator that is consistent with
|
* Implement a hash code for this comparator that is consistent with
|
||||||
* {@link #equals(Object)}.
|
* {@link #equals(Object)}.
|
||||||
|
@ -168,5 +172,4 @@ public class NullComparator implements Comparator, Serializable {
|
||||||
(this.nonNullComparator.equals(other.nonNullComparator)));
|
(this.nonNullComparator.equals(other.nonNullComparator)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final long serialVersionUID = -5820772575483504339L;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,27 +19,33 @@ import java.io.Serializable;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverses the order of another comparator by
|
* Reverses the order of another comparator by reversing the arguments
|
||||||
* reversing the arguments to its {@link #compare compare}
|
* to its {@link #compare(Object, Object) compare} method.
|
||||||
* method.
|
|
||||||
*
|
*
|
||||||
* @since Commons Collections 2.0
|
* @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 Henri Yandell
|
||||||
* @author Michael A. Smith
|
* @author Michael A. Smith
|
||||||
*
|
*
|
||||||
* @see java.util.Collections#reverseOrder
|
* @see java.util.Collections#reverseOrder()
|
||||||
*/
|
*/
|
||||||
public class ReverseComparator implements Comparator,Serializable {
|
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
|
* Creates a comparator that compares objects based on the inverse of their
|
||||||
* natural ordering. Using this Constructor will create a ReverseComparator
|
* natural ordering. Using this Constructor will create a ReverseComparator
|
||||||
* that is functionally identical to the Comparator returned by
|
* that is functionally identical to the Comparator returned by
|
||||||
* java.util.Collections.<b>reverseOrder()</b>.
|
* java.util.Collections.<b>reverseOrder()</b>.
|
||||||
*
|
*
|
||||||
* @see java.util.Collections#reverseOrder
|
* @see java.util.Collections#reverseOrder()
|
||||||
*/
|
*/
|
||||||
public ReverseComparator() {
|
public ReverseComparator() {
|
||||||
this(null);
|
this(null);
|
||||||
|
@ -50,7 +56,7 @@ public class ReverseComparator implements Comparator,Serializable {
|
||||||
* of the given comparator. If you pass in <code>null</code>,
|
* of the given comparator. If you pass in <code>null</code>,
|
||||||
* the ReverseComparator defaults to reversing the
|
* the ReverseComparator defaults to reversing the
|
||||||
* natural order, as per
|
* natural order, as per
|
||||||
* {@link java.util.Collections#reverseOrder}</b>.
|
* {@link java.util.Collections#reverseOrder()}</b>.
|
||||||
*
|
*
|
||||||
* @param comparator Comparator to reverse
|
* @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
|
* 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
|
* @since Commons Collections 3.0
|
||||||
*/
|
*/
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
|
@ -82,29 +98,27 @@ public class ReverseComparator implements Comparator,Serializable {
|
||||||
* equivalent to mine.
|
* equivalent to mine.
|
||||||
* <p>
|
* <p>
|
||||||
* This implementation returns <code>true</code>
|
* 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
|
* equals <code>this.getClass()</code>, and the underlying
|
||||||
* comparators are equal. Subclasses may want to override
|
* comparators are equal.
|
||||||
* this behavior to remain consistent with the
|
* Subclasses may want to override this behavior to remain consistent
|
||||||
* {@link Comparator#equals} contract.
|
* with the {@link Comparator#equals(Object) equals} contract.
|
||||||
*
|
*
|
||||||
|
* @param object the object to compare to
|
||||||
|
* @return true if equal
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
*/
|
*/
|
||||||
public boolean equals(Object that) {
|
public boolean equals(Object object) {
|
||||||
if(this == that) {
|
if(this == object) {
|
||||||
return true;
|
return true;
|
||||||
} else if(null == that) {
|
} else if(null == object) {
|
||||||
return false;
|
return false;
|
||||||
} else if(that.getClass().equals(this.getClass())) {
|
} else if(object.getClass().equals(this.getClass())) {
|
||||||
ReverseComparator thatrc = (ReverseComparator)that;
|
ReverseComparator thatrc = (ReverseComparator)object;
|
||||||
return comparator.equals(thatrc.comparator);
|
return comparator.equals(thatrc.comparator);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
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
|
* Decorates another Comparator with transformation behavior. That is, the
|
||||||
* return value from the transform operation will be passed to the decorated
|
* return value from the transform operation will be passed to the decorated
|
||||||
* {@link Comparator#compare compare} method.
|
* {@link Comparator#compare(Object,Object) compare} method.
|
||||||
* <p />
|
|
||||||
*
|
*
|
||||||
* @since Commons Collections 2.0 (?)
|
* @since Commons Collections 2.0 (?)
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
|
@ -31,41 +30,46 @@ import org.apache.commons.collections.Transformer;
|
||||||
* @see org.apache.commons.collections.Transformer
|
* @see org.apache.commons.collections.Transformer
|
||||||
* @see org.apache.commons.collections.comparators.ComparableComparator
|
* @see org.apache.commons.collections.comparators.ComparableComparator
|
||||||
*/
|
*/
|
||||||
public class TransformingComparator implements Comparator
|
public class TransformingComparator implements Comparator {
|
||||||
{
|
|
||||||
|
/** The decorated comparator. */
|
||||||
protected Comparator decorated;
|
protected Comparator decorated;
|
||||||
|
/** The transformer being used. */
|
||||||
protected Transformer transformer;
|
protected Transformer transformer;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Constructs an instance with the given Transformer and a
|
* Constructs an instance with the given Transformer and a
|
||||||
* {@link ComparableComparator ComparableComparator}.
|
* {@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());
|
this(transformer, new ComparableComparator());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an instance with the given Transformer and Comparator
|
* 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
|
* @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.decorated = decorated;
|
||||||
this.transformer = transformer;
|
this.transformer = transformer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Returns the result of comparing the values from the transform operation.
|
* 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)
|
public int compare(Object obj1, Object obj2) {
|
||||||
{
|
Object value1 = this.transformer.transform(obj1);
|
||||||
Object value1 = this.transformer.transform(o1);
|
Object value2 = this.transformer.transform(obj2);
|
||||||
Object value2 = this.transformer.transform(o2);
|
|
||||||
return this.decorated.compare(value1, value2);
|
return this.decorated.compare(value1, value2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue