2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
27 import java.util.Iterables;
28 import java.util.Iterator;
29 import java.util.Comparator;
30 import java.util.Fillable;
31 import java.util.functions.*;
34 * Implementing this interface allows an object to be the target of
35 * the "for-each loop" statement. See
37 * <a href="{@docRoot}/../technotes/guides/language/foreach.html">For-each Loop</a>
40 * @param <T> the type of elements returned by the iterator
44 public interface Iterable<T> {
47 * Returns an iterator over elements of type {@code T}.
49 * @return an Iterator.
51 Iterator<T> iterator();
54 * Return {@code true} if this Iterable contains no elements.
58 * @return {@code true} if this Iterable contains no elements.
60 boolean isEmpty() default Iterables.isEmpty;
63 * Filter elements according to the provided {@code predicate} and return a
64 * an Iterable view of the filtered elements. The filtered view will reflect
65 * changes in this Iterable.
69 * @param predicate Decides which elements should be included in the
70 * resulting Iterable view. Each element with a {@code true} result will be
71 * included in the resulting view.
72 * @return An Iterable view of the filtered elements.
74 Iterable<T> filter(Predicate<? super T> predicate) default Iterables.filter;
77 * Performs the operation specified by {@code block} upon each element.
81 * @param block The operation to be performed upon each each element.
82 * @return This Iterable.
84 Iterable<T> forEach(Block<? super T> block) default Iterables.forEach;
87 * Map elements using the provided {@code mapper} and return an Iterable
88 * view of the mapped elements. The mappeds view will reflect changes in
93 * @param <U> Type of the returned elements.
94 * @param mapper Performs the mapping between elements of type {@code T}
95 * and elements of type {@code U}.
96 * @return An Iterable view consisting of the mapped elements.
98 <U> Iterable<U> map(Mapper<? super T, ? extends U> mapper) default Iterables.map;
101 * Reduce elements to a single value.
105 * @param reducer Reduces elements to a result of type {@code U}.
106 * @param base Initial value for reducer.
107 * @return The reduced value of the elements.
109 T reduce(T base, Operator<T> reducer) default Iterables.reduce;
111 <U> U mapReduce(Mapper<? super T, ? extends U> mapper, U base, Operator<U> reducer) default Iterables.mapReduce;
112 int mapReduce(IntMapper<? super T> mapper, int base, IntOperator reducer) default Iterables.mapReduce;
113 long mapReduce(LongMapper<? super T> mapper, long base, LongOperator reducer) default Iterables.mapReduce;
114 double mapReduce(DoubleMapper<? super T> mapper, double base, DoubleOperator reducer) default Iterables.mapReduce;
117 * All elements are added to the specified collection.
119 * @param <A> Type of target container.
120 * @param target The collection to which the elements are added.
121 * @return The provided collection.
125 <A extends Fillable<? super T>> A into(A target) default Iterables.into;
128 * Returns {@code true} if any of the elements match the provided predicate.
130 * @param filter a predicate against which returns {@code true} for
132 * @return {@code true} if any elements returned {@code true} for the
133 * provided predicate.
135 public boolean anyMatch(Predicate<? super T> filter) default Iterables.anyMatch;
138 * Returns {@code true} if none of the elements match the provided predicate.
140 * @param filter a predicate against which returns {@code true} for
142 * @return {@code true} if all elements of this collection returned
143 * {@code true} for the provided predicate.
145 public boolean noneMatch(Predicate<? super T> filter) default Iterables.noneMatch;
148 * Returns {@code true} if all of the elements match the provided predicate.
150 * @param filter a predicate against which returns {@code true} for
152 * @return {@code true} if all elements returned {@code true} for the provided predicate.
154 public boolean allMatch(Predicate<? super T> filter) default Iterables.allMatch;
156 // public Iterable<T> sorted() default Iterables.sorted;
158 public Iterable<T> sorted(Comparator<? super T> comparator) default Iterables.sorted;
160 public Iterable<T> uniqueElements() default Iterables.uniqueElements;