* 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:
Rodney Waldhoff 2003-01-13 22:52:34 +00:00
parent 4b47c895c4
commit ab1cf8de16
3 changed files with 51 additions and 16 deletions

View File

@ -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
@ -93,12 +93,12 @@ import java.util.List;
*
* @since Commons Collections 2.0
* @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 {
protected List comparatorChain = null;
// 0 = ascend; 1 = descend
// false (clear) = ascend; true (set) = descend
protected BitSet orderingBits = null;
// 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 comparator Comparator to place at the given index
* @exception IndexOutOfBoundsException
* if index < 0 or index > size()
* if index &lt; 0 or index &gt; size()
*/
public void setComparator(int index, Comparator comparator)
throws IndexOutOfBoundsException {
@ -315,7 +315,11 @@ public class ComparatorChain implements Comparator,Serializable {
if (retval != 0) {
// invert the order if it is a reverse sort
if (orderingBits.get(comparatorIndex) == true) {
retval *= -1;
if(Integer.MIN_VALUE == retval) {
retval = Integer.MAX_VALUE;
} else {
retval *= -1;
}
}
return retval;

View File

@ -1,9 +1,9 @@
package org.apache.commons.collections.comparators;
/* ====================================================================
/*
* $Header:$
* ====================================================================
* 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.
*
* 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
* <http://www.apache.org/>.
*/
package org.apache.commons.collections.comparators;
import java.util.Comparator;
@ -61,8 +62,12 @@ 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
* <CODE>Comparator#compare</CODE> method.
* <p>
* {@link Comparator#compare compare} method.
* <p />
*
* @since Commons Collections 2.0 (?)
* @version $Revision:$ $Date:$
*
* @see org.apache.commons.collections.Transformer
* @see org.apache.commons.collections.comparators.ComparableComparator
*/
@ -72,8 +77,10 @@ public class TransformingComparator implements Comparator
protected Transformer transformer;
/**
* Constructs an instance with the given Transformer and a ComparableComparator.
* @param transformer what will transform the instance.
* Constructs an instance with the given Transformer and a
* {@link ComparableComparator ComparableComparator}.
* @param transformer what will transform the arguments to
* {@link #compare compare}
*/
public TransformingComparator(Transformer transformer)
{
@ -82,8 +89,8 @@ public class TransformingComparator implements Comparator
/**
* Constructs an instance with the given Transformer and Comparator
* @param decorated the decorated Comparator
* @param getterName the getter name
* @param transformer what will transform the arguments to {@link #compare compare}
* @param decorated the decorated Comparator
*/
public TransformingComparator(Transformer transformer, Comparator decorated)
{

View File

@ -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
*
@ -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() {
List list = new LinkedList();
// this is the correct order assuming a