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.Spliterator; 28 29 /** 30 * An operation in a stream pipeline that takes a stream as input and produces 31 * a result or side-effect. A {@code TerminalOp} has an input type and stream 32 * shape, and a result type. A {@code TerminalOp} also has a set of 33 * <em>operation flags</em> that describes how the operation processes elements 34 * of the stream (such as short-circuiting or respecting encounter order; see 35 * {@link StreamOpFlag}). 36 * 37 * <p>A {@code TerminalOp} must provide a sequential and parallel implementation 38 * of the operation relative to a given stream source and set of intermediate 39 * operations. 40 * 41 * @param <E_IN> the type of input elements 42 * @param <R> the type of the result 43 * @since 1.8 44 */ 45 interface TerminalOp<E_IN, R> { 46 /** 47 * Gets the shape of the input type of this operation. 48 * 49 * @implSpec The default returns {@code StreamShape.REFERENCE}. 50 * 51 * @return StreamShape of the input type of this operation 52 */ inputShape()53 default StreamShape inputShape() { return StreamShape.REFERENCE; } 54 55 /** 56 * Gets the stream flags of the operation. Terminal operations may set a 57 * limited subset of the stream flags defined in {@link StreamOpFlag}, and 58 * these flags are combined with the previously combined stream and 59 * intermediate operation flags for the pipeline. 60 * 61 * @implSpec The default implementation returns zero. 62 * 63 * @return the stream flags for this operation 64 * @see StreamOpFlag 65 */ getOpFlags()66 default int getOpFlags() { return 0; } 67 68 /** 69 * Performs a parallel evaluation of the operation using the specified 70 * {@code PipelineHelper}, which describes the upstream intermediate 71 * operations. 72 * 73 * @implSpec The default performs a sequential evaluation of the operation 74 * using the specified {@code PipelineHelper}. 75 * 76 * @param helper the pipeline helper 77 * @param spliterator the source spliterator 78 * @return the result of the evaluation 79 */ evaluateParallel(PipelineHelper<E_IN> helper, Spliterator<P_IN> spliterator)80 default <P_IN> R evaluateParallel(PipelineHelper<E_IN> helper, 81 Spliterator<P_IN> spliterator) { 82 if (Tripwire.ENABLED) 83 Tripwire.trip(getClass(), "{0} triggering TerminalOp.evaluateParallel serial default"); 84 return evaluateSequential(helper, spliterator); 85 } 86 87 /** 88 * Performs a sequential evaluation of the operation using the specified 89 * {@code PipelineHelper}, which describes the upstream intermediate 90 * operations. 91 * 92 * @param helper the pipeline helper 93 * @param spliterator the source spliterator 94 * @return the result of the evaluation 95 */ evaluateSequential(PipelineHelper<E_IN> helper, Spliterator<P_IN> spliterator)96 <P_IN> R evaluateSequential(PipelineHelper<E_IN> helper, 97 Spliterator<P_IN> spliterator); 98 } 99