📄 iteratorutils.java
字号:
* @throws NullPointerException if array is null
*/
public static ResettableIterator arrayIterator(Object array, int start, int end) {
return new ArrayIterator(array, start, end);
}
//-----------------------------------------------------------------------
/**
* 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
*/
public static ResettableListIterator arrayListIterator(Object[] array) {
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
*/
public static ResettableListIterator arrayListIterator(Object array) {
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
*/
public static ResettableListIterator arrayListIterator(Object[] array, int start) {
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
*/
public static ResettableListIterator arrayListIterator(Object array, int start) {
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
*/
public static ResettableListIterator arrayListIterator(Object[] array, int start, int end) {
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
*/
public static ResettableListIterator arrayListIterator(Object array, int start, int end) {
return new ArrayListIterator(array, start, end);
}
// Unmodifiable
//-----------------------------------------------------------------------
/**
* Gets an immutable version of an {@link Iterator}. The returned object
* will always throw an {@link UnsupportedOperationException} for
* the {@link Iterator#remove} method.
*
* @param iterator the iterator to make immutable
* @return an immutable version of the iterator
*/
public static Iterator unmodifiableIterator(Iterator iterator) {
return UnmodifiableIterator.decorate(iterator);
}
/**
* 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.
*
* @param listIterator the iterator to make immutable
* @return an immutable version of the iterator
*/
public static ListIterator unmodifiableListIterator(ListIterator 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);
}
// Chained
//-----------------------------------------------------------------------
/**
* 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);
}
// Collated
//-----------------------------------------------------------------------
/**
* 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);
}
// Object Graph
//-----------------------------------------------------------------------
/**
* Gets an iterator that operates over an object graph.
* <p>
* This iterator can extract multiple objects from a complex tree-like object graph.
* The iteration starts from a single root object.
* It uses a <code>Transformer</code> to extract the iterators and elements.
* Its main benefit is that no intermediate <code>List</code> is created.
* <p>
* For example, consider an object graph:
* <pre>
* |- Branch -- Leaf
* | \- Leaf
* |- Tree | /- Leaf
* | |- Branch -- Leaf
* Forest | \- Leaf
* | |- Branch -- Leaf
* | | \- Leaf
* |- Tree | /- Leaf
* |- Branch -- Leaf
* |- Branch -- Leaf</pre>
* The following <code>Transformer</code>, used in this class, will extract all
* the Leaf objects without creating a combined intermediate list:
* <pre>
* public Object transform(Object input) {
* if (input instanceof Forest) {
* return ((Forest) input).treeIterator();
* }
* if (input instanceof Tree) {
* return ((Tree) input).branchIterator();
* }
* if (input instanceof Branch) {
* return ((Branch) input).leafIterator();
* }
* if (input instanceof Leaf) {
* return input;
* }
* throw new ClassCastException();
* }</pre>
* <p>
* Internally, iteration starts from the root object. When next is called,
* the transformer is called to examine the object. The transformer will return
* either an iterator or an object. If the object is an Iterator, the next element
* from that iterator is obtained and the process repeats. If the element is an object
* it is returned.
* <p>
* Under many circumstances, linking Iterators together in this manner is
* more efficient (and convenient) than using nested for loops to extract a list.
*
* @param root the root object to start iterating from, null results in an empty iterator
* @param transformer the transformer to use, see above, null uses no effect transformer
* @return a new object graph iterator
* @since Commons Collections 3.1
*/
public static Iterator objectGraphIterator(Object root, Transformer transformer) {
return new ObjectGraphIterator(root, transformer);
}
// Transformed
//-----------------------------------------------------------------------
/**
* 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -