You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2003/10/09 13:05:27 UTC
cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/iterators SingletonListIterator.java SingletonIterator.java
rwaldhoff 2003/10/09 04:05:27
Modified: collections/src/test/org/apache/commons/collections/iterators
TestSingletonListIterator.java
TestSingletonIterator.java
collections/src/java/org/apache/commons/collections/iterators
SingletonListIterator.java SingletonIterator.java
Log:
* support remove in SingletonIterator and SingletonListIterator
* add tests
* enable tests
Revision Changes Path
1.7 +11 -15 jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestSingletonListIterator.java
Index: TestSingletonListIterator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestSingletonListIterator.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TestSingletonListIterator.java 1 Oct 2003 21:54:55 -0000 1.6
+++ TestSingletonListIterator.java 9 Oct 2003 11:05:27 -0000 1.7
@@ -83,13 +83,15 @@
}
/**
- * 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 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() {
1.7 +21 -17 jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestSingletonIterator.java
Index: TestSingletonIterator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestSingletonIterator.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TestSingletonIterator.java 1 Oct 2003 21:54:54 -0000 1.6
+++ TestSingletonIterator.java 9 Oct 2003 11:05:27 -0000 1.7
@@ -84,13 +84,15 @@
}
/**
- * 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 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,12 +117,21 @@
assertTrue("Iterator should now be empty", !iter.hasNext());
try {
- Object testValue = iter.next();
+ iter.next();
} catch (Exception e) {
assertTrue(
"NoSuchElementException must be thrown",
e.getClass().equals((new NoSuchElementException()).getClass()));
}
+ }
+
+ 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() {
1.7 +30 -20 jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/SingletonListIterator.java
Index: SingletonListIterator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/SingletonListIterator.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- SingletonListIterator.java 29 Sep 2003 22:02:33 -0000 1.6
+++ SingletonListIterator.java 9 Oct 2003 11:05:27 -0000 1.7
@@ -66,11 +66,13 @@
* @version $Revision$ $Date$
*
* @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 @@
}
/**
- * 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 @@
* @return 0 or 1 depending on current state.
*/
public int nextIndex() {
- return (first ? 0 : 1);
+ return (beforeFirst ? 0 : 1);
}
/**
@@ -123,7 +125,7 @@
* @return 0 or -1 depending on current state.
*/
public int previousIndex() {
- return (first ? -1 : 0);
+ return (beforeFirst ? -1 : 0);
}
/**
@@ -136,10 +138,10 @@
* 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 @@
* 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 @@
* 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 @@
* Reset the iterator back to the start.
*/
public void reset() {
- first = true;
+ beforeFirst = true;
nextCalled = false;
}
1.7 +23 -15 jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/SingletonIterator.java
Index: SingletonIterator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/SingletonIterator.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- SingletonIterator.java 29 Sep 2003 22:02:33 -0000 1.6
+++ SingletonIterator.java 9 Oct 2003 11:05:27 -0000 1.7
@@ -68,10 +68,12 @@
*
* @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 @@
}
/**
- * 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 @@
* 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;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org