* support remove in SingletonIterator and SingletonListIterator

* add tests
* enable tests


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131263 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Rodney Waldhoff 2003-10-09 11:05:27 +00:00
parent 96e2ca244e
commit 8a083ab8af
4 changed files with 85 additions and 67 deletions

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/SingletonIterator.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/SingletonIterator.java,v 1.7 2003/10/09 11:05:27 rwaldhoff Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -64,14 +64,16 @@ import java.util.NoSuchElementException;
* object instance.</p>
*
* @since Commons Collections 2.0
* @version $Revision: 1.6 $ $Date: 2003/09/29 22:02:33 $
* @version $Revision: 1.7 $ $Date: 2003/10/09 11:05:27 $
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author Stephen Colebourne
* @author Rodney Waldhoff
*/
public class SingletonIterator implements ResetableIterator {
private boolean first = true;
private boolean beforeFirst = true;
private boolean removed = false;
private Object object;
/**
@ -85,14 +87,14 @@ public class SingletonIterator implements ResetableIterator {
}
/**
* Is another object available from the iterator.
* Is another object available from the iterator?
* <p>
* 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 <tt>next</tt> method has not
* yet been called, or the <tt>remove</tt> method has already
* been called after the last call to the <tt>next</tt>
* 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;
}
}

View File

@ -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.</p>
*
* @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?
* <p>
* 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?
* <p>
* 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 <tt>next</tt> or <tt>previous</tt>
* method has not yet been called, or the <tt>remove</tt> method
* has already been called after the last call to <tt>next</tt>
* or <tt>previous</tt>.
*/
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 <tt>next</tt> has not been called
* @throws IllegalStateException if <tt>next</tt> 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;
}

View File

@ -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();

View File

@ -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() {