Add OrderedIterator

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131340 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-11-08 19:26:29 +00:00
parent 30a04d30ce
commit 6941ba1b29
6 changed files with 265 additions and 206 deletions

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/IteratorUtils.java,v 1.15 2003/11/08 18:43:12 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/IteratorUtils.java,v 1.16 2003/11/08 19:26:28 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -57,7 +57,6 @@
*/
package org.apache.commons.collections;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
@ -81,15 +80,20 @@ import org.apache.commons.collections.iterators.IteratorChain;
import org.apache.commons.collections.iterators.IteratorEnumeration;
import org.apache.commons.collections.iterators.ListIteratorWrapper;
import org.apache.commons.collections.iterators.LoopingIterator;
import org.apache.commons.collections.iterators.MapIterator;
import org.apache.commons.collections.iterators.ObjectArrayIterator;
import org.apache.commons.collections.iterators.ObjectArrayListIterator;
import org.apache.commons.collections.iterators.ResetableIterator;
import org.apache.commons.collections.iterators.ResetableListIterator;
import org.apache.commons.collections.iterators.ResetableMapIterator;
import org.apache.commons.collections.iterators.ResetableOrderedIterator;
import org.apache.commons.collections.iterators.ResetableOrderedMapIterator;
import org.apache.commons.collections.iterators.SingletonIterator;
import org.apache.commons.collections.iterators.SingletonListIterator;
import org.apache.commons.collections.iterators.TransformIterator;
import org.apache.commons.collections.iterators.UnmodifiableIterator;
import org.apache.commons.collections.iterators.UnmodifiableListIterator;
import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
/**
* Provides static utility methods and decorators for {@link Iterator}
@ -97,7 +101,7 @@ import org.apache.commons.collections.iterators.TransformIterator;
* {@link org.apache.commons.collections.iterators} subpackage.
*
* @since Commons Collections 2.1
* @version $Revision: 1.15 $ $Date: 2003/11/08 18:43:12 $
* @version $Revision: 1.16 $ $Date: 2003/11/08 19:26:28 $
*
* @author Stephen Colebourne
* @author Phil Steitz
@ -114,12 +118,16 @@ public class IteratorUtils {
* A list iterator over no elements
*/
public static final ResetableListIterator EMPTY_LIST_ITERATOR = new EmptyListIterator();
/**
* An ordered iterator over no elements
*/
public static final ResetableOrderedIterator EMPTY_ORDERED_ITERATOR = new EmptyOrderedIterator();
/**
* A map iterator over no elements
*/
public static final ResetableMapIterator EMPTY_MAP_ITERATOR = new EmptyMapIterator();
/**
* A map iterator over no elements
* An ordered map iterator over no elements
*/
public static final ResetableOrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR = new EmptyOrderedMapIterator();
@ -156,13 +164,25 @@ public class IteratorUtils {
return EMPTY_LIST_ITERATOR;
}
/**
* Gets an empty ordered iterator.
* <p>
* This iterator is a valid iterator object that will iterate
* over nothing.
*
* @return an ordered iterator over nothing
*/
public static ResetableOrderedIterator emptyOrderedIterator() {
return EMPTY_ORDERED_ITERATOR;
}
/**
* Gets an empty map iterator.
* <p>
* This iterator is a valid map iterator object that will iterate
* over nothing.
*
* @return a list iterator over nothing
* @return a map iterator over nothing
*/
public static ResetableMapIterator emptyMapIterator() {
return EMPTY_MAP_ITERATOR;
@ -174,7 +194,7 @@ public class IteratorUtils {
* This iterator is a valid map iterator object that will iterate
* over nothing.
*
* @return a list iterator over nothing
* @return a map iterator over nothing
*/
public static ResetableOrderedMapIterator emptyOrderedMapIterator() {
return EMPTY_ORDERED_MAP_ITERATOR;
@ -403,10 +423,7 @@ public class IteratorUtils {
* @return an immutable version of the iterator
*/
public static Iterator unmodifiableIterator(Iterator iterator) {
if (iterator instanceof ResetableIterator) {
return new ResetableUnmodifiableIterator((ResetableIterator) iterator);
}
return new UnmodifiableIterator(iterator);
return UnmodifiableIterator.decorate(iterator);
}
/**
@ -419,10 +436,19 @@ public class IteratorUtils {
* @return an immutable version of the iterator
*/
public static ListIterator unmodifiableListIterator(ListIterator listIterator) {
if (listIterator instanceof ResetableListIterator) {
return new ResetableUnmodifiableListIterator((ResetableListIterator) listIterator);
}
return new UnmodifiableListIterator(listIterator);
return UnmodifiableListIterator.decorate(listIterator);
}
/**
* Gets an immutable version of a {@link MapIterator}. The returned object
* will always throw an {@link UnsupportedOperationException} for
* the {@link Iterator#remove}, {@link MapIterator#setValue(Object)} methods.
*
* @param mapIterator the iterator to make immutable
* @return an immutable version of the iterator
*/
public static MapIterator unmodifiableMapIterator(MapIterator mapIterator) {
return UnmodifiableMapIterator.decorate(mapIterator);
}
/**
@ -857,6 +883,25 @@ public class IteratorUtils {
}
}
//-----------------------------------------------------------------------
/**
* EmptyOrderedIterator class
*/
static class EmptyOrderedIterator extends EmptyIterator implements ResetableOrderedIterator {
EmptyOrderedIterator() {
super();
}
public boolean hasPrevious() {
return false;
}
public Object previous() {
throw new NoSuchElementException("Iterator contains no elements");
}
}
//-----------------------------------------------------------------------
/**
* EmptyMapIterator class
@ -899,158 +944,4 @@ public class IteratorUtils {
}
}
//-----------------------------------------------------------------------
/**
* A wrapper for an {@link java.util.Iterator} which makes it immutable. All
* calls are passed through to the delegate. The {@link #remove()} method
* always throws an {@link java.lang.UnsupportedOperationException}.
*
* @author <a href="mailto:rich@rd.gen.nz">Rich Dougherty</a>
* @author Stephen Colebourne
*/
static class UnmodifiableIterator implements Iterator, Serializable {
/**
* All calls to this iterator are passed to the delegate.
*/
protected Iterator delegate;
/**
* Create an UnmodifiableIterator.
*
* @param delegate the delegate to pass all calls to
*/
public UnmodifiableIterator(Iterator delegate) {
this.delegate = delegate;
}
public boolean hasNext() {
return delegate.hasNext();
}
public Object next() {
return delegate.next();
}
public void remove() {
throw new UnsupportedOperationException("This iterator is immutable");
}
}
//-----------------------------------------------------------------------
/**
* An unmodifiable resetable iterator.
*
* @author Stephen Colebourne
*/
static class ResetableUnmodifiableIterator extends UnmodifiableIterator implements ResetableIterator {
/**
* Create a ResetableUnmodifiableIterator.
*
* @param delegate the delegate to pass all calls to
*/
public ResetableUnmodifiableIterator(ResetableIterator delegate) {
super(delegate);
}
/**
* Reset the iterator
*/
public void reset() {
((ResetableIterator) delegate).reset();
}
}
//-----------------------------------------------------------------------
/**
* A wrapper for an {@link java.util.ListIterator} which makes it immutable.
* All calls are passed through to the delegate. The {@link #remove()},
* {@link #add(Object)} and (@link #set(Object)} methods always throw an
* {@link java.lang.UnsupportedOperationException}.
*
* @author <a href="mailto:rich@rd.gen.nz">Rich Dougherty</a>
*/
static class UnmodifiableListIterator
implements ListIterator, Serializable {
/**
* All calls to this iterator are passed to the delegate.
*/
protected ListIterator delegate;
/**
* Create an UnmodifiableListIterator.
*
* @param delegate The delegate to pass all calls to.
*/
public UnmodifiableListIterator(ListIterator delegate) {
this.delegate = delegate;
}
public boolean hasNext() {
return delegate.hasNext();
}
public Object next() {
return delegate.next();
}
public boolean hasPrevious() {
return delegate.hasPrevious();
}
public Object previous() {
return delegate.previous();
}
public int nextIndex() {
return delegate.nextIndex();
}
public int previousIndex() {
return delegate.previousIndex();
}
public void remove() {
throw new UnsupportedOperationException("This iterator is immutable");
}
public void set(Object o) {
throw new UnsupportedOperationException("This iterator is immutable");
}
public void add(Object o) {
throw new UnsupportedOperationException("This iterator is immutable");
}
}
//-----------------------------------------------------------------------
/**
* An unmodifiable resetable list iterator.
*
* @author Stephen Colebourne
*/
static class ResetableUnmodifiableListIterator extends UnmodifiableListIterator implements ResetableListIterator {
/**
* Create a ResetableUnmodifiableListIterator.
*
* @param delegate the delegate to pass all calls to
*/
public ResetableUnmodifiableListIterator(ResetableListIterator delegate) {
super(delegate);
}
/**
* Reset the iterator
*/
public void reset() {
((ResetableListIterator) delegate).reset();
}
}
}

View File

@ -0,0 +1,89 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/Attic/OrderedIterator.java,v 1.1 2003/11/08 19:26:28 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.iterators;
import java.util.Iterator;
/**
* Defines an iterator that operates over a ordered collections.
* <p>
* This iterator allows both forward and reverse iteration through the collection.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/08 19:26:28 $
*
* @author Stephen Colebourne
*/
public interface OrderedIterator extends Iterator {
/**
* Checks to see if there is a previous entry that can be iterated to.
*
* @return <code>true</code> if the iterator has a previous element
*/
boolean hasPrevious();
/**
* Gets the previous element from the collection.
*
* @return the previous key in the iteration
* @throws NoSuchElementException if the iteration is finished
*/
Object previous();
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/Attic/OrderedMapIterator.java,v 1.1 2003/11/08 18:43:12 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/Attic/OrderedMapIterator.java,v 1.2 2003/11/08 19:26:28 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -63,11 +63,11 @@ package org.apache.commons.collections.iterators;
* This iterator allows both forward and reverse iteration through the map.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/08 18:43:12 $
* @version $Revision: 1.2 $ $Date: 2003/11/08 19:26:28 $
*
* @author Stephen Colebourne
*/
public interface OrderedMapIterator extends MapIterator {
public interface OrderedMapIterator extends MapIterator, OrderedIterator {
/**
* Checks to see if there is a previous entry that can be iterated to.

View File

@ -0,0 +1,77 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/Attic/ResetableOrderedIterator.java,v 1.1 2003/11/08 19:26:28 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.iterators;
/**
* Interface implemented by those ordered iterators that can be reset back
* to an initial state.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/08 19:26:28 $
*
* @author Stephen Colebourne
*/
public interface ResetableOrderedIterator extends OrderedIterator, ResetableIterator {
/**
* Resets the iterator back to the position at which the iterator
* was created.
*/
public void reset();
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/Attic/ResetableOrderedMapIterator.java,v 1.1 2003/11/08 18:43:13 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/Attic/ResetableOrderedMapIterator.java,v 1.2 2003/11/08 19:26:28 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -62,11 +62,12 @@ package org.apache.commons.collections.iterators;
* to an initial state.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/08 18:43:13 $
* @version $Revision: 1.2 $ $Date: 2003/11/08 19:26:28 $
*
* @author Stephen Colebourne
*/
public interface ResetableOrderedMapIterator extends OrderedMapIterator, ResetableMapIterator {
public interface ResetableOrderedMapIterator
extends OrderedMapIterator, ResetableMapIterator, ResetableOrderedIterator {
/**
* Resets the iterator back to the position at which the iterator

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestIteratorUtils.java,v 1.9 2003/11/08 18:47:38 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestIteratorUtils.java,v 1.10 2003/11/08 19:26:29 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -67,16 +67,18 @@ import java.util.NoSuchElementException;
import junit.framework.Test;
import org.apache.commons.collections.iterators.MapIterator;
import org.apache.commons.collections.iterators.OrderedIterator;
import org.apache.commons.collections.iterators.OrderedMapIterator;
import org.apache.commons.collections.iterators.ResetableIterator;
import org.apache.commons.collections.iterators.ResetableListIterator;
import org.apache.commons.collections.iterators.ResetableMapIterator;
import org.apache.commons.collections.iterators.ResetableOrderedIterator;
import org.apache.commons.collections.iterators.ResetableOrderedMapIterator;
/**
* Tests for IteratorUtils.
*
* @version $Revision: 1.9 $ $Date: 2003/11/08 18:47:38 $
* @version $Revision: 1.10 $ $Date: 2003/11/08 19:26:29 $
*
* @author Unknown
*/
@ -521,6 +523,34 @@ public class TestIteratorUtils extends BulkTest {
} catch (IllegalStateException ex) {}
}
//-----------------------------------------------------------------------
/**
* Test empty map iterator
*/
public void testEmptyOrderedIterator() {
assertTrue(IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof Iterator);
assertTrue(IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof OrderedIterator);
assertTrue(IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof ResetableIterator);
assertTrue(IteratorUtils.EMPTY_ORDERED_ITERATOR instanceof ResetableOrderedIterator);
assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR.hasNext());
assertEquals(false, IteratorUtils.EMPTY_ORDERED_ITERATOR.hasPrevious());
IteratorUtils.EMPTY_ORDERED_ITERATOR.reset();
assertSame(IteratorUtils.EMPTY_ORDERED_ITERATOR, IteratorUtils.EMPTY_ORDERED_ITERATOR);
assertSame(IteratorUtils.EMPTY_ORDERED_ITERATOR, IteratorUtils.emptyOrderedIterator());
try {
IteratorUtils.EMPTY_ORDERED_ITERATOR.next();
fail();
} catch (NoSuchElementException ex) {}
try {
IteratorUtils.EMPTY_ORDERED_ITERATOR.previous();
fail();
} catch (NoSuchElementException ex) {}
try {
IteratorUtils.EMPTY_ORDERED_ITERATOR.remove();
fail();
} catch (IllegalStateException ex) {}
}
//-----------------------------------------------------------------------
/**
* Test empty map iterator
@ -531,6 +561,7 @@ public class TestIteratorUtils extends BulkTest {
assertTrue(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof OrderedMapIterator);
assertTrue(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ResetableIterator);
assertTrue(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ResetableMapIterator);
assertTrue(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ResetableOrderedIterator);
assertTrue(IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR instanceof ResetableOrderedMapIterator);
assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.hasNext());
assertEquals(false, IteratorUtils.EMPTY_ORDERED_MAP_ITERATOR.hasPrevious());
@ -589,21 +620,6 @@ public class TestIteratorUtils extends BulkTest {
assertTrue(!iterator.hasNext());
}
/**
* Test resetability
*/
public void testResetableUnmodifiableIterator() {
Integer four = new Integer(4);
ResetableIterator it = (ResetableIterator)
IteratorUtils.unmodifiableIterator(IteratorUtils.singletonIterator(four));
assertEquals(true, it.hasNext());
assertSame(four, it.next());
assertEquals(false, it.hasNext());
it.reset();
assertEquals(true, it.hasNext());
}
/**
* Test next(), hasNext(), previous() and hasPrevious() for an immutable
* ListIterator.
@ -655,21 +671,6 @@ public class TestIteratorUtils extends BulkTest {
assertTrue(listIterator.hasNext());
}
/**
* Test resetability
*/
public void testResetableUnmodifiableListIterator() {
Integer four = new Integer(4);
ResetableListIterator it = (ResetableListIterator)
IteratorUtils.unmodifiableListIterator(IteratorUtils.singletonListIterator(four));
assertEquals(true, it.hasNext());
assertSame(four, it.next());
assertEquals(false, it.hasNext());
it.reset();
assertEquals(true, it.hasNext());
}
/**
* Test remove() for an immutable Iterator.
*/