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 {
|
||||
id 'java'
|
||||
id 'java-library'
|
||||
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 {
|
||||
@@ -14,9 +14,9 @@ nexusPublishing {
|
||||
|
||||
subprojects {
|
||||
group = 'dev.tommyjs'
|
||||
version = '2.3.4'
|
||||
version = '2.4'
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'java-library'
|
||||
apply plugin: 'com.github.johnrengelman.shadow'
|
||||
|
||||
tasks {
|
||||
@@ -30,8 +30,9 @@ subprojects {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.jetbrains:annotations:24.1.0'
|
||||
compileOnly 'org.jetbrains:annotations:24.1.0'
|
||||
implementation 'org.slf4j:slf4j-api:2.0.12'
|
||||
2
|
||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||
testImplementation 'io.projectreactor:reactor-core:3.6.4'
|
||||
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 java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public interface PromiseExecutor<T> {
|
||||
|
||||
T runSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
T runAsync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
default T runSync(@NotNull Runnable task) {
|
||||
return runSync(task, 0L, TimeUnit.MILLISECONDS);
|
||||
static PromiseExecutor<?> virtualThreaded() {
|
||||
return new VirtualThreadImpl();
|
||||
}
|
||||
|
||||
default T runAsync(@NotNull Runnable task) {
|
||||
return runAsync(task, 0L, TimeUnit.MILLISECONDS);
|
||||
static PromiseExecutor<?> singleThreaded() {
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
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
|
||||
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
|
||||
public interface ExceptionalRunnable {
|
||||
|
||||
void run() throws Throwable;
|
||||
void run() throws Exception;
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@ package dev.tommyjs.futur.function;
|
||||
@FunctionalInterface
|
||||
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;
|
||||
|
||||
import dev.tommyjs.futur.executor.PromiseExecutor;
|
||||
import dev.tommyjs.futur.function.ExceptionalConsumer;
|
||||
import dev.tommyjs.futur.function.ExceptionalFunction;
|
||||
import dev.tommyjs.futur.function.ExceptionalRunnable;
|
||||
@@ -10,102 +9,110 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
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;
|
||||
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) {
|
||||
public static <V> void propagateResult(Promise<V> from, CompletablePromise<V> to) {
|
||||
from.addDirectListener(to::complete, to::completeExceptionally);
|
||||
}
|
||||
|
||||
protected static void propagateCancel(Promise<?> from, Promise<?> to) {
|
||||
from.onCancel(to::completeExceptionally);
|
||||
public static void propagateCancel(Promise<?> from, Promise<?> to) {
|
||||
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) {
|
||||
toFinish.addDirectListener(_ -> toCancel.cancel());
|
||||
}
|
||||
|
||||
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 {
|
||||
completer.run();
|
||||
} catch (Error e) {
|
||||
promise.completeExceptionally(e);
|
||||
throw e;
|
||||
} catch (Throwable 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;
|
||||
|
||||
try {
|
||||
V nextResult = task.apply(result);
|
||||
promise.complete(nextResult);
|
||||
} catch (Throwable e) {
|
||||
promise.completeExceptionally(e);
|
||||
}
|
||||
runCompleter(promise, () -> promise.complete(completer.apply(result)));
|
||||
};
|
||||
}
|
||||
|
||||
public abstract @NotNull AbstractPromiseFactory<F> getFactory();
|
||||
|
||||
protected @NotNull PromiseExecutor<F> getExecutor() {
|
||||
return getFactory().getExecutor();
|
||||
}
|
||||
public abstract @NotNull AbstractPromiseFactory<FS, FA> getFactory();
|
||||
|
||||
protected @NotNull Logger getLogger() {
|
||||
return getFactory().getLogger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T awaitInterruptibly() throws InterruptedException {
|
||||
public T get() throws InterruptedException, ExecutionException {
|
||||
this.latch.await();
|
||||
return joinCompletion(Objects.requireNonNull(getCompletion()));
|
||||
return joinCompletion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T awaitInterruptibly(long timeoutMillis) throws TimeoutException, InterruptedException {
|
||||
boolean success = this.latch.await(timeoutMillis, TimeUnit.MILLISECONDS);
|
||||
public T get(long time, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||
boolean success = this.latch.await(time, unit);
|
||||
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
|
||||
public T await() {
|
||||
try {
|
||||
return awaitInterruptibly();
|
||||
this.latch.await();
|
||||
} catch (InterruptedException 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
|
||||
public T await(long timeoutMillis) throws TimeoutException {
|
||||
try {
|
||||
return awaitInterruptibly(timeoutMillis);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private T joinCompletion(PromiseCompletion<T> completion) {
|
||||
if (completion.isError())
|
||||
throw new RuntimeException(completion.getException());
|
||||
|
||||
return completion.getResult();
|
||||
public @NotNull Promise<T> fork() {
|
||||
CompletablePromise<T> fork = getFactory().unresolved();
|
||||
propagateResult(this, fork);
|
||||
return fork;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenRun(@NotNull ExceptionalRunnable task) {
|
||||
return thenApply(result -> {
|
||||
return thenApply(_ -> {
|
||||
task.run();
|
||||
return null;
|
||||
});
|
||||
@@ -121,14 +128,14 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenSupply(@NotNull ExceptionalSupplier<V> task) {
|
||||
return thenApply(result -> task.get());
|
||||
return thenApply(_ -> task.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenApply(@NotNull ExceptionalFunction<T, V> task) {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
CompletablePromise<V> promise = getFactory().unresolved();
|
||||
addDirectListener(
|
||||
res -> createRunnable(res, promise, task).run(),
|
||||
res -> createCompleter(res, promise, task).run(),
|
||||
promise::completeExceptionally
|
||||
);
|
||||
|
||||
@@ -138,7 +145,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
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(
|
||||
nestedPromise -> {
|
||||
if (nestedPromise == null) {
|
||||
@@ -157,7 +164,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task) {
|
||||
return thenApplySync(result -> {
|
||||
return thenApplySync(_ -> {
|
||||
task.run();
|
||||
return null;
|
||||
});
|
||||
@@ -165,7 +172,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedSync(result -> {
|
||||
return thenApplyDelayedSync(_ -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, delay, unit);
|
||||
@@ -189,27 +196,23 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task) {
|
||||
return thenApplySync(result -> task.get());
|
||||
return thenApplySync(_ -> task.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
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
|
||||
public <V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task) {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
CompletablePromise<V> promise = getFactory().unresolved();
|
||||
addDirectListener(
|
||||
res -> {
|
||||
try {
|
||||
Runnable runnable = createRunnable(res, promise, task);
|
||||
F future = getExecutor().runSync(runnable);
|
||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
promise.completeExceptionally(e);
|
||||
}
|
||||
},
|
||||
res -> runCompleter(promise, () -> {
|
||||
Runnable runnable = createCompleter(res, promise, task);
|
||||
FS future = getFactory().getSyncExecutor().run(runnable);
|
||||
promise.addDirectListener(_ -> getFactory().getSyncExecutor().cancel(future));
|
||||
}),
|
||||
promise::completeExceptionally
|
||||
);
|
||||
|
||||
@@ -219,17 +222,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
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(
|
||||
res -> {
|
||||
try {
|
||||
Runnable runnable = createRunnable(res, promise, task);
|
||||
F future = getExecutor().runSync(runnable, delay, unit);
|
||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
promise.completeExceptionally(e);
|
||||
}
|
||||
},
|
||||
res -> runCompleter(promise, () -> {
|
||||
Runnable runnable = createCompleter(res, promise, task);
|
||||
FS future = getFactory().getSyncExecutor().run(runnable, delay, unit);
|
||||
promise.addDirectListener(_ -> getFactory().getSyncExecutor().cancel(future));
|
||||
}),
|
||||
promise::completeExceptionally
|
||||
);
|
||||
|
||||
@@ -239,7 +238,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
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(
|
||||
nestedPromise -> {
|
||||
if (nestedPromise == null) {
|
||||
@@ -258,7 +257,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task) {
|
||||
return thenApplyAsync(result -> {
|
||||
return thenApplyAsync(_ -> {
|
||||
task.run();
|
||||
return null;
|
||||
});
|
||||
@@ -266,7 +265,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenRunDelayedAsync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedAsync(result -> {
|
||||
return thenApplyDelayedAsync(_ -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, delay, unit);
|
||||
@@ -290,17 +289,17 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task) {
|
||||
return thenApplyAsync(result -> task.get());
|
||||
return thenApplyAsync(_ -> task.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
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
|
||||
public @NotNull Promise<T> thenPopulateReference(@NotNull AtomicReference<T> reference) {
|
||||
return thenApplyAsync((result) -> {
|
||||
return thenApplyAsync(result -> {
|
||||
reference.set(result);
|
||||
return result;
|
||||
});
|
||||
@@ -308,17 +307,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task) {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
CompletablePromise<V> promise = getFactory().unresolved();
|
||||
addDirectListener(
|
||||
(res) -> {
|
||||
try {
|
||||
Runnable runnable = createRunnable(res, promise, task);
|
||||
F future = getExecutor().runAsync(runnable);
|
||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
promise.completeExceptionally(e);
|
||||
}
|
||||
},
|
||||
(res) -> runCompleter(promise, () -> {
|
||||
Runnable runnable = createCompleter(res, promise, task);
|
||||
FA future = getFactory().getAsyncExecutor().run(runnable);
|
||||
promise.addDirectListener(_ -> getFactory().getAsyncExecutor().cancel(future));
|
||||
}),
|
||||
promise::completeExceptionally
|
||||
);
|
||||
|
||||
@@ -328,17 +323,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
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(
|
||||
res -> {
|
||||
try {
|
||||
Runnable runnable = createRunnable(res, promise, task);
|
||||
F future = getExecutor().runAsync(runnable, delay, unit);
|
||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
promise.completeExceptionally(e);
|
||||
}
|
||||
},
|
||||
res -> runCompleter(promise, () -> {
|
||||
Runnable runnable = createCompleter(res, promise, task);
|
||||
FA future = getFactory().getAsyncExecutor().run(runnable, delay, unit);
|
||||
promise.addDirectListener(_ -> getFactory().getAsyncExecutor().cancel(future));
|
||||
}),
|
||||
promise::completeExceptionally
|
||||
);
|
||||
|
||||
@@ -348,7 +339,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
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(
|
||||
nestedPromise -> {
|
||||
if (nestedPromise == null) {
|
||||
@@ -367,7 +358,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> erase() {
|
||||
return thenSupplyAsync(() -> null);
|
||||
return thenSupply(() -> null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -378,10 +369,10 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
@Override
|
||||
public @NotNull Promise<T> addAsyncListener(@Nullable Consumer<T> successListener, @Nullable Consumer<Throwable> errorListener) {
|
||||
return addAsyncListener((res) -> {
|
||||
if (res.isError()) {
|
||||
if (errorListener != null) errorListener.accept(res.getException());
|
||||
} else {
|
||||
if (res.isSuccess()) {
|
||||
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
|
||||
public @NotNull Promise<T> addDirectListener(@Nullable Consumer<T> successListener, @Nullable Consumer<Throwable> errorListener) {
|
||||
return addDirectListener((res) -> {
|
||||
if (res.isError()) {
|
||||
if (errorListener != null) errorListener.accept(res.getException());
|
||||
} else {
|
||||
if (res.isSuccess()) {
|
||||
if (successListener != null) successListener.accept(res.getResult());
|
||||
} else {
|
||||
if (errorListener != null) errorListener.accept(res.getException());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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();
|
||||
try {
|
||||
completion = getCompletion();
|
||||
if (completion == null) {
|
||||
if (listeners == null) listeners = new LinkedList<>();
|
||||
listeners.add(listener);
|
||||
return this;
|
||||
if (res == null) {
|
||||
if (listener instanceof AsyncPromiseListener) {
|
||||
callListenerAsync(listener, Objects.requireNonNull(getCompletion()));
|
||||
} else {
|
||||
callListenerNow(listener, Objects.requireNonNull(getCompletion()));
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
callListener(listener, completion);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void callListener(PromiseListener<T> listener, PromiseCompletion<T> ctx) {
|
||||
if (listener instanceof AsyncPromiseListener) {
|
||||
try {
|
||||
getExecutor().runAsync(() -> callListenerNow(listener, ctx));
|
||||
} catch (RejectedExecutionException ignored) {
|
||||
|
||||
}
|
||||
} else {
|
||||
callListenerNow(listener, ctx);
|
||||
private void callListenerAsync(PromiseListener<T> listener, PromiseCompletion<T> res) {
|
||||
try {
|
||||
getFactory().getAsyncExecutor().run(() -> callListenerNow(listener, res));
|
||||
} catch (Exception e) {
|
||||
getLogger().warn("Exception caught while running promise listener", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void callListenerNow(PromiseListener<T> listener, PromiseCompletion<T> ctx) {
|
||||
private void callListenerNow(PromiseListener<T> listener, PromiseCompletion<T> res) {
|
||||
try {
|
||||
listener.handle(ctx);
|
||||
} catch (Exception e) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -453,13 +442,15 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
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
|
||||
public <E extends Throwable> @NotNull Promise<T> onError(@NotNull Class<E> clazz, @NotNull Consumer<E> listener) {
|
||||
return onError((e) -> {
|
||||
if (clazz.isAssignableFrom(e.getClass())) {
|
||||
getLogger().info("On Error {}", e.getClass());
|
||||
//noinspection unchecked
|
||||
listener.accept((E) e);
|
||||
}
|
||||
@@ -471,37 +462,51 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
return onError(CancellationException.class, listener);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
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
|
||||
public @NotNull Promise<T> maxWaitTime(long time, @NotNull TimeUnit unit) {
|
||||
try {
|
||||
Exception e = new TimeoutException("Promise stopped waiting after " + time + " " + unit);
|
||||
F future = getExecutor().runAsync(() -> completeExceptionally(e), time, unit);
|
||||
return addDirectListener((_v) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
completeExceptionally(e);
|
||||
return this;
|
||||
}
|
||||
Exception e = new TimeoutException("Promise stopped waiting after " + time + " " + unit);
|
||||
return completeExceptionallyDelayed(e, time, unit);
|
||||
}
|
||||
|
||||
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) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (!setCompletion(ctx)) return;
|
||||
if (!setCompletion(ctx)) return;
|
||||
latch.countDown();
|
||||
|
||||
this.latch.countDown();
|
||||
if (listeners != null) {
|
||||
for (PromiseListener<T> listener : listeners) {
|
||||
callListener(listener, ctx);
|
||||
Iterator<PromiseListener<T>> iter = listeners.getAndSet(null).iterator();
|
||||
while (iter.hasNext()) {
|
||||
PromiseListener<T> listener = iter.next();
|
||||
|
||||
if (listener instanceof AsyncPromiseListener) {
|
||||
callListenerAsync(listener, ctx);
|
||||
} else {
|
||||
try {
|
||||
callListenerNow(listener, ctx);
|
||||
} finally {
|
||||
iter.forEachRemaining(v -> callListenerAsyncLastResort(v, ctx));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
public void cancel(@Nullable String message) {
|
||||
completeExceptionally(new CancellationException(message));
|
||||
public void cancel(@NotNull CancellationException e) {
|
||||
completeExceptionally(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -538,7 +543,7 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
public @NotNull CompletableFuture<T> toFuture() {
|
||||
CompletableFuture<T> future = new CompletableFuture<>();
|
||||
this.addDirectListener(future::complete, future::completeExceptionally);
|
||||
future.whenComplete((res, e) -> {
|
||||
future.whenComplete((_, e) -> {
|
||||
if (e instanceof CancellationException) {
|
||||
this.cancel();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package dev.tommyjs.futur.promise;
|
||||
|
||||
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.Nullable;
|
||||
|
||||
@@ -8,142 +12,84 @@ import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
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
|
||||
public <K, V> @NotNull Promise<Map.Entry<K, V>> combine(boolean propagateCancel, @NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
||||
List<Promise<?>> promises = List.of(p1, p2);
|
||||
return all(propagateCancel, promises)
|
||||
.thenApplyAsync((res) -> new AbstractMap.SimpleImmutableEntry<>(
|
||||
Objects.requireNonNull(p1.getCompletion()).getResult(),
|
||||
Objects.requireNonNull(p2.getCompletion()).getResult()
|
||||
));
|
||||
public <K, V> @NotNull Promise<Map.Entry<K, V>> combine(
|
||||
@NotNull Promise<K> p1,
|
||||
@NotNull Promise<V> p2,
|
||||
boolean dontFork
|
||||
) {
|
||||
return all(dontFork, p1, p2).thenApply((_) -> new AbstractMap.SimpleImmutableEntry<>(
|
||||
Objects.requireNonNull(p1.getCompletion()).getResult(),
|
||||
Objects.requireNonNull(p2.getCompletion()).getResult()
|
||||
));
|
||||
}
|
||||
|
||||
@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());
|
||||
return new MappedResultJoiner<>(this,
|
||||
promises.entrySet().iterator(), exceptionHandler, promises.size(), link).joined();
|
||||
}
|
||||
|
||||
Map<K, V> map = new HashMap<>();
|
||||
Promise<Map<K, V>> promise = unresolved();
|
||||
for (Map.Entry<K, Promise<V>> entry : promises.entrySet()) {
|
||||
if (propagateCancel) {
|
||||
AbstractPromise.propagateCancel(promise, entry.getValue());
|
||||
}
|
||||
@Override
|
||||
public <V> @NotNull Promise<List<V>> combine(
|
||||
@NotNull Iterator<Promise<V>> promises, int expectedSize,
|
||||
@Nullable BiConsumer<Integer, Throwable> exceptionHandler, boolean link
|
||||
) {
|
||||
if (!promises.hasNext()) return resolve(Collections.emptyList());
|
||||
return new ResultJoiner<>(
|
||||
this, promises, exceptionHandler, expectedSize, link).joined();
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
@Override
|
||||
public @NotNull Promise<List<PromiseCompletion<?>>> allSettled(
|
||||
@NotNull Iterator<Promise<?>> promises,
|
||||
int expectedSize,
|
||||
boolean link
|
||||
) {
|
||||
if (!promises.hasNext()) return resolve(Collections.emptyList());
|
||||
return new CompletionJoiner(this, promises, expectedSize, link).joined();
|
||||
}
|
||||
|
||||
if (map.size() == promises.size()) {
|
||||
promise.complete(map);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public @NotNull Promise<Void> all(@NotNull Iterator<Promise<?>> promises, boolean link) {
|
||||
if (!promises.hasNext()) return resolve(null);
|
||||
return new VoidJoiner(this, promises, link).joined();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> race(@NotNull Iterator<Promise<V>> promises, boolean link) {
|
||||
CompletablePromise<V> promise = unresolved();
|
||||
promises.forEachRemaining(p -> {
|
||||
if (link) AbstractPromise.cancelOnFinish(p, promise);
|
||||
if (!promise.isCompleted())
|
||||
AbstractPromise.propagateResult(p, promise);
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<List<V>> combine(boolean propagateCancel, @NotNull Iterable<Promise<V>> promises, @Nullable BiConsumer<Integer, Throwable> exceptionHandler) {
|
||||
AtomicInteger index = new AtomicInteger();
|
||||
return this.combine(
|
||||
propagateCancel,
|
||||
StreamSupport.stream(promises.spliterator(), false)
|
||||
.collect(Collectors.toMap(k -> index.getAndIncrement(), v -> v)),
|
||||
exceptionHandler
|
||||
).thenApplyAsync(v ->
|
||||
v.entrySet().stream()
|
||||
.sorted(Map.Entry.comparingByKey())
|
||||
.map(Map.Entry::getValue)
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
public <V> @NotNull Promise<V> race(@NotNull Iterable<Promise<V>> promises, boolean link) {
|
||||
return race(promises.iterator(), link);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<List<PromiseCompletion<?>>> allSettled(boolean propagateCancel, @NotNull Iterable<Promise<?>> promiseIterable) {
|
||||
List<Promise<?>> promises = new ArrayList<>();
|
||||
promiseIterable.iterator().forEachRemaining(promises::add);
|
||||
|
||||
if (promises.isEmpty()) return resolve(Collections.emptyList());
|
||||
PromiseCompletion<?>[] results = new PromiseCompletion<?>[promises.size()];
|
||||
|
||||
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
|
||||
public @NotNull Promise<Void> all(boolean propagateCancel, @NotNull Iterable<Promise<?>> promiseIterable) {
|
||||
List<Promise<?>> promises = new ArrayList<>();
|
||||
promiseIterable.iterator().forEachRemaining(promises::add);
|
||||
|
||||
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
|
||||
public <V> @NotNull Promise<V> race(boolean cancelRaceLosers, @NotNull Iterable<Promise<V>> promises) {
|
||||
Promise<V> promise = unresolved();
|
||||
for (Promise<V> p : promises) {
|
||||
if (cancelRaceLosers) {
|
||||
promise.addListener((res) -> p.cancel());
|
||||
}
|
||||
AbstractPromise.propagateResult(p, promise);
|
||||
}
|
||||
return promise;
|
||||
public <V> @NotNull Promise<V> race(@NotNull Stream<Promise<V>> promises, boolean link) {
|
||||
return race(promises.iterator(), link);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -152,7 +98,7 @@ public abstract class AbstractPromiseFactory<F> implements PromiseFactory {
|
||||
}
|
||||
|
||||
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) -> {
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @NotNull Promise<T> resolve(T value) {
|
||||
Promise<T> promise = unresolved();
|
||||
CompletablePromise<T> promise = unresolved();
|
||||
promise.complete(value);
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @NotNull Promise<T> error(@NotNull Throwable error) {
|
||||
Promise<T> promise = unresolved();
|
||||
CompletablePromise<T> promise = unresolved();
|
||||
promise.completeExceptionally(error);
|
||||
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.Nullable;
|
||||
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface Promise<T> {
|
||||
|
||||
PromiseFactory getFactory();
|
||||
@NotNull PromiseFactory getFactory();
|
||||
|
||||
@NotNull Promise<Void> thenRun(@NotNull ExceptionalRunnable task);
|
||||
|
||||
@@ -80,6 +77,9 @@ public interface Promise<T> {
|
||||
*/
|
||||
@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);
|
||||
|
||||
/**
|
||||
@@ -94,6 +94,9 @@ public interface Promise<T> {
|
||||
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> onSuccess(@NotNull Consumer<T> listener);
|
||||
@@ -105,55 +108,70 @@ public interface Promise<T> {
|
||||
@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);
|
||||
|
||||
/**
|
||||
* @deprecated Use maxWaitTime instead
|
||||
* Cancels the promise with a TimeoutException after the specified time.
|
||||
*/
|
||||
@Deprecated
|
||||
default @NotNull Promise<T> timeout(long ms) {
|
||||
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);
|
||||
|
||||
/**
|
||||
* Completes the promise exceptionally with a TimeoutException after the specified time.
|
||||
*/
|
||||
default @NotNull Promise<T> maxWaitTime(long ms) {
|
||||
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() {
|
||||
cancel(null);
|
||||
cancel(new CancellationException());
|
||||
}
|
||||
|
||||
void complete(@Nullable T result);
|
||||
|
||||
void completeExceptionally(@NotNull Throwable result);
|
||||
|
||||
@Blocking
|
||||
T awaitInterruptibly() throws InterruptedException;
|
||||
|
||||
@Blocking
|
||||
T awaitInterruptibly(long timeout) throws TimeoutException, InterruptedException;
|
||||
|
||||
/**
|
||||
* Waits if necessary for this promise to complete, and then returns its result.
|
||||
* @throws CancellationException if the computation was cancelled
|
||||
* @throws CompletionException if this promise completed exceptionally
|
||||
*/
|
||||
@Blocking
|
||||
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
|
||||
@Deprecated
|
||||
default T join(long timeout) throws TimeoutException {
|
||||
return await(timeout);
|
||||
};
|
||||
T get() throws InterruptedException, ExecutionException;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
|
||||
@@ -22,12 +22,16 @@ public class PromiseCompletion<T> {
|
||||
this.result = null;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return exception == null;
|
||||
}
|
||||
|
||||
public boolean isError() {
|
||||
return getException() != null;
|
||||
return exception != null;
|
||||
}
|
||||
|
||||
public boolean wasCanceled() {
|
||||
return getException() instanceof CancellationException;
|
||||
return exception instanceof CancellationException;
|
||||
}
|
||||
|
||||
public @Nullable T getResult() {
|
||||
|
||||
@@ -1,90 +1,183 @@
|
||||
package dev.tommyjs.futur.promise;
|
||||
|
||||
import dev.tommyjs.futur.executor.PromiseExecutor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
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();
|
||||
|
||||
<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) {
|
||||
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) {
|
||||
return combine(propagateCancel, promises, null);
|
||||
default <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, @NotNull BiConsumer<K, Throwable> exceptionHandler) {
|
||||
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) {
|
||||
return combine(false, promises, exceptionHandler);
|
||||
default <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, boolean cancelOnError) {
|
||||
return combine(promises, null, cancelOnError);
|
||||
}
|
||||
|
||||
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) {
|
||||
return combine(propagateCancel, promises, null);
|
||||
default <V> @NotNull Promise<List<V>> combine(
|
||||
@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) {
|
||||
return combine(false, promises, exceptionHandler);
|
||||
default <V> @NotNull Promise<List<V>> combine(
|
||||
@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) {
|
||||
return combine(promises, null);
|
||||
default <V> @NotNull Promise<List<V>> combine(@NotNull Collection<Promise<V>> promises, boolean cancelOnError) {
|
||||
return combine(promises.iterator(), promises.size(), null, cancelOnError);
|
||||
}
|
||||
|
||||
@NotNull Promise<List<PromiseCompletion<?>>> allSettled(boolean propagateCancel, @NotNull Iterable<Promise<?>> promiseIterable);
|
||||
|
||||
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Iterable<Promise<?>> promiseIterable) {
|
||||
return allSettled(false, 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(boolean propagateCancel, @NotNull Promise<?>... promiseArray) {
|
||||
return allSettled(propagateCancel, Arrays.asList(promiseArray));
|
||||
default <V> @NotNull Promise<List<V>> combine(
|
||||
@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) {
|
||||
return allSettled(false, promiseArray);
|
||||
default <V> @NotNull Promise<List<V>> combine(
|
||||
@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 @NotNull Promise<Void> all(@NotNull Iterable<Promise<?>> promiseIterable) {
|
||||
return all(false, 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(boolean propagateCancel, @NotNull Promise<?>... promiseArray) {
|
||||
return all(propagateCancel, Arrays.asList(promiseArray));
|
||||
default <V> @NotNull Promise<List<V>> combine(@NotNull Stream<Promise<V>> promises) {
|
||||
return combine(promises.iterator(), size(promises), null, true);
|
||||
}
|
||||
|
||||
default @NotNull Promise<Void> all(@NotNull Promise<?>... promiseArray) {
|
||||
return all(false, promiseArray);
|
||||
@NotNull Promise<List<PromiseCompletion<?>>> allSettled(
|
||||
@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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @apiNote Even with cancelRaceLosers, it is not guaranteed that only one promise will complete.
|
||||
*/
|
||||
<V> @NotNull Promise<V> race(boolean cancelRaceLosers, @NotNull Iterable<Promise<V>> promises);
|
||||
default @NotNull Promise<List<PromiseCompletion<?>>> allSettled(@NotNull Collection<Promise<?>> promises) {
|
||||
return allSettled(promises.iterator(), promises.size(), true);
|
||||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
import dev.tommyjs.futur.executor.SinglePoolExecutor;
|
||||
import dev.tommyjs.futur.impl.SimplePromiseFactory;
|
||||
import dev.tommyjs.futur.promise.Promise;
|
||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -17,24 +15,36 @@ public final class PromiseTests {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(PromiseTests.class);
|
||||
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
|
||||
public void testShutdown() {
|
||||
executor.shutdown();
|
||||
Promise<?> promise = pfac.resolve(null).thenSupplyAsync(() -> null);
|
||||
executor.close();
|
||||
Promise<?> promise = promises.resolve(null).thenSupplyAsync(() -> null);
|
||||
try {
|
||||
promise.await();
|
||||
} catch (RuntimeException e) {
|
||||
} catch (CompletionException e) {
|
||||
assert e.getCause() instanceof RejectedExecutionException;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorCancellation() throws InterruptedException {
|
||||
public void testCancellation() throws InterruptedException {
|
||||
var finished = new AtomicBoolean();
|
||||
pfac.start()
|
||||
.thenRunDelayedAsync(() -> finished.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
promises.start().thenRunDelayedAsync(() -> finished.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
.thenRunAsync(() -> {})
|
||||
.cancel();
|
||||
|
||||
@@ -44,11 +54,11 @@ public final class PromiseTests {
|
||||
|
||||
@Test
|
||||
public void testToFuture() throws InterruptedException {
|
||||
assert pfac.resolve(true).toFuture().getNow(false);
|
||||
assert pfac.error(new Exception("Test")).toFuture().isCompletedExceptionally();
|
||||
assert promises.resolve(true).toFuture().getNow(false);
|
||||
assert promises.error(new Exception("Test")).toFuture().isCompletedExceptionally();
|
||||
|
||||
var finished = new AtomicBoolean();
|
||||
pfac.start()
|
||||
promises.start()
|
||||
.thenRunDelayedAsync(() -> finished.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
.toFuture()
|
||||
.cancel(true);
|
||||
@@ -58,86 +68,81 @@ public final class PromiseTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombineUtil() throws TimeoutException {
|
||||
pfac.all(
|
||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||
public void testCombineUtil() throws TimeoutException, ExecutionException, InterruptedException {
|
||||
promises.all(
|
||||
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
.join(100L);
|
||||
.get(100L, TimeUnit.MILLISECONDS);
|
||||
|
||||
pfac.allSettled(
|
||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||
promises.allSettled(
|
||||
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
.join(100L);
|
||||
.get(100L, TimeUnit.MILLISECONDS);
|
||||
|
||||
pfac.combine(
|
||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||
promises.combine(
|
||||
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
.join(100L);
|
||||
.get(100L, TimeUnit.MILLISECONDS);
|
||||
|
||||
pfac.combine(
|
||||
promises.combine(
|
||||
List.of(
|
||||
pfac.start().thenRunDelayedAsync(() -> {}, 49, TimeUnit.MILLISECONDS),
|
||||
pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
pfac.start().thenRunDelayedAsync(() -> {}, 51, TimeUnit.MILLISECONDS)
|
||||
promises.start().thenRunDelayedAsync(() -> {}, 49, TimeUnit.MILLISECONDS),
|
||||
promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
promises.start().thenRunDelayedAsync(() -> {}, 51, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
)
|
||||
.join(100L);
|
||||
.get(100L, TimeUnit.MILLISECONDS);
|
||||
|
||||
pfac.combine(
|
||||
promises.combine(
|
||||
Map.of(
|
||||
"a", pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
"b", pfac.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||
"a", promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS),
|
||||
"b", promises.start().thenRunDelayedAsync(() -> {}, 50, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
)
|
||||
.join(100L);
|
||||
.get(100L, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombineUtilPropagation() throws InterruptedException {
|
||||
var finished1 = new AtomicBoolean();
|
||||
pfac.all(
|
||||
true,
|
||||
pfac.start().thenRunDelayedAsync(() -> finished1.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
pfac.start().thenRunDelayedAsync(() -> finished1.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
promises.all(
|
||||
promises.start().thenRunDelayedAsync(() -> finished1.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
promises.start().thenRunDelayedAsync(() -> finished1.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
.cancel();
|
||||
|
||||
var finished2 = new AtomicBoolean();
|
||||
pfac.allSettled(
|
||||
true,
|
||||
pfac.start().thenRunDelayedAsync(() -> finished2.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
pfac.start().thenRunDelayedAsync(() -> finished2.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
promises.allSettled(
|
||||
promises.start().thenRunDelayedAsync(() -> finished2.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
promises.start().thenRunDelayedAsync(() -> finished2.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
.cancel();
|
||||
|
||||
var finished3 = new AtomicBoolean();
|
||||
pfac.combine(
|
||||
true,
|
||||
pfac.start().thenRunDelayedAsync(() -> finished3.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
pfac.start().thenRunDelayedAsync(() -> finished3.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
promises.combine(
|
||||
promises.start().thenRunDelayedAsync(() -> finished3.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
promises.start().thenRunDelayedAsync(() -> finished3.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
.cancel();
|
||||
|
||||
var finished4 = new AtomicBoolean();
|
||||
pfac.combine(
|
||||
true,
|
||||
promises.combine(
|
||||
List.of(
|
||||
pfac.start().thenRunDelayedAsync(() -> finished4.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
pfac.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),
|
||||
promises.start().thenRunDelayedAsync(() -> finished4.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
promises.start().thenRunDelayedAsync(() -> finished4.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
)
|
||||
.cancel();
|
||||
|
||||
var finished5 = new AtomicBoolean();
|
||||
pfac.combine(
|
||||
true,
|
||||
promises.combine(
|
||||
Map.of(
|
||||
"a", pfac.start().thenRunDelayedAsync(() -> finished5.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
"b", pfac.start().thenRunDelayedAsync(() -> finished5.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
"a", promises.start().thenRunDelayedAsync(() -> finished5.set(true), 50, TimeUnit.MILLISECONDS),
|
||||
"b", promises.start().thenRunDelayedAsync(() -> finished5.set(true), 50, TimeUnit.MILLISECONDS)
|
||||
)
|
||||
)
|
||||
.cancel();
|
||||
@@ -151,13 +156,13 @@ public final class PromiseTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRace() throws TimeoutException {
|
||||
assert pfac.race(
|
||||
public void testRace() {
|
||||
assert promises.race(
|
||||
List.of(
|
||||
pfac.start().thenSupplyDelayedAsync(() -> true, 150, TimeUnit.MILLISECONDS),
|
||||
pfac.start().thenSupplyDelayedAsync(() -> false, 200, TimeUnit.MILLISECONDS)
|
||||
promises.start().thenSupplyDelayedAsync(() -> true, 150, 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
|
||||
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
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
||||
33
gradlew
vendored
33
gradlew
vendored
@@ -15,6 +15,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
@@ -55,7 +57,7 @@
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (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.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
@@ -83,10 +85,8 @@ done
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || 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"'
|
||||
# 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
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
@@ -133,10 +133,13 @@ location of your Java installation."
|
||||
fi
|
||||
else
|
||||
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
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
@@ -144,7 +147,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
# 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 ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
@@ -152,7 +155,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
# 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" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
@@ -197,11 +200,15 @@ if "$cygwin" || "$msys" ; then
|
||||
done
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command;
|
||||
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
||||
# shell script including quotes and variable substitutions, so put them in
|
||||
# double quotes to make sure that they get re-expanded; and
|
||||
# * put everything else in single quotes, so that it's not re-expanded.
|
||||
|
||||
# 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"'
|
||||
|
||||
# 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 -- \
|
||||
"-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 limitations under the License.
|
||||
@rem
|
||||
@rem SPDX-License-Identifier: Apache-2.0
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%"=="" @echo off
|
||||
@rem ##########################################################################
|
||||
@@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if %ERRORLEVEL% equ 0 goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
@@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
rootProject.name = 'futur'
|
||||
|
||||
include 'futur-api'
|
||||
include 'futur-static'
|
||||
include 'futur-lazy'
|
||||
|
||||
Reference in New Issue
Block a user