    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.collections4.iterators;
    018
    019import java.util.Iterator;
    020
    021import org.apache.commons.collections4.ResettableIterator;
    022
    023/**
    024 * Adapter to make an {@link Iterator Iterator} instance appear to be an
    025 * {@link Iterable Iterable} instance.  The iterable can be constructed in one
    026 * of two variants:  single use, multiple use.
    027 *
    028 * <p>
    029 * In the single use iterable case, the iterable is only usable for one
    030 * iterative operation over the source iterator.  Subsequent iterative
    031 * operations use the same, exhausted source iterator.  To create a single use
    032 * iterable, construct a new {@link IteratorIterable} using a {@link Iterator}
    033 * that is NOT a {@link ResettableIterator} iterator:
    034 * </p>
    035 *
    036 * <pre>
    037 *   Iterator&lt;Integer&gt; iterator = // some non-resettable iterator
    038 *   Iterable&lt;Integer&gt; iterable = new IteratorIterable&lt;Integer&gt;(iterator);
    039 * </pre>
    040 *
    041 * <p>
    042 * In the multiple use iterable case, the iterable is usable for any number of
    043 * iterative operations over the source iterator.  Of special note, even though
    044 * the iterable supports multiple iterations, it does not support concurrent
    045 * iterations. To implicitly create a multiple use iterable, construct a new
    046 * {@link IteratorIterable} using a {@link ResettableIterator} iterator:
    047 * </p>
    048 *
    049 * <pre>
    050 *   Integer[] array = {Integer.valueOf(1),Integer.valueOf(2),Integer.valueOf(3)};
    051 *   Iterator&lt;Integer&gt; iterator = IteratorUtils.arrayIterator(array); // a resettable iterator
    052 *   Iterable&lt;Integer&gt; iterable = new IteratorIterable&lt;Integer&gt;(iterator);
    053 * </pre>
    054 *
    055 * <p>
    056 * A multiple use iterable can also be explicitly constructed using any
    057 * {@link Iterator} and specifying {@code true} for the
    058 * {@code multipleUse} flag:
    059 * </p>
    060 *
    061 * <pre>
    062 *   Iterator&lt;Integer&gt; iterator = // some non-resettable iterator
    063 *   Iterable&lt;Integer&gt; iterable = new IteratorIterable&lt;Integer&gt;(iterator, true);
    064 * </pre>
    065 *
    066 * @param <E> The type of elements returned by this iterator.
    067 * @since 4.0
    068 */
    069public class IteratorIterable<E> implements Iterable<E> {
    070
    071    /**
    072     * Factory method to create an {@link Iterator Iterator} from another
    073     * iterator over objects of a different subtype.
    074     */
    075    private static <E> Iterator<E> createTypesafeIterator(final Iterator<? extends E> iterator) {
    076        return new Iterator<E>() {
    077            @Override
    078            public boolean hasNext() {
    079                return iterator.hasNext();
    080            }
    081
    082            @Override
    083            public E next() {
    084                return iterator.next();
    085            }
    086
    087            @Override
    088            public void remove() {
    089                iterator.remove();
    090            }
    091        };
    092    }
    093
    094    /** The iterator being adapted into an iterable. */
    095    private final Iterator<? extends E> iterator;
    096
    097    /** The iterator parameterized as the {@link #iterator()} return type. */
    098    private final Iterator<E> typeSafeIterator;
    099
    100    /**
    101     * Constructs a new {@code IteratorIterable} that will use the given
    102     * iterator.
    103     *
    104     * @param iterator The iterator to use.
    105     */
    106    public IteratorIterable(final Iterator<? extends E> iterator) {
    107        this(iterator, false);
    108    }
    109
    110    /**
    111     * Constructs a new {@code IteratorIterable} that will use the given
    112     * iterator.
    113     *
    114     * @param iterator The iterator to use.
    115     * @param multipleUse {@code true} if the new iterable can be used in multiple iterations
    116     */
    117    public IteratorIterable(final Iterator<? extends E> iterator, final boolean multipleUse) {
    118        if (multipleUse && !(iterator instanceof ResettableIterator)) {
    119            this.iterator = new ListIteratorWrapper<>(iterator);
    120        } else {
    121            this.iterator = iterator;
    122        }
    123        this.typeSafeIterator = createTypesafeIterator(this.iterator);
    124    }
    125
    126    /**
    127     * Gets the iterator wrapped by this iterable.
    128     *
    129     * @return The iterator
    130     */
    131    @Override
    132    public Iterator<E> iterator() {
    133        if (iterator instanceof ResettableIterator) {
    134            ((ResettableIterator<? extends E>) iterator).reset();
    135        }
    136        return typeSafeIterator;
    137    }
    138}