| | |
| | | */ |
| | | public final class Iterators |
| | | { |
| | | private static final class ArrayIterator<M> implements Iterator<M> |
| | | { |
| | | private int i = 0; |
| | | private final M[] a; |
| | | |
| | | |
| | | |
| | | // Constructed via factory methods. |
| | | private ArrayIterator(final M[] a) |
| | | { |
| | | this.a = a; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public boolean hasNext() |
| | | { |
| | | return i < a.length; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public M next() |
| | | { |
| | | if (hasNext()) |
| | | { |
| | | return a[i++]; |
| | | } |
| | | else |
| | | { |
| | | throw new NoSuchElementException(); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public void remove() |
| | | { |
| | | throw new UnsupportedOperationException(); |
| | | } |
| | | |
| | | }; |
| | | |
| | | |
| | | |
| | | private static final class EmptyIterator<M> implements Iterator<M> |
| | | { |
| | | /** |
| | |
| | | { |
| | | throw new UnsupportedOperationException(); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | |
| | | |
| | | private static final class FilteredIterator<M, P> implements |
| | | Iterator<M> |
| | | private static final class FilteredIterator<M, P> implements Iterator<M> |
| | | { |
| | | |
| | | private boolean hasNextMustIterate = true; |
| | |
| | | |
| | | |
| | | // Constructed via factory methods. |
| | | private FilteredIterator(Iterator<M> iterator, |
| | | Predicate<? super M, P> predicate, P p) |
| | | private FilteredIterator(final Iterator<M> iterator, |
| | | final Predicate<? super M, P> predicate, final P p) |
| | | { |
| | | this.iterator = iterator; |
| | | this.predicate = predicate; |
| | |
| | | |
| | | |
| | | |
| | | private static final class SingletonIterator<M> implements |
| | | Iterator<M> |
| | | private static final class SingletonIterator<M> implements Iterator<M> |
| | | { |
| | | private M value; |
| | | |
| | | |
| | | |
| | | // Constructed via factory methods. |
| | | private SingletonIterator(M value) |
| | | private SingletonIterator(final M value) |
| | | { |
| | | this.value = value; |
| | | } |
| | |
| | | { |
| | | if (value != null) |
| | | { |
| | | M tmp = value; |
| | | final M tmp = value; |
| | | value = null; |
| | | return tmp; |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | private static final class ArrayIterator<M> implements Iterator<M> |
| | | { |
| | | private int i = 0; |
| | | private final M[] a; |
| | | |
| | | |
| | | |
| | | // Constructed via factory methods. |
| | | private ArrayIterator(M[] a) |
| | | { |
| | | this.a = a; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public boolean hasNext() |
| | | { |
| | | return i < a.length; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public M next() |
| | | { |
| | | if (hasNext()) |
| | | { |
| | | return a[i++]; |
| | | } |
| | | else |
| | | { |
| | | throw new NoSuchElementException(); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public void remove() |
| | | { |
| | | throw new UnsupportedOperationException(); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | private static final class TransformedIterator<M, N, P> implements |
| | | Iterator<N> |
| | | { |
| | |
| | | |
| | | |
| | | // Constructed via factory methods. |
| | | private TransformedIterator(Iterator<M> iterator, |
| | | Function<? super M, ? extends N, P> function, P p) |
| | | private TransformedIterator(final Iterator<M> iterator, |
| | | final Function<? super M, ? extends N, P> function, final P p) |
| | | { |
| | | this.iterator = iterator; |
| | | this.function = function; |
| | |
| | | |
| | | |
| | | |
| | | private static final class UnmodifiableIterator<M> implements |
| | | Iterator<M> |
| | | private static final class UnmodifiableIterator<M> implements Iterator<M> |
| | | { |
| | | private final Iterator<M> iterator; |
| | | |
| | | |
| | | |
| | | private UnmodifiableIterator(Iterator<M> iterator) |
| | | private UnmodifiableIterator(final Iterator<M> iterator) |
| | | { |
| | | this.iterator = iterator; |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private static final Iterator<Object> EMPTY_ITERATOR = |
| | | new EmptyIterator<Object>(); |
| | | |
| | | |
| | | private static final Iterator<Object> EMPTY_ITERATOR = new EmptyIterator<Object>(); |
| | | |
| | | |
| | | |
| | | /** |
| | | * Returns an iterator over the elements contained in {@code a}. The returned |
| | | * iterator does not support element removal via the {@code remove()} method. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code a}. |
| | | * @param a |
| | | * The array of elements to be returned by the iterator. |
| | | * @return An iterator over the elements contained in {@code a}. |
| | | */ |
| | | public static <M> Iterator<M> arrayIterator(final M[] a) |
| | | { |
| | | return new ArrayIterator<M>(a); |
| | | } |
| | | |
| | | |
| | | |
| | |
| | | |
| | | |
| | | /** |
| | | * Returns a filtered view of {@code iterator} containing only those |
| | | * elements which match {@code predicate}. The returned iterator |
| | | * supports element removal via the {@code remove()} method subject to |
| | | * any constraints imposed by {@code iterator}. |
| | | * Returns a filtered view of {@code iterator} containing only those elements |
| | | * which match {@code predicate}. The returned iterator supports element |
| | | * removal via the {@code remove()} method subject to any constraints imposed |
| | | * by {@code iterator}. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code iterator}. |
| | | * @param <P> |
| | | * The type of the additional parameter to the predicate's |
| | | * {@code matches} method. Use {@link java.lang.Void} for |
| | | * predicates that do not need an additional parameter. |
| | | * The type of the additional parameter to the predicate's {@code |
| | | * matches} method. Use {@link java.lang.Void} for predicates that do |
| | | * not need an additional parameter. |
| | | * @param iterator |
| | | * The iterator to be filtered. |
| | | * @param predicate |
| | | * The predicate. |
| | | * @param p |
| | | * A predicate specified parameter. |
| | | * @return A filtered view of {@code iterator} containing only those |
| | | * elements which match {@code predicate}. |
| | | * @return A filtered view of {@code iterator} containing only those elements |
| | | * which match {@code predicate}. |
| | | */ |
| | | public static <M, P> Iterator<M> filter(Iterator<M> iterator, |
| | | Predicate<? super M, P> predicate, P p) |
| | | public static <M, P> Iterator<M> filter(final Iterator<M> iterator, |
| | | final Predicate<? super M, P> predicate, final P p) |
| | | { |
| | | return new FilteredIterator<M, P>(iterator, predicate, p); |
| | | } |
| | |
| | | |
| | | |
| | | /** |
| | | * Returns a filtered view of {@code iterator} containing only those |
| | | * elements which match {@code predicate}. The returned iterator |
| | | * supports element removal via the {@code remove()} method subject to |
| | | * any constraints imposed by {@code iterator}. |
| | | * Returns a filtered view of {@code iterator} containing only those elements |
| | | * which match {@code predicate}. The returned iterator supports element |
| | | * removal via the {@code remove()} method subject to any constraints imposed |
| | | * by {@code iterator}. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code iterator}. |
| | |
| | | * The iterator to be filtered. |
| | | * @param predicate |
| | | * The predicate. |
| | | * @return A filtered view of {@code iterator} containing only those |
| | | * elements which match {@code predicate}. |
| | | * @return A filtered view of {@code iterator} containing only those elements |
| | | * which match {@code predicate}. |
| | | */ |
| | | public static <M> Iterator<M> filter(Iterator<M> iterator, |
| | | Predicate<? super M, Void> predicate) |
| | | public static <M> Iterator<M> filter(final Iterator<M> iterator, |
| | | final Predicate<? super M, Void> predicate) |
| | | { |
| | | return new FilteredIterator<M, Void>(iterator, predicate, null); |
| | | } |
| | |
| | | |
| | | |
| | | /** |
| | | * Returns an iterator containing the single element {@code value}. |
| | | * The returned iterator does not support element removal via the |
| | | * {@code remove()} method. |
| | | * Returns an iterator containing the single element {@code value}. The |
| | | * returned iterator does not support element removal via the {@code remove()} |
| | | * method. |
| | | * |
| | | * @param <M> |
| | | * The type of the single element {@code value}. |
| | |
| | | * The single element to be returned by the iterator. |
| | | * @return An iterator containing the single element {@code value}. |
| | | */ |
| | | public static <M> Iterator<M> singleton(M value) |
| | | public static <M> Iterator<M> singleton(final M value) |
| | | { |
| | | return new SingletonIterator<M>(value); |
| | | } |
| | |
| | | |
| | | |
| | | /** |
| | | * Returns an iterator over the elements contained in {@code a}. The |
| | | * returned iterator does not support element removal via the {@code |
| | | * remove()} method. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code a}. |
| | | * @param a |
| | | * The array of elements to be returned by the iterator. |
| | | * @return An iterator over the elements contained in {@code a}. |
| | | */ |
| | | public static <M> Iterator<M> arrayIterator(M[] a) |
| | | { |
| | | return new ArrayIterator<M>(a); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Returns a view of {@code iterator} whose values have been mapped to |
| | | * elements of type {@code N} using {@code function}. The returned |
| | | * iterator supports element removal via the {@code remove()} method |
| | | * subject to any constraints imposed by {@code iterator}. |
| | | * elements of type {@code N} using {@code function}. The returned iterator |
| | | * supports element removal via the {@code remove()} method subject to any |
| | | * constraints imposed by {@code iterator}. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code iterator}. |
| | | * @param <N> |
| | | * The type of elements contained in the returned iterator. |
| | | * @param <P> |
| | | * The type of the additional parameter to the function's |
| | | * {@code apply} method. Use {@link java.lang.Void} for |
| | | * functions that do not need an additional parameter. |
| | | * The type of the additional parameter to the function's {@code |
| | | * apply} method. Use {@link java.lang.Void} for functions that do |
| | | * not need an additional parameter. |
| | | * @param iterator |
| | | * The iterator to be transformed. |
| | | * @param function |
| | |
| | | * @return A view of {@code iterator} whose values have been mapped to |
| | | * elements of type {@code N} using {@code function}. |
| | | */ |
| | | public static <M, N, P> Iterator<N> transform(Iterator<M> iterator, |
| | | Function<? super M, ? extends N, P> function, P p) |
| | | public static <M, N, P> Iterator<N> transform(final Iterator<M> iterator, |
| | | final Function<? super M, ? extends N, P> function, final P p) |
| | | { |
| | | return new TransformedIterator<M, N, P>(iterator, function, p); |
| | | } |
| | |
| | | |
| | | /** |
| | | * Returns a view of {@code iterator} whose values have been mapped to |
| | | * elements of type {@code N} using {@code function}. The returned |
| | | * iterator supports element removal via the {@code remove()} method |
| | | * subject to any constraints imposed by {@code iterator}. |
| | | * elements of type {@code N} using {@code function}. The returned iterator |
| | | * supports element removal via the {@code remove()} method subject to any |
| | | * constraints imposed by {@code iterator}. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code iterator}. |
| | |
| | | * @return A view of {@code iterator} whose values have been mapped to |
| | | * elements of type {@code N} using {@code function}. |
| | | */ |
| | | public static <M, N> Iterator<N> transform(Iterator<M> iterator, |
| | | Function<? super M, ? extends N, Void> function) |
| | | public static <M, N> Iterator<N> transform(final Iterator<M> iterator, |
| | | final Function<? super M, ? extends N, Void> function) |
| | | { |
| | | return new TransformedIterator<M, N, Void>(iterator, function, null); |
| | | } |
| | |
| | | |
| | | |
| | | /** |
| | | * Returns a read-only view of {@code iterator} which does not support |
| | | * element removal via the {@code remove()}. Attempts to use the |
| | | * {@code remove()} method will result in a {@code |
| | | * UnsupportedOperationException}. |
| | | * Returns a read-only view of {@code iterator} which does not support element |
| | | * removal via the {@code remove()}. Attempts to use the {@code remove()} |
| | | * method will result in a {@code UnsupportedOperationException}. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code iterator}. |
| | | * @param iterator |
| | | * The iterator to be made read-only. |
| | | * @return A read-only view of {@code iterator} which does not support |
| | | * element removal via the {@code remove()}. |
| | | * @return A read-only view of {@code iterator} which does not support element |
| | | * removal via the {@code remove()}. |
| | | */ |
| | | public static <M> Iterator<M> unmodifiable(Iterator<M> iterator) |
| | | public static <M> Iterator<M> unmodifiable(final Iterator<M> iterator) |
| | | { |
| | | return new UnmodifiableIterator<M>(iterator); |
| | | } |