/** * Performs an action for each element of this stream. * * @param action an action to perform on the elements */ public void forEach(Consumer<? super T> action) { addProcessorNode(new ForEachProcessor<>(action), new Fields()); }
private <R> Stream<R> merge(CombinerAggregator<?, T, ? extends R> aggregator) { return new Stream<>(streamBuilder, addProcessorNode(new MergeAggregateProcessor<>(aggregator), VALUE)); }
/** * Returns a stream consisting of the elements of this stream that matches the given filter. * * @param predicate the predicate to apply to each element to determine if it should be included * @return the new stream */ public Stream<T> filter(Predicate<? super T> predicate) { return new Stream<>(streamBuilder, addProcessorNode(new FilterProcessor<>(predicate), VALUE, true)); }
/** * Returns a stream consisting of the result of applying the given mapping function to the values of this stream. * * @param function a mapping function to be applied to each value in this stream. * @return the new stream */ public <R> Stream<R> map(Function<? super T, ? extends R> function) { return new Stream<>(streamBuilder, addProcessorNode(new MapProcessor<>(function), VALUE)); }
/** * Returns a stream of key-value pairs by applying a {@link PairFunction} on each value of this stream. * * @param function the mapping function to be applied to each value in this stream * @param <K> the key type * @param <V> the value type * @return the new stream of key-value pairs */ public <K, V> PairStream<K, V> mapToPair(PairFunction<? super T, ? extends K, ? extends V> function) { return new PairStream<>(streamBuilder, addProcessorNode(new MapProcessor<>(function), KEY_VALUE)); }
/** * Returns a stream consisting of the results of replacing each value of this stream with the contents produced by applying the provided * mapping function to each value. This has the effect of applying a one-to-many transformation to the values of the stream, and then * flattening the resulting elements into a new stream. * * @param function a mapping function to be applied to each value in this stream which produces new values. * @return the new stream */ public <R> Stream<R> flatMap(FlatMapFunction<? super T, ? extends R> function) { return new Stream<>(streamBuilder, addProcessorNode(new FlatMapProcessor<>(function), VALUE)); }
/** * Returns a stream consisting of the results of replacing each value of this stream with the key-value pairs produced by applying the * provided mapping function to each value. * * @param function the mapping function to be applied to each value in this stream which produces new key-value pairs. * @param <K> the key type * @param <V> the value type * @return the new stream of key-value pairs * * @see #flatMap(FlatMapFunction) * @see #mapToPair(PairFunction) */ public <K, V> PairStream<K, V> flatMapToPair(PairFlatMapFunction<? super T, ? extends K, ? extends V> function) { return new PairStream<>(streamBuilder, addProcessorNode(new FlatMapProcessor<>(function), KEY_VALUE)); }
private <A, R> Stream<R> aggregatePartition(CombinerAggregator<? super T, A, ? extends R> aggregator) { return new Stream<>(streamBuilder, addProcessorNode(new AggregateProcessor<>(aggregator), VALUE)); }
private <A> Stream<A> combinePartition(CombinerAggregator<? super T, A, ?> aggregator) { return new Stream<>(streamBuilder, addProcessorNode(new AggregateProcessor<>(aggregator, true), VALUE, true)); }
private Stream<T> reducePartition(Reducer<T> reducer) { return new Stream<>(streamBuilder, addProcessorNode(new ReduceProcessor<>(reducer), VALUE)); }
/** * Queries the given stream state with the values in this stream as the keys. * * @param streamState the stream state * @param <V> the value type * @return the result stream */ public <V> PairStream<T, V> stateQuery(StreamState<T, V> streamState) { // need field grouping for state query so that the query is routed to the correct task Node newNode = partitionBy(VALUE, node.getParallelism()).addProcessorNode(new StateQueryProcessor<>(streamState), KEY_VALUE); return new PairStream<>(streamBuilder, newNode); }
/** * Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as they are * consumed from the resulting stream. * * @param action the action to perform on the element as they are consumed from the stream * @return the new stream */ public Stream<T> peek(Consumer<? super T> action) { return new Stream<>(streamBuilder, addProcessorNode(new PeekProcessor<>(action), node.getOutputFields(), true)); }
/** * Returns an array of streams by splitting the given stream into multiple branches based on the given predicates. The predicates are * applied in the given order to the values of this stream and the result is forwarded to the corresponding (index based) result stream * based on the (index of) predicate that matches. * <p> * <b>Note:</b> If none of the predicates match a value, that value is dropped. * </p> * * @param predicates the predicates * @return an array of result streams (branches) corresponding to the given predicates */ @SuppressWarnings("unchecked") public Stream<T>[] branch(Predicate<? super T>... predicates) { List<Stream<T>> childStreams = new ArrayList<>(); if (predicates.length > 0) { BranchProcessor<T> branchProcessor = new BranchProcessor<>(); Node branchNode = addProcessorNode(branchProcessor, VALUE); for (Predicate<? super T> predicate : predicates) { // create a child node (identity) per branch ProcessorNode child = makeProcessorNode(new MapProcessor<>(new IdentityFunction<>()), node.getOutputFields()); String branchStream = child.getOutputStreams().iterator().next() + "-branch"; // branchStream is the parent stream that connects branch processor to this child branchNode.addOutputStream(branchStream); addNode(branchNode, child, branchStream); childStreams.add(new Stream<>(streamBuilder, child)); branchProcessor.addPredicate(predicate, branchStream); } } return childStreams.toArray((Stream<T>[]) new Stream[childStreams.size()]); }