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-2024 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<E>; the iterator to wrap as an immutable iterator. 25 */ 26 public ImmutableIterator(final Iterator<E> iterator) 27 { 28 this.iterator = iterator; 29 } 30 31 @Override 32 public final boolean hasNext() 33 { 34 return this.iterator.hasNext(); 35 } 36 37 @Override 38 public final E next() 39 { 40 return this.iterator.next(); 41 } 42 43 @Override 44 public final String toString() 45 { 46 return "ImmutableIterator [iterator=" + this.iterator + "]"; 47 } 48 49 }