📄 iteratorutils.java
字号:
/*
* Copyright 1999-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 java.util.NoSuchElementException;
import org.apache.commons.collections.iterators.ArrayIterator;
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;
import org.apache.commons.collections.iterators.SingletonIterator;
import org.apache.commons.collections.iterators.SingletonListIterator;
import org.apache.commons.collections.iterators.TransformIterator;
/**
* Provides static utility methods and decorators for {@link Iterator}
* instances. The implementations are provided in the
* <code>org.apache.commons.collections.iterators</code> subpackage.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: IteratorUtils.java,v 1.4.2.2 2004/05/22 12:14:01 scolebourne Exp $
* @since 2.1
*/
public class IteratorUtils {
// validation is done in this class in certain cases because the
// public classes allow invalid states
/**
* An iterator over no elements.
* @deprecated Use <code>EmptyIterator.INSTANCE</code>
*/
public static final Iterator EMPTY_ITERATOR = new EmptyIterator();
/**
* A list iterator over no elements
* @deprecated Use <code>EmptyListIterator.INSTANCE</code>
*/
public static final ListIterator EMPTY_LIST_ITERATOR = new EmptyListIterator();
/**
* Prevents instantiation.
*/
private IteratorUtils() {
}
/**
* Gets an empty iterator.
* <p>
* This iterator is a valid iterator object that will iterate over
* nothing.
*
* @return an iterator over nothing
* @deprecated Use <code>EmptyIterator.INSTANCE</code>
*/
public static Iterator emptyIterator() {
return EMPTY_ITERATOR;
}
/**
* 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
* @deprecated Use <code>EmptyListIterator.INSTANCE</code>
*/
public static ListIterator emptyListIterator() {
return EMPTY_LIST_ITERATOR;
}
/**
* 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
* @deprecated Use <code>new SingletonIterator(object)</code>
*/
public static Iterator 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);
}
/**
* Gets an iterator over an array.
*
* @param array the array over which to iterate
* @return an iterator over the array
* @throws NullPointerException if array is null
* @deprecated Use <code>new ArrayIterator(array)</code>
*/
public static Iterator arrayIterator(Object[] array) {
return new ArrayIterator(array);
}
/**
* Gets an iterator over the end part of an array.
*
* @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 array bounds are invalid
* @throws NullPointerException if array is null
* @deprecated Use <code>new ArrayIterator(array,start)</code>
*/
public static Iterator arrayIterator(Object[] array, int start) {
return new ArrayIterator(array, start);
}
/**
* Gets an iterator over part of an 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 an iterator over part of the array
* @throws IllegalArgumentException if array bounds are invalid
* @throws NullPointerException if array is null
* @deprecated Use <code>new ArrayIterator(array,start,end)</code>
*/
public static Iterator arrayIterator(Object[] array, int start, int end) {
return new ArrayIterator(array, start, end);
}
// /**
// * Gets a list iterator over an array.
// *
// * @param array the array over which to iterate
// * @return a list iterator over the array
// * @throws NullPointerException if array is null
// */
// public static ListIterator arrayListIterator(Object[] array) {
// return new ArrayListIterator(array);
// }
//
// /**
// * Gets a list iterator over the end part of an 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 IllegalArgumentException if array bounds are invalid
// * @throws NullPointerException if array is null
// */
// public static ListIterator arrayListIterator(Object[] array, int start) {
// return new ArrayListIterator(array, start);
// }
//
// /**
// * Gets a list iterator over part of an 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 IllegalArgumentException if array bounds are invalid
// * @throws NullPointerException if array is null
// */
// public static ListIterator arrayListIterator(Object[] array, int start, int end) {
// return new ArrayListIterator(array, start, end);
// }
/**
* 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>,
* the {@link Iterator#next()} method will return the lesser of
* <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>,
* the {@link Iterator#next()} method will return the lesser of
* <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>,
* the {@link Iterator#next()} method will return the lesser of
* <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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -