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