You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/04/09 16:38:47 UTC
cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/iterators SingletonIterator.java
scolebourne 2004/04/09 07:38:47
Modified: collections/src/test/org/apache/commons/collections/iterators
TestAll.java
collections RELEASE-NOTES.html
collections/src/java/org/apache/commons/collections/iterators
SingletonIterator.java
Added: collections/src/test/org/apache/commons/collections/iterators
TestSingletonIterator2.java
Log:
SingletonIterator - make remove() functionality optional
Revision Changes Path
1.15 +2 -1 jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestAll.java
Index: TestAll.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestAll.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- TestAll.java 20 Mar 2004 00:21:08 -0000 1.14
+++ TestAll.java 9 Apr 2004 14:38:47 -0000 1.15
@@ -48,6 +48,7 @@
suite.addTest(TestListIteratorWrapper.suite());
suite.addTest(TestLoopingIterator.suite());
suite.addTest(TestSingletonIterator.suite());
+ suite.addTest(TestSingletonIterator2.suite());
suite.addTest(TestSingletonListIterator.suite());
suite.addTest(TestObjectGraphIterator.suite());
suite.addTest(TestUniqueFilterIterator.suite());
1.1 jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestSingletonIterator2.java
Index: TestSingletonIterator2.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.iterators;
import java.util.Iterator;
import java.util.NoSuchElementException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.ResettableIterator;
/**
* Tests the SingletonIterator to ensure that the next() method will actually
* perform the iteration rather than the hasNext() method.
*
* @version $Revision: 1.1 $ $Date: 2004/04/09 14:38:47 $
*
* @author James Strachan
*/
public class TestSingletonIterator2 extends AbstractTestIterator {
private static final Object testValue = "foo";
public static Test suite() {
return new TestSuite(TestSingletonIterator2.class);
}
public TestSingletonIterator2(String testName) {
super(testName);
}
//-----------------------------------------------------------------------
public Iterator makeEmptyIterator() {
SingletonIterator iter = new SingletonIterator(testValue);
iter.next();
iter.remove();
iter.reset();
return iter;
}
public Iterator makeFullIterator() {
return new SingletonIterator(testValue, false);
}
public boolean supportsRemove() {
return false;
}
public boolean supportsEmptyIterator() {
return false;
}
//-----------------------------------------------------------------------
public void testIterator() {
Iterator iter = (Iterator) makeObject();
assertTrue("Iterator has a first item", iter.hasNext());
Object iterValue = iter.next();
assertEquals("Iteration value is correct", testValue, iterValue);
assertTrue("Iterator should now be empty", !iter.hasNext());
try {
iter.next();
} catch (Exception e) {
assertTrue(
"NoSuchElementException must be thrown",
e.getClass().equals((new NoSuchElementException()).getClass()));
}
}
public void testReset() {
ResettableIterator it = (ResettableIterator) makeObject();
assertEquals(true, it.hasNext());
assertEquals(testValue, it.next());
assertEquals(false, it.hasNext());
it.reset();
assertEquals(true, it.hasNext());
assertEquals(testValue, it.next());
assertEquals(false, it.hasNext());
it.reset();
it.reset();
assertEquals(true, it.hasNext());
}
}
1.30 +1 -0 jakarta-commons/collections/RELEASE-NOTES.html
Index: RELEASE-NOTES.html
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/RELEASE-NOTES.html,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -r1.29 -r1.30
--- RELEASE-NOTES.html 9 Apr 2004 14:35:10 -0000 1.29
+++ RELEASE-NOTES.html 9 Apr 2004 14:38:47 -0000 1.30
@@ -40,6 +40,7 @@
<li>Functors - Add additional getInstance() methods for consistency [27856,27857]</li>
<li>CollectionUtils - get(Object,int) method now supports primitive arrays</li>
<li>CollectionUtils - Add size(Object) method to find the size of various collection-like objects [27909]</li>
+<li>SingletonIterator - make remove() functionality optional</li>
</ul>
<h4>Made Serializable</h4>
1.13 +32 -7 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.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- SingletonIterator.java 18 Feb 2004 00:59:50 -0000 1.12
+++ SingletonIterator.java 9 Apr 2004 14:38:47 -0000 1.13
@@ -34,20 +34,39 @@
public class SingletonIterator
implements Iterator, ResettableIterator {
+ /** Whether remove is allowed */
+ private final boolean removeAllowed;
+ /** Is the cursor before the first element */
private boolean beforeFirst = true;
+ /** Has the element been removed */
private boolean removed = false;
+ /** The object */
private Object object;
/**
- * Constructs a new <code>SingletonIterator</code>.
+ * Constructs a new <code>SingletonIterator</code> where <code>remove</code>
+ * is a permitted operation.
*
* @param object the single object to return from the iterator
*/
public SingletonIterator(Object object) {
+ this(object, true);
+ }
+
+ /**
+ * Constructs a new <code>SingletonIterator</code> optionally choosing if
+ * <code>remove</code> is a permitted operation.
+ *
+ * @param object the single object to return from the iterator
+ * @param removeAllowed true if remove is allowed
+ */
+ public SingletonIterator(Object object, boolean removeAllowed) {
super();
this.object = object;
+ this.removeAllowed = removeAllowed;
}
+ //-----------------------------------------------------------------------
/**
* Is another object available from the iterator?
* <p>
@@ -78,17 +97,23 @@
/**
* 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.
+ * @throws UnsupportedOperationException if remove is not supported
*/
- public void remove() {
- if(removed || beforeFirst) {
- throw new IllegalStateException();
+ public void remove() {
+ if (removeAllowed) {
+ if (removed || beforeFirst) {
+ throw new IllegalStateException();
+ } else {
+ object = null;
+ removed = true;
+ }
} else {
- object = null;
- removed = true;
+ throw new UnsupportedOperationException();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org