2002-08-17 07:38:53 -04:00
|
|
|
/*
|
2003-09-29 18:44:14 -04:00
|
|
|
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/IteratorUtils.java,v 1.13 2003/09/29 22:44:14 scolebourne Exp $
|
2002-08-17 07:38:53 -04:00
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* The Apache Software License, Version 1.1
|
|
|
|
*
|
2003-05-16 10:40:56 -04:00
|
|
|
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
|
2002-08-17 07:38:53 -04:00
|
|
|
* 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
|
2003-08-31 13:30:27 -04:00
|
|
|
* any, must include the following acknowledgement:
|
2002-08-17 07:38:53 -04:00
|
|
|
* "This product includes software developed by the
|
|
|
|
* Apache Software Foundation (http://www.apache.org/)."
|
2003-08-31 13:30:27 -04:00
|
|
|
* Alternately, this acknowledgement may appear in the software itself,
|
|
|
|
* if and wherever such third-party acknowledgements normally appear.
|
2002-08-17 07:38:53 -04:00
|
|
|
*
|
|
|
|
* 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
|
2003-01-10 15:21:25 -05:00
|
|
|
* permission of the Apache Software Foundation.
|
2002-08-17 07:38:53 -04:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2002-12-08 10:42:35 -05:00
|
|
|
import java.io.Serializable;
|
2002-08-17 18:14:22 -04:00
|
|
|
import java.lang.reflect.Array;
|
2002-08-17 07:38:53 -04:00
|
|
|
import java.lang.reflect.Method;
|
2002-08-17 18:14:22 -04:00
|
|
|
import java.util.ArrayList;
|
2002-08-17 07:38:53 -04:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.Dictionary;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.Iterator;
|
2002-08-17 18:14:22 -04:00
|
|
|
import java.util.List;
|
2002-08-17 07:38:53 -04:00
|
|
|
import java.util.ListIterator;
|
|
|
|
import java.util.Map;
|
2002-08-17 18:14:22 -04:00
|
|
|
import java.util.NoSuchElementException;
|
2002-08-17 07:38:53 -04:00
|
|
|
|
|
|
|
import org.apache.commons.collections.iterators.ArrayIterator;
|
2002-12-13 07:04:16 -05:00
|
|
|
import org.apache.commons.collections.iterators.ArrayListIterator;
|
2002-08-17 07:38:53 -04:00
|
|
|
import org.apache.commons.collections.iterators.CollatingIterator;
|
|
|
|
import org.apache.commons.collections.iterators.EnumerationIterator;
|
|
|
|
import org.apache.commons.collections.iterators.FilterIterator;
|
|
|
|
import org.apache.commons.collections.iterators.FilterListIterator;
|
|
|
|
import org.apache.commons.collections.iterators.IteratorChain;
|
|
|
|
import org.apache.commons.collections.iterators.IteratorEnumeration;
|
|
|
|
import org.apache.commons.collections.iterators.ListIteratorWrapper;
|
2002-11-21 18:08:27 -05:00
|
|
|
import org.apache.commons.collections.iterators.LoopingIterator;
|
2002-12-13 07:04:16 -05:00
|
|
|
import org.apache.commons.collections.iterators.ObjectArrayIterator;
|
|
|
|
import org.apache.commons.collections.iterators.ObjectArrayListIterator;
|
2003-01-15 16:55:43 -05:00
|
|
|
import org.apache.commons.collections.iterators.ResetableIterator;
|
|
|
|
import org.apache.commons.collections.iterators.ResetableListIterator;
|
2002-08-17 07:38:53 -04:00
|
|
|
import org.apache.commons.collections.iterators.SingletonIterator;
|
|
|
|
import org.apache.commons.collections.iterators.SingletonListIterator;
|
|
|
|
import org.apache.commons.collections.iterators.TransformIterator;
|
2003-05-16 10:40:56 -04:00
|
|
|
|
2002-08-17 07:38:53 -04:00
|
|
|
/**
|
|
|
|
* Provides static utility methods and decorators for {@link Iterator}
|
|
|
|
* instances. The implementations are provided in the
|
2003-01-10 15:21:25 -05:00
|
|
|
* {@link org.apache.commons.collections.iterators} subpackage.
|
2002-08-17 07:38:53 -04:00
|
|
|
*
|
2003-01-10 15:21:25 -05:00
|
|
|
* @since Commons Collections 2.1
|
2003-09-29 18:44:14 -04:00
|
|
|
* @version $Revision: 1.13 $ $Date: 2003/09/29 22:44:14 $
|
2003-01-15 16:55:43 -05:00
|
|
|
*
|
|
|
|
* @author Stephen Colebourne
|
2003-09-28 23:38:44 -04:00
|
|
|
* @author Phil Steitz
|
2002-08-17 07:38:53 -04:00
|
|
|
*/
|
|
|
|
public class IteratorUtils {
|
|
|
|
// validation is done in this class in certain cases because the
|
|
|
|
// public classes allow invalid states
|
|
|
|
|
2002-08-17 18:14:22 -04:00
|
|
|
/**
|
|
|
|
* An iterator over no elements
|
|
|
|
*/
|
|
|
|
public static final Iterator EMPTY_ITERATOR = new EmptyIterator();
|
|
|
|
/**
|
|
|
|
* A list iterator over no elements
|
|
|
|
*/
|
|
|
|
public static final ListIterator EMPTY_LIST_ITERATOR = new EmptyListIterator();
|
|
|
|
|
2002-08-17 07:38:53 -04:00
|
|
|
/**
|
|
|
|
* Prevents instantiation.
|
|
|
|
*/
|
|
|
|
private IteratorUtils() {
|
|
|
|
}
|
|
|
|
|
2002-12-08 10:42:35 -05:00
|
|
|
// Iterator creators
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2002-08-17 07:38:53 -04:00
|
|
|
/**
|
|
|
|
* Gets an empty iterator.
|
|
|
|
* <p>
|
|
|
|
* This iterator is a valid iterator object that will iterate over
|
|
|
|
* nothing.
|
|
|
|
*
|
|
|
|
* @return an iterator over nothing
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableIterator emptyIterator() {
|
|
|
|
return (ResetableIterator) EMPTY_ITERATOR;
|
2002-08-17 07:38:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an empty list iterator.
|
|
|
|
* <p>
|
|
|
|
* This iterator is a valid list iterator object that will iterate
|
|
|
|
* over nothing.
|
|
|
|
*
|
|
|
|
* @return a list iterator over nothing
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableListIterator emptyListIterator() {
|
|
|
|
return (ResetableListIterator) EMPTY_LIST_ITERATOR;
|
2002-08-17 07:38:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a singleton iterator.
|
|
|
|
* <p>
|
|
|
|
* This iterator is a valid iterator object that will iterate over
|
|
|
|
* the specified object.
|
|
|
|
*
|
|
|
|
* @param object the single object over which to iterate
|
|
|
|
* @return a singleton iterator over the object
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableIterator singletonIterator(Object object) {
|
2002-08-17 07:38:53 -04:00
|
|
|
return new SingletonIterator(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a singleton list iterator.
|
|
|
|
* <p>
|
|
|
|
* This iterator is a valid list iterator object that will iterate over
|
|
|
|
* the specified object.
|
|
|
|
*
|
|
|
|
* @param object the single object over which to iterate
|
|
|
|
* @return a singleton list iterator over the object
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableListIterator singletonListIterator(Object object) {
|
2002-08-17 07:38:53 -04:00
|
|
|
return new SingletonListIterator(object);
|
|
|
|
}
|
|
|
|
|
2002-12-13 07:04:16 -05:00
|
|
|
// Arrays
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2002-08-17 07:38:53 -04:00
|
|
|
/**
|
2002-12-13 07:04:16 -05:00
|
|
|
* Gets an iterator over an object array.
|
2002-08-17 07:38:53 -04:00
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @return an iterator over the array
|
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableIterator arrayIterator(Object[] array) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ObjectArrayIterator(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator over an object or primitive array.
|
|
|
|
* <p>
|
|
|
|
* This method will handle primitive arrays as well as object arrays.
|
|
|
|
* The primitives will be wrapped in the appropriate wrapper class.
|
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @return an iterator over the array
|
|
|
|
* @throws IllegalArgumentException if the array is not an array
|
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableIterator arrayIterator(Object array) {
|
2002-08-17 07:38:53 -04:00
|
|
|
return new ArrayIterator(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-12-13 07:04:16 -05:00
|
|
|
* Gets an iterator over the end part of an object array.
|
2002-08-17 07:38:53 -04:00
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @param start the index to start iterating at
|
|
|
|
* @return an iterator over part of the array
|
2003-09-28 23:38:44 -04:00
|
|
|
* @throws IndexOutOfBoundsException if start is less than zero or greater
|
|
|
|
* than the length of the array
|
2002-08-17 07:38:53 -04:00
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableIterator arrayIterator(Object[] array, int start) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ObjectArrayIterator(array, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator over the end part of an object or primitive array.
|
|
|
|
* <p>
|
|
|
|
* This method will handle primitive arrays as well as object arrays.
|
|
|
|
* The primitives will be wrapped in the appropriate wrapper class.
|
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @param start the index to start iterating at
|
|
|
|
* @return an iterator over part of the array
|
|
|
|
* @throws IllegalArgumentException if the array is not an array
|
2003-09-28 23:38:44 -04:00
|
|
|
* @throws IndexOutOfBoundsException if start is less than zero or greater
|
|
|
|
* than the length of the array
|
2002-12-13 07:04:16 -05:00
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableIterator arrayIterator(Object array, int start) {
|
2002-08-17 07:38:53 -04:00
|
|
|
return new ArrayIterator(array, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-12-13 07:04:16 -05:00
|
|
|
* Gets an iterator over part of an object array.
|
2002-08-17 07:38:53 -04:00
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @param start the index to start iterating at
|
|
|
|
* @param end the index to finish iterating at
|
|
|
|
* @return an iterator over part of the array
|
2002-12-13 07:04:16 -05:00
|
|
|
* @throws IndexOutOfBoundsException if array bounds are invalid
|
|
|
|
* @throws IllegalArgumentException if end is before start
|
2002-08-17 07:38:53 -04:00
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableIterator arrayIterator(Object[] array, int start, int end) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ObjectArrayIterator(array, start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator over part of an object or primitive array.
|
|
|
|
* <p>
|
|
|
|
* This method will handle primitive arrays as well as object arrays.
|
|
|
|
* The primitives will be wrapped in the appropriate wrapper class.
|
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @param start the index to start iterating at
|
|
|
|
* @param end the index to finish iterating at
|
|
|
|
* @return an iterator over part of the array
|
|
|
|
* @throws IllegalArgumentException if the array is not an array
|
|
|
|
* @throws IndexOutOfBoundsException if array bounds are invalid
|
|
|
|
* @throws IllegalArgumentException if end is before start
|
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableIterator arrayIterator(Object array, int start, int end) {
|
2002-08-17 07:38:53 -04:00
|
|
|
return new ArrayIterator(array, start, end);
|
|
|
|
}
|
|
|
|
|
2002-12-13 07:04:16 -05:00
|
|
|
/**
|
|
|
|
* Gets a list iterator over an object array.
|
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @return a list iterator over the array
|
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableListIterator arrayListIterator(Object[] array) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ObjectArrayListIterator(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list iterator over an object or primitive array.
|
|
|
|
* <p>
|
|
|
|
* This method will handle primitive arrays as well as object arrays.
|
|
|
|
* The primitives will be wrapped in the appropriate wrapper class.
|
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @return a list iterator over the array
|
|
|
|
* @throws IllegalArgumentException if the array is not an array
|
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableListIterator arrayListIterator(Object array) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ArrayListIterator(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list iterator over the end part of an object array.
|
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @param start the index to start iterating at
|
|
|
|
* @return a list iterator over part of the array
|
|
|
|
* @throws IndexOutOfBoundsException if start is less than zero
|
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableListIterator arrayListIterator(Object[] array, int start) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ObjectArrayListIterator(array, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list iterator over the end part of an object or primitive array.
|
|
|
|
* <p>
|
|
|
|
* This method will handle primitive arrays as well as object arrays.
|
|
|
|
* The primitives will be wrapped in the appropriate wrapper class.
|
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @param start the index to start iterating at
|
|
|
|
* @return a list iterator over part of the array
|
|
|
|
* @throws IllegalArgumentException if the array is not an array
|
|
|
|
* @throws IndexOutOfBoundsException if start is less than zero
|
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableListIterator arrayListIterator(Object array, int start) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ArrayListIterator(array, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list iterator over part of an object array.
|
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @param start the index to start iterating at
|
|
|
|
* @param end the index to finish iterating at
|
|
|
|
* @return a list iterator over part of the array
|
|
|
|
* @throws IndexOutOfBoundsException if array bounds are invalid
|
|
|
|
* @throws IllegalArgumentException if end is before start
|
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableListIterator arrayListIterator(Object[] array, int start, int end) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ObjectArrayListIterator(array, start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list iterator over part of an object or primitive array.
|
|
|
|
* <p>
|
|
|
|
* This method will handle primitive arrays as well as object arrays.
|
|
|
|
* The primitives will be wrapped in the appropriate wrapper class.
|
|
|
|
*
|
|
|
|
* @param array the array over which to iterate
|
|
|
|
* @param start the index to start iterating at
|
|
|
|
* @param end the index to finish iterating at
|
|
|
|
* @return a list iterator over part of the array
|
|
|
|
* @throws IllegalArgumentException if the array is not an array
|
|
|
|
* @throws IndexOutOfBoundsException if array bounds are invalid
|
|
|
|
* @throws IllegalArgumentException if end is before start
|
|
|
|
* @throws NullPointerException if array is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableListIterator arrayListIterator(Object array, int start, int end) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ArrayListIterator(array, start, end);
|
|
|
|
}
|
2002-12-08 10:42:35 -05:00
|
|
|
|
|
|
|
// Iterator wrappers
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an immutable version of an {@link Iterator}. The returned object
|
2003-01-10 15:21:25 -05:00
|
|
|
* will always throw an {@link UnsupportedOperationException} for
|
|
|
|
* the {@link Iterator#remove} method.
|
2002-12-08 10:42:35 -05:00
|
|
|
*
|
2003-01-15 16:55:43 -05:00
|
|
|
* @param iterator the iterator to make immutable
|
|
|
|
* @return an immutable version of the iterator
|
2002-12-08 10:42:35 -05:00
|
|
|
*/
|
|
|
|
public static Iterator unmodifiableIterator(Iterator iterator) {
|
2003-01-15 16:55:43 -05:00
|
|
|
if (iterator instanceof ResetableIterator) {
|
|
|
|
return new ResetableUnmodifiableIterator((ResetableIterator) iterator);
|
|
|
|
}
|
2002-12-08 10:42:35 -05:00
|
|
|
return new UnmodifiableIterator(iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-10 15:21:25 -05:00
|
|
|
* Gets an immutable version of a {@link ListIterator}. The returned object
|
|
|
|
* will always throw an {@link UnsupportedOperationException} for
|
|
|
|
* the {@link Iterator#remove}, {@link ListIterator#add} and
|
|
|
|
* {@link ListIterator#set} methods.
|
2002-12-08 10:42:35 -05:00
|
|
|
*
|
2003-01-15 16:55:43 -05:00
|
|
|
* @param listIterator the iterator to make immutable
|
|
|
|
* @return an immutable version of the iterator
|
2002-12-08 10:42:35 -05:00
|
|
|
*/
|
|
|
|
public static ListIterator unmodifiableListIterator(ListIterator listIterator) {
|
2003-01-15 16:55:43 -05:00
|
|
|
if (listIterator instanceof ResetableListIterator) {
|
|
|
|
return new ResetableUnmodifiableListIterator((ResetableListIterator) listIterator);
|
|
|
|
}
|
2002-12-08 10:42:35 -05:00
|
|
|
return new UnmodifiableListIterator(listIterator);
|
|
|
|
}
|
2002-08-17 07:38:53 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator that iterates through two {@link Iterator}s
|
|
|
|
* one after another.
|
|
|
|
*
|
|
|
|
* @param iterator1 the first iterators to use, not null
|
|
|
|
* @param iterator2 the first iterators to use, not null
|
|
|
|
* @return a combination iterator over the iterators
|
|
|
|
* @throws NullPointerException if either iterator is null
|
|
|
|
*/
|
|
|
|
public static Iterator chainedIterator(Iterator iterator1, Iterator iterator2) {
|
|
|
|
return new IteratorChain(iterator1, iterator2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator that iterates through an array of {@link Iterator}s
|
|
|
|
* one after another.
|
|
|
|
*
|
|
|
|
* @param iterators the iterators to use, not null or empty or contain nulls
|
|
|
|
* @return a combination iterator over the iterators
|
|
|
|
* @throws NullPointerException if iterators array is null or contains a null
|
|
|
|
*/
|
|
|
|
public static Iterator chainedIterator(Iterator[] iterators) {
|
|
|
|
return new IteratorChain(iterators);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator that iterates through a collections of {@link Iterator}s
|
|
|
|
* one after another.
|
|
|
|
*
|
|
|
|
* @param iterators the iterators to use, not null or empty or contain nulls
|
|
|
|
* @return a combination iterator over the iterators
|
|
|
|
* @throws NullPointerException if iterators collection is null or contains a null
|
|
|
|
* @throws ClassCastException if the iterators collection contains the wrong object type
|
|
|
|
*/
|
|
|
|
public static Iterator chainedIterator(Collection iterators) {
|
|
|
|
return new IteratorChain(iterators);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator that provides an ordered iteration over the elements
|
|
|
|
* contained in a collection of ordered {@link Iterator}s.
|
|
|
|
* <p>
|
|
|
|
* Given two ordered {@link Iterator}s <code>A</code> and <code>B</code>,
|
2002-08-19 17:56:18 -04:00
|
|
|
* the {@link Iterator#next()} method will return the lesser of
|
2002-08-17 07:38:53 -04:00
|
|
|
* <code>A.next()</code> and <code>B.next()</code>.
|
|
|
|
* <p>
|
|
|
|
* The comparator is optional. If null is specified then natural order is used.
|
|
|
|
*
|
|
|
|
* @param comparator the comparator to use, may be null for natural order
|
|
|
|
* @param iterator1 the first iterators to use, not null
|
|
|
|
* @param iterator2 the first iterators to use, not null
|
|
|
|
* @return a combination iterator over the iterators
|
|
|
|
* @throws NullPointerException if either iterator is null
|
|
|
|
*/
|
|
|
|
public static Iterator collatedIterator(Comparator comparator, Iterator iterator1, Iterator iterator2) {
|
|
|
|
return new CollatingIterator(comparator, iterator1, iterator2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator that provides an ordered iteration over the elements
|
|
|
|
* contained in an array of {@link Iterator}s.
|
|
|
|
* <p>
|
|
|
|
* Given two ordered {@link Iterator}s <code>A</code> and <code>B</code>,
|
2002-08-19 17:56:18 -04:00
|
|
|
* the {@link Iterator#next()} method will return the lesser of
|
2002-08-17 07:38:53 -04:00
|
|
|
* <code>A.next()</code> and <code>B.next()</code> and so on.
|
|
|
|
* <p>
|
|
|
|
* The comparator is optional. If null is specified then natural order is used.
|
|
|
|
*
|
|
|
|
* @param comparator the comparator to use, may be null for natural order
|
|
|
|
* @param iterators the iterators to use, not null or empty or contain nulls
|
|
|
|
* @return a combination iterator over the iterators
|
|
|
|
* @throws NullPointerException if iterators array is null or contains a null
|
|
|
|
*/
|
|
|
|
public static Iterator collatedIterator(Comparator comparator, Iterator[] iterators) {
|
|
|
|
return new CollatingIterator(comparator, iterators);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator that provides an ordered iteration over the elements
|
|
|
|
* contained in a collection of {@link Iterator}s.
|
|
|
|
* <p>
|
|
|
|
* Given two ordered {@link Iterator}s <code>A</code> and <code>B</code>,
|
2002-08-19 17:56:18 -04:00
|
|
|
* the {@link Iterator#next()} method will return the lesser of
|
2002-08-17 07:38:53 -04:00
|
|
|
* <code>A.next()</code> and <code>B.next()</code> and so on.
|
|
|
|
* <p>
|
|
|
|
* The comparator is optional. If null is specified then natural order is used.
|
|
|
|
*
|
|
|
|
* @param comparator the comparator to use, may be null for natural order
|
|
|
|
* @param iterators the iterators to use, not null or empty or contain nulls
|
|
|
|
* @return a combination iterator over the iterators
|
|
|
|
* @throws NullPointerException if iterators collection is null or contains a null
|
|
|
|
* @throws ClassCastException if the iterators collection contains the wrong object type
|
|
|
|
*/
|
|
|
|
public static Iterator collatedIterator(Comparator comparator, Collection iterators) {
|
|
|
|
return new CollatingIterator(comparator, iterators);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator that transforms the elements of another iterator.
|
|
|
|
* <p>
|
|
|
|
* The transformation occurs during the next() method and the underlying
|
|
|
|
* iterator is unaffected by the transformation.
|
|
|
|
*
|
|
|
|
* @param iterator the iterator to use, not null
|
|
|
|
* @param transform the transform to use, not null
|
|
|
|
* @throws NullPointerException if either parameter is null
|
|
|
|
*/
|
|
|
|
public static Iterator transformedIterator(Iterator iterator, Transformer transform) {
|
|
|
|
if (iterator == null) {
|
|
|
|
throw new NullPointerException("Iterator must not be null");
|
|
|
|
}
|
|
|
|
if (transform == null) {
|
|
|
|
throw new NullPointerException("Transformer must not be null");
|
|
|
|
}
|
|
|
|
return new TransformIterator(iterator, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator that filters another iterator.
|
|
|
|
* <p>
|
|
|
|
* The returned iterator will only return objects that match the specified
|
|
|
|
* filtering predicate.
|
|
|
|
*
|
|
|
|
* @param iterator the iterator to use, not null
|
|
|
|
* @param predicate the predicate to use as a filter, not null
|
|
|
|
* @throws NullPointerException if either parameter is null
|
|
|
|
*/
|
|
|
|
public static Iterator filteredIterator(Iterator iterator, Predicate predicate) {
|
|
|
|
if (iterator == null) {
|
|
|
|
throw new NullPointerException("Iterator must not be null");
|
|
|
|
}
|
|
|
|
if (predicate == null) {
|
|
|
|
throw new NullPointerException("Predicate must not be null");
|
|
|
|
}
|
|
|
|
return new FilterIterator(iterator, predicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list iterator that filters another list iterator.
|
|
|
|
* <p>
|
|
|
|
* The returned iterator will only return objects that match the specified
|
|
|
|
* filtering predicate.
|
|
|
|
*
|
|
|
|
* @param listIterator the list iterator to use, not null
|
|
|
|
* @param predicate the predicate to use as a filter, not null
|
|
|
|
* @throws NullPointerException if either parameter is null
|
|
|
|
*/
|
|
|
|
public static ListIterator filteredListIterator(ListIterator listIterator, Predicate predicate) {
|
|
|
|
if (listIterator == null) {
|
|
|
|
throw new NullPointerException("ListIterator must not be null");
|
|
|
|
}
|
|
|
|
if (predicate == null) {
|
|
|
|
throw new NullPointerException("Predicate must not be null");
|
|
|
|
}
|
|
|
|
return new FilterListIterator(listIterator, predicate);
|
|
|
|
}
|
|
|
|
|
2002-11-21 18:08:27 -05:00
|
|
|
/**
|
|
|
|
* Gets an iterator that loops continuously over the supplied collection.
|
|
|
|
* <p>
|
|
|
|
* The iterator will only stop looping if the remove method is called
|
|
|
|
* enough times to empty the collection, or if the collection is empty
|
|
|
|
* to start with.
|
|
|
|
*
|
|
|
|
* @param coll the collection to iterate over, not null
|
|
|
|
* @throws NullPointerException if the collection is null
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
public static ResetableIterator loopingIterator(Collection coll) {
|
2002-11-21 18:08:27 -05:00
|
|
|
if (coll == null) {
|
|
|
|
throw new NullPointerException("Collection must not be null");
|
|
|
|
}
|
|
|
|
return new LoopingIterator(coll);
|
|
|
|
}
|
|
|
|
|
2002-08-17 07:38:53 -04:00
|
|
|
/**
|
|
|
|
* Gets an iterator that provides an iterator view of the given enumeration.
|
|
|
|
*
|
|
|
|
* @param enumeration the enumeration to use
|
|
|
|
*/
|
2002-08-17 18:14:22 -04:00
|
|
|
public static Iterator asIterator(Enumeration enumeration) {
|
2002-08-17 07:38:53 -04:00
|
|
|
if (enumeration == null) {
|
|
|
|
throw new NullPointerException("Enumeration must not be null");
|
|
|
|
}
|
|
|
|
return new EnumerationIterator(enumeration);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an iterator that provides an iterator view of the given enumeration
|
|
|
|
* that will remove elements from the specified collection.
|
|
|
|
*
|
|
|
|
* @param enumeration the enumeration to use
|
2003-09-29 18:44:14 -04:00
|
|
|
* @param removeCollection the collection to remove elements from
|
2002-08-17 07:38:53 -04:00
|
|
|
*/
|
2002-08-17 18:14:22 -04:00
|
|
|
public static Iterator asIterator(Enumeration enumeration, Collection removeCollection) {
|
2002-08-17 07:38:53 -04:00
|
|
|
if (enumeration == null) {
|
|
|
|
throw new NullPointerException("Enumeration must not be null");
|
|
|
|
}
|
|
|
|
if (removeCollection == null) {
|
|
|
|
throw new NullPointerException("Collection must not be null");
|
|
|
|
}
|
|
|
|
return new EnumerationIterator(enumeration, removeCollection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an enumeration that wraps an iterator.
|
|
|
|
*
|
|
|
|
* @param iterator the iterator to use, not null
|
|
|
|
* @throws NullPointerException if iterator is null
|
|
|
|
*/
|
2002-08-17 18:14:22 -04:00
|
|
|
public static Enumeration asEnumeration(Iterator iterator) {
|
2002-08-17 07:38:53 -04:00
|
|
|
if (iterator == null) {
|
|
|
|
throw new NullPointerException("Iterator must not be null");
|
|
|
|
}
|
|
|
|
return new IteratorEnumeration(iterator);
|
|
|
|
}
|
|
|
|
|
2002-08-17 18:14:22 -04:00
|
|
|
/**
|
|
|
|
* Gets a list iterator based on a simple iterator.
|
|
|
|
* <p>
|
|
|
|
* As the wrapped Iterator is traversed, a LinkedList of its values is
|
|
|
|
* cached, permitting all required operations of ListIterator.
|
|
|
|
*
|
|
|
|
* @param iterator the iterator to use, not null
|
|
|
|
* @throws NullPointerException if iterator parameter is null
|
|
|
|
*/
|
|
|
|
public static ListIterator toListIterator(Iterator iterator) {
|
|
|
|
if (iterator == null) {
|
|
|
|
throw new NullPointerException("Iterator must not be null");
|
|
|
|
}
|
|
|
|
return new ListIteratorWrapper(iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an array based on an iterator.
|
|
|
|
* <p>
|
|
|
|
* As the wrapped Iterator is traversed, an ArrayList of its values is
|
|
|
|
* created. At the end, this is converted to an array.
|
|
|
|
*
|
|
|
|
* @param iterator the iterator to use, not null
|
|
|
|
* @throws NullPointerException if iterator parameter is null
|
|
|
|
*/
|
|
|
|
public static Object[] toArray(Iterator iterator) {
|
|
|
|
if (iterator == null) {
|
|
|
|
throw new NullPointerException("Iterator must not be null");
|
|
|
|
}
|
|
|
|
List list = toList(iterator, 100);
|
|
|
|
return list.toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an array based on an iterator.
|
|
|
|
* <p>
|
|
|
|
* As the wrapped Iterator is traversed, an ArrayList of its values is
|
|
|
|
* created. At the end, this is converted to an array.
|
|
|
|
*
|
|
|
|
* @param iterator the iterator to use, not null
|
|
|
|
* @param arrayClass the class of array to create
|
|
|
|
* @throws NullPointerException if iterator parameter is null
|
|
|
|
* @throws NullPointerException if arrayClass is null
|
|
|
|
* @throws ClassCastException if the arrayClass is invalid
|
|
|
|
*/
|
|
|
|
public static Object[] toArray(Iterator iterator, Class arrayClass) {
|
|
|
|
if (iterator == null) {
|
|
|
|
throw new NullPointerException("Iterator must not be null");
|
|
|
|
}
|
|
|
|
if (arrayClass == null) {
|
|
|
|
throw new NullPointerException("Array class must not be null");
|
|
|
|
}
|
|
|
|
List list = toList(iterator, 100);
|
|
|
|
return list.toArray((Object[]) Array.newInstance(arrayClass, list.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list based on an iterator.
|
|
|
|
* <p>
|
|
|
|
* As the wrapped Iterator is traversed, an ArrayList of its values is
|
|
|
|
* created. At the end, the list is returned.
|
|
|
|
*
|
|
|
|
* @param iterator the iterator to use, not null
|
|
|
|
* @throws NullPointerException if iterator parameter is null
|
|
|
|
*/
|
|
|
|
public static List toList(Iterator iterator) {
|
|
|
|
return toList(iterator, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list based on an iterator.
|
|
|
|
* <p>
|
|
|
|
* As the wrapped Iterator is traversed, an ArrayList of its values is
|
|
|
|
* created. At the end, the list is returned.
|
|
|
|
*
|
|
|
|
* @param iterator the iterator to use, not null
|
|
|
|
* @param estimatedSize the initial size of the ArrayList
|
|
|
|
* @throws NullPointerException if iterator parameter is null
|
|
|
|
* @throws IllegalArgumentException if the size is less than 1
|
|
|
|
*/
|
|
|
|
public static List toList(Iterator iterator, int estimatedSize) {
|
|
|
|
if (iterator == null) {
|
|
|
|
throw new NullPointerException("Iterator must not be null");
|
|
|
|
}
|
|
|
|
if (estimatedSize < 1) {
|
|
|
|
throw new IllegalArgumentException("Estimated size must be greater than 0");
|
|
|
|
}
|
|
|
|
List list = new ArrayList(estimatedSize);
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
list.add(iterator.next());
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2002-08-17 07:38:53 -04:00
|
|
|
/**
|
|
|
|
* Gets a suitable Iterator for the given object.
|
|
|
|
* <p>
|
|
|
|
* This method can handles objects as follows
|
|
|
|
* <ul>
|
|
|
|
* <li>null - empty iterator
|
|
|
|
* <li>Iterator - returned directly
|
|
|
|
* <li>Enumeration - wrapped
|
|
|
|
* <li>Collection - iterator from collection returned
|
|
|
|
* <li>Map - values iterator returned
|
|
|
|
* <li>Dictionary - values (elements) enumeration returned as iterator
|
|
|
|
* <li>array - iterator over array returned
|
|
|
|
* <li>object with iterator() public method accessed by reflection
|
|
|
|
* <li>object - singleton iterator
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* @param obj the object to convert to an iterator
|
|
|
|
* @return a suitable iterator, never null
|
|
|
|
*/
|
|
|
|
public static Iterator getIterator(Object obj) {
|
|
|
|
if (obj == null) {
|
|
|
|
return emptyIterator();
|
|
|
|
|
|
|
|
} else if (obj instanceof Iterator) {
|
|
|
|
return (Iterator) obj;
|
|
|
|
|
|
|
|
} else if (obj instanceof Collection) {
|
|
|
|
return ((Collection) obj).iterator();
|
|
|
|
|
|
|
|
} else if (obj instanceof Object[]) {
|
2002-12-13 07:04:16 -05:00
|
|
|
return new ObjectArrayIterator((Object[]) obj);
|
2002-08-17 07:38:53 -04:00
|
|
|
|
|
|
|
} else if (obj instanceof Enumeration) {
|
|
|
|
return new EnumerationIterator((Enumeration) obj);
|
|
|
|
|
|
|
|
} else if (obj instanceof Map) {
|
|
|
|
return ((Map) obj).values().iterator();
|
|
|
|
|
|
|
|
} else if (obj instanceof Dictionary) {
|
|
|
|
return new EnumerationIterator(((Dictionary) obj).elements());
|
|
|
|
|
|
|
|
} else if (obj != null && obj.getClass().isArray()) {
|
|
|
|
return new ArrayIterator(obj);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
Method method = obj.getClass().getMethod("iterator", null);
|
|
|
|
if (Iterator.class.isAssignableFrom(method.getReturnType())) {
|
|
|
|
Iterator it = (Iterator) method.invoke(obj, null);
|
|
|
|
if (it != null) {
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
return singletonIterator(obj);
|
|
|
|
}
|
|
|
|
}
|
2002-08-17 18:14:22 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* EmptyIterator class
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
static class EmptyIterator implements ResetableIterator {
|
2002-08-17 18:14:22 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see java.util.Iterator#hasNext()
|
|
|
|
*/
|
|
|
|
public boolean hasNext() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see java.util.Iterator#next()
|
|
|
|
*/
|
|
|
|
public Object next() {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see java.util.Iterator#remove()
|
|
|
|
*/
|
|
|
|
public void remove() {
|
|
|
|
throw new UnsupportedOperationException("remove() not supported for empty Iterator");
|
|
|
|
}
|
2003-01-15 16:55:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the iterator
|
|
|
|
*/
|
|
|
|
public void reset() {
|
|
|
|
// do nothing
|
|
|
|
}
|
2002-08-17 18:14:22 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EmptyListIterator class
|
|
|
|
*/
|
2003-01-15 16:55:43 -05:00
|
|
|
static class EmptyListIterator extends EmptyIterator implements ResetableListIterator {
|
2002-08-17 18:14:22 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see java.util.ListIterator#hasPrevious()
|
|
|
|
*/
|
|
|
|
public boolean hasPrevious() {
|
|
|
|
return false;
|
|
|
|
}
|
2002-08-17 07:38:53 -04:00
|
|
|
|
2002-08-17 18:14:22 -04:00
|
|
|
/**
|
|
|
|
* @see java.util.ListIterator#previous()
|
|
|
|
*/
|
|
|
|
public Object previous() {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see java.util.ListIterator#nextIndex()
|
|
|
|
*/
|
|
|
|
public int nextIndex() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see java.util.ListIterator#previousIndex()
|
|
|
|
*/
|
|
|
|
public int previousIndex() {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see java.util.ListIterator#add(Object)
|
|
|
|
*/
|
|
|
|
public void add(Object o) {
|
|
|
|
throw new UnsupportedOperationException("add() not supported for empty Iterator");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see java.util.ListIterator#set(Object)
|
|
|
|
*/
|
|
|
|
public void set(Object o) {
|
|
|
|
throw new UnsupportedOperationException("set() not supported for empty Iterator");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2002-12-08 10:42:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>
|
2003-01-15 16:55:43 -05:00
|
|
|
* @author Stephen Colebourne
|
2002-12-08 10:42:35 -05:00
|
|
|
*/
|
|
|
|
static class UnmodifiableIterator implements Iterator, Serializable {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All calls to this iterator are passed to the delegate.
|
|
|
|
*/
|
|
|
|
protected Iterator delegate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an UnmodifiableIterator.
|
|
|
|
*
|
2003-01-15 16:55:43 -05:00
|
|
|
* @param delegate the delegate to pass all calls to
|
2002-12-08 10:42:35 -05:00
|
|
|
*/
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-01-15 16:55:43 -05:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-12-08 10:42:35 -05:00
|
|
|
/**
|
|
|
|
* 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");
|
|
|
|
}
|
|
|
|
}
|
2003-01-15 16:55:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-08-17 07:38:53 -04:00
|
|
|
}
|