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
This commit is contained in:
Henri Yandell 2011-09-11 05:47:33 +00:00
parent 2fcc80e71f
commit 8819b52ba1
3 changed files with 89 additions and 1 deletions

View File

@ -43,7 +43,7 @@
* @since Commons Collections 2.0 * @since Commons Collections 2.0
* @version $Revision$ $Date$ * @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. */ /** The list of comparators in the chain. */
protected List<Comparator<E>> comparatorChain = null; protected List<Comparator<E>> comparatorChain = null;
@ -107,6 +107,16 @@ public int compare(E o1, E o2) {
return 0; return 0;
} }
//-----------------------------------------------------------------------
/**
* 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 * Implement a hash code for this comparator that is consistent with

View File

@ -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");
}
}

View File

@ -20,6 +20,7 @@
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -51,4 +52,20 @@ public int compare(String o1, String o2) {
assertTrue("Comparison failed", cc.compare( "ABC-123", "ABC-123" ) == 0 ); 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() );
}
} }