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