Making ComparatorChain implement Iterable. Including test and package private copy of Collections' UnmodifiableIterator
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1167673 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/commons/lang3/compare/ComparatorChain.java b/src/main/java/org/apache/commons/lang3/compare/ComparatorChain.java
index 9d45efc..e826418 100644
--- a/src/main/java/org/apache/commons/lang3/compare/ComparatorChain.java
+++ b/src/main/java/org/apache/commons/lang3/compare/ComparatorChain.java
@@ -43,7 +43,7 @@
* @since Commons Collections 2.0
* @version $Revision$ $Date$
*/
-public class ComparatorChain<E> implements Comparator<E>, Serializable {
+public class ComparatorChain<E> implements Comparator<E>, Serializable, Iterable {
/** The list of comparators in the chain. */
protected List<Comparator<E>> comparatorChain = null;
@@ -109,6 +109,16 @@
//-----------------------------------------------------------------------
/**
+ * Iterate through the chained comparators.
+ *
+ * @return Unmodifiable iterator over the chained comparators
+ */
+ public Iterator<Comparator<E>> iterator() {
+ return new UnmodifiableIterator(comparatorChain.iterator());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
* Implement a hash code for this comparator that is consistent with
* {@link #equals(Object) equals}.
*
diff --git a/src/main/java/org/apache/commons/lang3/compare/UnmodifiableIterator.java b/src/main/java/org/apache/commons/lang3/compare/UnmodifiableIterator.java
new file mode 100644
index 0000000..a0a3048
--- /dev/null
+++ b/src/main/java/org/apache/commons/lang3/compare/UnmodifiableIterator.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.compare;
+
+import java.util.Iterator;
+
+/**
+ * Decorates an iterator such that it cannot be modified.
+ * <p>
+ * Attempts to modify it will result in an UnsupportedOperationException.
+ *
+ * @since Commons Collections 3.0
+ * @version $Revision: 1148801 $ $Date: 2011-07-20 07:44:46 -0700 (Wed, 20 Jul 2011) $
+ */
+final class UnmodifiableIterator<E> implements Iterator<E> {
+
+ /** The iterator being decorated */
+ private final Iterator<E> iterator;
+
+ //-----------------------------------------------------------------------
+ /**
+ * Constructor.
+ *
+ * @param iterator the iterator to decorate
+ */
+ public UnmodifiableIterator(Iterator<E> iterator) {
+ super();
+ if (iterator == null) {
+ throw new IllegalArgumentException("Iterator must not be null");
+ }
+ this.iterator = iterator;
+ }
+
+ //-----------------------------------------------------------------------
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ public E next() {
+ return iterator.next();
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException("remove() is not supported");
+ }
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/compare/ComparatorChainTest.java b/src/test/java/org/apache/commons/lang3/compare/ComparatorChainTest.java
index 752e476..34694c5 100644
--- a/src/test/java/org/apache/commons/lang3/compare/ComparatorChainTest.java
+++ b/src/test/java/org/apache/commons/lang3/compare/ComparatorChainTest.java
@@ -20,6 +20,7 @@
import static org.junit.Assert.*;
import java.util.Comparator;
+import java.util.Iterator;
import org.junit.Before;
import org.junit.Test;
@@ -51,4 +52,20 @@
assertTrue("Comparison failed", cc.compare( "ABC-123", "ABC-123" ) == 0 );
}
+ @Test
+ public void testIterate() {
+ Comparator c1 = ComparableComparator.INSTANCE;
+ Comparator c2 = new ComparableComparator();
+ Comparator c3 = new NullComparator();
+ Comparator c4 = new ReverseComparator();
+ Iterable cc = new ComparatorChain(c1, c2, c3, c4);
+
+ Iterator itr = cc.iterator();
+ assertEquals( "Iteration failed", c1, itr.next() );
+ assertEquals( "Iteration failed", c2, itr.next() );
+ assertEquals( "Iteration failed", c3, itr.next() );
+ assertEquals( "Iteration failed", c4, itr.next() );
+ assertFalse( "Iteration failed", itr.hasNext() );
+ }
+
}