Make ListIteratorWrapper resettable

rfe 39449, from Thomas Schapitz

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@400314 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-05-06 13:59:30 +00:00
parent daccd005b3
commit cfc1b177ef
3 changed files with 45 additions and 9 deletions

View File

@ -79,6 +79,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
<li>ListOrderedMap - additional list-like method, setValue(int,Object)</li>
<li>ListOrderedMap - additional method, put(int,Object,Object)</li>
<li>PriorityBuffer - now Serializable [36163]</li>
<li>ListIteratorWrapper - now implements ResettableListIterator [39449]</li>
<li>IfClosure - add single argument constructor [38495]</li>
<li>All/Any/One/None Predicate - allow construction with zero or one predicates [37979]</li>
</ul>

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2004 The Apache Software Foundation
* Copyright 1999-2006 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.
@ -15,11 +15,13 @@
*/
package org.apache.commons.collections.iterators;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.List;
import java.util.NoSuchElementException;
import org.apache.commons.collections.ResettableListIterator;
/**
* Converts an iterator into a list iterator by caching the returned entries.
* <p>
@ -37,7 +39,7 @@ import java.util.NoSuchElementException;
* @author Morgan Delagrange
* @author Stephen Colebourne
*/
public class ListIteratorWrapper implements ListIterator {
public class ListIteratorWrapper implements ResettableListIterator {
/** Message used when remove, set or add are called. */
private static final String UNSUPPORTED_OPERATION_MESSAGE =
@ -46,7 +48,7 @@ public class ListIteratorWrapper implements ListIterator {
/** The underlying iterator being decorated. */
private final Iterator iterator;
/** The list being used to cache the iterator. */
private final LinkedList list = new LinkedList();
private final List list = new ArrayList();
/** The current index of this iterator. */
private int currentIndex = 0;
@ -176,4 +178,14 @@ public class ListIteratorWrapper implements ListIterator {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION_MESSAGE);
}
// ResettableIterator interface
//-------------------------------------------------------------------------
/**
* Resets this iterator back to the position at which the iterator
* was created.
*/
public void reset() {
currentIndex = 0;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2004 The Apache Software Foundation
* Copyright 2001-2006 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.
@ -23,6 +23,7 @@ import java.util.NoSuchElementException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.ResettableListIterator;
/**
* Tests the ListIteratorWrapper to insure that it simulates
@ -81,7 +82,7 @@ public class TestListIteratorWrapper 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",
e.getClass().equals((new NoSuchElementException()).getClass()));
@ -96,7 +97,7 @@ public class TestListIteratorWrapper extends AbstractTestIterator {
}
try {
Object testValue = iter.previous();
iter.previous();
} catch (Exception e) {
assertTrue("NoSuchElementException must be thrown",
e.getClass().equals((new NoSuchElementException()).getClass()));
@ -124,5 +125,27 @@ public class TestListIteratorWrapper extends AbstractTestIterator {
}
}
public void testReset() {
ResettableListIterator iter = (ResettableListIterator) makeFullIterator();
Object first = iter.next();
Object second = iter.next();
iter.reset();
// after reset, there shouldn't be any previous elements
assertFalse("No previous elements after reset()", iter.hasPrevious());
// after reset, the results should be the same as before
assertEquals("First element should be the same", first, iter.next());
assertEquals("Second elment should be the same", second, iter.next());
// 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();
assertEquals("Iteration value is correct", testValue, iterValue);
}
}
}