public class LazyReact
extends java.lang.Object
implements com.aol.cyclops.internal.react.stream.ReactBuilder
new LazyReact(Executors.newFixedThreadPool(4)).of(6,5,2,1)
.map(this::loadData)
.map(e->e*100)
.filter(e->e<551)
.peek(e->{
System.out.println("e is " + e
+ " on thread "
+ Thread.currentThread().getId());
})
.runOnCurrent();
Constructor and Description |
---|
LazyReact()
Construct a LazyReact builder using standard thread pool.
|
LazyReact(java.util.concurrent.Executor executor)
Construct a LazyReact builder with provided Executor
|
LazyReact(java.util.concurrent.Executor executor,
com.nurkiewicz.asyncretry.AsyncRetryExecutor retrier,
boolean async,
MaxActive maxActive2) |
LazyReact(java.util.concurrent.Executor executor,
com.nurkiewicz.asyncretry.RetryExecutor retrier,
java.lang.Boolean async,
MaxActive maxActive,
boolean streamOfFutures,
boolean objectPoolingActive,
boolean autoOptimize,
boolean autoMemoize,
Cacheable memoizeCache) |
LazyReact(int maxActive,
java.util.concurrent.Executor executor) |
LazyReact(int threadPoolSize,
int maxActiveTasks)
LazyReact builder with a new TaskExecutor with threads determined by threadPoolSize
Max concurrent tasks is determined by concurrency
|
Modifier and Type | Method and Description |
---|---|
LazyReact |
async()
Start any created Streams in asyncrhonous mode - that is tasks will be submited to an Executor to be run.
|
LazyReact |
autoMemoizeOn(Cacheable<?> memoizeCache)
Turn automatic caching of values on for the FutureStream to be generated
by this Stream builder
|
LazyReact |
autoOptimizeOff()
Turn off automatic threading management.
|
LazyReact |
autoOptimizeOn()
Turn on automatic threading optimization.
|
<U> LazyFutureStream<U> |
construct(java.util.stream.Stream<U> s) |
<U> LazyFutureStream<U> |
constructFutures(java.util.stream.Stream<java.util.concurrent.CompletableFuture<U>> s)
Construct a LazyFutureStream from a Stream of CompletableFutures
|
<R> LazyFutureStream<R> |
from(java.util.Collection<R> collection)
Start a reactive flow from a Collection using an Iterator
|
<U> LazyFutureStream<U> |
from(java.util.concurrent.CompletableFuture<U>... cf)
Construct a LazyFutureStream from an array of CompletableFutures
|
<U> LazyFutureStream<U> |
from(java.util.concurrent.CompletableFuture<U> cf)
Construct a LazyFutureStream containing a single Future
|
LazyFutureStream<java.lang.Double> |
from(java.util.stream.DoubleStream stream)
Start a reactive dataflow from a stream.
|
LazyFutureStream<java.lang.Integer> |
from(java.util.stream.IntStream stream)
Start a reactive dataflow from a stream.
|
<U> LazyFutureStream<U> |
from(java.util.Iterator<U> iterator)
Start a reactive flow from a JDK Iterator
|
LazyFutureStream<java.lang.Long> |
from(java.util.stream.LongStream stream)
Start a reactive dataflow from a stream.
|
<U> LazyFutureStream<U> |
fromAdapter(Adapter<U> adapter)
Generate a LazyFutureStream from the data flowing into the prodiced Adapter
|
<U> LazyFutureStream<U> |
fromIterable(java.lang.Iterable<U> iter) |
<U> LazyFutureStream<U> |
fromIterableAsync(java.lang.Iterable<? extends java.util.function.Supplier<U>> actions) |
<U> LazyFutureStream<U> |
fromIteratorAsync(java.util.Iterator<? extends java.util.function.Supplier<U>> actions) |
<T> LazyFutureStream<T> |
fromPublisher(org.reactivestreams.Publisher<? extends T> publisher)
Construct a LazyFutureStream from an Publisher
|
<U> LazyFutureStream<U> |
fromStream(java.util.stream.Stream<U> stream) |
<U> LazyFutureStream<U> |
fromStreamAsync(java.util.stream.Stream<? extends java.util.function.Supplier<U>> actions) |
<U> LazyFutureStream<U> |
fromStreamFutures(java.util.stream.Stream<java.util.concurrent.CompletableFuture<U>> stream) |
<U> LazyFutureStream<U> |
generate(java.util.function.Supplier<U> generate)
Generate an infinite Stream
|
<U> LazyFutureStream<U> |
generateAsync(java.util.function.Supplier<U> s)
Generate an infinite FutureStream executing the provided Supplier continually and asynhcronously
|
boolean |
isAsync() |
<U> LazyFutureStream<U> |
iterate(U seed,
java.util.function.UnaryOperator<U> f)
Iterate infinitely using the supplied seed and function
Iteration is synchronized to support multiple threads using the same iterator.
|
LazyReact |
objectPoolingOff()
Turn objectPooling off for any Streams created by the returned LazyReact builder.
|
LazyReact |
objectPoolingOn()
Turn objectPooling on for any Streams created by the returned LazyReact builder
This improves performance for Streams with very large numbers of elements, by reusing
Future instances.
|
<U> LazyFutureStream<U> |
of(U... array)
Construct a LazyFurureStream from the values in the supplied array
|
<U> LazyFutureStream<U> |
ofAsync(java.util.function.Supplier<U>... actions) |
static LazyReact |
parallelBuilder()
* Construct a LazyReact builder using standard thread pool.
|
static LazyReact |
parallelBuilder(int parallelism)
Construct a new LazyReact builder, with a new task executor and retry
executor with configured number of threads
|
static LazyReact |
parallelCommonBuilder() |
LazyFutureStream<java.lang.Integer> |
range(int startInclusive,
int endExclusive) |
<U> LazyFutureStream<U> |
react(java.util.Collection<java.util.function.Supplier<U>> actions) |
static LazyReact |
sequentialBuilder() |
static LazyReact |
sequentialCommonBuilder() |
static LazyReact |
sequentialCurrentBuilder() |
LazyReact |
sync()
Start any created Streams in syncrhonous mode - that is tasks will be executed on the calling thread
|
public LazyReact()
ThreadPools.getStandard()
public LazyReact(java.util.concurrent.Executor executor)
executor
- Executor to usepublic LazyReact(int maxActive, java.util.concurrent.Executor executor)
maxActive
- Max active Future Tasksexecutor
- Executor to usepublic LazyReact(int threadPoolSize, int maxActiveTasks)
threadPoolSize
- maxActiveTasks
- public LazyReact(java.util.concurrent.Executor executor, com.nurkiewicz.asyncretry.RetryExecutor retrier, java.lang.Boolean async, MaxActive maxActive, boolean streamOfFutures, boolean objectPoolingActive, boolean autoOptimize, boolean autoMemoize, Cacheable memoizeCache)
executor
- Task Executor for concurrent tasksretrier
- Async Retrierasync
- If true each task will be submitted to an executor servicepublic LazyReact(java.util.concurrent.Executor executor, com.nurkiewicz.asyncretry.AsyncRetryExecutor retrier, boolean async, MaxActive maxActive2)
executor
- Task Executor for concurrent tasksretrier
- Async Retrierasync
- If true each task will be submitted to an executor servicemaxActive2
- Max Active Future Taskspublic LazyReact autoMemoizeOn(Cacheable<?> memoizeCache)
Map cache = new ConcurrentHashMap<>();
LazyReact builder = new LazyReact().autoMemoizeOn((key,fn)-> cache.computeIfAbsent(key,fn));
Set<Integer> result = builder.of(1,1,1,1)
.capture(e->e.printStackTrace())
.map(i->calc(i))
.peek(System.out::println)
.toSet();
memoizeCache
- Cacheable instance that controls memoization (Caching)public boolean isAsync()
public <U> LazyFutureStream<U> from(java.util.concurrent.CompletableFuture<U> cf)
cf
- CompletableFuture to create Stream frompublic <U> LazyFutureStream<U> from(java.util.concurrent.CompletableFuture<U>... cf)
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->"hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(()->"hello2");
new LazyReact().from(future1,future2)
.peek(System.out::println)
.then(action->"result")
.toList()
.size()
//2
cf
- Array of Futures to construct Stream frompublic <U> LazyFutureStream<U> construct(java.util.stream.Stream<U> s)
public <U> LazyFutureStream<U> constructFutures(java.util.stream.Stream<java.util.concurrent.CompletableFuture<U>> s)
s
- Stream of CompletableFuturespublic LazyReact objectPoolingOn()
return new LazyReact()
.objectPoolingOn()
.range(0,5_000_000_000)
.map(this::process)
.forEach(System.out::println);
public LazyReact objectPoolingOff()
LazyReact react;
react.objectPoolingOff()
.range(0,5_000)
.map(this::process)
.forEach(System.out::println);
public LazyReact autoOptimizeOn()
new LazyReact().autoOptimizeOn()
.range(0, 1_000_000)
.map(i->i+2)
.map(i->Thread.currentThread().getId())
.peek(System.out::println)
.runOnCurrent();
public LazyReact autoOptimizeOff()
LazyReact react;
react.autoOptimizeOff()
.range(0, 1_000_000)
.map(i->i+2)
.map(i->Thread.currentThread().getId())
.peek(System.out::println)
.runOnCurrent();
public LazyReact async()
Turn async on
LazyReact.sequentialBuilder()
.withMaxActive(MaxActive.IO)
.async()
.generateAsync(()->1)
.limit(1_000_000);
public LazyReact sync()
public <T> LazyFutureStream<T> fromPublisher(org.reactivestreams.Publisher<? extends T> publisher)
new LazyReact().fromPublisher(Flux.just(1,2,3)).toList();
publisher
- to construct LazyFutureStream frompublic LazyFutureStream<java.lang.Integer> range(int startInclusive, int endExclusive)
public <U> LazyFutureStream<U> fromStreamFutures(java.util.stream.Stream<java.util.concurrent.CompletableFuture<U>> stream)
@SafeVarargs public final <U> LazyFutureStream<U> ofAsync(java.util.function.Supplier<U>... actions)
public <U> LazyFutureStream<U> fromStream(java.util.stream.Stream<U> stream)
public <U> LazyFutureStream<U> react(java.util.Collection<java.util.function.Supplier<U>> actions)
public <U> LazyFutureStream<U> fromIterable(java.lang.Iterable<U> iter)
public <U> LazyFutureStream<U> fromStreamAsync(java.util.stream.Stream<? extends java.util.function.Supplier<U>> actions)
public <U> LazyFutureStream<U> fromIteratorAsync(java.util.Iterator<? extends java.util.function.Supplier<U>> actions)
public <U> LazyFutureStream<U> fromIterableAsync(java.lang.Iterable<? extends java.util.function.Supplier<U>> actions)
public static LazyReact parallelBuilder()
ThreadPools.getStandard()
public static LazyReact parallelBuilder(int parallelism)
parallelism
- Number of threads task executor should havepublic static LazyReact parallelCommonBuilder()
see RetryBuilder#getDefaultInstance()
public static LazyReact sequentialBuilder()
public static LazyReact sequentialCommonBuilder()
public static LazyReact sequentialCurrentBuilder()
public <U> LazyFutureStream<U> iterate(U seed, java.util.function.UnaryOperator<U> f)
new LazyReact().objectPoolingOn()
.iterate(1,i->i+1)
.limit(1_000_000)
.map(this::process)
.forEach(this::save);
seed
- Initial valuef
- Function that performs the iterationfor an alternative which does not synchronize iteration
public <U> LazyFutureStream<U> fromAdapter(Adapter<U> adapter)
Topic<Integer> topic = new Topic<>();
new LazyReact(10,10).fromAdapter(topic)
.forEach(this::process);
//on anther thread
topic.offer(100);
topic.offer(200);
adapter
- Adapter to construct LazyFutureStream frompublic <U> LazyFutureStream<U> generate(java.util.function.Supplier<U> generate)
new LazyReact().generate(()->"hello")
.limit(5)
.reduce(Semigroups.stringConcat);
//Optional[hellohellohellohellohello]
generate
- Supplier that generates stream inputpublic <U> LazyFutureStream<U> generateAsync(java.util.function.Supplier<U> s)
new LazyReact().generate(this::load)
.limit(5)
.reduce(Semigroups.stringConcat);
//Optional["data1data2data3data4data5"]
s
- Supplier to execute asynchronously to create an infinite Streampublic <U> LazyFutureStream<U> from(java.util.Iterator<U> iterator)
Iterator<Integer> iterator;
new LazyReact(10,10).from(iterator)
.map(this::process);
iterator
- SimpleReact will iterate over this iterator concurrently to start the reactive dataflowpublic <R> LazyFutureStream<R> from(java.util.Collection<R> collection)
new LazyReact(10,10).from(myList)
.map(this::process);
collection
- - Collection SimpleReact will iterate over at the start of the flowpublic LazyFutureStream<java.lang.Integer> from(java.util.stream.IntStream stream)
stream
- that will be used to drive the reactive dataflowpublic LazyFutureStream<java.lang.Double> from(java.util.stream.DoubleStream stream)
stream
- that will be used to drive the reactive dataflowpublic LazyFutureStream<java.lang.Long> from(java.util.stream.LongStream stream)
stream
- that will be used to drive the reactive dataflow@SafeVarargs public final <U> LazyFutureStream<U> of(U... array)
new LazyReact(10,10).of(1,2,3,4)
.map(this::process);
array
- Array to construct LazyFutureStream from