Author: scolebourne Date: Sat May 6 06:59:30 2006 New Revision: 400314 URL: http://svn.apache.org/viewcvs?rev=400314&view=rev Log: Make ListIteratorWrapper resettable rfe 39449, from Thomas Schapitz
Modified: jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/ListIteratorWrapper.java jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestListIteratorWrapper.java Modified: jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html?rev=400314&r1=400313&r2=400314&view=diff ============================================================================== --- jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html (original) +++ jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html Sat May 6 06:59:30 2006 @@ -79,6 +79,7 @@ <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> Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/ListIteratorWrapper.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/ListIteratorWrapper.java?rev=400314&r1=400313&r2=400314&view=diff ============================================================================== --- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/ListIteratorWrapper.java (original) +++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/ListIteratorWrapper.java Sat May 6 06:59:30 2006 @@ -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 @@ * @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 @@ /** 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; @@ -174,6 +176,16 @@ */ public void set(Object obj) throws UnsupportedOperationException { 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; } } Modified: jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestListIteratorWrapper.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestListIteratorWrapper.java?rev=400314&r1=400313&r2=400314&view=diff ============================================================================== --- jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestListIteratorWrapper.java (original) +++ jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestListIteratorWrapper.java Sat May 6 06:59:30 2006 @@ -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 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 @@ 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 @@ } 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 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); + } + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]