From ab1cf8de16226ba6bbdd857266c69557b24d9402 Mon Sep 17 00:00:00 2001 From: Rodney Waldhoff Date: Mon, 13 Jan 2003 22:52:34 +0000 Subject: [PATCH] * 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 --- .../comparators/ComparatorChain.java | 14 ++++++---- .../comparators/TransformingComparator.java | 27 ++++++++++++------- .../comparators/TestComparatorChain.java | 26 +++++++++++++++++- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/java/org/apache/commons/collections/comparators/ComparatorChain.java b/src/java/org/apache/commons/collections/comparators/ComparatorChain.java index d7dc67cb5..9dd24c4b2 100644 --- a/src/java/org/apache/commons/collections/comparators/ComparatorChain.java +++ b/src/java/org/apache/commons/collections/comparators/ComparatorChain.java @@ -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 < 0 or index > 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; diff --git a/src/java/org/apache/commons/collections/comparators/TransformingComparator.java b/src/java/org/apache/commons/collections/comparators/TransformingComparator.java index 2f0ad4138..f93cf831d 100644 --- a/src/java/org/apache/commons/collections/comparators/TransformingComparator.java +++ b/src/java/org/apache/commons/collections/comparators/TransformingComparator.java @@ -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 * . */ +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 - * Comparator#compare method. - *

+ * {@link Comparator#compare compare} method. + *

+ * + * @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) { diff --git a/src/test/org/apache/commons/collections/comparators/TestComparatorChain.java b/src/test/org/apache/commons/collections/comparators/TestComparatorChain.java index 830abd9a3..d2c0f1b47 100644 --- a/src/test/org/apache/commons/collections/comparators/TestComparatorChain.java +++ b/src/test/org/apache/commons/collections/comparators/TestComparatorChain.java @@ -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