Add IteratorUtils unmodifiable iterators, from Rich Dougherty
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130885 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
257fbcc0a5
commit
ba27ecd2d4
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/IteratorUtils.java,v 1.5 2002/11/21 23:08:27 scolebourne Exp $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2002/11/21 23:08:27 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/IteratorUtils.java,v 1.6 2002/12/08 15:42:35 scolebourne Exp $
|
||||
* $Revision: 1.6 $
|
||||
* $Date: 2002/12/08 15:42:35 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -60,6 +60,7 @@
|
|||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
|
@ -91,7 +92,7 @@ import org.apache.commons.collections.iterators.TransformIterator;
|
|||
* <code>org.apache.commons.collections.iterators</code> subpackage.
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: IteratorUtils.java,v 1.5 2002/11/21 23:08:27 scolebourne Exp $
|
||||
* @version $Id: IteratorUtils.java,v 1.6 2002/12/08 15:42:35 scolebourne Exp $
|
||||
* @since 2.1
|
||||
*/
|
||||
public class IteratorUtils {
|
||||
|
@ -113,6 +114,9 @@ public class IteratorUtils {
|
|||
private IteratorUtils() {
|
||||
}
|
||||
|
||||
// Iterator creators
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Gets an empty iterator.
|
||||
* <p>
|
||||
|
@ -238,6 +242,34 @@ public class IteratorUtils {
|
|||
// public static ListIterator arrayListIterator(Object[] array, int start, int end) {
|
||||
// return new ArrayListIterator(array, start, end);
|
||||
// }
|
||||
|
||||
// Iterator wrappers
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Gets an immutable version of an {@link Iterator}. The returned object
|
||||
* will always throw an {@link java.lang.UnsupportedOperationException} for
|
||||
* the {@link Iterator#remove()} method.
|
||||
*
|
||||
* @param iterator The iterator to make immutable.
|
||||
* @return An immutable version of the iterator.
|
||||
*/
|
||||
public static Iterator unmodifiableIterator(Iterator iterator) {
|
||||
return new UnmodifiableIterator(iterator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an immutable version of a {@link ListIterator}.The returned object
|
||||
* will always throw an {@link java.lang.UnsupportedOperationException} for
|
||||
* the {@link Iterator#remove()}, {@link ListIterator#add()} and
|
||||
* {@link ListIterator#set(Object)} methods.
|
||||
*
|
||||
* @param listIterator The iterator to make immutable.
|
||||
* @return An immutable version of the iterator.
|
||||
*/
|
||||
public static ListIterator unmodifiableListIterator(ListIterator listIterator) {
|
||||
return new UnmodifiableListIterator(listIterator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an iterator that iterates through two {@link Iterator}s
|
||||
|
@ -685,5 +717,103 @@ public class IteratorUtils {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A wrapper for an {@link java.util.Iterator} which makes it immutable. All
|
||||
* calls are passed through to the delegate. The {@link #remove()} method
|
||||
* always throws an {@link java.lang.UnsupportedOperationException}.
|
||||
*
|
||||
* @author <a href="mailto:rich@rd.gen.nz">Rich Dougherty</a>
|
||||
*/
|
||||
static class UnmodifiableIterator implements Iterator, Serializable {
|
||||
|
||||
/**
|
||||
* All calls to this iterator are passed to the delegate.
|
||||
*/
|
||||
protected Iterator delegate;
|
||||
|
||||
/**
|
||||
* Create an UnmodifiableIterator.
|
||||
*
|
||||
* @param delegate The delegate to pass all calls to.
|
||||
*/
|
||||
public UnmodifiableIterator(Iterator delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return delegate.hasNext();
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
return delegate.next();
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("This iterator is immutable");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A wrapper for an {@link java.util.ListIterator} which makes it immutable.
|
||||
* All calls are passed through to the delegate. The {@link #remove()},
|
||||
* {@link #add(Object)} and (@link #set(Object)} methods always throw an
|
||||
* {@link java.lang.UnsupportedOperationException}.
|
||||
*
|
||||
* @author <a href="mailto:rich@rd.gen.nz">Rich Dougherty</a>
|
||||
*/
|
||||
static class UnmodifiableListIterator
|
||||
implements ListIterator, Serializable {
|
||||
|
||||
/**
|
||||
* All calls to this iterator are passed to the delegate.
|
||||
*/
|
||||
protected ListIterator delegate;
|
||||
|
||||
/**
|
||||
* Create an UnmodifiableListIterator.
|
||||
*
|
||||
* @param delegate The delegate to pass all calls to.
|
||||
*/
|
||||
public UnmodifiableListIterator(ListIterator delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return delegate.hasNext();
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
return delegate.next();
|
||||
}
|
||||
|
||||
public boolean hasPrevious() {
|
||||
return delegate.hasPrevious();
|
||||
}
|
||||
|
||||
public Object previous() {
|
||||
return delegate.previous();
|
||||
}
|
||||
|
||||
public int nextIndex() {
|
||||
return delegate.nextIndex();
|
||||
}
|
||||
|
||||
public int previousIndex() {
|
||||
return delegate.previousIndex();
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("This iterator is immutable");
|
||||
}
|
||||
|
||||
public void set(Object o) {
|
||||
throw new UnsupportedOperationException("This iterator is immutable");
|
||||
}
|
||||
|
||||
public void add(Object o) {
|
||||
throw new UnsupportedOperationException("This iterator is immutable");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestIteratorUtils.java,v 1.2 2002/10/12 22:36:21 scolebourne Exp $
|
||||
* $Revision: 1.2 $
|
||||
* $Date: 2002/10/12 22:36:21 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestIteratorUtils.java,v 1.3 2002/12/08 15:42:35 scolebourne Exp $
|
||||
* $Revision: 1.3 $
|
||||
* $Date: 2002/12/08 15:42:35 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -62,7 +62,9 @@ package org.apache.commons.collections;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import junit.framework.Test;
|
||||
/**
|
||||
|
@ -74,6 +76,9 @@ public class TestIteratorUtils extends BulkTest {
|
|||
super(name);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
junit.textui.TestRunner.run(suite());
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return BulkTest.makeSuite(TestIteratorUtils.class);
|
||||
|
@ -106,4 +111,186 @@ public class TestIteratorUtils extends BulkTest {
|
|||
assertEquals(list, Arrays.asList(result));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an immutable Iterator operating on the elements ["a", "b", "c", "d"].
|
||||
*/
|
||||
private Iterator getImmutableIterator() {
|
||||
List list = new ArrayList();
|
||||
list.add("a");
|
||||
list.add("b");
|
||||
list.add("c");
|
||||
list.add("d");
|
||||
return IteratorUtils.unmodifiableIterator(list.iterator());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an immutable ListIterator operating on the elements ["a", "b", "c", "d"].
|
||||
*/
|
||||
private ListIterator getImmutableListIterator() {
|
||||
List list = new ArrayList();
|
||||
list.add("a");
|
||||
list.add("b");
|
||||
list.add("c");
|
||||
list.add("d");
|
||||
return IteratorUtils.unmodifiableListIterator(list.listIterator());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test next() and hasNext() for an immutable Iterator.
|
||||
*/
|
||||
public void testUnmodifiableIteratorIteration() {
|
||||
Iterator iterator = getImmutableIterator();
|
||||
|
||||
assertTrue(iterator.hasNext());
|
||||
|
||||
assertEquals("a", iterator.next());
|
||||
|
||||
assertTrue(iterator.hasNext());
|
||||
|
||||
assertEquals("b", iterator.next());
|
||||
|
||||
assertTrue(iterator.hasNext());
|
||||
|
||||
assertEquals("c", iterator.next());
|
||||
|
||||
assertTrue(iterator.hasNext());
|
||||
|
||||
assertEquals("d", iterator.next());
|
||||
|
||||
assertTrue(!iterator.hasNext());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test next(), hasNext(), previous() and hasPrevious() for an immutable
|
||||
* ListIterator.
|
||||
*/
|
||||
public void testUnmodifiableListIteratorIteration() {
|
||||
ListIterator listIterator = getImmutableListIterator();
|
||||
|
||||
assertTrue(!listIterator.hasPrevious());
|
||||
assertTrue(listIterator.hasNext());
|
||||
|
||||
assertEquals("a", listIterator.next());
|
||||
|
||||
assertTrue(listIterator.hasPrevious());
|
||||
assertTrue(listIterator.hasNext());
|
||||
|
||||
assertEquals("b", listIterator.next());
|
||||
|
||||
assertTrue(listIterator.hasPrevious());
|
||||
assertTrue(listIterator.hasNext());
|
||||
|
||||
assertEquals("c", listIterator.next());
|
||||
|
||||
assertTrue(listIterator.hasPrevious());
|
||||
assertTrue(listIterator.hasNext());
|
||||
|
||||
assertEquals("d", listIterator.next());
|
||||
|
||||
assertTrue(listIterator.hasPrevious());
|
||||
assertTrue(!listIterator.hasNext());
|
||||
|
||||
assertEquals("d", listIterator.previous());
|
||||
|
||||
assertTrue(listIterator.hasPrevious());
|
||||
assertTrue(listIterator.hasNext());
|
||||
|
||||
assertEquals("c", listIterator.previous());
|
||||
|
||||
assertTrue(listIterator.hasPrevious());
|
||||
assertTrue(listIterator.hasNext());
|
||||
|
||||
assertEquals("b", listIterator.previous());
|
||||
|
||||
assertTrue(listIterator.hasPrevious());
|
||||
assertTrue(listIterator.hasNext());
|
||||
|
||||
assertEquals("a", listIterator.previous());
|
||||
|
||||
assertTrue(!listIterator.hasPrevious());
|
||||
assertTrue(listIterator.hasNext());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove() for an immutable Iterator.
|
||||
*/
|
||||
public void testUnmodifiableIteratorImmutability() {
|
||||
Iterator iterator = getImmutableIterator();
|
||||
|
||||
try {
|
||||
iterator.remove();
|
||||
// We shouldn't get to here.
|
||||
fail("remove() should throw an UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// This is correct; ignore the exception.
|
||||
}
|
||||
|
||||
iterator.next();
|
||||
|
||||
try {
|
||||
iterator.remove();
|
||||
// We shouldn't get to here.
|
||||
fail("remove() should throw an UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// This is correct; ignore the exception.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove() for an immutable ListIterator.
|
||||
*/
|
||||
public void testUnmodifiableListIteratorImmutability() {
|
||||
ListIterator listIterator = getImmutableListIterator();
|
||||
|
||||
try {
|
||||
listIterator.remove();
|
||||
// We shouldn't get to here.
|
||||
fail("remove() should throw an UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// This is correct; ignore the exception.
|
||||
}
|
||||
|
||||
try {
|
||||
listIterator.set("a");
|
||||
// We shouldn't get to here.
|
||||
fail("set(Object) should throw an UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// This is correct; ignore the exception.
|
||||
}
|
||||
|
||||
try {
|
||||
listIterator.add("a");
|
||||
// We shouldn't get to here.
|
||||
fail("add(Object) should throw an UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// This is correct; ignore the exception.
|
||||
}
|
||||
|
||||
listIterator.next();
|
||||
|
||||
try {
|
||||
listIterator.remove();
|
||||
// We shouldn't get to here.
|
||||
fail("remove() should throw an UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// This is correct; ignore the exception.
|
||||
}
|
||||
|
||||
try {
|
||||
listIterator.set("a");
|
||||
// We shouldn't get to here.
|
||||
fail("set(Object) should throw an UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// This is correct; ignore the exception.
|
||||
}
|
||||
|
||||
try {
|
||||
listIterator.add("a");
|
||||
// We shouldn't get to here.
|
||||
fail("add(Object) should throw an UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// This is correct; ignore the exception.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue