1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javany.util;
  27 
  28 import java.util.Objects;
  29 import javany.util.function.Consumer;
  30 
  31 /**
  32  * An iterator over a collection.  {@code Iterator} takes the place of
  33  * {@link Enumeration} in the Java Collections Framework.  Iterators
  34  * differ from enumerations in two ways:
  35  *
  36  * <ul>
  37  *      <li> Iterators allow the caller to remove elements from the
  38  *           underlying collection during the iteration with well-defined
  39  *           semantics.
  40  *      <li> Method names have been improved.
  41  * </ul>
  42  *
  43  * <p>This interface is a member of the
  44  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  45  * Java Collections Framework</a>.
  46  *
  47  * @param <E> the type of elements returned by this iterator
  48  *
  49  * @author  Josh Bloch
  50  * @see Collection
  51  * @see ListIterator
  52  * @see Iterable
  53  * @since 1.2
  54  */
  55 public interface Iterator<any E> {
  56     /**
  57      * Returns {@code true} if the iteration has more elements.
  58      * (In other words, returns {@code true} if {@link #next} would
  59      * return an element rather than throwing an exception.)
  60      *
  61      * @return {@code true} if the iteration has more elements
  62      */
  63     boolean hasNext();
  64 
  65     /**
  66      * Returns the next element in the iteration.
  67      *
  68      * @return the next element in the iteration
  69      * @throws java.util.NoSuchElementException if the iteration has no more elements
  70      */
  71     E next();
  72 
  73     /**
  74      * Removes from the underlying collection the last element returned
  75      * by this iterator (optional operation).  This method can be called
  76      * only once per call to {@link #next}.  The behavior of an iterator
  77      * is unspecified if the underlying collection is modified while the
  78      * iteration is in progress in any way other than by calling this
  79      * method.
  80      *
  81      * @implSpec
  82      * The default implementation throws an instance of
  83      * {@link UnsupportedOperationException} and performs no other action.
  84      *
  85      * @throws UnsupportedOperationException if the {@code remove}
  86      *         operation is not supported by this iterator
  87      *
  88      * @throws IllegalStateException if the {@code next} method has not
  89      *         yet been called, or the {@code remove} method has already
  90      *         been called after the last call to the {@code next}
  91      *         method
  92      */
  93     default void remove() {
  94         throw new UnsupportedOperationException("remove");
  95     }
  96 
  97     /**
  98      * Performs the given action for each remaining element until all elements
  99      * have been processed or the action throws an exception.  Actions are
 100      * performed in the order of iteration, if that order is specified.
 101      * Exceptions thrown by the action are relayed to the caller.
 102      *
 103      * @implSpec
 104      * <p>The default implementation behaves as if:
 105      * <pre>{@code
 106      *     while (hasNext())
 107      *         action.accept(next());
 108      * }</pre>
 109      *
 110      * @param action The action to be performed for each element
 111      * @throws NullPointerException if the specified action is null
 112      * @since 1.8
 113      */
 114     default void forEachRemaining(Consumer<? super E> action) {
 115         Objects.requireNonNull(action);
 116         while (hasNext())
 117             action.accept(next());
 118     }
 119 }