View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.Iterator;
4   
5   /**
6    * An immutable iterator over elements, wrapping a "mutable" iterator. The default remove method from the interface will throw
7    * an exception.
8    * <p>
9    * Copyright (c) 2016-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
10   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
11   * distributed under a three-clause BSD-style license, which can be found at
12   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
13   * </p>
14   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
16   * @param <E> the element type
17   */
18  public class ImmutableIterator<E> implements Iterator<E>
19  {
20      /** the wrapped iterator. */
21      private final Iterator<E> iterator;
22  
23      /**
24       * @param iterator Iterator&lt;E&gt;; the iterator to wrap as an immutable iterator.
25       */
26      public ImmutableIterator(final Iterator<E> iterator)
27      {
28          this.iterator = iterator;
29      }
30  
31      /** {@inheritDoc} */
32      @Override
33      public final boolean hasNext()
34      {
35          return this.iterator.hasNext();
36      }
37  
38      /** {@inheritDoc} */
39      @Override
40      public final E next()
41      {
42          return this.iterator.next();
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      public final String toString()
48      {
49          return "ImmutableIterator [iterator=" + this.iterator + "]";
50      }
51  
52  }