morgand 02/03/19 14:25:51 Modified: collections/src/java/org/apache/commons/collections/comparators ComparatorChain.java Log: now throws an exception for comparisons on an empty chain and added no-op constructor Revision Changes Path 1.5 +34 -6 jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ComparatorChain.java Index: ComparatorChain.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ComparatorChain.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- ComparatorChain.java 4 Mar 2002 19:18:56 -0000 1.4 +++ ComparatorChain.java 19 Mar 2002 22:25:51 -0000 1.5 @@ -1,7 +1,7 @@ /* - * $Header: /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/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,10 +101,20 @@ 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 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) {
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>