* fix ComparatorChain for the case in which an underlying comparator returns Integer.MIN_VALUE, add test that demonstrates
* minor javadocs git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130943 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4b47c895c4
commit
ab1cf8de16
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/comparators/ComparatorChain.java,v 1.9 2003/01/10 20:21:25 rwaldhoff Exp $
|
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/comparators/ComparatorChain.java,v 1.10 2003/01/13 22:52:34 rwaldhoff Exp $
|
||||||
* ====================================================================
|
* ====================================================================
|
||||||
*
|
*
|
||||||
* The Apache Software License, Version 1.1
|
* The Apache Software License, Version 1.1
|
||||||
|
@ -93,12 +93,12 @@ import java.util.List;
|
||||||
*
|
*
|
||||||
* @since Commons Collections 2.0
|
* @since Commons Collections 2.0
|
||||||
* @author Morgan Delagrange
|
* @author Morgan Delagrange
|
||||||
* @version $Revision: 1.9 $ $Date: 2003/01/10 20:21:25 $
|
* @version $Revision: 1.10 $ $Date: 2003/01/13 22:52:34 $
|
||||||
*/
|
*/
|
||||||
public class ComparatorChain implements Comparator,Serializable {
|
public class ComparatorChain implements Comparator,Serializable {
|
||||||
|
|
||||||
protected List comparatorChain = null;
|
protected List comparatorChain = null;
|
||||||
// 0 = ascend; 1 = descend
|
// false (clear) = ascend; true (set) = descend
|
||||||
protected BitSet orderingBits = null;
|
protected BitSet orderingBits = null;
|
||||||
|
|
||||||
// ComparatorChain is "locked" after the first time
|
// ComparatorChain is "locked" after the first time
|
||||||
|
@ -206,7 +206,7 @@ public class ComparatorChain implements Comparator,Serializable {
|
||||||
* @param index index of the Comparator to replace
|
* @param index index of the Comparator to replace
|
||||||
* @param comparator Comparator to place at the given index
|
* @param comparator Comparator to place at the given index
|
||||||
* @exception IndexOutOfBoundsException
|
* @exception IndexOutOfBoundsException
|
||||||
* if index < 0 or index > size()
|
* if index < 0 or index > size()
|
||||||
*/
|
*/
|
||||||
public void setComparator(int index, Comparator comparator)
|
public void setComparator(int index, Comparator comparator)
|
||||||
throws IndexOutOfBoundsException {
|
throws IndexOutOfBoundsException {
|
||||||
|
@ -315,8 +315,12 @@ public class ComparatorChain implements Comparator,Serializable {
|
||||||
if (retval != 0) {
|
if (retval != 0) {
|
||||||
// invert the order if it is a reverse sort
|
// invert the order if it is a reverse sort
|
||||||
if (orderingBits.get(comparatorIndex) == true) {
|
if (orderingBits.get(comparatorIndex) == true) {
|
||||||
|
if(Integer.MIN_VALUE == retval) {
|
||||||
|
retval = Integer.MAX_VALUE;
|
||||||
|
} else {
|
||||||
retval *= -1;
|
retval *= -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package org.apache.commons.collections.comparators;
|
/*
|
||||||
|
* $Header:$
|
||||||
/* ====================================================================
|
* ====================================================================
|
||||||
* The Apache Software License, Version 1.1
|
* The Apache Software License, Version 1.1
|
||||||
*
|
*
|
||||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
||||||
* reserved.
|
* reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -53,6 +53,7 @@ package org.apache.commons.collections.comparators;
|
||||||
* information on the Apache Software Foundation, please see
|
* information on the Apache Software Foundation, please see
|
||||||
* <http://www.apache.org/>.
|
* <http://www.apache.org/>.
|
||||||
*/
|
*/
|
||||||
|
package org.apache.commons.collections.comparators;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
@ -61,8 +62,12 @@ 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
|
||||||
* <CODE>Comparator#compare</CODE> method.
|
* {@link Comparator#compare compare} method.
|
||||||
* <p>
|
* <p />
|
||||||
|
*
|
||||||
|
* @since Commons Collections 2.0 (?)
|
||||||
|
* @version $Revision:$ $Date:$
|
||||||
|
*
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
|
@ -72,8 +77,10 @@ public class TransformingComparator implements Comparator
|
||||||
protected Transformer transformer;
|
protected Transformer transformer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an instance with the given Transformer and a ComparableComparator.
|
* Constructs an instance with the given Transformer and a
|
||||||
* @param transformer what will transform the instance.
|
* {@link ComparableComparator ComparableComparator}.
|
||||||
|
* @param transformer what will transform the arguments to
|
||||||
|
* {@link #compare compare}
|
||||||
*/
|
*/
|
||||||
public TransformingComparator(Transformer transformer)
|
public TransformingComparator(Transformer transformer)
|
||||||
{
|
{
|
||||||
|
@ -82,8 +89,8 @@ public class TransformingComparator implements Comparator
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 decorated the decorated Comparator
|
||||||
* @param getterName the getter name
|
|
||||||
*/
|
*/
|
||||||
public TransformingComparator(Transformer transformer, Comparator decorated)
|
public TransformingComparator(Transformer transformer, Comparator decorated)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* $Id: TestComparatorChain.java,v 1.4 2003/01/07 23:44:20 rwaldhoff Exp $
|
* $Id: TestComparatorChain.java,v 1.5 2003/01/13 22:52:34 rwaldhoff Exp $
|
||||||
* ====================================================================
|
* ====================================================================
|
||||||
* The Apache Software License, Version 1.1
|
* The Apache Software License, Version 1.1
|
||||||
*
|
*
|
||||||
|
@ -127,6 +127,30 @@ public class TestComparatorChain extends TestComparator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testComparatorChainOnMinvaluedCompatator() {
|
||||||
|
// -1 * Integer.MIN_VALUE is less than 0,
|
||||||
|
// test that ComparatorChain handles this edge case correctly
|
||||||
|
ComparatorChain chain = new ComparatorChain();
|
||||||
|
chain.addComparator(
|
||||||
|
new Comparator() {
|
||||||
|
public int compare(Object a, Object b) {
|
||||||
|
int result = ((Comparable)a).compareTo(b);
|
||||||
|
if(result < 0) {
|
||||||
|
return Integer.MIN_VALUE;
|
||||||
|
} else if(result > 0) {
|
||||||
|
return Integer.MAX_VALUE;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
assertTrue(chain.compare(new Integer(4), new Integer(5)) > 0);
|
||||||
|
assertTrue(chain.compare(new Integer(5), new Integer(4)) < 0);
|
||||||
|
assertTrue(chain.compare(new Integer(4), new Integer(4)) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
public List getComparableObjectsOrdered() {
|
public List getComparableObjectsOrdered() {
|
||||||
List list = new LinkedList();
|
List list = new LinkedList();
|
||||||
// this is the correct order assuming a
|
// this is the correct order assuming a
|
||||||
|
|
Loading…
Reference in New Issue