1 /*
2 * Copyright (c) 2012, 2015, 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
1168
1169 /**
1170 * Returns an infinite sequential ordered {@code Stream} produced by iterative
1171 * application of a function {@code f} to an initial element {@code seed},
1172 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
1173 * {@code f(f(seed))}, etc.
1174 *
1175 * <p>The first element (position {@code 0}) in the {@code Stream} will be
1176 * the provided {@code seed}. For {@code n > 0}, the element at position
1177 * {@code n}, will be the result of applying the function {@code f} to the
1178 * element at position {@code n - 1}.
1179 *
1180 * @param <T> the type of stream elements
1181 * @param seed the initial element
1182 * @param f a function to be applied to the previous element to produce
1183 * a new element
1184 * @return a new sequential {@code Stream}
1185 */
1186 public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
1187 Objects.requireNonNull(f);
1188 final Iterator<T> iterator = new Iterator<T>() {
1189 @SuppressWarnings("unchecked")
1190 T t = (T) Streams.NONE;
1191
1192 @Override
1193 public boolean hasNext() {
1194 return true;
1195 }
1196
1197 @Override
1198 public T next() {
1199 return t = (t == Streams.NONE) ? seed : f.apply(t);
1200 }
1201 };
1202 return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
1203 iterator,
1204 Spliterator.ORDERED | Spliterator.IMMUTABLE), false);
1205 }
1206
1207 /**
1208 * Returns an infinite sequential unordered stream where each element is
1209 * generated by the provided {@code Supplier}. This is suitable for
1210 * generating constant streams, streams of random elements, etc.
1211 *
1212 * @param <T> the type of stream elements
1213 * @param s the {@code Supplier} of generated elements
1214 * @return a new infinite sequential unordered {@code Stream}
1215 */
1216 public static<T> Stream<T> generate(Supplier<T> s) {
1217 Objects.requireNonNull(s);
1218 return StreamSupport.stream(
1219 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
1220 }
1221
1222 /**
1223 * Creates a lazily concatenated stream whose elements are all the
1224 * elements of the first stream followed by all the elements of the
|
1 /*
2 * Copyright (c) 2012, 2016, 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
1168
1169 /**
1170 * Returns an infinite sequential ordered {@code Stream} produced by iterative
1171 * application of a function {@code f} to an initial element {@code seed},
1172 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
1173 * {@code f(f(seed))}, etc.
1174 *
1175 * <p>The first element (position {@code 0}) in the {@code Stream} will be
1176 * the provided {@code seed}. For {@code n > 0}, the element at position
1177 * {@code n}, will be the result of applying the function {@code f} to the
1178 * element at position {@code n - 1}.
1179 *
1180 * @param <T> the type of stream elements
1181 * @param seed the initial element
1182 * @param f a function to be applied to the previous element to produce
1183 * a new element
1184 * @return a new sequential {@code Stream}
1185 */
1186 public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
1187 Objects.requireNonNull(f);
1188 Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE,
1189 Spliterator.ORDERED | Spliterator.IMMUTABLE) {
1190 T prev;
1191 boolean started;
1192
1193 @Override
1194 public boolean tryAdvance(Consumer<? super T> action) {
1195 Objects.requireNonNull(action);
1196 T t;
1197 if (started)
1198 t = f.apply(prev);
1199 else {
1200 t = seed;
1201 started = true;
1202 }
1203 action.accept(prev = t);
1204 return true;
1205 }
1206 };
1207 return StreamSupport.stream(spliterator, false);
1208 }
1209
1210 /**
1211 * Returns a sequential ordered {@code Stream} produced by iterative
1212 * application of a function to an initial element, conditioned on
1213 * satisfying the supplied predicate. The stream terminates as soon as
1214 * the predicate function returns false.
1215 *
1216 * <p>
1217 * {@code Stream.iterate} should produce the same sequence of elements as
1218 * produced by the corresponding for-loop:
1219 * <pre>{@code
1220 * for (T index=seed; predicate.test(index); index = f.apply(index)) {
1221 * ...
1222 * }
1223 * }</pre>
1224 *
1225 * <p>
1226 * The resulting sequence may be empty if the predicate does not hold on
1227 * the seed value. Otherwise the first element will be the supplied seed
1228 * value, the next element (if present) will be the result of applying the
1229 * function f to the seed value, and so on iteratively until the predicate
1230 * indicates that the stream should terminate.
1231 *
1232 * @param <T> the type of stream elements
1233 * @param seed the initial element
1234 * @param predicate a predicate to apply to elements to determine when the
1235 * stream must terminate.
1236 * @param f a function to be applied to the previous element to produce
1237 * a new element
1238 * @return a new sequential {@code Stream}
1239 * @since 9
1240 */
1241 public static<T> Stream<T> iterate(T seed, Predicate<? super T> predicate, UnaryOperator<T> f) {
1242 Objects.requireNonNull(f);
1243 Objects.requireNonNull(predicate);
1244 Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE,
1245 Spliterator.ORDERED | Spliterator.IMMUTABLE) {
1246 T prev;
1247 boolean started, finished;
1248
1249 @Override
1250 public boolean tryAdvance(Consumer<? super T> action) {
1251 Objects.requireNonNull(action);
1252 if (finished)
1253 return false;
1254 T t;
1255 if (started)
1256 t = f.apply(prev);
1257 else {
1258 t = seed;
1259 started = true;
1260 }
1261 if (!predicate.test(t)) {
1262 prev = null;
1263 finished = true;
1264 return false;
1265 }
1266 action.accept(prev = t);
1267 return true;
1268 }
1269
1270 @Override
1271 public void forEachRemaining(Consumer<? super T> action) {
1272 Objects.requireNonNull(action);
1273 if (finished)
1274 return;
1275 finished = true;
1276 T t = started ? f.apply(prev) : seed;
1277 prev = null;
1278 while (predicate.test(t)) {
1279 action.accept(t);
1280 t = f.apply(t);
1281 }
1282 }
1283 };
1284 return StreamSupport.stream(spliterator, false);
1285 }
1286
1287 /**
1288 * Returns an infinite sequential unordered stream where each element is
1289 * generated by the provided {@code Supplier}. This is suitable for
1290 * generating constant streams, streams of random elements, etc.
1291 *
1292 * @param <T> the type of stream elements
1293 * @param s the {@code Supplier} of generated elements
1294 * @return a new infinite sequential unordered {@code Stream}
1295 */
1296 public static<T> Stream<T> generate(Supplier<T> s) {
1297 Objects.requireNonNull(s);
1298 return StreamSupport.stream(
1299 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
1300 }
1301
1302 /**
1303 * Creates a lazily concatenated stream whose elements are all the
1304 * elements of the first stream followed by all the elements of the
|