1 /* 2 * Copyright (c) 2012, 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.util.stream; 26 27 import java.util.MayHoldCloseableResource; 28 import java.util.Iterator; 29 import java.util.Spliterator; 30 31 /** 32 * Base interface for stream types such as {@link Stream}, {@link IntStream}, 33 * etc. Contains methods common to all stream types. 34 * 35 * @param <T> type of stream elements 36 * @param <S> type of stream implementing {@code BaseStream} 37 * @since 1.8 38 */ 39 public interface BaseStream<T, S extends BaseStream<T, S>> 40 extends MayHoldCloseableResource { 41 /** 42 * Returns an iterator for the elements of this stream. 43 * 44 * <p>This is a <a href="package-summary.html#StreamOps">terminal 45 * operation</a>. 46 * 47 * @return the element iterator for this stream 48 */ 49 Iterator<T> iterator(); 50 51 /** 52 * Returns a spliterator for the elements of this stream. 53 * 54 * <p>This is a <a href="package-summary.html#StreamOps">terminal 55 * operation</a>. 56 * 57 * @return the element spliterator for this stream 58 */ 59 Spliterator<T> spliterator(); 60 61 /** 62 * Returns whether this stream, when executed, would execute in parallel 63 * (assuming no further modification of the stream, such as appending 64 * further intermediate operations or changing its parallelism). Calling 65 * this method after invoking an intermediate or terminal stream operation 66 * method may yield unpredictable results. 67 * 68 * @return {@code true} if this stream would execute in parallel if executed 69 * without further modification otherwise {@code false} 70 */ 71 boolean isParallel(); 72 73 /** 74 * Returns an equivalent stream that is sequential. May return 75 * itself, either because the stream was already sequential, or because 76 * the underlying stream state was modified to be sequential. 77 * 78 * <p>This is an <a href="package-summary.html#StreamOps">intermediate 79 * operation</a>. 80 * 81 * @return a sequential stream 82 */ 83 S sequential(); 84 85 /** 86 * Returns an equivalent stream that is parallel. May return 87 * itself, either because the stream was already parallel, or because 88 * the underlying stream state was modified to be parallel. 89 * 90 * <p>This is an <a href="package-summary.html#StreamOps">intermediate 91 * operation</a>. 92 * 93 * @return a parallel stream 94 */ 95 S parallel(); 96 97 /** 98 * Returns an equivalent stream that is 99 * <a href="package-summary.html#Ordering">unordered</a>. May return 100 * itself if the stream was already unordered. 101 * 102 * <p>This is an <a href="package-summary.html#StreamOps">intermediate 103 * operation</a>. 104 * 105 * @return an unordered stream 106 */ 107 S unordered(); 108 109 /** 110 * Returns an equivalent stream with an additional close handler. Close 111 * handlers are run when the {@link MayHoldCloseableResource#close()} method 112 * is called on the stream, and are executed in the order they were 113 * added. All close handlers are run, even if earlier close handlers throw 114 * exceptions. If any close handler throws an exception, the first 115 * exception thrown will be relayed to the caller of {@code close()}, with 116 * any remaining exceptions added to that exception as suppressed exceptions. 117 * 118 * @param closeHandler A task to execute when the stream is closed 119 * @return a stream with a handler that is run if the stream is closed 120 */ 121 S onClose(Runnable closeHandler); 122 }