From 8a083ab8af46eb4f4bb9e0796b1d62e35b1441eb Mon Sep 17 00:00:00 2001
From: Rodney Waldhoff
* This returns true if the single object hasn't been returned yet. * * @return true if the single object hasn't been returned yet */ public boolean hasNext() { - return first; + return (beforeFirst && !removed); } /** @@ -105,28 +107,34 @@ public class SingletonIterator implements ResetableIterator { * been returned */ public Object next() { - if (!first) { + if (!beforeFirst || removed) { throw new NoSuchElementException(); } - Object answer = object; - first = false; - return answer; + beforeFirst = false; + return object; } /** - * Remove always throws {@link UnsupportedOperationException}. - * - * @throws UnsupportedOperationException always + * Remove the object from this iterator. + * @throws IllegalStateException if the next method has not + * yet been called, or the remove method has already + * been called after the last call to the next + * method. */ - public void remove() { - throw new UnsupportedOperationException("remove() is not supported by this iterator"); + public void remove() { + if(removed || beforeFirst) { + throw new IllegalStateException(); + } else { + object = null; + removed = true; + } } /** * Reset the iterator to the start. */ public void reset() { - first = true; + beforeFirst = true; } } diff --git a/src/java/org/apache/commons/collections/iterators/SingletonListIterator.java b/src/java/org/apache/commons/collections/iterators/SingletonListIterator.java index 8d45bc6a8..4d5acf18b 100644 --- a/src/java/org/apache/commons/collections/iterators/SingletonListIterator.java +++ b/src/java/org/apache/commons/collections/iterators/SingletonListIterator.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/SingletonListIterator.java,v 1.6 2003/09/29 22:02:33 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/SingletonListIterator.java,v 1.7 2003/10/09 11:05:27 rwaldhoff Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -63,14 +63,16 @@ import java.util.NoSuchElementException; * object instance.
* * @since Commons Collections 2.1 - * @version $Revision: 1.6 $ $Date: 2003/09/29 22:02:33 $ + * @version $Revision: 1.7 $ $Date: 2003/10/09 11:05:27 $ * * @author Stephen Colebourne + * @author Rodney Waldhoff */ public class SingletonListIterator implements ResetableListIterator { - private boolean first = true; + private boolean beforeFirst = true; private boolean nextCalled = false; + private boolean removed = false; private Object object; /** @@ -84,25 +86,25 @@ public class SingletonListIterator implements ResetableListIterator { } /** - * Is another object available from the iterator. + * Is another object available from the iterator? ** This returns true if the single object hasn't been returned yet. * * @return true if the single object hasn't been returned yet */ public boolean hasNext() { - return first; + return beforeFirst && !removed; } /** - * Is a previous object available from the iterator. + * Is a previous object available from the iterator? *
* This returns true if the single object has been returned. * * @return true if the single object has been returned */ public boolean hasPrevious() { - return !first; + return !beforeFirst && !removed; } /** @@ -112,7 +114,7 @@ public class SingletonListIterator implements ResetableListIterator { * @return 0 or 1 depending on current state. */ public int nextIndex() { - return (first ? 0 : 1); + return (beforeFirst ? 0 : 1); } /** @@ -123,7 +125,7 @@ public class SingletonListIterator implements ResetableListIterator { * @return 0 or -1 depending on current state. */ public int previousIndex() { - return (first ? -1 : 0); + return (beforeFirst ? -1 : 0); } /** @@ -136,10 +138,10 @@ public class SingletonListIterator implements ResetableListIterator { * been returned */ public Object next() { - if (!first) { + if (!beforeFirst || removed) { throw new NoSuchElementException(); } - first = false; + beforeFirst = false; nextCalled = true; return object; } @@ -154,20 +156,27 @@ public class SingletonListIterator implements ResetableListIterator { * been returned */ public Object previous() { - if (first) { + if (beforeFirst || removed) { throw new NoSuchElementException(); } - first = true; + beforeFirst = true; return object; } /** - * Remove always throws {@link UnsupportedOperationException}. - * - * @throws UnsupportedOperationException always + * Remove the object from this iterator. + * @throws IllegalStateException if the next or previous + * method has not yet been called, or the remove method + * has already been called after the last call to next + * or previous. */ public void remove() { - throw new UnsupportedOperationException("remove() is not supported by this iterator"); + if(!nextCalled || removed) { + throw new IllegalStateException(); + } else { + object = null; + removed = true; + } } /** @@ -183,10 +192,11 @@ public class SingletonListIterator implements ResetableListIterator { * Set sets the value of the singleton. * * @param obj the object to set - * @throws IllegalStateException if next has not been called + * @throws IllegalStateException if next has not been called + * or the object has been removed */ public void set(Object obj) { - if (nextCalled == false) { + if (!nextCalled || removed) { throw new IllegalStateException(); } this.object = obj; @@ -196,7 +206,7 @@ public class SingletonListIterator implements ResetableListIterator { * Reset the iterator back to the start. */ public void reset() { - first = true; + beforeFirst = true; nextCalled = false; } diff --git a/src/test/org/apache/commons/collections/iterators/TestSingletonIterator.java b/src/test/org/apache/commons/collections/iterators/TestSingletonIterator.java index 7cb4f4311..94992d40c 100644 --- a/src/test/org/apache/commons/collections/iterators/TestSingletonIterator.java +++ b/src/test/org/apache/commons/collections/iterators/TestSingletonIterator.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestSingletonIterator.java,v 1.6 2003/10/01 21:54:54 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestSingletonIterator.java,v 1.7 2003/10/09 11:05:27 rwaldhoff Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -67,7 +67,7 @@ import junit.framework.TestSuite; * Tests the SingletonIterator to ensure that the next() method will actually * perform the iteration rather than the hasNext() method. * - * @version $Revision: 1.6 $ $Date: 2003/10/01 21:54:54 $ + * @version $Revision: 1.7 $ $Date: 2003/10/09 11:05:27 $ * * @author James Strachan */ @@ -84,13 +84,15 @@ public class TestSingletonIterator extends AbstractTestIterator { } /** - * Returns null. SingletonIterators can never be empty; - * they always have exactly one element. - * - * @return null + * Returns a SingletonIterator from which + * the element has already been removed. */ public Iterator makeEmptyIterator() { - return null; + SingletonIterator iter = (SingletonIterator)makeFullIterator(); + iter.next(); + iter.remove(); + iter.reset(); + return iter; } public Iterator makeFullIterator() { @@ -98,18 +100,11 @@ public class TestSingletonIterator extends AbstractTestIterator { } public boolean supportsRemove() { - return false; + return true; } - - /** - * Whether or not we are testing an iterator that can be - * empty. SingletonIterators are never empty; - * - * @return false - */ public boolean supportsEmptyIterator() { - return false; + return true; } public void testIterator() { @@ -122,7 +117,7 @@ public class TestSingletonIterator extends AbstractTestIterator { assertTrue("Iterator should now be empty", !iter.hasNext()); try { - Object testValue = iter.next(); + iter.next(); } catch (Exception e) { assertTrue( "NoSuchElementException must be thrown", @@ -130,6 +125,15 @@ public class TestSingletonIterator extends AbstractTestIterator { } } + public void testSingletonIteratorRemove() { + ResetableIterator iter = new SingletonIterator("xyzzy"); + assertTrue(iter.hasNext()); + assertEquals("xyzzy",iter.next()); + iter.remove(); + iter.reset(); + assertTrue(! iter.hasNext()); + } + public void testReset() { ResetableIterator it = (ResetableIterator) makeObject(); diff --git a/src/test/org/apache/commons/collections/iterators/TestSingletonListIterator.java b/src/test/org/apache/commons/collections/iterators/TestSingletonListIterator.java index d07ab8583..ec88839c1 100644 --- a/src/test/org/apache/commons/collections/iterators/TestSingletonListIterator.java +++ b/src/test/org/apache/commons/collections/iterators/TestSingletonListIterator.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestSingletonListIterator.java,v 1.6 2003/10/01 21:54:55 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestSingletonListIterator.java,v 1.7 2003/10/09 11:05:27 rwaldhoff Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -66,7 +66,7 @@ import junit.framework.TestSuite; /** * Tests the SingletonListIterator. * - * @version $Revision: 1.6 $ $Date: 2003/10/01 21:54:55 $ + * @version $Revision: 1.7 $ $Date: 2003/10/09 11:05:27 $ * * @author Stephen Colebourne */ @@ -83,13 +83,15 @@ public class TestSingletonListIterator extends AbstractTestListIterator { } /** - * Returns null. SingletonListIterator can never be empty; - * they always have exactly one element. - * - * @return null + * Returns a SingletonListIterator from which + * the element has already been removed. */ public ListIterator makeEmptyListIterator() { - return null; + SingletonListIterator iter = (SingletonListIterator)makeFullIterator(); + iter.next(); + iter.remove(); + iter.reset(); + return iter; } public ListIterator makeFullListIterator() { @@ -101,17 +103,11 @@ public class TestSingletonListIterator extends AbstractTestListIterator { } public boolean supportsRemove() { - return false; + return true; } - /** - * Whether or not we are testing an iterator that can be - * empty. SingletonIterators are never empty; - * - * @return false - */ public boolean supportsEmptyIterator() { - return false; + return true; } public void testIterator() {