1 /*
   2  * Copyright (c) 2003, 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 package java.lang;
  26 
  27 import java.util.Iterator;
  28 import java.util.Objects;
  29 import java.util.function.Consumer;
  30 
  31 /**
  32  * Implementing this interface allows an object to be the target of
  33  * the "for-each loop" statement. See
  34  * <strong>
  35  * <a href="{@docRoot}/../technotes/guides/language/foreach.html">For-each Loop</a>
  36  * </strong>
  37  *
  38  * @param <T> the type of elements returned by the iterator
  39  *
  40  * @since 1.5
  41  * @jls 14.14.2 The enhanced for statement
  42  */
  43 @FunctionalInterface
  44 public interface Iterable<T> {
  45     /**
  46      * Returns an iterator over elements of type {@code T}.
  47      *
  48      * @return an Iterator.
  49      */
  50     Iterator<T> iterator();
  51 
  52     /**
  53      * Performs the given action on the contents of the {@code Iterable}, in the
  54      * order elements occur when iterating, until all elements have been
  55      * processed or the action throws an exception.  Errors or runtime
  56      * exceptions thrown by the action are relayed to the caller.
  57      *
  58      * @implSpec
  59      * <p>The default implementation behaves as if:
  60      * <pre>{@code
  61      *     for (T t : this)
  62      *         action.accept(t);
  63      * }</pre>
  64      *
  65      * @param action The action to be performed for each element
  66      * @throws NullPointerException if the specified action is null
  67      * @since 1.8
  68      */
  69     default void forEach(Consumer<? super T> action) {
  70         Objects.requireNonNull(action);
  71         for (T t : this) {
  72             action.accept(t);
  73         }
  74     }
  75 }
  76