Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.
Also see the following revisions: ------------------------------------------------------------------------ r751857 | mbenson | 2009-03-09 14:43:53 -0700 (Mon, 09 Mar 2009) | 1 line handle more ListIterator functionality when possible ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815116 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2365356393
commit
2282bf731f
|
@ -17,7 +17,6 @@
|
|||
package org.apache.commons.collections.iterators;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
@ -34,13 +33,13 @@ import org.apache.commons.collections.ResettableListIterator;
|
|||
*
|
||||
* @author Morgan Delagrange
|
||||
*/
|
||||
public class TestListIteratorWrapper extends AbstractTestIterator {
|
||||
public class TestListIteratorWrapper<E> extends AbstractTestIterator<E> {
|
||||
|
||||
protected String[] testArray = {
|
||||
"One", "Two", "Three", "Four", "Five", "Six"
|
||||
};
|
||||
|
||||
protected List list1 = null;
|
||||
protected List<E> list1 = null;
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(TestListIteratorWrapper.class);
|
||||
|
@ -50,29 +49,28 @@ public class TestListIteratorWrapper extends AbstractTestIterator {
|
|||
super(testName);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
list1 = new ArrayList();
|
||||
list1.add("One");
|
||||
list1.add("Two");
|
||||
list1.add("Three");
|
||||
list1.add("Four");
|
||||
list1.add("Five");
|
||||
list1.add("Six");
|
||||
list1 = new ArrayList<E>();
|
||||
list1.add((E) "One");
|
||||
list1.add((E) "Two");
|
||||
list1.add((E) "Three");
|
||||
list1.add((E) "Four");
|
||||
list1.add((E) "Five");
|
||||
list1.add((E) "Six");
|
||||
}
|
||||
|
||||
public Iterator makeEmptyIterator() {
|
||||
ArrayList list = new ArrayList();
|
||||
return new ListIteratorWrapper(list.iterator());
|
||||
public ResettableListIterator<E> makeEmptyIterator() {
|
||||
ArrayList<E> list = new ArrayList<E>();
|
||||
return new ListIteratorWrapper<E>(list.iterator());
|
||||
}
|
||||
|
||||
public Iterator makeFullIterator() {
|
||||
Iterator i = list1.iterator();
|
||||
|
||||
return new ListIteratorWrapper(i);
|
||||
public ResettableListIterator<E> makeObject() {
|
||||
return new ListIteratorWrapper<E>(list1.iterator());
|
||||
}
|
||||
|
||||
public void testIterator() {
|
||||
ListIterator iter = (ListIterator) makeFullIterator();
|
||||
ListIterator<E> iter = makeObject();
|
||||
for (int i = 0; i < testArray.length; i++) {
|
||||
Object testValue = testArray[i];
|
||||
Object iterValue = iter.next();
|
||||
|
@ -92,7 +90,7 @@ public class TestListIteratorWrapper extends AbstractTestIterator {
|
|||
// now, read it backwards
|
||||
for (int i = testArray.length - 1; i > -1; --i) {
|
||||
Object testValue = testArray[i];
|
||||
Object iterValue = iter.previous();
|
||||
E iterValue = iter.previous();
|
||||
|
||||
assertEquals( "Iteration value is correct", testValue, iterValue );
|
||||
}
|
||||
|
@ -115,21 +113,107 @@ public class TestListIteratorWrapper extends AbstractTestIterator {
|
|||
}
|
||||
|
||||
public void testRemove() {
|
||||
Iterator iter = (Iterator) makeFullIterator();
|
||||
ListIterator<E> iter = makeObject();
|
||||
|
||||
//initial state:
|
||||
assertEquals(-1, iter.previousIndex());
|
||||
assertEquals(0, iter.nextIndex());
|
||||
|
||||
try {
|
||||
iter.remove();
|
||||
fail("FilterIterator does not support the remove() method");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
|
||||
fail("ListIteratorWrapper#remove() should fail; must be initially positioned first");
|
||||
} catch (IllegalStateException e) {
|
||||
}
|
||||
|
||||
//no change from invalid op:
|
||||
assertEquals(-1, iter.previousIndex());
|
||||
assertEquals(0, iter.nextIndex());
|
||||
|
||||
//establish size:
|
||||
int sz = list1.size();
|
||||
|
||||
//verify initial next() call:
|
||||
assertEquals(list1.get(0), iter.next());
|
||||
assertEquals(0, iter.previousIndex());
|
||||
assertEquals(1, iter.nextIndex());
|
||||
|
||||
//verify remove():
|
||||
iter.remove();
|
||||
assertEquals(--sz, list1.size());
|
||||
//like we never started iterating:
|
||||
assertEquals(-1, iter.previousIndex());
|
||||
assertEquals(0, iter.nextIndex());
|
||||
|
||||
try {
|
||||
iter.remove();
|
||||
fail("ListIteratorWrapper#remove() should fail; must be repositioned first");
|
||||
} catch (IllegalStateException e) {
|
||||
}
|
||||
|
||||
//no change from invalid op:
|
||||
assertEquals(-1, iter.previousIndex());
|
||||
assertEquals(0, iter.nextIndex());
|
||||
|
||||
//two consecutive next() calls:
|
||||
assertEquals(list1.get(0), iter.next());
|
||||
assertEquals(0, iter.previousIndex());
|
||||
assertEquals(1, iter.nextIndex());
|
||||
|
||||
assertEquals(list1.get(1), iter.next());
|
||||
assertEquals(1, iter.previousIndex());
|
||||
assertEquals(2, iter.nextIndex());
|
||||
|
||||
//call previous():
|
||||
assertEquals(list1.get(1), iter.previous());
|
||||
assertEquals(0, iter.previousIndex());
|
||||
assertEquals(1, iter.nextIndex());
|
||||
|
||||
//should support remove() after calling previous() once from tip because we haven't changed the underlying iterator's position:
|
||||
iter.remove();
|
||||
assertEquals(--sz, list1.size());
|
||||
assertEquals(0, iter.previousIndex());
|
||||
assertEquals(1, iter.nextIndex());
|
||||
|
||||
//dig into cache
|
||||
assertEquals(list1.get(0), iter.previous());
|
||||
assertEquals(-1, iter.previousIndex());
|
||||
assertEquals(0, iter.nextIndex());
|
||||
|
||||
try {
|
||||
iter.remove();
|
||||
fail("ListIteratorWrapper does not support the remove() method while dug into the cache via previous()");
|
||||
} catch (IllegalStateException e) {
|
||||
}
|
||||
|
||||
//no change from invalid op:
|
||||
assertEquals(-1, iter.previousIndex());
|
||||
assertEquals(0, iter.nextIndex());
|
||||
|
||||
//dig out of cache, first next() maintains current position:
|
||||
assertEquals(list1.get(0), iter.next());
|
||||
assertEquals(0, iter.previousIndex());
|
||||
assertEquals(1, iter.nextIndex());
|
||||
//continue traversing underlying iterator with this next() call, and we're out of the hole, so to speak:
|
||||
assertEquals(list1.get(1), iter.next());
|
||||
assertEquals(1, iter.previousIndex());
|
||||
assertEquals(2, iter.nextIndex());
|
||||
|
||||
//verify remove() works again:
|
||||
iter.remove();
|
||||
assertEquals(--sz, list1.size());
|
||||
assertEquals(0, iter.previousIndex());
|
||||
assertEquals(1, iter.nextIndex());
|
||||
|
||||
assertEquals(list1.get(1), iter.next());
|
||||
assertEquals(1, iter.previousIndex());
|
||||
assertEquals(2, iter.nextIndex());
|
||||
|
||||
}
|
||||
|
||||
public void testReset() {
|
||||
ResettableListIterator iter = (ResettableListIterator) makeFullIterator();
|
||||
Object first = iter.next();
|
||||
Object second = iter.next();
|
||||
ResettableListIterator<E> iter = makeObject();
|
||||
E first = iter.next();
|
||||
E second = iter.next();
|
||||
|
||||
iter.reset();
|
||||
|
||||
|
@ -143,7 +227,7 @@ public class TestListIteratorWrapper extends AbstractTestIterator {
|
|||
// after passing the point, where we resetted, continuation should work as expected
|
||||
for (int i = 2; i < testArray.length; i++) {
|
||||
Object testValue = testArray[i];
|
||||
Object iterValue = iter.next();
|
||||
E iterValue = iter.next();
|
||||
|
||||
assertEquals("Iteration value is correct", testValue, iterValue);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue