📄 iteratorutils.java
字号:
/*
* Copyright 2002-2004 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.apache.commons.collections.iterators.ArrayIterator;
import org.apache.commons.collections.iterators.ArrayListIterator;
import org.apache.commons.collections.iterators.CollatingIterator;
import org.apache.commons.collections.iterators.EmptyIterator;
import org.apache.commons.collections.iterators.EmptyListIterator;
import org.apache.commons.collections.iterators.EmptyMapIterator;
import org.apache.commons.collections.iterators.EmptyOrderedIterator;
import org.apache.commons.collections.iterators.EmptyOrderedMapIterator;
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;
import org.apache.commons.collections.iterators.LoopingIterator;
import org.apache.commons.collections.iterators.LoopingListIterator;
import org.apache.commons.collections.iterators.ObjectArrayIterator;
import org.apache.commons.collections.iterators.ObjectArrayListIterator;
import org.apache.commons.collections.iterators.ObjectGraphIterator;
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}
* instances. The implementations are provided in the iterators subpackage.
* <p>
* WARNING: Due to human error certain binary incompatabilities were introduced
* between Commons Collections 2.1 and 3.0. The class remained source and test
* compatible, so if you can recompile all your classes and dependencies
* everything is OK. Those methods which are binary incompatible are marked as
* such, together with alternate solutions that are binary compatible
* against versions 2.1.1 and 3.1.
*
* @since Commons Collections 2.1
* @version $Revision: 405920 $ $Date: 2006-05-12 23:48:04 +0100 (Fri, 12 May 2006) $
*
* @author Stephen Colebourne
* @author Phil Steitz
*/
public class IteratorUtils {
// validation is done in this class in certain cases because the
// public classes allow invalid states
/**
* An iterator over no elements.
* <p>
* WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1.
* Use <code>EmptyIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1.
*/
public static final ResettableIterator EMPTY_ITERATOR = EmptyIterator.RESETTABLE_INSTANCE;
/**
* A list iterator over no elements.
* <p>
* WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1.
* Use <code>EmptyListIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1.
*/
public static final ResettableListIterator EMPTY_LIST_ITERATOR = EmptyListIterator.RESETTABLE_INSTANCE;
/**
* An ordered iterator over no elements.
*/
public static final OrderedIterator EMPTY_ORDERED_ITERATOR = EmptyOrderedIterator.INSTANCE;
/**
* A map iterator over no elements.
*/
public static final MapIterator EMPTY_MAP_ITERATOR = EmptyMapIterator.INSTANCE;
/**
* An ordered map iterator over no elements.
*/
public static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR = EmptyOrderedMapIterator.INSTANCE;
/**
* IteratorUtils is not normally instantiated.
*/
public IteratorUtils() {
}
// Empty
//-----------------------------------------------------------------------
/**
* Gets an empty iterator.
* <p>
* This iterator is a valid iterator object that will iterate over
* nothing.
* <p>
* WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
* Use <code>EmptyIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1.
*
* @return an iterator over nothing
*/
public static ResettableIterator emptyIterator() {
return EMPTY_ITERATOR;
}
/**
* Gets an empty list iterator.
* <p>
* This iterator is a valid list iterator object that will iterate
* over nothing.
* <p>
* WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
* Use <code>EmptyListIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1.
*
* @return a list iterator over nothing
*/
public static ResettableListIterator emptyListIterator() {
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 OrderedIterator 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 map iterator over nothing
*/
public static MapIterator emptyMapIterator() {
return EMPTY_MAP_ITERATOR;
}
/**
* Gets an empty ordered map iterator.
* <p>
* This iterator is a valid map iterator object that will iterate
* over nothing.
*
* @return a map iterator over nothing
*/
public static OrderedMapIterator emptyOrderedMapIterator() {
return EMPTY_ORDERED_MAP_ITERATOR;
}
// Singleton
//-----------------------------------------------------------------------
/**
* Gets a singleton iterator.
* <p>
* This iterator is a valid iterator object that will iterate over
* the specified object.
* <p>
* WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
* Use <code>new SingletonIterator(object)</code> for compatability.
*
* @param object the single object over which to iterate
* @return a singleton iterator over the object
*/
public static ResettableIterator singletonIterator(Object object) {
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
*/
public static ListIterator singletonListIterator(Object object) {
return new SingletonListIterator(object);
}
// Arrays
//-----------------------------------------------------------------------
/**
* Gets an iterator over an object array.
* <p>
* WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
* Use <code>new ArrayIterator(array)</code> for compatability.
*
* @param array the array over which to iterate
* @return an iterator over the array
* @throws NullPointerException if array is null
*/
public static ResettableIterator arrayIterator(Object[] array) {
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
*/
public static ResettableIterator arrayIterator(Object array) {
return new ArrayIterator(array);
}
/**
* Gets an iterator over the end part of an object array.
* <p>
* WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
* Use <code>new ArrayIterator(array,start)</code> for compatability.
*
* @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 IndexOutOfBoundsException if start is less than zero or greater
* than the length of the array
* @throws NullPointerException if array is null
*/
public static ResettableIterator arrayIterator(Object[] array, int start) {
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
* @throws IndexOutOfBoundsException if start is less than zero or greater
* than the length of the array
* @throws NullPointerException if array is null
*/
public static ResettableIterator arrayIterator(Object array, int start) {
return new ArrayIterator(array, start);
}
/**
* Gets an iterator over part of an object array.
* <p>
* WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
* Use <code>new ArrayIterator(array,start,end)</code> for compatability.
*
* @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 IndexOutOfBoundsException if array bounds are invalid
* @throws IllegalArgumentException if end is before start
* @throws NullPointerException if array is null
*/
public static ResettableIterator arrayIterator(Object[] array, int start, int end) {
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -