All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aol.cyclops.streams.future.FutureOperationsImpl Maven / Gradle / Ivy

There is a newer version: 7.3.1
Show newest version
package com.aol.cyclops.streams.future;

import java.util.Collection;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;

import lombok.AllArgsConstructor;
import lombok.Getter;

import com.aol.cyclops.sequence.SequenceM;
import com.aol.cyclops.sequence.future.FutureOperations;



@AllArgsConstructor
public class FutureOperationsImpl implements FutureOperations, DoubleOperatorsMixin, 
						IntOperatorsMixin, LongOperatorsMixin {

	@Getter
	private final Executor exec;
	@Getter
	private final SequenceM stream;
	
	/**
	 * Asynchronously convert  this Stream into a List
	 *  
	 * {@code
	 *  CompletableFuture> myList = EagerFutureStream.of(1,2,3,4)
	 *  														.map(this::loadFromDb)
	 *  														.withTaskExecutor(parallelBuilder().getExecutor())
	 *  														.map(this::processOnDifferentExecutor)
	 *  														.toList();
	 * }
	 * 
* * @return Future List */ public CompletableFuture> toList(){ return CompletableFuture.supplyAsync(()->stream.toList(),exec); } /** * @return Last value in this Stream (must be non-empty) */ public CompletableFuture lastValue(){ return CompletableFuture.supplyAsync(()->{ List l= stream.toList(); return l.get(l.size()-1);},exec); } /** * @return the only entry in this Stream if it is a single entry Stream, * otherwise throws an UnsupportedOperationException */ public CompletableFuture single(){ return CompletableFuture.supplyAsync(()->{ List l= stream.toList(); if(l.size()==1){ return l.get(l.size()-1); } throw new UnsupportedOperationException("single only works for Streams with a single value");},exec); } /** * Asynchronously convert this Stream into a List *
	 * {@code
	 *  CompletableFuture> myList = LazyFutureStream.of(1,2,3,4)
	 *  														.map(this::loadFromDb)
	 *  														.withTaskExecutor(parallelBuilder().getExecutor())
	 *  														.map(this::processOnDifferentExecutor)
	 *  														.toSet();
	 * }
	 * 
* * @return Future Set */ public CompletableFuture> toSet(){ return CompletableFuture.supplyAsync(()->stream.toSet(),exec); } /** * Asynchronously capture the minimum value in this stream using the provided function * * @see org.jooq.lambda.Seq#minBy(Function) */ public > CompletableFuture> minBy(Function function){ return CompletableFuture.supplyAsync(()->stream.minBy(function)); } /** * Asynchronously capture the maximum value in this stream using the provided function * * @see org.jooq.lambda.Seq#maxBy(Function) */ public > CompletableFuture> maxBy(Function function){ return CompletableFuture.supplyAsync(()->stream.maxBy(function)); } /** * Asynchronously perform a Stream collection * * @see java.util.stream.Stream#collect(Collector) * */ public CompletableFuture collect(Collector collector){ return CompletableFuture.supplyAsync(()->stream.collect(collector)); } /** * Asynchronously perform a Stream collection * @see org.jooq.lambda.Seq#toCollection(Supplier) */ public > CompletableFuture toCollection(Supplier collectionFactory){ return CompletableFuture.supplyAsync(()->stream.toCollection(collectionFactory),exec); } /** * Asyncrhonously generate an Array * * @see java.util.stream.Stream#toArray(IntFunction) */ public CompletableFuture toArray(IntFunction generator){ return CompletableFuture.supplyAsync(()->stream.toArray(generator),exec); } /** * Asyncrhonously generate an Array * * @see java.util.stream.Stream#toArray(IntFunction) */ public CompletableFuture toArray() { return CompletableFuture.supplyAsync(()->stream.toArray(),exec); } /** * Perform an asyncrhonous groupBy operation * @see org.jooq.lambda.Seq#groupBy(Function) */ public CompletableFuture>> groupBy(Function classifier){ return CompletableFuture.supplyAsync(()->stream.groupBy(classifier),exec); } /** * Perform an asyncrhonous groupBy operation * @see org.jooq.lambda.Seq#groupBy(Function, Collector) */ public CompletableFuture> groupBy(Function classifier, Collector downstream) { return CompletableFuture.supplyAsync(()->stream.groupBy(classifier, downstream),exec); } /** * Perform an asyncrhonous groupBy operation * @see org.jooq.lambda.Seq#groupBy(Function, Supplier, Collector) */ public > CompletableFuture groupBy(Function classifier, Supplier mapFactory, Collector downstream){ return CompletableFuture.supplyAsync(()->stream.groupBy(classifier, mapFactory, downstream),exec); } /** * Perform an asynchronous foldLeft operation * @see org.jooq.lambda.Seq#foldLeft(Object,BiFunction) * */ public CompletableFuture foldLeft(U seed, BiFunction function){ return CompletableFuture.supplyAsync(()->stream.foldLeft(seed, function),exec); } /** * Perform an asynchronous foldRight operation * @see org.jooq.lambda.Seq#foldRight(Object,BiFunction) * */ public CompletableFuture foldRight(U seed, BiFunction function){ return CompletableFuture.supplyAsync(()->stream.foldRight(seed, function),exec); } /** * Perform an asyncrhonous min operation * @see java.util.stream.Stream#min(Comparator) */ public CompletableFuture> min(Comparator comparator){ return CompletableFuture.supplyAsync(()->stream.min(comparator),exec); } /** * Perform an asyncrhonous min operation * @see java.util.stream.Stream#max(Comparator) */ public CompletableFuture> max(Comparator comparator){ return CompletableFuture.supplyAsync(()->stream.max(comparator),exec); } /** * Asynchronously perform a Stream collection * * @see java.util.stream.Stream#collect(Supplier, BiConsumer, BiConsumer) * */ public CompletableFuture collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner){ return CompletableFuture.supplyAsync(()->stream.collect(supplier, accumulator, combiner),exec); } /** * Asynchronously perform a Stream reduction * * @see java.util.stream.Stream#reduce(Object, BiFunction, BinaryOperator) * */ public CompletableFuture reduce(U identity, BiFunction accumulator, BinaryOperator combiner){ return CompletableFuture.supplyAsync(()->stream.reduce(identity, accumulator,combiner),exec); } /** * Asynchronously perform a Stream reduction * * @see java.util.stream.Stream#reduce(BinaryOperator) * */ public CompletableFuture> reduce(BinaryOperator accumulator){ return CompletableFuture.supplyAsync(()->stream.reduce(accumulator)); } /** * Asynchronously perform a Stream reduction * * @see java.util.stream.Stream#reduce(Object, BinaryOperator) * */ public CompletableFuture reduce(T identity, BinaryOperator accumulator){ return CompletableFuture.supplyAsync(()->stream.reduce(identity, accumulator),exec); } /** * Asynchronously perform a Stream count * * @see java.util.stream.Stream#count() * */ public CompletableFuture count(){ return CompletableFuture.supplyAsync(()->stream.count(),exec); } public CompletableFuture join(CharSequence sep){ return CompletableFuture.supplyAsync(()->stream.join(sep),exec); } /** * Perform an asynchronous join operation * @see org.jooq.lambda.Seq#join() * */ public CompletableFuture join(){ return CompletableFuture.supplyAsync(()->stream.join(),exec); } /** * Perform an asynchronous join operation * @see org.jooq.lambda.Seq#join(CharSequence) * */ public CompletableFuture join(CharSequence delimiter, CharSequence prefix, CharSequence suffix){ return CompletableFuture.supplyAsync(()->stream.join(delimiter,prefix,suffix),exec); } /** * Perform an asynchronous findAny operation * @see java.util.stream.Stream#findAny() * */ public CompletableFuture> findAny(){ return CompletableFuture.supplyAsync(()->stream.findAny(),exec); } /** * Perform an asynchronous findAny operation * @see java.util.stream.Stream#findFirst() * */ public CompletableFuture> findFirst(){ return CompletableFuture.supplyAsync(()->stream.findFirst(),exec); } /** * Perform an asynchronous findAny operation * @see java.util.stream.Stream#findFirst() * */ public CompletableFuture firstValue(){ return CompletableFuture.supplyAsync(()->stream.firstValue(),exec); } /** * Perform an asynchronous All Match operation * @see java.util.stream.Stream#allMatch(Predicate) * */ public CompletableFuture allMatch(Predicate predicate){ return CompletableFuture.supplyAsync(()->stream.allMatch(predicate),exec); } /** * Perform an asynchronous Any Match operation * @see java.util.stream.Stream#anyMatch(Predicate) * */ public CompletableFuture anyMatch(Predicate predicate){ return CompletableFuture.supplyAsync(()->stream.anyMatch(predicate),exec); } /** * Perform an asynchronous Any Match operation * @see java.util.stream.Stream#noneMatch(Predicate) * */ public CompletableFuture noneMatch(Predicate predicate){ return CompletableFuture.supplyAsync(()->stream.noneMatch(predicate),exec); } @Override public void forEach(Consumer c) { CompletableFuture.runAsync(()->stream.forEach(c),exec); } }