scolebourne 2002/12/08 07:42:35 Modified: collections/src/test/org/apache/commons/collections TestIteratorUtils.java collections/src/java/org/apache/commons/collections IteratorUtils.java Log: Add IteratorUtils unmodifiable iterators, from Rich Dougherty Revision Changes Path 1.3 +190 -3 jakarta-commons/collections/src/test/org/apache/commons/collections/TestIteratorUtils.java Index: TestIteratorUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestIteratorUtils.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TestIteratorUtils.java 12 Oct 2002 22:36:21 -0000 1.2 +++ TestIteratorUtils.java 8 Dec 2002 15:42:35 -0000 1.3 @@ -62,7 +62,9 @@ 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 @@ 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 @@ 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. + } + } } 1.6 +135 -5 jakarta-commons/collections/src/java/org/apache/commons/collections/IteratorUtils.java Index: IteratorUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/IteratorUtils.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- IteratorUtils.java 21 Nov 2002 23:08:27 -0000 1.5 +++ IteratorUtils.java 8 Dec 2002 15:42:35 -0000 1.6 @@ -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; @@ -113,6 +114,9 @@ private IteratorUtils() { } + // Iterator creators + //---------------------------------------------------------------------- + /** * Gets an empty iterator. * <p> @@ -238,6 +242,34 @@ // 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 @@ } } - + + /** + * 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:[EMAIL PROTECTED]">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:[EMAIL PROTECTED]">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"); + } + } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>