Fix deviation from Comparator contract
[sgn(compare(x, y)) == -sgn(compare(y, x))] Eliminate multiplication by switching order of objects passed to underlying comparator. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130667 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
96cd122c69
commit
d0d74a4e1a
|
@ -61,20 +61,19 @@ import java.util.Comparator;
|
||||||
* Reverses the order of another comparator.
|
* Reverses the order of another comparator.
|
||||||
*
|
*
|
||||||
* @author bayard@generationjava.com
|
* @author bayard@generationjava.com
|
||||||
* @version $Id: ReverseComparator.java,v 1.5 2002/03/01 19:18:49 morgand Exp $
|
* @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
|
||||||
|
* @version $Id: ReverseComparator.java,v 1.6 2002/03/20 00:25:37 mas Exp $
|
||||||
*/
|
*/
|
||||||
public class ReverseComparator implements Comparator,Serializable {
|
public class ReverseComparator implements Comparator,Serializable {
|
||||||
|
|
||||||
private Comparator comparator;
|
private Comparator comparator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a reverse comparator that will invert any List.
|
* Creates a comparator that compares objects based on the inverse of their
|
||||||
* It merely reverses the existing order; it does not
|
* natural ordering.
|
||||||
* reorder the List according to reverse "natural" order.
|
|
||||||
* In many cases, a faster alternative to this is
|
|
||||||
* the reverse(List) method of java.util.Collection.
|
|
||||||
*/
|
*/
|
||||||
public ReverseComparator() {
|
public ReverseComparator() {
|
||||||
|
this(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -82,15 +81,15 @@ public class ReverseComparator implements Comparator,Serializable {
|
||||||
* of the passed in comparator.
|
* of the passed in comparator.
|
||||||
*/
|
*/
|
||||||
public ReverseComparator(Comparator comparator) {
|
public ReverseComparator(Comparator comparator) {
|
||||||
|
if(comparator != null) {
|
||||||
this.comparator = comparator;
|
this.comparator = comparator;
|
||||||
|
} else {
|
||||||
|
this.comparator = ComparableComparator.getInstance();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int compare(Object o1, Object o2) {
|
public int compare(Object o1, Object o2) {
|
||||||
if(comparator == null) {
|
return comparator.compare(o2, o1);
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
return -1*comparator.compare(o1,o2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue