mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-17 23:16:01 +00:00
optimizations, more comfortable PromiseFactory api and support virtual threaded executors
This commit is contained in:
11
build.gradle
11
build.gradle
@@ -1,7 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java-library'
|
||||||
id 'com.github.johnrengelman.shadow' version '8.1.1'
|
id 'com.github.johnrengelman.shadow' version '8.1.1'
|
||||||
id 'io.github.gradle-nexus.publish-plugin' version '1.3.0'
|
id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
nexusPublishing {
|
nexusPublishing {
|
||||||
@@ -14,9 +14,9 @@ nexusPublishing {
|
|||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
group = 'dev.tommyjs'
|
group = 'dev.tommyjs'
|
||||||
version = '2.3.4'
|
version = '2.4'
|
||||||
|
|
||||||
apply plugin: 'java'
|
apply plugin: 'java-library'
|
||||||
apply plugin: 'com.github.johnrengelman.shadow'
|
apply plugin: 'com.github.johnrengelman.shadow'
|
||||||
|
|
||||||
tasks {
|
tasks {
|
||||||
@@ -30,8 +30,9 @@ subprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.jetbrains:annotations:24.1.0'
|
compileOnly 'org.jetbrains:annotations:24.1.0'
|
||||||
implementation 'org.slf4j:slf4j-api:2.0.12'
|
implementation 'org.slf4j:slf4j-api:2.0.12'
|
||||||
|
2
|
||||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||||
testImplementation 'io.projectreactor:reactor-core:3.6.4'
|
testImplementation 'io.projectreactor:reactor-core:3.6.4'
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
|
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
package dev.tommyjs.futur.executor;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
public class DualPoolExecutor implements PromiseExecutor<Future<?>> {
|
|
||||||
|
|
||||||
private final @NotNull ScheduledExecutorService syncSvc;
|
|
||||||
private final @NotNull ScheduledExecutorService asyncSvc;
|
|
||||||
|
|
||||||
public DualPoolExecutor(@NotNull ScheduledExecutorService syncSvc, @NotNull ScheduledExecutorService asyncSvc) {
|
|
||||||
this.syncSvc = syncSvc;
|
|
||||||
this.asyncSvc = asyncSvc;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull DualPoolExecutor create(int asyncPoolSize) {
|
|
||||||
return new DualPoolExecutor(Executors.newSingleThreadScheduledExecutor(), Executors.newScheduledThreadPool(asyncPoolSize));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Future<?> runSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit) {
|
|
||||||
return syncSvc.schedule(task, delay, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Future<?> runAsync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit) {
|
|
||||||
return asyncSvc.schedule(task, delay, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void cancel(Future<?> task) {
|
|
||||||
task.cancel(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package dev.tommyjs.futur.executor;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
class ExecutorServiceImpl implements PromiseExecutor<Future<?>> {
|
||||||
|
|
||||||
|
private final ScheduledExecutorService service;
|
||||||
|
|
||||||
|
public ExecutorServiceImpl(@NotNull ScheduledExecutorService service) {
|
||||||
|
this.service = service;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Future<?> run(@NotNull Runnable task) {
|
||||||
|
return service.submit(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Future<?> run(@NotNull Runnable task, long delay, @NotNull TimeUnit unit) {
|
||||||
|
return service.schedule(task, delay, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancel(Future<?> task) {
|
||||||
|
task.cancel(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,22 +2,32 @@ package dev.tommyjs.futur.executor;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public interface PromiseExecutor<T> {
|
public interface PromiseExecutor<T> {
|
||||||
|
|
||||||
T runSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit);
|
static PromiseExecutor<?> virtualThreaded() {
|
||||||
|
return new VirtualThreadImpl();
|
||||||
T runAsync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit);
|
|
||||||
|
|
||||||
default T runSync(@NotNull Runnable task) {
|
|
||||||
return runSync(task, 0L, TimeUnit.MILLISECONDS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default T runAsync(@NotNull Runnable task) {
|
static PromiseExecutor<?> singleThreaded() {
|
||||||
return runAsync(task, 0L, TimeUnit.MILLISECONDS);
|
return of(Executors.newSingleThreadScheduledExecutor());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PromiseExecutor<?> multiThreaded(int threads) {
|
||||||
|
return of(Executors.newScheduledThreadPool(threads));
|
||||||
|
}
|
||||||
|
|
||||||
|
static PromiseExecutor<?> of(@NotNull ScheduledExecutorService service) {
|
||||||
|
return new ExecutorServiceImpl(service);
|
||||||
|
}
|
||||||
|
|
||||||
|
T run(@NotNull Runnable task) throws Exception;
|
||||||
|
|
||||||
|
T run(@NotNull Runnable task, long delay, @NotNull TimeUnit unit) throws Exception;
|
||||||
|
|
||||||
void cancel(T task);
|
void cancel(T task);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package dev.tommyjs.futur.executor;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
|
|
||||||
public class SinglePoolExecutor extends DualPoolExecutor {
|
|
||||||
|
|
||||||
public SinglePoolExecutor(@NotNull ScheduledExecutorService service) {
|
|
||||||
super(service, service);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull SinglePoolExecutor create(int threadPoolSize) {
|
|
||||||
return new SinglePoolExecutor(Executors.newScheduledThreadPool(threadPoolSize));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package dev.tommyjs.futur.executor;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
class VirtualThreadImpl implements PromiseExecutor<Thread> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Thread run(@NotNull Runnable task) {
|
||||||
|
return Thread.ofVirtual().start(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Thread run(@NotNull Runnable task, long delay, @NotNull TimeUnit unit) {
|
||||||
|
return Thread.ofVirtual().start(() -> {
|
||||||
|
try {
|
||||||
|
Thread.sleep(unit.toMillis(delay));
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
task.run();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancel(Thread task) {
|
||||||
|
task.interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,6 +3,6 @@ package dev.tommyjs.futur.function;
|
|||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface ExceptionalConsumer<T> {
|
public interface ExceptionalConsumer<T> {
|
||||||
|
|
||||||
void accept(T value) throws Throwable;
|
void accept(T value) throws Exception;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ package dev.tommyjs.futur.function;
|
|||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface ExceptionalFunction<K, V> {
|
public interface ExceptionalFunction<K, V> {
|
||||||
|
|
||||||
V apply(K value) throws Throwable;
|
V apply(K value) throws Exception;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ package dev.tommyjs.futur.function;
|
|||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface ExceptionalRunnable {
|
public interface ExceptionalRunnable {
|
||||||
|
|
||||||
void run() throws Throwable;
|
void run() throws Exception;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ package dev.tommyjs.futur.function;
|
|||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface ExceptionalSupplier<T> {
|
public interface ExceptionalSupplier<T> {
|
||||||
|
|
||||||
T get() throws Throwable;
|
T get() throws Exception;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
package dev.tommyjs.futur.impl;
|
|
||||||
|
|
||||||
import dev.tommyjs.futur.executor.PromiseExecutor;
|
|
||||||
import dev.tommyjs.futur.promise.AbstractPromise;
|
|
||||||
import dev.tommyjs.futur.promise.AbstractPromiseFactory;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
public class SimplePromise<T, F> extends AbstractPromise<T, F> {
|
|
||||||
|
|
||||||
private final @NotNull AbstractPromiseFactory<F> factory;
|
|
||||||
|
|
||||||
public SimplePromise(@NotNull AbstractPromiseFactory<F> factory) {
|
|
||||||
this.factory = factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public SimplePromise(@NotNull PromiseExecutor<F> executor, @NotNull Logger logger, @NotNull AbstractPromiseFactory<F> factory) {
|
|
||||||
this(factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull AbstractPromiseFactory<F> getFactory() {
|
|
||||||
return factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package dev.tommyjs.futur.impl;
|
|
||||||
|
|
||||||
import dev.tommyjs.futur.executor.PromiseExecutor;
|
|
||||||
import dev.tommyjs.futur.promise.AbstractPromiseFactory;
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
public class SimplePromiseFactory<F> extends AbstractPromiseFactory<F> {
|
|
||||||
|
|
||||||
private final PromiseExecutor<F> executor;
|
|
||||||
private final Logger logger;
|
|
||||||
|
|
||||||
public SimplePromiseFactory(PromiseExecutor<F> executor, Logger logger) {
|
|
||||||
this.executor = executor;
|
|
||||||
this.logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull <T> Promise<T> unresolved() {
|
|
||||||
return new SimplePromise<>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Logger getLogger() {
|
|
||||||
return logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull PromiseExecutor<F> getExecutor() {
|
|
||||||
return executor;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package dev.tommyjs.futur.joiner;
|
||||||
|
|
||||||
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseCompletion;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CompletionJoiner extends PromiseJoiner<Promise<?>, Void, Void, List<PromiseCompletion<?>>> {
|
||||||
|
|
||||||
|
private final ConcurrentResultArray<PromiseCompletion<?>> results;
|
||||||
|
|
||||||
|
public CompletionJoiner(
|
||||||
|
@NotNull PromiseFactory factory,
|
||||||
|
@NotNull Iterator<Promise<?>> promises,
|
||||||
|
int expectedSize, boolean link
|
||||||
|
) {
|
||||||
|
super(factory);
|
||||||
|
results = new ConcurrentResultArray<>(expectedSize);
|
||||||
|
join(promises, link);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void getKey(Promise<?> value) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull Promise<Void> getPromise(Promise<?> value) {
|
||||||
|
//noinspection unchecked
|
||||||
|
return (Promise<Void>) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @Nullable Throwable onFinish(int index, Void key, @NotNull PromiseCompletion<Void> res) {
|
||||||
|
results.set(index, res);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<PromiseCompletion<?>> getResult() {
|
||||||
|
return results.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package dev.tommyjs.futur.joiner;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
class ConcurrentResultArray<T> {
|
||||||
|
|
||||||
|
private final AtomicReference<T[]> ref;
|
||||||
|
|
||||||
|
public ConcurrentResultArray(int expectedSize) {
|
||||||
|
//noinspection unchecked
|
||||||
|
this.ref = new AtomicReference<>((T[]) new Object[expectedSize]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(int index, T element) {
|
||||||
|
ref.updateAndGet(array -> {
|
||||||
|
if (array.length <= index)
|
||||||
|
return Arrays.copyOf(array, index + 6);
|
||||||
|
|
||||||
|
array[index] = element;
|
||||||
|
return array;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull List<T> toList() {
|
||||||
|
return Arrays.asList(ref.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package dev.tommyjs.futur.joiner;
|
||||||
|
|
||||||
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseCompletion;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
public class MappedResultJoiner<K, V> extends PromiseJoiner<Map.Entry<K, Promise<V>>, K, V, Map<K, V>> {
|
||||||
|
|
||||||
|
private final @Nullable BiConsumer<K, Throwable> exceptionHandler;
|
||||||
|
private final @NotNull ConcurrentResultArray<Map.Entry<K, V>> results;
|
||||||
|
|
||||||
|
public MappedResultJoiner(
|
||||||
|
@NotNull PromiseFactory factory,
|
||||||
|
@NotNull Iterator<Map.Entry<K, Promise<V>>> promises,
|
||||||
|
@Nullable BiConsumer<K, Throwable> exceptionHandler,
|
||||||
|
int expectedSize, boolean link
|
||||||
|
) {
|
||||||
|
super(factory);
|
||||||
|
this.exceptionHandler = exceptionHandler;
|
||||||
|
this.results = new ConcurrentResultArray<>(expectedSize);
|
||||||
|
join(promises, link);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected K getKey(Map.Entry<K, Promise<V>> entry) {
|
||||||
|
return entry.getKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull Promise<V> getPromise(Map.Entry<K, Promise<V>> entry) {
|
||||||
|
return entry.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @Nullable Throwable onFinish(int index, K key, @NotNull PromiseCompletion<V> res) {
|
||||||
|
if (res.isError()) {
|
||||||
|
if (exceptionHandler == null) return res.getException();
|
||||||
|
exceptionHandler.accept(key, res.getException());
|
||||||
|
}
|
||||||
|
|
||||||
|
results.set(index, new AbstractMap.SimpleImmutableEntry<>(key, res.getResult()));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Map<K, V> getResult() {
|
||||||
|
List<Map.Entry<K, V>> list = results.toList();
|
||||||
|
Map<K, V> map = new HashMap<>(list.size());
|
||||||
|
for (Map.Entry<K, V> entry : list) {
|
||||||
|
map.put(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package dev.tommyjs.futur.joiner;
|
||||||
|
|
||||||
|
import dev.tommyjs.futur.promise.*;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
abstract class PromiseJoiner<V, K, T, R> {
|
||||||
|
|
||||||
|
private final CompletablePromise<R> joined;
|
||||||
|
|
||||||
|
protected PromiseJoiner(@NotNull PromiseFactory factory) {
|
||||||
|
this.joined = factory.unresolved();
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Promise<R> joined() {
|
||||||
|
return joined;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract K getKey(V value);
|
||||||
|
|
||||||
|
protected abstract @NotNull Promise<T> getPromise(V value);
|
||||||
|
|
||||||
|
protected abstract @Nullable Throwable onFinish(int index, K key, @NotNull PromiseCompletion<T> completion);
|
||||||
|
|
||||||
|
protected abstract R getResult();
|
||||||
|
|
||||||
|
protected void join(@NotNull Iterator<V> promises, boolean link) {
|
||||||
|
AtomicBoolean waiting = new AtomicBoolean();
|
||||||
|
AtomicInteger count = new AtomicInteger();
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
do {
|
||||||
|
V value = promises.next();
|
||||||
|
Promise<T> p = getPromise(value);
|
||||||
|
if (link) {
|
||||||
|
AbstractPromise.cancelOnFinish(p, joined);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!joined.isCompleted()) {
|
||||||
|
count.incrementAndGet();
|
||||||
|
K key = getKey(value);
|
||||||
|
int index = i++;
|
||||||
|
|
||||||
|
p.addListener((res) -> {
|
||||||
|
Throwable e = onFinish(index, key, res);
|
||||||
|
if (e != null) {
|
||||||
|
joined.completeExceptionally(e);
|
||||||
|
} else if (count.decrementAndGet() == 0 && waiting.get()) {
|
||||||
|
joined.complete(getResult());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} while (promises.hasNext());
|
||||||
|
|
||||||
|
count.updateAndGet((v) -> {
|
||||||
|
if (v == 0) joined.complete(getResult());
|
||||||
|
else waiting.set(true);
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package dev.tommyjs.futur.joiner;
|
||||||
|
|
||||||
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseCompletion;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
public class ResultJoiner<T> extends PromiseJoiner<Promise<T>, Void, T, List<T>> {
|
||||||
|
|
||||||
|
private final @Nullable BiConsumer<Integer, Throwable> exceptionHandler;
|
||||||
|
private final ConcurrentResultArray<T> results;
|
||||||
|
|
||||||
|
public ResultJoiner(
|
||||||
|
@NotNull PromiseFactory factory,
|
||||||
|
@NotNull Iterator<Promise<T>> promises,
|
||||||
|
@Nullable BiConsumer<Integer, Throwable> exceptionHandler,
|
||||||
|
int expectedSize, boolean link
|
||||||
|
) {
|
||||||
|
super(factory);
|
||||||
|
this.exceptionHandler = exceptionHandler;
|
||||||
|
this.results = new ConcurrentResultArray<>(expectedSize);
|
||||||
|
join(promises, link);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void getKey(Promise<T> value) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull Promise<T> getPromise(Promise<T> value) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @Nullable Throwable onFinish(int index, Void key, @NotNull PromiseCompletion<T> res) {
|
||||||
|
if (res.isError()) {
|
||||||
|
if (exceptionHandler == null) return res.getException();
|
||||||
|
exceptionHandler.accept(index, res.getException());
|
||||||
|
}
|
||||||
|
|
||||||
|
results.set(index, res.getResult());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<T> getResult() {
|
||||||
|
return results.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package dev.tommyjs.futur.joiner;
|
||||||
|
|
||||||
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseCompletion;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class VoidJoiner extends PromiseJoiner<Promise<?>, Void, Void, Void> {
|
||||||
|
|
||||||
|
public VoidJoiner(@NotNull PromiseFactory factory, @NotNull Iterator<Promise<?>> promises, boolean link) {
|
||||||
|
super(factory);
|
||||||
|
join(promises, link);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void getKey(Promise<?> value) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull Promise<Void> getPromise(Promise<?> value) {
|
||||||
|
//noinspection unchecked
|
||||||
|
return (Promise<Void>) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @Nullable Throwable onFinish(int index, Void key, @NotNull PromiseCompletion<Void> completion) {
|
||||||
|
return completion.getException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void getResult() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package dev.tommyjs.futur.promise;
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
import dev.tommyjs.futur.executor.PromiseExecutor;
|
|
||||||
import dev.tommyjs.futur.function.ExceptionalConsumer;
|
import dev.tommyjs.futur.function.ExceptionalConsumer;
|
||||||
import dev.tommyjs.futur.function.ExceptionalFunction;
|
import dev.tommyjs.futur.function.ExceptionalFunction;
|
||||||
import dev.tommyjs.futur.function.ExceptionalRunnable;
|
import dev.tommyjs.futur.function.ExceptionalRunnable;
|
||||||
@@ -10,102 +9,110 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedList;
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.concurrent.locks.Lock;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public abstract class AbstractPromise<T, F> implements Promise<T> {
|
public abstract class AbstractPromise<T, FS, FA> implements CompletablePromise<T> {
|
||||||
|
|
||||||
private Collection<PromiseListener<T>> listeners;
|
public static <V> void propagateResult(Promise<V> from, CompletablePromise<V> to) {
|
||||||
private final AtomicReference<PromiseCompletion<T>> completion;
|
|
||||||
private final CountDownLatch latch;
|
|
||||||
private final Lock lock;
|
|
||||||
|
|
||||||
public AbstractPromise() {
|
|
||||||
this.completion = new AtomicReference<>();
|
|
||||||
this.latch = new CountDownLatch(1);
|
|
||||||
this.lock = new ReentrantLock();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static <V> void propagateResult(Promise<V> from, Promise<V> to) {
|
|
||||||
from.addDirectListener(to::complete, to::completeExceptionally);
|
from.addDirectListener(to::complete, to::completeExceptionally);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void propagateCancel(Promise<?> from, Promise<?> to) {
|
public static void propagateCancel(Promise<?> from, Promise<?> to) {
|
||||||
from.onCancel(to::completeExceptionally);
|
from.onCancel(to::cancel);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <V> @NotNull Runnable createRunnable(T result, @NotNull Promise<V> promise, @NotNull ExceptionalFunction<T, V> task) {
|
public static void cancelOnFinish(Promise<?> toCancel, Promise<?> toFinish) {
|
||||||
return () -> {
|
toFinish.addDirectListener(_ -> toCancel.cancel());
|
||||||
if (promise.isCompleted()) return;
|
}
|
||||||
|
|
||||||
|
private final AtomicReference<Collection<PromiseListener<T>>> listeners;
|
||||||
|
private final AtomicReference<PromiseCompletion<T>> completion;
|
||||||
|
private final CountDownLatch latch;
|
||||||
|
|
||||||
|
public AbstractPromise() {
|
||||||
|
this.listeners = new AtomicReference<>(Collections.emptyList());
|
||||||
|
this.completion = new AtomicReference<>();
|
||||||
|
this.latch = new CountDownLatch(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runCompleter(@NotNull CompletablePromise<?> promise, @NotNull ExceptionalRunnable completer) {
|
||||||
try {
|
try {
|
||||||
V nextResult = task.apply(result);
|
completer.run();
|
||||||
promise.complete(nextResult);
|
} catch (Error e) {
|
||||||
|
promise.completeExceptionally(e);
|
||||||
|
throw e;
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
promise.completeExceptionally(e);
|
promise.completeExceptionally(e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <V> @NotNull Runnable createCompleter(
|
||||||
|
T result,
|
||||||
|
@NotNull CompletablePromise<V> promise,
|
||||||
|
@NotNull ExceptionalFunction<T, V> completer
|
||||||
|
) {
|
||||||
|
return () -> {
|
||||||
|
if (promise.isCompleted()) return;
|
||||||
|
runCompleter(promise, () -> promise.complete(completer.apply(result)));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract @NotNull AbstractPromiseFactory<F> getFactory();
|
public abstract @NotNull AbstractPromiseFactory<FS, FA> getFactory();
|
||||||
|
|
||||||
protected @NotNull PromiseExecutor<F> getExecutor() {
|
|
||||||
return getFactory().getExecutor();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected @NotNull Logger getLogger() {
|
protected @NotNull Logger getLogger() {
|
||||||
return getFactory().getLogger();
|
return getFactory().getLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T awaitInterruptibly() throws InterruptedException {
|
public T get() throws InterruptedException, ExecutionException {
|
||||||
this.latch.await();
|
this.latch.await();
|
||||||
return joinCompletion(Objects.requireNonNull(getCompletion()));
|
return joinCompletion();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T awaitInterruptibly(long timeoutMillis) throws TimeoutException, InterruptedException {
|
public T get(long time, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
boolean success = this.latch.await(timeoutMillis, TimeUnit.MILLISECONDS);
|
boolean success = this.latch.await(time, unit);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
throw new TimeoutException("Promise stopped waiting after " + timeoutMillis + "ms");
|
throw new TimeoutException("Promise stopped waiting after " + time + " " + unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
return joinCompletion(Objects.requireNonNull(getCompletion()));
|
return joinCompletion();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T await() {
|
public T await() {
|
||||||
try {
|
try {
|
||||||
return awaitInterruptibly();
|
this.latch.await();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PromiseCompletion<T> completion = Objects.requireNonNull(getCompletion());
|
||||||
|
if (completion.isSuccess()) return completion.getResult();
|
||||||
|
throw new CompletionException(completion.getException());
|
||||||
|
}
|
||||||
|
|
||||||
|
private T joinCompletion() throws ExecutionException {
|
||||||
|
PromiseCompletion<T> completion = Objects.requireNonNull(getCompletion());
|
||||||
|
if (completion.isSuccess()) return completion.getResult();
|
||||||
|
throw new ExecutionException(completion.getException());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T await(long timeoutMillis) throws TimeoutException {
|
public @NotNull Promise<T> fork() {
|
||||||
try {
|
CompletablePromise<T> fork = getFactory().unresolved();
|
||||||
return awaitInterruptibly(timeoutMillis);
|
propagateResult(this, fork);
|
||||||
} catch (InterruptedException e) {
|
return fork;
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private T joinCompletion(PromiseCompletion<T> completion) {
|
|
||||||
if (completion.isError())
|
|
||||||
throw new RuntimeException(completion.getException());
|
|
||||||
|
|
||||||
return completion.getResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<Void> thenRun(@NotNull ExceptionalRunnable task) {
|
public @NotNull Promise<Void> thenRun(@NotNull ExceptionalRunnable task) {
|
||||||
return thenApply(result -> {
|
return thenApply(_ -> {
|
||||||
task.run();
|
task.run();
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
@@ -121,14 +128,14 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenSupply(@NotNull ExceptionalSupplier<V> task) {
|
public <V> @NotNull Promise<V> thenSupply(@NotNull ExceptionalSupplier<V> task) {
|
||||||
return thenApply(result -> task.get());
|
return thenApply(_ -> task.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenApply(@NotNull ExceptionalFunction<T, V> task) {
|
public <V> @NotNull Promise<V> thenApply(@NotNull ExceptionalFunction<T, V> task) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
CompletablePromise<V> promise = getFactory().unresolved();
|
||||||
addDirectListener(
|
addDirectListener(
|
||||||
res -> createRunnable(res, promise, task).run(),
|
res -> createCompleter(res, promise, task).run(),
|
||||||
promise::completeExceptionally
|
promise::completeExceptionally
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -138,7 +145,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
public <V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
CompletablePromise<V> promise = getFactory().unresolved();
|
||||||
thenApply(task).addDirectListener(
|
thenApply(task).addDirectListener(
|
||||||
nestedPromise -> {
|
nestedPromise -> {
|
||||||
if (nestedPromise == null) {
|
if (nestedPromise == null) {
|
||||||
@@ -157,7 +164,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task) {
|
public @NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task) {
|
||||||
return thenApplySync(result -> {
|
return thenApplySync(_ -> {
|
||||||
task.run();
|
task.run();
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
@@ -165,7 +172,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
public @NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
||||||
return thenApplyDelayedSync(result -> {
|
return thenApplyDelayedSync(_ -> {
|
||||||
task.run();
|
task.run();
|
||||||
return null;
|
return null;
|
||||||
}, delay, unit);
|
}, delay, unit);
|
||||||
@@ -189,27 +196,23 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task) {
|
public <V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task) {
|
||||||
return thenApplySync(result -> task.get());
|
return thenApplySync(_ -> task.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenSupplyDelayedSync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
public <V> @NotNull Promise<V> thenSupplyDelayedSync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
||||||
return thenApplyDelayedSync(result -> task.get(), delay, unit);
|
return thenApplyDelayedSync(_ -> task.get(), delay, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task) {
|
public <V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
CompletablePromise<V> promise = getFactory().unresolved();
|
||||||
addDirectListener(
|
addDirectListener(
|
||||||
res -> {
|
res -> runCompleter(promise, () -> {
|
||||||
try {
|
Runnable runnable = createCompleter(res, promise, task);
|
||||||
Runnable runnable = createRunnable(res, promise, task);
|
FS future = getFactory().getSyncExecutor().run(runnable);
|
||||||
F future = getExecutor().runSync(runnable);
|
promise.addDirectListener(_ -> getFactory().getSyncExecutor().cancel(future));
|
||||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
}),
|
||||||
} catch (RejectedExecutionException e) {
|
|
||||||
promise.completeExceptionally(e);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
promise::completeExceptionally
|
promise::completeExceptionally
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -219,17 +222,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
public <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
CompletablePromise<V> promise = getFactory().unresolved();
|
||||||
addDirectListener(
|
addDirectListener(
|
||||||
res -> {
|
res -> runCompleter(promise, () -> {
|
||||||
try {
|
Runnable runnable = createCompleter(res, promise, task);
|
||||||
Runnable runnable = createRunnable(res, promise, task);
|
FS future = getFactory().getSyncExecutor().run(runnable, delay, unit);
|
||||||
F future = getExecutor().runSync(runnable, delay, unit);
|
promise.addDirectListener(_ -> getFactory().getSyncExecutor().cancel(future));
|
||||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
}),
|
||||||
} catch (RejectedExecutionException e) {
|
|
||||||
promise.completeExceptionally(e);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
promise::completeExceptionally
|
promise::completeExceptionally
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -239,7 +238,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
public <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
CompletablePromise<V> promise = getFactory().unresolved();
|
||||||
thenApplySync(task).addDirectListener(
|
thenApplySync(task).addDirectListener(
|
||||||
nestedPromise -> {
|
nestedPromise -> {
|
||||||
if (nestedPromise == null) {
|
if (nestedPromise == null) {
|
||||||
@@ -258,7 +257,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task) {
|
public @NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task) {
|
||||||
return thenApplyAsync(result -> {
|
return thenApplyAsync(_ -> {
|
||||||
task.run();
|
task.run();
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
@@ -266,7 +265,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<Void> thenRunDelayedAsync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
public @NotNull Promise<Void> thenRunDelayedAsync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
||||||
return thenApplyDelayedAsync(result -> {
|
return thenApplyDelayedAsync(_ -> {
|
||||||
task.run();
|
task.run();
|
||||||
return null;
|
return null;
|
||||||
}, delay, unit);
|
}, delay, unit);
|
||||||
@@ -290,17 +289,17 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task) {
|
public <V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task) {
|
||||||
return thenApplyAsync(result -> task.get());
|
return thenApplyAsync(_ -> task.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenSupplyDelayedAsync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
public <V> @NotNull Promise<V> thenSupplyDelayedAsync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
||||||
return thenApplyDelayedAsync(result -> task.get(), delay, unit);
|
return thenApplyDelayedAsync(_ -> task.get(), delay, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> thenPopulateReference(@NotNull AtomicReference<T> reference) {
|
public @NotNull Promise<T> thenPopulateReference(@NotNull AtomicReference<T> reference) {
|
||||||
return thenApplyAsync((result) -> {
|
return thenApplyAsync(result -> {
|
||||||
reference.set(result);
|
reference.set(result);
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
@@ -308,17 +307,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task) {
|
public <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
CompletablePromise<V> promise = getFactory().unresolved();
|
||||||
addDirectListener(
|
addDirectListener(
|
||||||
(res) -> {
|
(res) -> runCompleter(promise, () -> {
|
||||||
try {
|
Runnable runnable = createCompleter(res, promise, task);
|
||||||
Runnable runnable = createRunnable(res, promise, task);
|
FA future = getFactory().getAsyncExecutor().run(runnable);
|
||||||
F future = getExecutor().runAsync(runnable);
|
promise.addDirectListener(_ -> getFactory().getAsyncExecutor().cancel(future));
|
||||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
}),
|
||||||
} catch (RejectedExecutionException e) {
|
|
||||||
promise.completeExceptionally(e);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
promise::completeExceptionally
|
promise::completeExceptionally
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -328,17 +323,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
CompletablePromise<V> promise = getFactory().unresolved();
|
||||||
addDirectListener(
|
addDirectListener(
|
||||||
res -> {
|
res -> runCompleter(promise, () -> {
|
||||||
try {
|
Runnable runnable = createCompleter(res, promise, task);
|
||||||
Runnable runnable = createRunnable(res, promise, task);
|
FA future = getFactory().getAsyncExecutor().run(runnable, delay, unit);
|
||||||
F future = getExecutor().runAsync(runnable, delay, unit);
|
promise.addDirectListener(_ -> getFactory().getAsyncExecutor().cancel(future));
|
||||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
}),
|
||||||
} catch (RejectedExecutionException e) {
|
|
||||||
promise.completeExceptionally(e);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
promise::completeExceptionally
|
promise::completeExceptionally
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -348,7 +339,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
public <V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
CompletablePromise<V> promise = getFactory().unresolved();
|
||||||
thenApplyAsync(task).addDirectListener(
|
thenApplyAsync(task).addDirectListener(
|
||||||
nestedPromise -> {
|
nestedPromise -> {
|
||||||
if (nestedPromise == null) {
|
if (nestedPromise == null) {
|
||||||
@@ -367,7 +358,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<Void> erase() {
|
public @NotNull Promise<Void> erase() {
|
||||||
return thenSupplyAsync(() -> null);
|
return thenSupply(() -> null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -378,10 +369,10 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> addAsyncListener(@Nullable Consumer<T> successListener, @Nullable Consumer<Throwable> errorListener) {
|
public @NotNull Promise<T> addAsyncListener(@Nullable Consumer<T> successListener, @Nullable Consumer<Throwable> errorListener) {
|
||||||
return addAsyncListener((res) -> {
|
return addAsyncListener((res) -> {
|
||||||
if (res.isError()) {
|
if (res.isSuccess()) {
|
||||||
if (errorListener != null) errorListener.accept(res.getException());
|
|
||||||
} else {
|
|
||||||
if (successListener != null) successListener.accept(res.getResult());
|
if (successListener != null) successListener.accept(res.getResult());
|
||||||
|
} else {
|
||||||
|
if (errorListener != null) errorListener.accept(res.getException());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -394,49 +385,47 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> addDirectListener(@Nullable Consumer<T> successListener, @Nullable Consumer<Throwable> errorListener) {
|
public @NotNull Promise<T> addDirectListener(@Nullable Consumer<T> successListener, @Nullable Consumer<Throwable> errorListener) {
|
||||||
return addDirectListener((res) -> {
|
return addDirectListener((res) -> {
|
||||||
if (res.isError()) {
|
if (res.isSuccess()) {
|
||||||
if (errorListener != null) errorListener.accept(res.getException());
|
|
||||||
} else {
|
|
||||||
if (successListener != null) successListener.accept(res.getResult());
|
if (successListener != null) successListener.accept(res.getResult());
|
||||||
|
} else {
|
||||||
|
if (errorListener != null) errorListener.accept(res.getException());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private @NotNull Promise<T> addAnyListener(PromiseListener<T> listener) {
|
private @NotNull Promise<T> addAnyListener(PromiseListener<T> listener) {
|
||||||
PromiseCompletion<T> completion;
|
Collection<PromiseListener<T>> res = listeners.updateAndGet(v -> {
|
||||||
|
if (v == Collections.EMPTY_LIST) v = new ConcurrentLinkedQueue<>();
|
||||||
|
if (v != null) v.add(listener);
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
|
||||||
lock.lock();
|
if (res == null) {
|
||||||
try {
|
|
||||||
completion = getCompletion();
|
|
||||||
if (completion == null) {
|
|
||||||
if (listeners == null) listeners = new LinkedList<>();
|
|
||||||
listeners.add(listener);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
callListener(listener, completion);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void callListener(PromiseListener<T> listener, PromiseCompletion<T> ctx) {
|
|
||||||
if (listener instanceof AsyncPromiseListener) {
|
if (listener instanceof AsyncPromiseListener) {
|
||||||
try {
|
callListenerAsync(listener, Objects.requireNonNull(getCompletion()));
|
||||||
getExecutor().runAsync(() -> callListenerNow(listener, ctx));
|
|
||||||
} catch (RejectedExecutionException ignored) {
|
|
||||||
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
callListenerNow(listener, ctx);
|
callListenerNow(listener, Objects.requireNonNull(getCompletion()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void callListenerNow(PromiseListener<T> listener, PromiseCompletion<T> ctx) {
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void callListenerAsync(PromiseListener<T> listener, PromiseCompletion<T> res) {
|
||||||
try {
|
try {
|
||||||
listener.handle(ctx);
|
getFactory().getAsyncExecutor().run(() -> callListenerNow(listener, res));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
getLogger().warn("Exception caught while running promise listener", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void callListenerNow(PromiseListener<T> listener, PromiseCompletion<T> res) {
|
||||||
|
try {
|
||||||
|
listener.handle(res);
|
||||||
|
} catch (Error e) {
|
||||||
|
getLogger().error("Error caught in promise listener", e);
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable e) {
|
||||||
getLogger().error("Exception caught in promise listener", e);
|
getLogger().error("Exception caught in promise listener", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -453,13 +442,15 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> logExceptions(@NotNull String message) {
|
public @NotNull Promise<T> logExceptions(@NotNull String message) {
|
||||||
return onError(e -> getLogger().error(message, e));
|
Exception wrapper = new DeferredExecutionException();
|
||||||
|
return onError(e -> getLogger().error(message, wrapper.initCause(e)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends Throwable> @NotNull Promise<T> onError(@NotNull Class<E> clazz, @NotNull Consumer<E> listener) {
|
public <E extends Throwable> @NotNull Promise<T> onError(@NotNull Class<E> clazz, @NotNull Consumer<E> listener) {
|
||||||
return onError((e) -> {
|
return onError((e) -> {
|
||||||
if (clazz.isAssignableFrom(e.getClass())) {
|
if (clazz.isAssignableFrom(e.getClass())) {
|
||||||
|
getLogger().info("On Error {}", e.getClass());
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
listener.accept((E) e);
|
listener.accept((E) e);
|
||||||
}
|
}
|
||||||
@@ -471,37 +462,51 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
return onError(CancellationException.class, listener);
|
return onError(CancellationException.class, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit) {
|
public @NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit) {
|
||||||
return maxWaitTime(time, unit);
|
Exception e = new CancellationException("Promise timed out after " + time + " " + unit);
|
||||||
|
return completeExceptionallyDelayed(e, time, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> maxWaitTime(long time, @NotNull TimeUnit unit) {
|
public @NotNull Promise<T> maxWaitTime(long time, @NotNull TimeUnit unit) {
|
||||||
try {
|
|
||||||
Exception e = new TimeoutException("Promise stopped waiting after " + time + " " + unit);
|
Exception e = new TimeoutException("Promise stopped waiting after " + time + " " + unit);
|
||||||
F future = getExecutor().runAsync(() -> completeExceptionally(e), time, unit);
|
return completeExceptionallyDelayed(e, time, unit);
|
||||||
return addDirectListener((_v) -> getExecutor().cancel(future));
|
|
||||||
} catch (RejectedExecutionException e) {
|
|
||||||
completeExceptionally(e);
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Promise<T> completeExceptionallyDelayed(Throwable e, long delay, TimeUnit unit) {
|
||||||
|
runCompleter(this, () -> {
|
||||||
|
FA future = getFactory().getAsyncExecutor().run(() -> completeExceptionally(e), delay, unit);
|
||||||
|
addDirectListener(_ -> getFactory().getAsyncExecutor().cancel(future));
|
||||||
|
});
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleCompletion(@NotNull PromiseCompletion<T> ctx) {
|
private void handleCompletion(@NotNull PromiseCompletion<T> ctx) {
|
||||||
lock.lock();
|
|
||||||
try {
|
|
||||||
if (!setCompletion(ctx)) return;
|
if (!setCompletion(ctx)) return;
|
||||||
|
latch.countDown();
|
||||||
|
|
||||||
this.latch.countDown();
|
Iterator<PromiseListener<T>> iter = listeners.getAndSet(null).iterator();
|
||||||
if (listeners != null) {
|
while (iter.hasNext()) {
|
||||||
for (PromiseListener<T> listener : listeners) {
|
PromiseListener<T> listener = iter.next();
|
||||||
callListener(listener, ctx);
|
|
||||||
}
|
if (listener instanceof AsyncPromiseListener) {
|
||||||
}
|
callListenerAsync(listener, ctx);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
callListenerNow(listener, ctx);
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
iter.forEachRemaining(v -> callListenerAsyncLastResort(v, ctx));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void callListenerAsyncLastResort(PromiseListener<T> listener, PromiseCompletion<T> ctx) {
|
||||||
|
try {
|
||||||
|
getFactory().getAsyncExecutor().run(() -> callListenerNow(listener, ctx));
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -510,8 +515,8 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cancel(@Nullable String message) {
|
public void cancel(@NotNull CancellationException e) {
|
||||||
completeExceptionally(new CancellationException(message));
|
completeExceptionally(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -538,7 +543,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
public @NotNull CompletableFuture<T> toFuture() {
|
public @NotNull CompletableFuture<T> toFuture() {
|
||||||
CompletableFuture<T> future = new CompletableFuture<>();
|
CompletableFuture<T> future = new CompletableFuture<>();
|
||||||
this.addDirectListener(future::complete, future::completeExceptionally);
|
this.addDirectListener(future::complete, future::completeExceptionally);
|
||||||
future.whenComplete((res, e) -> {
|
future.whenComplete((_, e) -> {
|
||||||
if (e instanceof CancellationException) {
|
if (e instanceof CancellationException) {
|
||||||
this.cancel();
|
this.cancel();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package dev.tommyjs.futur.promise;
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
import dev.tommyjs.futur.executor.PromiseExecutor;
|
import dev.tommyjs.futur.executor.PromiseExecutor;
|
||||||
|
import dev.tommyjs.futur.joiner.CompletionJoiner;
|
||||||
|
import dev.tommyjs.futur.joiner.MappedResultJoiner;
|
||||||
|
import dev.tommyjs.futur.joiner.ResultJoiner;
|
||||||
|
import dev.tommyjs.futur.joiner.VoidJoiner;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -8,151 +12,93 @@ import java.util.*;
|
|||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Stream;
|
||||||
import java.util.stream.StreamSupport;
|
|
||||||
|
|
||||||
public abstract class AbstractPromiseFactory<F> implements PromiseFactory {
|
public abstract class AbstractPromiseFactory<FS, FA> implements PromiseFactory {
|
||||||
|
|
||||||
public abstract @NotNull PromiseExecutor<F> getExecutor();
|
public abstract @NotNull PromiseExecutor<FS> getSyncExecutor();
|
||||||
|
|
||||||
|
public abstract @NotNull PromiseExecutor<FA> getAsyncExecutor();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <K, V> @NotNull Promise<Map.Entry<K, V>> combine(boolean propagateCancel, @NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
public <K, V> @NotNull Promise<Map.Entry<K, V>> combine(
|
||||||
List<Promise<?>> promises = List.of(p1, p2);
|
@NotNull Promise<K> p1,
|
||||||
return all(propagateCancel, promises)
|
@NotNull Promise<V> p2,
|
||||||
.thenApplyAsync((res) -> new AbstractMap.SimpleImmutableEntry<>(
|
boolean dontFork
|
||||||
|
) {
|
||||||
|
return all(dontFork, p1, p2).thenApply((_) -> new AbstractMap.SimpleImmutableEntry<>(
|
||||||
Objects.requireNonNull(p1.getCompletion()).getResult(),
|
Objects.requireNonNull(p1.getCompletion()).getResult(),
|
||||||
Objects.requireNonNull(p2.getCompletion()).getResult()
|
Objects.requireNonNull(p2.getCompletion()).getResult()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <K, V> @NotNull Promise<Map<K, V>> combine(boolean propagateCancel, @NotNull Map<K, Promise<V>> promises, @Nullable BiConsumer<K, Throwable> exceptionHandler) {
|
public <K, V> @NotNull Promise<Map<K, V>> combine(
|
||||||
|
@NotNull Map<K, Promise<V>> promises,
|
||||||
|
@Nullable BiConsumer<K, Throwable> exceptionHandler,
|
||||||
|
boolean link
|
||||||
|
) {
|
||||||
if (promises.isEmpty()) return resolve(Collections.emptyMap());
|
if (promises.isEmpty()) return resolve(Collections.emptyMap());
|
||||||
|
return new MappedResultJoiner<>(this,
|
||||||
Map<K, V> map = new HashMap<>();
|
promises.entrySet().iterator(), exceptionHandler, promises.size(), link).joined();
|
||||||
Promise<Map<K, V>> promise = unresolved();
|
|
||||||
for (Map.Entry<K, Promise<V>> entry : promises.entrySet()) {
|
|
||||||
if (propagateCancel) {
|
|
||||||
AbstractPromise.propagateCancel(promise, entry.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
entry.getValue().addDirectListener((ctx) -> {
|
|
||||||
synchronized (map) {
|
|
||||||
if (ctx.getException() != null) {
|
|
||||||
if (exceptionHandler == null) {
|
|
||||||
promise.completeExceptionally(ctx.getException());
|
|
||||||
} else {
|
|
||||||
exceptionHandler.accept(entry.getKey(), ctx.getException());
|
|
||||||
map.put(entry.getKey(), null);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
map.put(entry.getKey(), ctx.getResult());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (map.size() == promises.size()) {
|
|
||||||
promise.complete(map);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<List<V>> combine(boolean propagateCancel, @NotNull Iterable<Promise<V>> promises, @Nullable BiConsumer<Integer, Throwable> exceptionHandler) {
|
public <V> @NotNull Promise<List<V>> combine(
|
||||||
AtomicInteger index = new AtomicInteger();
|
@NotNull Iterator<Promise<V>> promises, int expectedSize,
|
||||||
return this.combine(
|
@Nullable BiConsumer<Integer, Throwable> exceptionHandler, boolean link
|
||||||
propagateCancel,
|
) {
|
||||||
StreamSupport.stream(promises.spliterator(), false)
|
if (!promises.hasNext()) return resolve(Collections.emptyList());
|
||||||
.collect(Collectors.toMap(k -> index.getAndIncrement(), v -> v)),
|
return new ResultJoiner<>(
|
||||||
exceptionHandler
|
this, promises, exceptionHandler, expectedSize, link).joined();
|
||||||
).thenApplyAsync(v ->
|
|
||||||
v.entrySet().stream()
|
|
||||||
.sorted(Map.Entry.comparingByKey())
|
|
||||||
.map(Map.Entry::getValue)
|
|
||||||
.collect(Collectors.toList())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<List<PromiseCompletion<?>>> allSettled(boolean propagateCancel, @NotNull Iterable<Promise<?>> promiseIterable) {
|
public @NotNull Promise<List<PromiseCompletion<?>>> allSettled(
|
||||||
List<Promise<?>> promises = new ArrayList<>();
|
@NotNull Iterator<Promise<?>> promises,
|
||||||
promiseIterable.iterator().forEachRemaining(promises::add);
|
int expectedSize,
|
||||||
|
boolean link
|
||||||
if (promises.isEmpty()) return resolve(Collections.emptyList());
|
) {
|
||||||
PromiseCompletion<?>[] results = new PromiseCompletion<?>[promises.size()];
|
if (!promises.hasNext()) return resolve(Collections.emptyList());
|
||||||
|
return new CompletionJoiner(this, promises, expectedSize, link).joined();
|
||||||
Promise<List<PromiseCompletion<?>>> promise = unresolved();
|
|
||||||
var iter = promises.listIterator();
|
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
|
||||||
int index = iter.nextIndex();
|
|
||||||
var p = iter.next();
|
|
||||||
|
|
||||||
if (propagateCancel) {
|
|
||||||
AbstractPromise.propagateCancel(promise, p);
|
|
||||||
}
|
|
||||||
|
|
||||||
p.addDirectListener((res) -> {
|
|
||||||
synchronized (results) {
|
|
||||||
results[index] = res;
|
|
||||||
if (Arrays.stream(results).allMatch(Objects::nonNull))
|
|
||||||
promise.complete(Arrays.asList(results));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<Void> all(boolean propagateCancel, @NotNull Iterable<Promise<?>> promiseIterable) {
|
public @NotNull Promise<Void> all(@NotNull Iterator<Promise<?>> promises, boolean link) {
|
||||||
List<Promise<?>> promises = new ArrayList<>();
|
if (!promises.hasNext()) return resolve(null);
|
||||||
promiseIterable.iterator().forEachRemaining(promises::add);
|
return new VoidJoiner(this, promises, link).joined();
|
||||||
|
|
||||||
if (promises.isEmpty()) return resolve(null);
|
|
||||||
AtomicInteger completed = new AtomicInteger();
|
|
||||||
Promise<Void> promise = unresolved();
|
|
||||||
|
|
||||||
for (Promise<?> p : promises) {
|
|
||||||
if (propagateCancel) {
|
|
||||||
AbstractPromise.propagateCancel(promise, p);
|
|
||||||
}
|
|
||||||
|
|
||||||
p.addDirectListener((res) -> {
|
|
||||||
if (res.getException() != null) {
|
|
||||||
promise.completeExceptionally(res.getException());
|
|
||||||
} else if (completed.incrementAndGet() == promises.size()) {
|
|
||||||
promise.complete(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> race(boolean cancelRaceLosers, @NotNull Iterable<Promise<V>> promises) {
|
public <V> @NotNull Promise<V> race(@NotNull Iterator<Promise<V>> promises, boolean link) {
|
||||||
Promise<V> promise = unresolved();
|
CompletablePromise<V> promise = unresolved();
|
||||||
for (Promise<V> p : promises) {
|
promises.forEachRemaining(p -> {
|
||||||
if (cancelRaceLosers) {
|
if (link) AbstractPromise.cancelOnFinish(p, promise);
|
||||||
promise.addListener((res) -> p.cancel());
|
if (!promise.isCompleted())
|
||||||
}
|
|
||||||
AbstractPromise.propagateResult(p, promise);
|
AbstractPromise.propagateResult(p, promise);
|
||||||
}
|
});
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <V> @NotNull Promise<V> race(@NotNull Iterable<Promise<V>> promises, boolean link) {
|
||||||
|
return race(promises.iterator(), link);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <V> @NotNull Promise<V> race(@NotNull Stream<Promise<V>> promises, boolean link) {
|
||||||
|
return race(promises.iterator(), link);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future) {
|
public <T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future) {
|
||||||
return wrap(future, future);
|
return wrap(future, future);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> @NotNull Promise<T> wrap(@NotNull CompletionStage<T> completion, Future<T> future) {
|
private <T> @NotNull Promise<T> wrap(@NotNull CompletionStage<T> completion, Future<T> future) {
|
||||||
Promise<T> promise = unresolved();
|
CompletablePromise<T> promise = unresolved();
|
||||||
|
|
||||||
completion.whenComplete((v, e) -> {
|
completion.whenComplete((v, e) -> {
|
||||||
if (e != null) {
|
if (e != null) {
|
||||||
@@ -162,20 +108,20 @@ public abstract class AbstractPromiseFactory<F> implements PromiseFactory {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
promise.onCancel((e) -> future.cancel(true));
|
promise.onCancel(_ -> future.cancel(true));
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> @NotNull Promise<T> resolve(T value) {
|
public <T> @NotNull Promise<T> resolve(T value) {
|
||||||
Promise<T> promise = unresolved();
|
CompletablePromise<T> promise = unresolved();
|
||||||
promise.complete(value);
|
promise.complete(value);
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> @NotNull Promise<T> error(@NotNull Throwable error) {
|
public <T> @NotNull Promise<T> error(@NotNull Throwable error) {
|
||||||
Promise<T> promise = unresolved();
|
CompletablePromise<T> promise = unresolved();
|
||||||
promise.completeExceptionally(error);
|
promise.completeExceptionally(error);
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public interface CompletablePromise<T> extends Promise<T> {
|
||||||
|
|
||||||
|
void complete(@Nullable T result);
|
||||||
|
|
||||||
|
void completeExceptionally(@NotNull Throwable result);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
class DeferredExecutionException extends ExecutionException {
|
||||||
|
|
||||||
|
public DeferredExecutionException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,16 +8,13 @@ import org.jetbrains.annotations.Blocking;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public interface Promise<T> {
|
public interface Promise<T> {
|
||||||
|
|
||||||
PromiseFactory getFactory();
|
@NotNull PromiseFactory getFactory();
|
||||||
|
|
||||||
@NotNull Promise<Void> thenRun(@NotNull ExceptionalRunnable task);
|
@NotNull Promise<Void> thenRun(@NotNull ExceptionalRunnable task);
|
||||||
|
|
||||||
@@ -80,6 +77,9 @@ public interface Promise<T> {
|
|||||||
*/
|
*/
|
||||||
@NotNull Promise<T> addDirectListener(@NotNull PromiseListener<T> listener);
|
@NotNull Promise<T> addDirectListener(@NotNull PromiseListener<T> listener);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @apiNote Direct listeners run on the same thread as the completion.
|
||||||
|
*/
|
||||||
@NotNull Promise<T> addDirectListener(@Nullable Consumer<T> successHandler, @Nullable Consumer<Throwable> errorHandler);
|
@NotNull Promise<T> addDirectListener(@Nullable Consumer<T> successHandler, @Nullable Consumer<Throwable> errorHandler);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -94,6 +94,9 @@ public interface Promise<T> {
|
|||||||
return addAsyncListener(listener);
|
return addAsyncListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @apiNote Async listeners are run in parallel.
|
||||||
|
*/
|
||||||
@NotNull Promise<T> addAsyncListener(@Nullable Consumer<T> successHandler, @Nullable Consumer<Throwable> errorHandler);
|
@NotNull Promise<T> addAsyncListener(@Nullable Consumer<T> successHandler, @Nullable Consumer<Throwable> errorHandler);
|
||||||
|
|
||||||
@NotNull Promise<T> onSuccess(@NotNull Consumer<T> listener);
|
@NotNull Promise<T> onSuccess(@NotNull Consumer<T> listener);
|
||||||
@@ -105,55 +108,70 @@ public interface Promise<T> {
|
|||||||
@NotNull Promise<T> onCancel(@NotNull Consumer<CancellationException> listener);
|
@NotNull Promise<T> onCancel(@NotNull Consumer<CancellationException> listener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use maxWaitTime instead
|
* Cancels the promise with a TimeoutException after the specified time.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
|
||||||
@NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit);
|
@NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use maxWaitTime instead
|
* Cancels the promise with a TimeoutException after the specified time.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
|
||||||
default @NotNull Promise<T> timeout(long ms) {
|
default @NotNull Promise<T> timeout(long ms) {
|
||||||
return timeout(ms, TimeUnit.MILLISECONDS);
|
return timeout(ms, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Completes the promise exceptionally with a TimeoutException after the specified time.
|
||||||
|
*/
|
||||||
@NotNull Promise<T> maxWaitTime(long time, @NotNull TimeUnit unit);
|
@NotNull Promise<T> maxWaitTime(long time, @NotNull TimeUnit unit);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Completes the promise exceptionally with a TimeoutException after the specified time.
|
||||||
|
*/
|
||||||
default @NotNull Promise<T> maxWaitTime(long ms) {
|
default @NotNull Promise<T> maxWaitTime(long ms) {
|
||||||
return maxWaitTime(ms, TimeUnit.MILLISECONDS);
|
return maxWaitTime(ms, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cancel(@Nullable String reason);
|
void cancel(@NotNull CancellationException exception);
|
||||||
|
|
||||||
|
default void cancel(@NotNull String reason) {
|
||||||
|
cancel(new CancellationException(reason));
|
||||||
|
};
|
||||||
|
|
||||||
default void cancel() {
|
default void cancel() {
|
||||||
cancel(null);
|
cancel(new CancellationException());
|
||||||
}
|
}
|
||||||
|
|
||||||
void complete(@Nullable T result);
|
/**
|
||||||
|
* Waits if necessary for this promise to complete, and then returns its result.
|
||||||
void completeExceptionally(@NotNull Throwable result);
|
* @throws CancellationException if the computation was cancelled
|
||||||
|
* @throws CompletionException if this promise completed exceptionally
|
||||||
@Blocking
|
*/
|
||||||
T awaitInterruptibly() throws InterruptedException;
|
|
||||||
|
|
||||||
@Blocking
|
|
||||||
T awaitInterruptibly(long timeout) throws TimeoutException, InterruptedException;
|
|
||||||
|
|
||||||
@Blocking
|
@Blocking
|
||||||
T await();
|
T await();
|
||||||
|
|
||||||
@Blocking
|
|
||||||
T await(long timeout) throws TimeoutException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use await instead.
|
* Waits if necessary for this promise to complete, and then returns its result.
|
||||||
|
* @throws CancellationException if the computation was cancelled
|
||||||
|
* @throws ExecutionException if this promise completed exceptionally
|
||||||
|
* @throws InterruptedException if the current thread was interrupted while waiting
|
||||||
*/
|
*/
|
||||||
@Blocking
|
@Blocking
|
||||||
@Deprecated
|
T get() throws InterruptedException, ExecutionException;
|
||||||
default T join(long timeout) throws TimeoutException {
|
|
||||||
return await(timeout);
|
/**
|
||||||
};
|
* Waits if necessary for at most the given time for this future to complete, and then returns its result, if available.
|
||||||
|
* @throws CancellationException if the computation was cancelled
|
||||||
|
* @throws ExecutionException if this promise completed exceptionally
|
||||||
|
* @throws InterruptedException if the current thread was interrupted while waiting
|
||||||
|
* @throws TimeoutException if the wait timed out
|
||||||
|
*/
|
||||||
|
@Blocking
|
||||||
|
T get(long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops this promise from propagating up cancellations.
|
||||||
|
*/
|
||||||
|
@NotNull Promise<T> fork();
|
||||||
|
|
||||||
@Nullable PromiseCompletion<T> getCompletion();
|
@Nullable PromiseCompletion<T> getCompletion();
|
||||||
|
|
||||||
|
|||||||
@@ -22,12 +22,16 @@ public class PromiseCompletion<T> {
|
|||||||
this.result = null;
|
this.result = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isSuccess() {
|
||||||
|
return exception == null;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isError() {
|
public boolean isError() {
|
||||||
return getException() != null;
|
return exception != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean wasCanceled() {
|
public boolean wasCanceled() {
|
||||||
return getException() instanceof CancellationException;
|
return exception instanceof CancellationException;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable T getResult() {
|
public @Nullable T getResult() {
|
||||||
|
|||||||
@@ -1,90 +1,183 @@
|
|||||||
package dev.tommyjs.futur.promise;
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
|
import dev.tommyjs.futur.executor.PromiseExecutor;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public interface PromiseFactory {
|
public interface PromiseFactory {
|
||||||
|
|
||||||
|
static @NotNull PromiseFactory of(@NotNull Logger logger, @NotNull PromiseExecutor<?> syncExecutor, @NotNull PromiseExecutor<?> asyncExecutor) {
|
||||||
|
return new PromiseFactoryImpl<>(logger, syncExecutor, asyncExecutor);
|
||||||
|
}
|
||||||
|
|
||||||
|
static @NotNull PromiseFactory of(@NotNull Logger logger, @NotNull PromiseExecutor<?> executor) {
|
||||||
|
return new PromiseFactoryImpl<>(logger, executor, executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
static @NotNull PromiseFactory of(@NotNull Logger logger, @NotNull ScheduledExecutorService executor) {
|
||||||
|
return of(logger, PromiseExecutor.of(executor));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int size(@NotNull Stream<?> stream) {
|
||||||
|
long estimate = stream.spliterator().estimateSize();
|
||||||
|
return estimate == Long.MAX_VALUE ? 10 : (int) estimate;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull Logger getLogger();
|
@NotNull Logger getLogger();
|
||||||
|
|
||||||
<T> @NotNull Promise<T> unresolved();
|
<T> @NotNull CompletablePromise<T> unresolved();
|
||||||
|
|
||||||
<K, V> @NotNull Promise<Map.Entry<K, V>> combine(boolean propagateCancel, @NotNull Promise<K> p1, @NotNull Promise<V> p2);
|
<K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2, boolean cancelOnError);
|
||||||
|
|
||||||
default <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
default <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
||||||
return combine(false, p1, p2);
|
return combine(p1, p2, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
<K, V> @NotNull Promise<Map<K, V>> combine(boolean propagateCancel, @NotNull Map<K, Promise<V>> promises, @Nullable BiConsumer<K, Throwable> exceptionHandler);
|
<K, V> @NotNull Promise<Map<K, V>> combine(
|
||||||
|
@NotNull Map<K, Promise<V>> promises,
|
||||||
|
@Nullable BiConsumer<K, Throwable> exceptionHandler,
|
||||||
|
boolean propagateCancel
|
||||||
|
);
|
||||||
|
|
||||||
default <K, V> @NotNull Promise<Map<K, V>> combine(boolean propagateCancel, @NotNull Map<K, Promise<V>> promises) {
|
default <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, @NotNull BiConsumer<K, Throwable> exceptionHandler) {
|
||||||
return combine(propagateCancel, promises, null);
|
return combine(promises, exceptionHandler, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
default <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, @Nullable BiConsumer<K, Throwable> exceptionHandler) {
|
default <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, boolean cancelOnError) {
|
||||||
return combine(false, promises, exceptionHandler);
|
return combine(promises, null, cancelOnError);
|
||||||
}
|
}
|
||||||
|
|
||||||
default <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises) {
|
default <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises) {
|
||||||
return combine(promises, null);
|
return combine(promises, null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
<V> @NotNull Promise<List<V>> combine(boolean propagateCancel, @NotNull Iterable<Promise<V>> promises, @Nullable BiConsumer<Integer, Throwable> exceptionHandler);
|
<V> @NotNull Promise<List<V>> combine(
|
||||||
|
@NotNull Iterator<Promise<V>> promises, int expectedSize,
|
||||||
|
@Nullable BiConsumer<Integer, Throwable> exceptionHandler, boolean propagateCancel
|
||||||
|
);
|
||||||
|
|
||||||
default <V> @NotNull Promise<List<V>> combine(boolean propagateCancel, @NotNull Iterable<Promise<V>> promises) {
|
default <V> @NotNull Promise<List<V>> combine(
|
||||||
return combine(propagateCancel, promises, null);
|
@NotNull Collection<Promise<V>> promises,
|
||||||
|
@NotNull BiConsumer<Integer, Throwable> exceptionHandler,
|
||||||
|
boolean propagateCancel
|
||||||
|
) {
|
||||||
|
return combine(promises.iterator(), promises.size(), exceptionHandler, propagateCancel);
|
||||||
}
|
}
|
||||||
|
|
||||||
default <V> @NotNull Promise<List<V>> combine(@NotNull Iterable<Promise<V>> promises, @Nullable BiConsumer<Integer, Throwable> exceptionHandler) {
|
default <V> @NotNull Promise<List<V>> combine(
|
||||||
return combine(false, promises, exceptionHandler);
|
@NotNull Collection<Promise<V>> promises,
|
||||||
|
@NotNull BiConsumer<Integer, Throwable> exceptionHandler
|
||||||
|
) {
|
||||||
|
return combine(promises.iterator(), promises.size(), exceptionHandler, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
default <V> @NotNull Promise<List<V>> combine(@NotNull Iterable<Promise<V>> promises) {
|
default <V> @NotNull Promise<List<V>> combine(@NotNull Collection<Promise<V>> promises, boolean cancelOnError) {
|
||||||
return combine(promises, null);
|
return combine(promises.iterator(), promises.size(), null, cancelOnError);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull Promise<List<PromiseCompletion<?>>> allSettled(boolean propagateCancel, @NotNull Iterable<Promise<?>> promiseIterable);
|
default <V> @NotNull Promise<List<V>> combine(@NotNull Collection<Promise<V>> promises) {
|
||||||
|
return combine(promises.iterator(), promises.size(), null, true);
|
||||||
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Iterable<Promise<?>> promiseIterable) {
|
|
||||||
return allSettled(false, promiseIterable);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(boolean propagateCancel, @NotNull Promise<?>... promiseArray) {
|
default <V> @NotNull Promise<List<V>> combine(
|
||||||
return allSettled(propagateCancel, Arrays.asList(promiseArray));
|
@NotNull Stream<Promise<V>> promises,
|
||||||
|
@NotNull BiConsumer<Integer, Throwable> exceptionHandler,
|
||||||
|
boolean propagateCancel
|
||||||
|
) {
|
||||||
|
return combine(promises.iterator(), size(promises), exceptionHandler, propagateCancel);
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Promise<?>... promiseArray) {
|
default <V> @NotNull Promise<List<V>> combine(
|
||||||
return allSettled(false, promiseArray);
|
@NotNull Stream<Promise<V>> promises,
|
||||||
|
@NotNull BiConsumer<Integer, Throwable> exceptionHandler
|
||||||
|
) {
|
||||||
|
return combine(promises.iterator(), size(promises), exceptionHandler, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull Promise<Void> all(boolean propagateCancel, @NotNull Iterable<Promise<?>> promiseIterable);
|
default <V> @NotNull Promise<List<V>> combine(@NotNull Stream<Promise<V>> promises, boolean cancelOnError) {
|
||||||
|
return combine(promises.iterator(), size(promises), null, cancelOnError);
|
||||||
default @NotNull Promise<Void> all(@NotNull Iterable<Promise<?>> promiseIterable) {
|
|
||||||
return all(false, promiseIterable);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull Promise<Void> all(boolean propagateCancel, @NotNull Promise<?>... promiseArray) {
|
default <V> @NotNull Promise<List<V>> combine(@NotNull Stream<Promise<V>> promises) {
|
||||||
return all(propagateCancel, Arrays.asList(promiseArray));
|
return combine(promises.iterator(), size(promises), null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
default @NotNull Promise<Void> all(@NotNull Promise<?>... promiseArray) {
|
@NotNull Promise<List<PromiseCompletion<?>>> allSettled(
|
||||||
return all(false, promiseArray);
|
@NotNull Iterator<Promise<?>> promises, int estimatedSize, boolean propagateCancel);
|
||||||
|
|
||||||
|
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Collection<Promise<?>> promises, boolean propagateCancel) {
|
||||||
|
return allSettled(promises.iterator(), promises.size(), propagateCancel);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Collection<Promise<?>> promises) {
|
||||||
* @apiNote Even with cancelRaceLosers, it is not guaranteed that only one promise will complete.
|
return allSettled(promises.iterator(), promises.size(), true);
|
||||||
*/
|
}
|
||||||
<V> @NotNull Promise<V> race(boolean cancelRaceLosers, @NotNull Iterable<Promise<V>> promises);
|
|
||||||
|
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Stream<Promise<?>> promises, boolean propagateCancel) {
|
||||||
|
return allSettled(promises.iterator(), size(promises), propagateCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Stream<Promise<?>> promises) {
|
||||||
|
return allSettled(promises.iterator(), size(promises), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(boolean propagateCancel, @NotNull Promise<?>... promises) {
|
||||||
|
return allSettled(Arrays.asList(promises).iterator(), promises.length, propagateCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Promise<?>... promises) {
|
||||||
|
return allSettled(Arrays.asList(promises).iterator(), promises.length, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull Promise<Void> all(@NotNull Iterator<Promise<?>> promises, boolean cancelAllOnError);
|
||||||
|
|
||||||
|
default @NotNull Promise<Void> all(@NotNull Iterable<Promise<?>> promises, boolean cancelAllOnError) {
|
||||||
|
return all(promises.iterator(), cancelAllOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
default @NotNull Promise<Void> all(@NotNull Iterable<Promise<?>> promises) {
|
||||||
|
return all(promises.iterator(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
default @NotNull Promise<Void> all(@NotNull Stream<Promise<?>> promises, boolean cancelAllOnError) {
|
||||||
|
return all(promises.iterator(), cancelAllOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
default @NotNull Promise<Void> all(@NotNull Stream<Promise<?>> promises) {
|
||||||
|
return all(promises.iterator(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
default @NotNull Promise<Void> all(boolean cancelAllOnError, @NotNull Promise<?>... promises) {
|
||||||
|
return all(Arrays.asList(promises).iterator(), cancelAllOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
default @NotNull Promise<Void> all(@NotNull Promise<?>... promises) {
|
||||||
|
return all(Arrays.asList(promises).iterator(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
<V> @NotNull Promise<V> race(@NotNull Iterator<Promise<V>> promises, boolean cancelLosers);
|
||||||
|
|
||||||
|
default <V> @NotNull Promise<V> race(@NotNull Iterable<Promise<V>> promises, boolean cancelLosers) {
|
||||||
|
return race(promises.iterator(), cancelLosers);
|
||||||
|
}
|
||||||
|
|
||||||
default <V> @NotNull Promise<V> race(@NotNull Iterable<Promise<V>> promises) {
|
default <V> @NotNull Promise<V> race(@NotNull Iterable<Promise<V>> promises) {
|
||||||
return race(false, promises);
|
return race(promises.iterator(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <V> @NotNull Promise<V> race(@NotNull Stream<Promise<V>> promises, boolean cancelLosers) {
|
||||||
|
return race(promises.iterator(), cancelLosers);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <V> @NotNull Promise<V> race(@NotNull Stream<Promise<V>> promises) {
|
||||||
|
return race(promises.iterator(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
<T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future);
|
<T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future);
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
|
import dev.tommyjs.futur.executor.PromiseExecutor;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
public class PromiseFactoryImpl<FS, FA> extends AbstractPromiseFactory<FS, FA> {
|
||||||
|
|
||||||
|
private final @NotNull Logger logger;
|
||||||
|
private final @NotNull PromiseExecutor<FS> syncExecutor;
|
||||||
|
private final @NotNull PromiseExecutor<FA> asyncExecutor;
|
||||||
|
|
||||||
|
public PromiseFactoryImpl(
|
||||||
|
@NotNull Logger logger,
|
||||||
|
@NotNull PromiseExecutor<FS> syncExecutor,
|
||||||
|
@NotNull PromiseExecutor<FA> asyncExecutor
|
||||||
|
) {
|
||||||
|
this.logger = logger;
|
||||||
|
this.syncExecutor = syncExecutor;
|
||||||
|
this.asyncExecutor = asyncExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull <T> CompletablePromise<T> unresolved() {
|
||||||
|
return new PromiseImpl<>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Logger getLogger() {
|
||||||
|
return logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull PromiseExecutor<FS> getSyncExecutor() {
|
||||||
|
return syncExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull PromiseExecutor<FA> getAsyncExecutor() {
|
||||||
|
return asyncExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class PromiseImpl<T, FS, FA> extends AbstractPromise<T, FS, FA> {
|
||||||
|
|
||||||
|
private final @NotNull AbstractPromiseFactory<FS, FA> factory;
|
||||||
|
|
||||||
|
public PromiseImpl(@NotNull AbstractPromiseFactory<FS, FA> factory) {
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull AbstractPromiseFactory<FS, FA> getFactory() {
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
package dev.tommyjs.futur.promise;
|
|
||||||
|
|
||||||
import dev.tommyjs.futur.function.ExceptionalFunction;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.function.BiConsumer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated Use PromiseFactory instance methods instead.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public class Promises {
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
|
||||||
return combine(p1, p2, p1.getFactory());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2, PromiseFactory factory) {
|
|
||||||
return factory.combine(p1, p2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, long timeout, PromiseFactory factory) {
|
|
||||||
return combine(promises, timeout, true, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, long timeout, boolean strict, PromiseFactory factory) {
|
|
||||||
return combine(promises, timeout, strict ? null : (_k, _v) -> {}, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, long timeout, @Nullable BiConsumer<K, Throwable> exceptionHandler, PromiseFactory factory) {
|
|
||||||
return factory.combine(promises, exceptionHandler).timeout(timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, PromiseFactory factory) {
|
|
||||||
return combine(promises, 1500L, true, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout, PromiseFactory factory) {
|
|
||||||
return combine(promises, timeout, true, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout, boolean strict, PromiseFactory factory) {
|
|
||||||
return factory.combine(promises, strict ? null : (_i, _v) -> {}).timeout(timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, PromiseFactory factory) {
|
|
||||||
return combine(promises, 1500L, true, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> all(@NotNull List<Promise<?>> promises, PromiseFactory factory) {
|
|
||||||
return factory.all(promises);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper, long timeout, PromiseFactory factory) {
|
|
||||||
return combine(keys, mapper, timeout, true, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper, long timeout, boolean strict, PromiseFactory factory) {
|
|
||||||
Map<K, Promise<V>> promises = new HashMap<>();
|
|
||||||
for (K key : keys) {
|
|
||||||
Promise<V> promise = factory.resolve(key).thenApplyAsync(mapper);
|
|
||||||
promises.put(key, promise);
|
|
||||||
}
|
|
||||||
|
|
||||||
return combine(promises, timeout, strict, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper, PromiseFactory factory) {
|
|
||||||
return combine(keys, mapper, 1500L, true, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> erase(@NotNull Promise<?> p) {
|
|
||||||
return erase(p, p.getFactory());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> erase(@NotNull Promise<?> p, PromiseFactory factory) {
|
|
||||||
return p.erase();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future, PromiseFactory factory) {
|
|
||||||
return factory.wrap(future);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package dev.tommyjs.futur;
|
package dev.tommyjs.futur;
|
||||||
|
|
||||||
import dev.tommyjs.futur.executor.SinglePoolExecutor;
|
|
||||||
import dev.tommyjs.futur.impl.SimplePromiseFactory;
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -17,24 +15,36 @@ public final class PromiseTests {
|
|||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(PromiseTests.class);
|
private final Logger logger = LoggerFactory.getLogger(PromiseTests.class);
|
||||||
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
|
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
|
||||||
private final PromiseFactory pfac = new SimplePromiseFactory<>(new SinglePoolExecutor(executor), logger);
|
private final PromiseFactory promises = PromiseFactory.of(logger, executor);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testErrors() {
|
||||||
|
Promise<?> promise = promises.start().thenSupplyAsync(() -> {
|
||||||
|
throw new StackOverflowError();
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
promise.await();
|
||||||
|
} catch (CompletionException e) {
|
||||||
|
assert e.getCause() instanceof StackOverflowError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testShutdown() {
|
public void testShutdown() {
|
||||||
executor.shutdown();
|
executor.close();
|
||||||
Promise<?> promise = pfac.resolve(null).thenSupplyAsync(() -> null);
|
Promise<?> promise = promises.resolve(null).thenSupplyAsync(() -> null);
|
||||||
try {
|
try {
|
||||||
promise.await();
|
promise.await();
|
||||||
} catch (RuntimeException e) {
|
} catch (CompletionException e) {
|
||||||
assert e.getCause() instanceof RejectedExecutionException;
|
assert e.getCause() instanceof RejectedExecutionException;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testErrorCancellation() throws InterruptedException {
|
public void testCancellation() throws InterruptedException {
|
||||||
var finished = new AtomicBoolean();
|
var finished = new AtomicBoolean();
|
||||||
pfac.start()
|
promises.start().thenRunDelayedAsync(() -> finished.set(true), 50, TimeUnit.MILLISECONDS)
|
||||||
.thenRunDelayedAsync(() -> finished.set(true), 50, TimeUnit.MILLISECONDS)
|
|
||||||
.thenRunAsync(() -> {})
|
.thenRunAsync(() -> {})
|
||||||
.cancel();
|
.cancel();
|
||||||
|
|
||||||
@@ -44,11 +54,11 @@ public final class PromiseTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToFuture() throws InterruptedException {
|
public void testToFuture() throws InterruptedException {
|
||||||
assert pfac.resolve(true).toFuture().getNow(false);
|
assert promises.resolve(true).toFuture().getNow(false);
|
||||||
assert pfac.error(new Exception("Test")).toFuture().isCompletedExceptionally();
|
assert promises.error(new Exception("Test")).toFuture().isCompletedExceptionally();
|
||||||
|
|
||||||
var finished = new AtomicBoolean();
|
var finished = new AtomicBoolean();
|
||||||
pfac.start()
|
promises.start()
|
||||||
.thenRunDelayedAsync(() -> finished.set(true), 50, TimeUnit.MILLISECONDS)
|
.thenRunDelayedAsync(() -> finished.set(true), 50, TimeUnit.MILLISECONDS)
|
||||||
.toFuture()
|
.toFuture()
|
||||||
.cancel(true);
|
.cancel(true);
|
||||||
@@ -58,86 +68,81 @@ public final class PromiseTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCombineUtil() throws TimeoutException {
|
public void testCombineUtil() throws TimeoutException, ExecutionException, InterruptedException {
|
||||||
pfac.all(
|
promises.all(
|
||||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||||
)
|
)
|
||||||
.join(100L);
|
.get(100L, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
pfac.allSettled(
|
promises.allSettled(
|
||||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||||
)
|
)
|
||||||
.join(100L);
|
.get(100L, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
pfac.combine(
|
promises.combine(
|
||||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||||
)
|
)
|
||||||
.join(100L);
|
.get(100L, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
pfac.combine(
|
promises.combine(
|
||||||
List.of(
|
List.of(
|
||||||
pfac.start().thenRunDelayedAsync(() -> {}, 49, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> {}, 49, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> {}, 51, TimeUnit.MILLISECONDS)
|
promises.start().thenRunDelayedAsync(() -> {}, 51, TimeUnit.MILLISECONDS)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.join(100L);
|
.get(100L, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
pfac.combine(
|
promises.combine(
|
||||||
Map.of(
|
Map.of(
|
||||||
"a", pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
"a", promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||||
"b", pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
"b", promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.join(100L);
|
.get(100L, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCombineUtilPropagation() throws InterruptedException {
|
public void testCombineUtilPropagation() throws InterruptedException {
|
||||||
var finished1 = new AtomicBoolean();
|
var finished1 = new AtomicBoolean();
|
||||||
pfac.all(
|
promises.all(
|
||||||
true,
|
promises.start().thenRunDelayedAsync(() -> finished1.set(true), 50, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> finished1.set(true), 50, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> finished1.set(true), 50, TimeUnit.MILLISECONDS)
|
||||||
pfac.start().thenRunDelayedAsync(() -> finished1.set(true), 50, TimeUnit.MILLISECONDS)
|
|
||||||
)
|
)
|
||||||
.cancel();
|
.cancel();
|
||||||
|
|
||||||
var finished2 = new AtomicBoolean();
|
var finished2 = new AtomicBoolean();
|
||||||
pfac.allSettled(
|
promises.allSettled(
|
||||||
true,
|
promises.start().thenRunDelayedAsync(() -> finished2.set(true), 50, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> finished2.set(true), 50, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> finished2.set(true), 50, TimeUnit.MILLISECONDS)
|
||||||
pfac.start().thenRunDelayedAsync(() -> finished2.set(true), 50, TimeUnit.MILLISECONDS)
|
|
||||||
)
|
)
|
||||||
.cancel();
|
.cancel();
|
||||||
|
|
||||||
var finished3 = new AtomicBoolean();
|
var finished3 = new AtomicBoolean();
|
||||||
pfac.combine(
|
promises.combine(
|
||||||
true,
|
promises.start().thenRunDelayedAsync(() -> finished3.set(true), 50, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> finished3.set(true), 50, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> finished3.set(true), 50, TimeUnit.MILLISECONDS)
|
||||||
pfac.start().thenRunDelayedAsync(() -> finished3.set(true), 50, TimeUnit.MILLISECONDS)
|
|
||||||
)
|
)
|
||||||
.cancel();
|
.cancel();
|
||||||
|
|
||||||
var finished4 = new AtomicBoolean();
|
var finished4 = new AtomicBoolean();
|
||||||
pfac.combine(
|
promises.combine(
|
||||||
true,
|
|
||||||
List.of(
|
List.of(
|
||||||
pfac.start().thenRunDelayedAsync(() -> finished4.set(true), 50, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> finished4.set(true), 50, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> finished4.set(true), 50, TimeUnit.MILLISECONDS),
|
promises.start().thenRunDelayedAsync(() -> finished4.set(true), 50, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenRunDelayedAsync(() -> finished4.set(true), 50, TimeUnit.MILLISECONDS)
|
promises.start().thenRunDelayedAsync(() -> finished4.set(true), 50, TimeUnit.MILLISECONDS)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.cancel();
|
.cancel();
|
||||||
|
|
||||||
var finished5 = new AtomicBoolean();
|
var finished5 = new AtomicBoolean();
|
||||||
pfac.combine(
|
promises.combine(
|
||||||
true,
|
|
||||||
Map.of(
|
Map.of(
|
||||||
"a", pfac.start().thenRunDelayedAsync(() -> finished5.set(true), 50, TimeUnit.MILLISECONDS),
|
"a", promises.start().thenRunDelayedAsync(() -> finished5.set(true), 50, TimeUnit.MILLISECONDS),
|
||||||
"b", pfac.start().thenRunDelayedAsync(() -> finished5.set(true), 50, TimeUnit.MILLISECONDS)
|
"b", promises.start().thenRunDelayedAsync(() -> finished5.set(true), 50, TimeUnit.MILLISECONDS)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.cancel();
|
.cancel();
|
||||||
@@ -151,13 +156,13 @@ public final class PromiseTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRace() throws TimeoutException {
|
public void testRace() {
|
||||||
assert pfac.race(
|
assert promises.race(
|
||||||
List.of(
|
List.of(
|
||||||
pfac.start().thenSupplyDelayedAsync(() -> true, 150, TimeUnit.MILLISECONDS),
|
promises.start().thenSupplyDelayedAsync(() -> true, 150, TimeUnit.MILLISECONDS),
|
||||||
pfac.start().thenSupplyDelayedAsync(() -> false, 200, TimeUnit.MILLISECONDS)
|
promises.start().thenSupplyDelayedAsync(() -> false, 200, TimeUnit.MILLISECONDS)
|
||||||
)
|
)
|
||||||
).join(300L);
|
).await();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
3
futur-lazy/build.gradle
Normal file
3
futur-lazy/build.gradle
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
dependencies {
|
||||||
|
api project(':futur-api')
|
||||||
|
}
|
||||||
208
futur-lazy/src/main/java/dev/tommyjs/futur/lazy/Promises.java
Normal file
208
futur-lazy/src/main/java/dev/tommyjs/futur/lazy/Promises.java
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
package dev.tommyjs.futur.lazy;
|
||||||
|
|
||||||
|
import dev.tommyjs.futur.executor.PromiseExecutor;
|
||||||
|
import dev.tommyjs.futur.promise.CompletablePromise;
|
||||||
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseCompletion;
|
||||||
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public final class Promises {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(Promises.class);
|
||||||
|
private static PromiseFactory factory = PromiseFactory.of(LOGGER, PromiseExecutor.virtualThreaded());
|
||||||
|
|
||||||
|
public static void useFactory(@NotNull PromiseFactory factory) {
|
||||||
|
Promises.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> @NotNull CompletablePromise<T> unresolved() {
|
||||||
|
return factory.unresolved();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2, boolean cancelOnError) {
|
||||||
|
return factory.combine(p1, p2, cancelOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
||||||
|
return factory.combine(p1, p2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K, V> @NotNull Promise<Map<K, V>> combine(
|
||||||
|
@NotNull Map<K, Promise<V>> promises,
|
||||||
|
@Nullable BiConsumer<K, Throwable> exceptionHandler,
|
||||||
|
boolean propagateCancel
|
||||||
|
) {
|
||||||
|
return factory.combine(promises, exceptionHandler, propagateCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, @NotNull BiConsumer<K, Throwable> exceptionHandler) {
|
||||||
|
return factory.combine(promises, exceptionHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, boolean cancelOnError) {
|
||||||
|
return factory.combine(promises, cancelOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises) {
|
||||||
|
return factory.combine(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<List<V>> combine(
|
||||||
|
@NotNull Iterator<Promise<V>> promises, int expectedSize,
|
||||||
|
@Nullable BiConsumer<Integer, Throwable> exceptionHandler, boolean propagateCancel
|
||||||
|
) {
|
||||||
|
return factory.combine(promises, expectedSize, exceptionHandler, propagateCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<List<V>> combine(
|
||||||
|
@NotNull Collection<Promise<V>> promises,
|
||||||
|
@NotNull BiConsumer<Integer, Throwable> exceptionHandler,
|
||||||
|
boolean propagateCancel
|
||||||
|
) {
|
||||||
|
return factory.combine(promises, exceptionHandler, propagateCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<List<V>> combine(
|
||||||
|
@NotNull Collection<Promise<V>> promises,
|
||||||
|
@NotNull BiConsumer<Integer, Throwable> exceptionHandler
|
||||||
|
) {
|
||||||
|
return factory.combine(promises, exceptionHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<List<V>> combine(@NotNull Collection<Promise<V>> promises, boolean cancelOnError) {
|
||||||
|
return factory.combine(promises, cancelOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<List<V>> combine(@NotNull Collection<Promise<V>> promises) {
|
||||||
|
return factory.combine(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<List<V>> combine(
|
||||||
|
@NotNull Stream<Promise<V>> promises,
|
||||||
|
@NotNull BiConsumer<Integer, Throwable> exceptionHandler,
|
||||||
|
boolean propagateCancel
|
||||||
|
) {
|
||||||
|
return factory.combine(promises, exceptionHandler, propagateCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<List<V>> combine(
|
||||||
|
@NotNull Stream<Promise<V>> promises,
|
||||||
|
@NotNull BiConsumer<Integer, Throwable> exceptionHandler
|
||||||
|
) {
|
||||||
|
return factory.combine(promises, exceptionHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<List<V>> combine(@NotNull Stream<Promise<V>> promises, boolean cancelOnError) {
|
||||||
|
return factory.combine(promises, cancelOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<List<V>> combine(@NotNull Stream<Promise<V>> promises) {
|
||||||
|
return factory.combine(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(
|
||||||
|
@NotNull Iterator<Promise<?>> promises, int estimatedSize, boolean propagateCancel) {
|
||||||
|
return factory.allSettled(promises, estimatedSize, propagateCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Collection<Promise<?>> promises, boolean propagateCancel) {
|
||||||
|
return factory.allSettled(promises, propagateCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Collection<Promise<?>> promises) {
|
||||||
|
return factory.allSettled(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Stream<Promise<?>> promises, boolean propagateCancel) {
|
||||||
|
return factory.allSettled(promises, propagateCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Stream<Promise<?>> promises) {
|
||||||
|
return factory.allSettled(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(boolean propagateCancel, @NotNull Promise<?>... promises) {
|
||||||
|
return factory.allSettled(propagateCancel, promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Promise<?>... promises) {
|
||||||
|
return factory.allSettled(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<Void> all(@NotNull Iterator<Promise<?>> promises, boolean cancelAllOnError) {
|
||||||
|
return factory.all(promises, cancelAllOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<Void> all(@NotNull Iterable<Promise<?>> promises, boolean cancelAllOnError) {
|
||||||
|
return factory.all(promises, cancelAllOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<Void> all(@NotNull Iterable<Promise<?>> promises) {
|
||||||
|
return factory.all(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<Void> all(@NotNull Stream<Promise<?>> promises, boolean cancelAllOnError) {
|
||||||
|
return factory.all(promises, cancelAllOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<Void> all(@NotNull Stream<Promise<?>> promises) {
|
||||||
|
return factory.all(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<Void> all(boolean cancelAllOnError, @NotNull Promise<?>... promises) {
|
||||||
|
return factory.all(cancelAllOnError, promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<Void> all(@NotNull Promise<?>... promises) {
|
||||||
|
return factory.all(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<V> race(@NotNull Iterator<Promise<V>> promises, boolean cancelLosers) {
|
||||||
|
return factory.race(promises, cancelLosers);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<V> race(@NotNull Iterable<Promise<V>> promises, boolean cancelLosers) {
|
||||||
|
return factory.race(promises, cancelLosers);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<V> race(@NotNull Iterable<Promise<V>> promises) {
|
||||||
|
return factory.race(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<V> race(@NotNull Stream<Promise<V>> promises, boolean cancelLosers) {
|
||||||
|
return factory.race(promises, cancelLosers);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> @NotNull Promise<V> race(@NotNull Stream<Promise<V>> promises) {
|
||||||
|
return factory.race(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future) {
|
||||||
|
return factory.wrap(future);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull Promise<Void> start() {
|
||||||
|
return factory.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> @NotNull Promise<T> resolve(T value) {
|
||||||
|
return factory.resolve(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> @NotNull Promise<T> error(@NotNull Throwable error) {
|
||||||
|
return factory.error(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
apply plugin: 'java-library'
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
api project(':futur-api')
|
|
||||||
testImplementation project(':futur-api')
|
|
||||||
}
|
|
||||||
@@ -1,127 +0,0 @@
|
|||||||
package dev.tommyjs.futur.lazy;
|
|
||||||
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
|
||||||
import dev.tommyjs.futur.promise.PromiseCompletion;
|
|
||||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.function.BiConsumer;
|
|
||||||
|
|
||||||
public final class PromiseUtil {
|
|
||||||
|
|
||||||
private static PromiseFactory pfac = StaticPromiseFactory.INSTANCE;
|
|
||||||
|
|
||||||
public static @NotNull Logger getLogger() {
|
|
||||||
return pfac.getLogger();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setPromiseFactory(PromiseFactory pfac) {
|
|
||||||
PromiseUtil.pfac = pfac;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <T> Promise<T> unresolved() {
|
|
||||||
return pfac.unresolved();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <K, V> Promise<Map.Entry<K, V>> combine(boolean propagateCancel, @NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
|
||||||
return pfac.combine(propagateCancel, p1, p2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <K, V> Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
|
||||||
return pfac.combine(p1, p2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <K, V> Promise<Map<K, V>> combine(boolean propagateCancel, @NotNull Map<K, Promise<V>> promises, @Nullable BiConsumer<K, Throwable> exceptionHandler) {
|
|
||||||
return pfac.combine(propagateCancel, promises, exceptionHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <K, V> Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, @Nullable BiConsumer<K, Throwable> exceptionHandler) {
|
|
||||||
return pfac.combine(promises, exceptionHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <K, V> Promise<Map<K, V>> combine(boolean propagateCancel, @NotNull Map<K, Promise<V>> promises) {
|
|
||||||
return pfac.combine(propagateCancel, promises);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <K, V> Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises) {
|
|
||||||
return pfac.combine(promises);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <V> Promise<List<V>> combine(boolean propagateCancel, @NotNull Iterable<Promise<V>> promises, @Nullable BiConsumer<Integer, Throwable> exceptionHandler) {
|
|
||||||
return pfac.combine(propagateCancel, promises, exceptionHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <V> Promise<List<V>> combine(@NotNull Iterable<Promise<V>> promises, @Nullable BiConsumer<Integer, Throwable> exceptionHandler) {
|
|
||||||
return pfac.combine(promises, exceptionHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <V> Promise<List<V>> combine(boolean propagateCancel, @NotNull Iterable<Promise<V>> promises) {
|
|
||||||
return pfac.combine(propagateCancel, promises);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <V> Promise<List<V>> combine(@NotNull Iterable<Promise<V>> promises) {
|
|
||||||
return pfac.combine(promises);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(boolean propagateCancel, @NotNull Iterable<Promise<?>> promiseIterable) {
|
|
||||||
return pfac.allSettled(propagateCancel, promiseIterable);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Iterable<Promise<?>> promiseIterable) {
|
|
||||||
return pfac.allSettled(promiseIterable);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(boolean propagateCancel, @NotNull Promise<?>... promiseArray) {
|
|
||||||
return pfac.allSettled(propagateCancel, promiseArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Promise<?>... promiseArray) {
|
|
||||||
return pfac.allSettled(promiseArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> all(boolean propagateCancel, @NotNull Iterable<Promise<?>> promiseIterable) {
|
|
||||||
return pfac.all(propagateCancel, promiseIterable);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> all(@NotNull Iterable<Promise<?>> promiseIterable) {
|
|
||||||
return pfac.all(promiseIterable);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> all(boolean propagateCancel, @NotNull Promise<?>... promiseArray) {
|
|
||||||
return pfac.all(propagateCancel, promiseArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> all(@NotNull Promise<?>... promiseArray) {
|
|
||||||
return pfac.all(promiseArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> @NotNull Promise<V> race(@NotNull Iterable<Promise<V>> promises) {
|
|
||||||
return pfac.race(promises);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> @NotNull Promise<V> race(boolean cancelRaceLosers, @NotNull Iterable<Promise<V>> promises) {
|
|
||||||
return pfac.race(cancelRaceLosers, promises);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <T> Promise<T> wrap(@NotNull CompletableFuture<T> future) {
|
|
||||||
return pfac.wrap(future);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <T> Promise<T> resolve(T value) {
|
|
||||||
return pfac.resolve(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> start() {
|
|
||||||
return pfac.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull <T> Promise<T> error(@NotNull Throwable error) {
|
|
||||||
return pfac.error(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package dev.tommyjs.futur.lazy;
|
|
||||||
|
|
||||||
import dev.tommyjs.futur.executor.PromiseExecutor;
|
|
||||||
import dev.tommyjs.futur.executor.SinglePoolExecutor;
|
|
||||||
import dev.tommyjs.futur.impl.SimplePromise;
|
|
||||||
import dev.tommyjs.futur.promise.AbstractPromiseFactory;
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
|
|
||||||
public final class StaticPromiseFactory extends AbstractPromiseFactory<Future<?>> {
|
|
||||||
|
|
||||||
public final static StaticPromiseFactory INSTANCE = new StaticPromiseFactory();
|
|
||||||
private final static @NotNull SinglePoolExecutor EXECUTOR = SinglePoolExecutor.create(1);
|
|
||||||
private final static @NotNull Logger LOGGER = LoggerFactory.getLogger(StaticPromiseFactory.class);
|
|
||||||
|
|
||||||
private StaticPromiseFactory() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Logger getLogger() {
|
|
||||||
return LOGGER;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull <T> Promise<T> unresolved() {
|
|
||||||
return new SimplePromise<>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull PromiseExecutor<Future<?>> getExecutor() {
|
|
||||||
return EXECUTOR;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
3
gradle/wrapper/gradle-wrapper.properties
vendored
3
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,7 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
|
||||||
networkTimeout=10000
|
networkTimeout=10000
|
||||||
|
validateDistributionUrl=true
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|||||||
33
gradlew
vendored
33
gradlew
vendored
@@ -15,6 +15,8 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
#
|
#
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
#
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
#
|
#
|
||||||
@@ -55,7 +57,7 @@
|
|||||||
# Darwin, MinGW, and NonStop.
|
# Darwin, MinGW, and NonStop.
|
||||||
#
|
#
|
||||||
# (3) This script is generated from the Groovy template
|
# (3) This script is generated from the Groovy template
|
||||||
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
# within the Gradle project.
|
# within the Gradle project.
|
||||||
#
|
#
|
||||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
@@ -83,10 +85,8 @@ done
|
|||||||
# This is normally unused
|
# This is normally unused
|
||||||
# shellcheck disable=SC2034
|
# shellcheck disable=SC2034
|
||||||
APP_BASE_NAME=${0##*/}
|
APP_BASE_NAME=${0##*/}
|
||||||
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||||
|
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
|
||||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
|
||||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
|
||||||
|
|
||||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
MAX_FD=maximum
|
MAX_FD=maximum
|
||||||
@@ -133,10 +133,13 @@ location of your Java installation."
|
|||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
JAVACMD=java
|
JAVACMD=java
|
||||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
if ! command -v java >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
Please set the JAVA_HOME variable in your environment to match the
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
location of your Java installation."
|
location of your Java installation."
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Increase the maximum file descriptors if we can.
|
# Increase the maximum file descriptors if we can.
|
||||||
@@ -144,7 +147,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
|||||||
case $MAX_FD in #(
|
case $MAX_FD in #(
|
||||||
max*)
|
max*)
|
||||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||||
# shellcheck disable=SC3045
|
# shellcheck disable=SC2039,SC3045
|
||||||
MAX_FD=$( ulimit -H -n ) ||
|
MAX_FD=$( ulimit -H -n ) ||
|
||||||
warn "Could not query maximum file descriptor limit"
|
warn "Could not query maximum file descriptor limit"
|
||||||
esac
|
esac
|
||||||
@@ -152,7 +155,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
|||||||
'' | soft) :;; #(
|
'' | soft) :;; #(
|
||||||
*)
|
*)
|
||||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||||
# shellcheck disable=SC3045
|
# shellcheck disable=SC2039,SC3045
|
||||||
ulimit -n "$MAX_FD" ||
|
ulimit -n "$MAX_FD" ||
|
||||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||||
esac
|
esac
|
||||||
@@ -197,11 +200,15 @@ if "$cygwin" || "$msys" ; then
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Collect all arguments for the java command;
|
|
||||||
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
# shell script including quotes and variable substitutions, so put them in
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
# double quotes to make sure that they get re-expanded; and
|
|
||||||
# * put everything else in single quotes, so that it's not re-expanded.
|
# Collect all arguments for the java command:
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||||
|
# and any embedded shellness will be escaped.
|
||||||
|
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||||
|
# treated as '${Hostname}' itself on the command line.
|
||||||
|
|
||||||
set -- \
|
set -- \
|
||||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
|
|||||||
22
gradlew.bat
vendored
22
gradlew.bat
vendored
@@ -13,6 +13,8 @@
|
|||||||
@rem See the License for the specific language governing permissions and
|
@rem See the License for the specific language governing permissions and
|
||||||
@rem limitations under the License.
|
@rem limitations under the License.
|
||||||
@rem
|
@rem
|
||||||
|
@rem SPDX-License-Identifier: Apache-2.0
|
||||||
|
@rem
|
||||||
|
|
||||||
@if "%DEBUG%"=="" @echo off
|
@if "%DEBUG%"=="" @echo off
|
||||||
@rem ##########################################################################
|
@rem ##########################################################################
|
||||||
@@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
|
|||||||
%JAVA_EXE% -version >NUL 2>&1
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
if %ERRORLEVEL% equ 0 goto execute
|
if %ERRORLEVEL% equ 0 goto execute
|
||||||
|
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
echo location of your Java installation.
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
goto fail
|
goto fail
|
||||||
|
|
||||||
@@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
|||||||
|
|
||||||
if exist "%JAVA_EXE%" goto execute
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
echo location of your Java installation.
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
goto fail
|
goto fail
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
rootProject.name = 'futur'
|
rootProject.name = 'futur'
|
||||||
|
|
||||||
include 'futur-api'
|
include 'futur-api'
|
||||||
include 'futur-static'
|
include 'futur-lazy'
|
||||||
|
|||||||
Reference in New Issue
Block a user