View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.iterators;
18  
19  import java.util.Iterator;
20  
21  import org.apache.commons.collections4.ResettableIterator;
22  
23  /**
24   * Adapter to make an {@link Iterator Iterator} instance appear to be an
25   * {@link Iterable Iterable} instance.  The iterable can be constructed in one
26   * of two variants:  single use, multiple use.
27   *
28   * <p>
29   * In the single use iterable case, the iterable is only usable for one
30   * iterative operation over the source iterator.  Subsequent iterative
31   * operations use the same, exhausted source iterator.  To create a single use
32   * iterable, construct a new {@link IteratorIterable} using a {@link Iterator}
33   * that is NOT a {@link ResettableIterator} iterator:
34   * </p>
35   *
36   * <pre>
37   *   Iterator&lt;Integer&gt; iterator = // some non-resettable iterator
38   *   Iterable&lt;Integer&gt; iterable = new IteratorIterable&lt;Integer&gt;(iterator);
39   * </pre>
40   *
41   * <p>
42   * In the multiple use iterable case, the iterable is usable for any number of
43   * iterative operations over the source iterator.  Of special note, even though
44   * the iterable supports multiple iterations, it does not support concurrent
45   * iterations. To implicitly create a multiple use iterable, construct a new
46   * {@link IteratorIterable} using a {@link ResettableIterator} iterator:
47   * </p>
48   *
49   * <pre>
50   *   Integer[] array = {Integer.valueOf(1),Integer.valueOf(2),Integer.valueOf(3)};
51   *   Iterator&lt;Integer&gt; iterator = IteratorUtils.arrayIterator(array); // a resettable iterator
52   *   Iterable&lt;Integer&gt; iterable = new IteratorIterable&lt;Integer&gt;(iterator);
53   * </pre>
54   *
55   * <p>
56   * A multiple use iterable can also be explicitly constructed using any
57   * {@link Iterator} and specifying {@code true} for the
58   * {@code multipleUse} flag:
59   * </p>
60   *
61   * <pre>
62   *   Iterator&lt;Integer&gt; iterator = // some non-resettable iterator
63   *   Iterable&lt;Integer&gt; iterable = new IteratorIterable&lt;Integer&gt;(iterator, true);
64   * </pre>
65   *
66   * @param <E> the type of elements returned by this iterator.
67   * @since 4.0
68   */
69  public class IteratorIterable<E> implements Iterable<E> {
70  
71      /**
72       * Factory method to create an {@link Iterator Iterator} from another
73       * iterator over objects of a different subtype.
74       */
75      private static <E> Iterator<E> createTypesafeIterator(final Iterator<? extends E> iterator) {
76          return new Iterator<E>() {
77              @Override
78              public boolean hasNext() {
79                  return iterator.hasNext();
80              }
81  
82              @Override
83              public E next() {
84                  return iterator.next();
85              }
86  
87              @Override
88              public void remove() {
89                  iterator.remove();
90              }
91          };
92      }
93  
94      /** The iterator being adapted into an iterable. */
95      private final Iterator<? extends E> iterator;
96  
97      /** The iterator parameterized as the {@link #iterator()} return type. */
98      private final Iterator<E> typeSafeIterator;
99  
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 }