now throws an exception for comparisons on an empty chain and
added no-op constructor git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130665 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fedb457600
commit
54c23b2da3
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/comparators/ComparatorChain.java,v 1.4 2002/03/04 19:18:56 morgand Exp $
|
||||
* $Revision: 1.4 $
|
||||
* $Date: 2002/03/04 19:18:56 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/comparators/ComparatorChain.java,v 1.5 2002/03/19 22:25:51 morgand Exp $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2002/03/19 22:25:51 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -101,9 +101,19 @@ public class ComparatorChain implements Comparator,Serializable {
|
|||
protected BitSet orderingBits = null;
|
||||
|
||||
// ComparatorChain is "locked" after the first time
|
||||
// compare(Object,Object) is called)
|
||||
// compare(Object,Object) is called
|
||||
protected boolean isLocked = false;
|
||||
|
||||
/**
|
||||
* Construct a ComparatorChain with no Comparators.
|
||||
* You must add at least one Comparator before calling
|
||||
* the compare(Object,Object) method, or an
|
||||
* UnsupportedOperationException is thrown
|
||||
*/
|
||||
public ComparatorChain() {
|
||||
this(new ArrayList(),new BitSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a ComparatorChain with a single Comparator,
|
||||
* sorting in the forward order
|
||||
|
@ -274,11 +284,29 @@ public class ComparatorChain implements Comparator,Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
public int compare(Object o1, Object o2) {
|
||||
private void checkChainIntegrity() {
|
||||
if (comparatorChain.size() == 0) {
|
||||
throw new UnsupportedOperationException("ComparatorChains must contain at least one Comparator");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform comaparisons on the Objects as per
|
||||
* Comparator.compare(o1,o2).
|
||||
*
|
||||
* @param o1 object 1
|
||||
* @param o2 object 2
|
||||
* @return -1, 0, or 1
|
||||
* @exception UnsupportedOperationException
|
||||
* if the ComparatorChain does not contain at least one
|
||||
* Comparator
|
||||
*/
|
||||
public int compare(Object o1, Object o2) throws UnsupportedOperationException {
|
||||
if (isLocked == false) {
|
||||
checkChainIntegrity();
|
||||
isLocked = true;
|
||||
}
|
||||
|
||||
|
||||
// iterate over all comparators in the chain
|
||||
Iterator comparators = comparatorChain.iterator();
|
||||
for (int comparatorIndex = 0; comparators.hasNext(); ++comparatorIndex) {
|
||||
|
|
|
@ -31,6 +31,52 @@ public class TestComparatorChain extends TestComparator {
|
|||
return chain;
|
||||
}
|
||||
|
||||
public void testNoopComparatorChain() {
|
||||
ComparatorChain chain = new ComparatorChain();
|
||||
Integer i1 = new Integer(4);
|
||||
Integer i2 = new Integer(6);
|
||||
chain.addComparator(new ComparableComparator());
|
||||
|
||||
int correctValue = i1.compareTo(i2);
|
||||
assertTrue("Comparison returns the right order",chain.compare(i1,i2) == correctValue);
|
||||
}
|
||||
|
||||
public void testBadNoopComparatorChain() {
|
||||
ComparatorChain chain = new ComparatorChain();
|
||||
Integer i1 = new Integer(4);
|
||||
Integer i2 = new Integer(6);
|
||||
try {
|
||||
chain.compare(i1,i2);
|
||||
fail("An exception should be thrown when a chain contains zero comparators.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void testListComparatorChain() {
|
||||
List list = new LinkedList();
|
||||
list.add(new ComparableComparator());
|
||||
ComparatorChain chain = new ComparatorChain(list);
|
||||
Integer i1 = new Integer(4);
|
||||
Integer i2 = new Integer(6);
|
||||
|
||||
int correctValue = i1.compareTo(i2);
|
||||
assertTrue("Comparison returns the right order",chain.compare(i1,i2) == correctValue);
|
||||
}
|
||||
|
||||
public void testBadListComparatorChain() {
|
||||
List list = new LinkedList();
|
||||
ComparatorChain chain = new ComparatorChain(list);
|
||||
Integer i1 = new Integer(4);
|
||||
Integer i2 = new Integer(6);
|
||||
try {
|
||||
chain.compare(i1,i2);
|
||||
fail("An exception should be thrown when a chain contains zero comparators.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public List getComparableObjectsOrdered() {
|
||||
List list = new LinkedList();
|
||||
// this is the correct order assuming a
|
||||
|
|
Loading…
Reference in New Issue