From cfc1b177ef4365a5dbabc54cb5fc11b52a640387 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 6 May 2006 13:59:30 +0000 Subject: [PATCH] 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 --- RELEASE-NOTES.html | 1 + .../iterators/ListIteratorWrapper.java | 22 ++++++++++--- .../iterators/TestListIteratorWrapper.java | 31 ++++++++++++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/RELEASE-NOTES.html b/RELEASE-NOTES.html index 9b2ae1fc8..2cb5baed6 100644 --- a/RELEASE-NOTES.html +++ b/RELEASE-NOTES.html @@ -79,6 +79,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
  • ListOrderedMap - additional list-like method, setValue(int,Object)
  • ListOrderedMap - additional method, put(int,Object,Object)
  • PriorityBuffer - now Serializable [36163]
  • +
  • ListIteratorWrapper - now implements ResettableListIterator [39449]
  • IfClosure - add single argument constructor [38495]
  • All/Any/One/None Predicate - allow construction with zero or one predicates [37979]
  • diff --git a/src/java/org/apache/commons/collections/iterators/ListIteratorWrapper.java b/src/java/org/apache/commons/collections/iterators/ListIteratorWrapper.java index 1d44249b6..3d04196be 100644 --- a/src/java/org/apache/commons/collections/iterators/ListIteratorWrapper.java +++ b/src/java/org/apache/commons/collections/iterators/ListIteratorWrapper.java @@ -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. *

    @@ -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; + } + } diff --git a/src/test/org/apache/commons/collections/iterators/TestListIteratorWrapper.java b/src/test/org/apache/commons/collections/iterators/TestListIteratorWrapper.java index f913cca87..dc2aff22b 100644 --- a/src/test/org/apache/commons/collections/iterators/TestListIteratorWrapper.java +++ b/src/test/org/apache/commons/collections/iterators/TestListIteratorWrapper.java @@ -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); + } + } + +}