mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-17 23:16:01 +00:00
basic 1.2.0 changes
This commit is contained in:
@@ -0,0 +1,429 @@
|
||||
package dev.tommyjs.futur.promise;
|
||||
|
||||
import dev.tommyjs.futur.function.ExceptionalConsumer;
|
||||
import dev.tommyjs.futur.function.ExceptionalFunction;
|
||||
import dev.tommyjs.futur.function.ExceptionalRunnable;
|
||||
import dev.tommyjs.futur.function.ExceptionalSupplier;
|
||||
import dev.tommyjs.futur.scheduler.Scheduler;
|
||||
import dev.tommyjs.futur.trace.ExecutorTrace;
|
||||
import dev.tommyjs.futur.trace.TraceUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public abstract class AbstractPromise<T> implements Promise<T> {
|
||||
|
||||
private static final String PACKAGE;
|
||||
|
||||
static {
|
||||
String[] packageElements = AbstractPromise.class.getPackageName().split("\\.");
|
||||
int i = 0;
|
||||
|
||||
StringBuilder packageBuilder = new StringBuilder();
|
||||
while (i < 3) {
|
||||
packageBuilder.append(packageElements[i]);
|
||||
i++;
|
||||
}
|
||||
|
||||
PACKAGE = packageBuilder.toString();
|
||||
}
|
||||
|
||||
private final Collection<PromiseListener<T>> listeners;
|
||||
private final StackTraceElement[] stackTrace;
|
||||
|
||||
private @Nullable PromiseCompletion<T> completion;
|
||||
|
||||
public AbstractPromise() {
|
||||
this.listeners = new ConcurrentLinkedQueue<>();
|
||||
this.completion = null;
|
||||
this.stackTrace = Arrays.stream(Thread.currentThread().getStackTrace())
|
||||
.filter(v -> !v.getClassName().startsWith(PACKAGE))
|
||||
.toArray(StackTraceElement[]::new);
|
||||
}
|
||||
|
||||
protected abstract Scheduler getScheduler();
|
||||
|
||||
protected abstract Logger getLogger();
|
||||
|
||||
@Override
|
||||
public T join(long interval, long timeout) throws TimeoutException {
|
||||
long start = System.currentTimeMillis();
|
||||
while (!isCompleted()) {
|
||||
if (System.currentTimeMillis() > start + timeout)
|
||||
throw new TimeoutException("Promise timed out after " + timeout + "ms");
|
||||
|
||||
try {
|
||||
Thread.sleep(interval);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
PromiseCompletion<T> completion = getCompletion();
|
||||
if (completion == null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
if (completion.isError()) {
|
||||
throw new RuntimeException(completion.getException());
|
||||
}
|
||||
|
||||
return completion.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task) {
|
||||
return thenApplySync(result -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedSync(result -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, delay, unit, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenConsumeSync(@NotNull ExceptionalConsumer<T> task) {
|
||||
return thenApplySync(result -> {
|
||||
task.accept(result);
|
||||
return null;
|
||||
}, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenConsumeDelayedSync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedSync(result -> {
|
||||
task.accept(result);
|
||||
return null;
|
||||
}, delay, unit, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task) {
|
||||
return thenApplySync(result -> task.get(), TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenSupplyDelayedSync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedSync(result -> task.get(), delay, unit, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
protected <V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task, @NotNull ExecutorTrace trace) {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
addListener(ctx -> {
|
||||
if (ctx.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx.getException());
|
||||
return;
|
||||
}
|
||||
|
||||
Runnable runnable = createRunnable(ctx, promise, task);
|
||||
getScheduler().runSync(runnable, trace);
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task) {
|
||||
return thenApplySync(task, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
addListener(ctx -> {
|
||||
if (ctx.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx.getException());
|
||||
return;
|
||||
}
|
||||
|
||||
Runnable runnable = createRunnable(ctx, promise, task);
|
||||
getScheduler().runDelayedSync(runnable, delay, unit, trace);
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedSync(task, delay, unit, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task) {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
thenApplySync(task, TraceUtil.getTrace(task)).thenConsumeAsync(nestedPromise -> {
|
||||
nestedPromise.addListener(ctx1 -> {
|
||||
if (ctx1.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx1.getException());
|
||||
return;
|
||||
}
|
||||
|
||||
promise.complete(ctx1.getResult());
|
||||
});
|
||||
}).addListener(ctx2 -> {
|
||||
if (ctx2.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx2.getException());
|
||||
}
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task) {
|
||||
return thenApplyAsync(result -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenRunDelayedAsync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedAsync(result -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, delay, unit, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenConsumeAsync(@NotNull ExceptionalConsumer<T> task) {
|
||||
return thenApplyAsync(result -> {
|
||||
task.accept(result);
|
||||
return null;
|
||||
}, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> thenConsumeDelayedAsync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedAsync(result -> {
|
||||
task.accept(result);
|
||||
return null;
|
||||
}, delay, unit, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task) {
|
||||
return thenApplyAsync(result -> task.get(), TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenSupplyDelayedAsync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedAsync(result -> task.get(), delay, unit, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> thenPopulateReference(@NotNull AtomicReference<T> reference) {
|
||||
return thenApplyAsync((result) -> {
|
||||
reference.set(result);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
protected <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task, @NotNull ExecutorTrace trace) {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
addListener(ctx -> {
|
||||
if (ctx.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx.getException());
|
||||
return;
|
||||
}
|
||||
|
||||
Runnable runnable = createRunnable(ctx, promise, task);
|
||||
getScheduler().runAsync(runnable, trace);
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task) {
|
||||
return thenApplyAsync(task, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
addListener(ctx -> {
|
||||
Runnable runnable = createRunnable(ctx, promise, task);
|
||||
getScheduler().runDelayedAsync(runnable, delay, unit, trace);
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedAsync(task, delay, unit, TraceUtil.getTrace(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
||||
return this.thenComposeAsync(task);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
thenApplyAsync(task, TraceUtil.getTrace(task)).thenConsumeAsync(nestedPromise -> {
|
||||
nestedPromise.addListener(ctx1 -> {
|
||||
if (ctx1.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx1.getException());
|
||||
return;
|
||||
}
|
||||
|
||||
promise.complete(ctx1.getResult());
|
||||
});
|
||||
}).addListener(ctx2 -> {
|
||||
if (ctx2.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx2.getException());
|
||||
}
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
private <V> @NotNull Runnable createRunnable(@NotNull PromiseCompletion<T> ctx, @NotNull Promise<V> promise, @NotNull ExceptionalFunction<T, V> task) {
|
||||
return () -> {
|
||||
if (ctx.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx.getException());
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
V result = task.apply(ctx.getResult());
|
||||
promise.complete(result);
|
||||
} catch (Exception e) {
|
||||
promise.completeExceptionally(e, true);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> logExceptions() {
|
||||
return addListener(ctx -> {
|
||||
if (ctx.isError()) {
|
||||
getLogger().error("Exception caught in promise chain", ctx.getException());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> addListener(@NotNull PromiseListener<T> listener) {
|
||||
if (isCompleted()) {
|
||||
getScheduler().runAsync(() -> {
|
||||
try {
|
||||
//noinspection ConstantConditions
|
||||
listener.handle(getCompletion());
|
||||
} catch (Exception e) {
|
||||
getLogger().error("Exception caught in promise listener", e);
|
||||
}
|
||||
}, TraceUtil.getTrace(listener));
|
||||
} else {
|
||||
getListeners().add(listener);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit) {
|
||||
Runnable func = () -> {
|
||||
if (!isCompleted()) {
|
||||
completeExceptionally(new TimeoutException("Promise timed out after " + time + " " + unit), true);
|
||||
}
|
||||
};
|
||||
|
||||
getScheduler().runDelayedAsync(func, time, unit, TraceUtil.getTrace(func));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> timeout(long ms) {
|
||||
return timeout(ms, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
protected void handleCompletion(@NotNull PromiseCompletion<T> ctx) {
|
||||
if (this.isCompleted()) return;
|
||||
setCompletion(ctx);
|
||||
|
||||
Runnable func = () -> {
|
||||
for (PromiseListener<T> listener : getListeners()) {
|
||||
if (!ctx.isActive()) return;
|
||||
|
||||
try {
|
||||
listener.handle(ctx);
|
||||
} catch (Exception e) {
|
||||
getLogger().error("Exception caught in promise listener", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
getScheduler().runAsync(func, TraceUtil.getTrace(func));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void complete(@Nullable T result) {
|
||||
handleCompletion(new PromiseCompletion<>(result));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completeExceptionally(@NotNull Throwable result, boolean appendStacktrace) {
|
||||
if (appendStacktrace && this.stackTrace != null) {
|
||||
result.setStackTrace(Stream.of(result.getStackTrace(), this.stackTrace)
|
||||
.flatMap(Stream::of)
|
||||
.filter(v -> !v.getClassName().startsWith(PACKAGE))
|
||||
.filter(v -> !v.getClassName().startsWith("java.lang.Thread"))
|
||||
.filter(v -> !v.getClassName().startsWith("java.util.concurrent"))
|
||||
.toArray(StackTraceElement[]::new));
|
||||
}
|
||||
|
||||
handleCompletion(new PromiseCompletion<>(result));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completeExceptionally(@NotNull Throwable result) {
|
||||
completeExceptionally(result, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCompleted() {
|
||||
return getCompletion() != null;
|
||||
}
|
||||
|
||||
protected Collection<PromiseListener<T>> getListeners() {
|
||||
return listeners;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PromiseCompletion<T> getCompletion() {
|
||||
return completion;
|
||||
}
|
||||
|
||||
protected void setCompletion(@NotNull PromiseCompletion<T> completion) {
|
||||
this.completion = completion;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package dev.tommyjs.futur.promise;
|
||||
|
||||
import dev.tommyjs.futur.scheduler.Scheduler;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class PooledPromise<T> extends AbstractPromise<T> {
|
||||
|
||||
private final Scheduler scheduler;
|
||||
private final Logger logger;
|
||||
private final PromiseFactory factory;
|
||||
|
||||
public PooledPromise(Scheduler scheduler, Logger logger, PromiseFactory factory) {
|
||||
this.scheduler = scheduler;
|
||||
this.logger = logger;
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Scheduler getScheduler() {
|
||||
return scheduler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PromiseFactory getFactory() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package dev.tommyjs.futur.promise;
|
||||
|
||||
import dev.tommyjs.futur.scheduler.Scheduler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class PooledPromiseFactory implements PromiseFactory {
|
||||
|
||||
private final Scheduler scheduler;
|
||||
private final Logger logger;
|
||||
|
||||
public PooledPromiseFactory(Scheduler scheduler, Logger logger) {
|
||||
this.scheduler = scheduler;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T> Promise<T> resolve(T value) {
|
||||
AbstractPromise<T> promise = new PooledPromise<>(scheduler, logger, this);
|
||||
promise.setCompletion(new PromiseCompletion<>(value));
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T> Promise<T> unresolved() {
|
||||
return new PooledPromise<>(scheduler, logger, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T> Promise<T> error(Throwable error) {
|
||||
AbstractPromise<T> promise = new PooledPromise<>(scheduler, logger, this);
|
||||
promise.completeExceptionally(error);
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> start() {
|
||||
return resolve(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,406 +4,109 @@ import dev.tommyjs.futur.function.ExceptionalConsumer;
|
||||
import dev.tommyjs.futur.function.ExceptionalFunction;
|
||||
import dev.tommyjs.futur.function.ExceptionalRunnable;
|
||||
import dev.tommyjs.futur.function.ExceptionalSupplier;
|
||||
import dev.tommyjs.futur.scheduler.Schedulers;
|
||||
import dev.tommyjs.futur.trace.ExecutorTrace;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Promise<T> {
|
||||
public interface Promise<T> {
|
||||
|
||||
private static final String PACKAGE;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Promise.class);
|
||||
|
||||
static {
|
||||
String[] packageElements = Promise.class.getPackageName().split("\\.");
|
||||
int i = 0;
|
||||
|
||||
StringBuilder packageBuilder = new StringBuilder();
|
||||
while (i < 3) {
|
||||
packageBuilder.append(packageElements[i]);
|
||||
i++;
|
||||
}
|
||||
|
||||
PACKAGE = packageBuilder.toString();
|
||||
static <T> @NotNull Promise<T> resolve(T value, PromiseFactory factory) {
|
||||
return factory.resolve(value);
|
||||
}
|
||||
|
||||
private final Collection<PromiseListener<T>> listeners;
|
||||
private final StackTraceElement[] stackTrace;
|
||||
|
||||
private @Nullable PromiseCompletion<T> completion;
|
||||
|
||||
public Promise() {
|
||||
this.listeners = new ConcurrentLinkedQueue<>();
|
||||
this.completion = null;
|
||||
this.stackTrace = Arrays.stream(Thread.currentThread().getStackTrace())
|
||||
.filter(v -> !v.getClassName().startsWith(PACKAGE))
|
||||
.toArray(StackTraceElement[]::new);
|
||||
static <T> @NotNull Promise<T> error(Throwable error, PromiseFactory factory) {
|
||||
return factory.error(error);
|
||||
}
|
||||
|
||||
public T join(long interval, long timeout) throws TimeoutException {
|
||||
long start = System.currentTimeMillis();
|
||||
while (!isCompleted()) {
|
||||
if (System.currentTimeMillis() > start + timeout)
|
||||
throw new TimeoutException("Promise timed out after " + timeout + "ms");
|
||||
|
||||
try {
|
||||
Thread.sleep(interval);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
PromiseCompletion<T> completion = getCompletion();
|
||||
if (completion == null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
if (completion.isError()) {
|
||||
throw new RuntimeException(completion.getException());
|
||||
}
|
||||
|
||||
return completion.getResult();
|
||||
static @NotNull Promise<Void> start(PromiseFactory factory) {
|
||||
return factory.start();
|
||||
}
|
||||
|
||||
public @NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task) {
|
||||
return thenApplySync(result -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, Schedulers.getTrace(task));
|
||||
static <T> @NotNull Promise<T> resolve(T value) {
|
||||
return resolve(value, UnpooledPromiseFactory.INSTANCE);
|
||||
}
|
||||
|
||||
public @NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedSync(result -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, delay, unit, Schedulers.getTrace(task));
|
||||
static <T> @NotNull Promise<T> error(Throwable error) {
|
||||
return error(error, UnpooledPromiseFactory.INSTANCE);
|
||||
}
|
||||
|
||||
public @NotNull Promise<Void> thenConsumeSync(@NotNull ExceptionalConsumer<T> task) {
|
||||
return thenApplySync(result -> {
|
||||
task.accept(result);
|
||||
return null;
|
||||
}, Schedulers.getTrace(task));
|
||||
static @NotNull Promise<Void> start() {
|
||||
return start(UnpooledPromiseFactory.INSTANCE);
|
||||
}
|
||||
|
||||
public @NotNull Promise<Void> thenConsumeDelayedSync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedSync(result -> {
|
||||
task.accept(result);
|
||||
return null;
|
||||
}, delay, unit, Schedulers.getTrace(task));
|
||||
@Deprecated
|
||||
static <T> @NotNull Promise<T> start(T start) {
|
||||
return resolve(start);
|
||||
}
|
||||
|
||||
public <V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task) {
|
||||
return thenApplySync(result -> task.get(), Schedulers.getTrace(task));
|
||||
}
|
||||
PromiseFactory getFactory();
|
||||
|
||||
public <V> @NotNull Promise<V> thenSupplyDelayedSync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedSync(result -> task.get(), delay, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
T join(long interval, long timeout) throws TimeoutException;
|
||||
|
||||
public <V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task, @NotNull ExecutorTrace trace) {
|
||||
Promise<V> promise = new Promise<>();
|
||||
addListener(ctx -> {
|
||||
if (ctx.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx.getException());
|
||||
return;
|
||||
}
|
||||
@NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task);
|
||||
|
||||
Runnable runnable = createRunnable(ctx, promise, task);
|
||||
Schedulers.runSync(runnable, trace);
|
||||
});
|
||||
@NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
return promise;
|
||||
}
|
||||
@NotNull Promise<Void> thenConsumeSync(@NotNull ExceptionalConsumer<T> task);
|
||||
|
||||
public <V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task) {
|
||||
return thenApplySync(task, Schedulers.getTrace(task));
|
||||
}
|
||||
@NotNull Promise<Void> thenConsumeDelayedSync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
public <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
||||
Promise<V> promise = new Promise<>();
|
||||
addListener(ctx -> {
|
||||
if (ctx.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx.getException());
|
||||
return;
|
||||
}
|
||||
<V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task);
|
||||
|
||||
Runnable runnable = createRunnable(ctx, promise, task);
|
||||
Schedulers.runDelayedSync(runnable, delay, unit, trace);
|
||||
});
|
||||
<V> @NotNull Promise<V> thenSupplyDelayedSync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
return promise;
|
||||
}
|
||||
<V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task);
|
||||
|
||||
public <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedSync(task, delay, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
<V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
||||
|
||||
public <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task) {
|
||||
Promise<V> promise = new Promise<>();
|
||||
thenApplySync(task, Schedulers.getTrace(task)).thenConsumeAsync(nestedPromise -> {
|
||||
nestedPromise.addListener(ctx1 -> {
|
||||
if (ctx1.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx1.getException());
|
||||
return;
|
||||
}
|
||||
<V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
promise.complete(ctx1.getResult());
|
||||
});
|
||||
}).addListener(ctx2 -> {
|
||||
if (ctx2.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx2.getException());
|
||||
}
|
||||
});
|
||||
<V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task);
|
||||
|
||||
return promise;
|
||||
}
|
||||
@NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task);
|
||||
|
||||
public @NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task) {
|
||||
return thenApplyAsync(result -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, Schedulers.getTrace(task));
|
||||
}
|
||||
@NotNull Promise<Void> thenRunDelayedAsync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
public @NotNull Promise<Void> thenRunDelayedAsync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedAsync(result -> {
|
||||
task.run();
|
||||
return null;
|
||||
}, delay, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
@NotNull Promise<Void> thenConsumeAsync(@NotNull ExceptionalConsumer<T> task);
|
||||
|
||||
public @NotNull Promise<Void> thenConsumeAsync(@NotNull ExceptionalConsumer<T> task) {
|
||||
return thenApplyAsync(result -> {
|
||||
task.accept(result);
|
||||
return null;
|
||||
}, Schedulers.getTrace(task));
|
||||
}
|
||||
@NotNull Promise<Void> thenConsumeDelayedAsync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
public @NotNull Promise<Void> thenConsumeDelayedAsync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedAsync(result -> {
|
||||
task.accept(result);
|
||||
return null;
|
||||
}, delay, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
<V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task);
|
||||
|
||||
@Deprecated(forRemoval = true)
|
||||
public @NotNull Promise<Void> thenConsumerDelayedAsync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenConsumeDelayedAsync(task, delay, unit);
|
||||
}
|
||||
<V> @NotNull Promise<V> thenSupplyDelayedAsync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
public <V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task) {
|
||||
return thenApplyAsync(result -> task.get(), Schedulers.getTrace(task));
|
||||
}
|
||||
@NotNull Promise<T> thenPopulateReference(@NotNull AtomicReference<T> reference);
|
||||
|
||||
public <V> @NotNull Promise<V> thenSupplyDelayedAsync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedAsync(result -> task.get(), delay, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
<V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task);
|
||||
|
||||
public @NotNull Promise<T> thenPopulateReference(@NotNull AtomicReference<T> reference) {
|
||||
return thenApplyAsync((result) -> {
|
||||
reference.set(result);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
<V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
||||
|
||||
public <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task, @NotNull ExecutorTrace trace) {
|
||||
Promise<V> promise = new Promise<>();
|
||||
addListener(ctx -> {
|
||||
createRunnable(ctx, promise, task).run();
|
||||
});
|
||||
<V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit);
|
||||
|
||||
return promise;
|
||||
}
|
||||
<V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task);
|
||||
|
||||
public <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task) {
|
||||
return thenApplyAsync(task, Schedulers.getTrace(task));
|
||||
}
|
||||
<V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task);
|
||||
|
||||
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
||||
Promise<V> promise = new Promise<>();
|
||||
addListener(ctx -> {
|
||||
Runnable runnable = createRunnable(ctx, promise, task);
|
||||
Schedulers.runDelayedAsync(runnable, delay, unit, trace);
|
||||
});
|
||||
@NotNull Promise<T> logExceptions();
|
||||
|
||||
return promise;
|
||||
}
|
||||
@NotNull Promise<T> addListener(@NotNull PromiseListener<T> listener);
|
||||
|
||||
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
||||
return thenApplyDelayedAsync(task, delay, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
@NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit);
|
||||
|
||||
public <V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
||||
return this.thenComposeAsync(task);
|
||||
}
|
||||
@NotNull Promise<T> timeout(long ms);
|
||||
|
||||
public <V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
||||
Promise<V> promise = new Promise<>();
|
||||
thenApplyAsync(task, Schedulers.getTrace(task)).thenConsumeAsync(nestedPromise -> {
|
||||
nestedPromise.addListener(ctx1 -> {
|
||||
if (ctx1.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx1.getException());
|
||||
return;
|
||||
}
|
||||
void complete(@Nullable T result);
|
||||
|
||||
promise.complete(ctx1.getResult());
|
||||
});
|
||||
}).addListener(ctx2 -> {
|
||||
if (ctx2.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx2.getException());
|
||||
}
|
||||
});
|
||||
void completeExceptionally(@NotNull Throwable result, boolean appendStacktrace);
|
||||
|
||||
return promise;
|
||||
}
|
||||
void completeExceptionally(@NotNull Throwable result);
|
||||
|
||||
private <V> @NotNull Runnable createRunnable(@NotNull PromiseCompletion<T> ctx, @NotNull Promise<V> promise, @NotNull ExceptionalFunction<T, V> task) {
|
||||
return () -> {
|
||||
if (ctx.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
promise.completeExceptionally(ctx.getException());
|
||||
return;
|
||||
}
|
||||
boolean isCompleted();
|
||||
|
||||
try {
|
||||
V result = task.apply(ctx.getResult());
|
||||
promise.complete(result);
|
||||
} catch (Exception e) {
|
||||
promise.completeExceptionally(e, true);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public @NotNull Promise<T> logExceptions() {
|
||||
return addListener(ctx -> {
|
||||
if (ctx.isError()) {
|
||||
LOGGER.error("Exception caught in promise pipeline", ctx.getException());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public @NotNull Promise<T> addListener(@NotNull PromiseListener<T> listener) {
|
||||
if (isCompleted()) {
|
||||
Schedulers.runAsync(() -> {
|
||||
try {
|
||||
listener.handle(getCompletion());
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Exception caught in promise listener", e);
|
||||
}
|
||||
}, Schedulers.getTrace(listener));
|
||||
} else {
|
||||
getListeners().add(listener);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit) {
|
||||
Schedulers.runDelayedAsync(() -> {
|
||||
if (!isCompleted()) {
|
||||
completeExceptionally(new TimeoutException("Promise timed out after " + time + " " + unit), true);
|
||||
}
|
||||
}, time, unit);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull Promise<T> timeout(long ms) {
|
||||
return timeout(ms, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
protected void handleCompletion(@NotNull PromiseCompletion<T> ctx) {
|
||||
if (this.isCompleted()) return;
|
||||
setCompletion(ctx);
|
||||
|
||||
Schedulers.runAsync(() -> {
|
||||
for (PromiseListener<T> listener : getListeners()) {
|
||||
if (!ctx.isActive()) return;
|
||||
|
||||
try {
|
||||
listener.handle(ctx);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Exception caught in promise listener", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void complete(@Nullable T result) {
|
||||
handleCompletion(new PromiseCompletion<>(result));
|
||||
}
|
||||
|
||||
public void completeExceptionally(@NotNull Throwable result, boolean appendStacktrace) {
|
||||
if (appendStacktrace && this.stackTrace != null) {
|
||||
result.setStackTrace(Stream.of(result.getStackTrace(), this.stackTrace)
|
||||
.flatMap(Stream::of)
|
||||
.filter(v -> !v.getClassName().startsWith(PACKAGE))
|
||||
.filter(v -> !v.getClassName().startsWith("java.lang.Thread"))
|
||||
.filter(v -> !v.getClassName().startsWith("java.util.concurrent"))
|
||||
.toArray(StackTraceElement[]::new));
|
||||
}
|
||||
|
||||
handleCompletion(new PromiseCompletion<>(result));
|
||||
}
|
||||
|
||||
public void completeExceptionally(@NotNull Throwable result) {
|
||||
completeExceptionally(result, false);
|
||||
}
|
||||
|
||||
public boolean isCompleted() {
|
||||
return getCompletion() != null;
|
||||
}
|
||||
|
||||
protected Collection<PromiseListener<T>> getListeners() {
|
||||
return listeners;
|
||||
}
|
||||
|
||||
public @Nullable PromiseCompletion<T> getCompletion() {
|
||||
return completion;
|
||||
}
|
||||
|
||||
protected void setCompletion(@NotNull PromiseCompletion<T> completion) {
|
||||
this.completion = completion;
|
||||
}
|
||||
|
||||
public static <T> @NotNull Promise<T> resolve(T value) {
|
||||
Promise<T> promise = new Promise<>();
|
||||
promise.setCompletion(new PromiseCompletion<>(value));
|
||||
return promise;
|
||||
}
|
||||
|
||||
public static <T> @NotNull Promise<T> error(Throwable error) {
|
||||
Promise<T> promise = new Promise<>();
|
||||
promise.completeExceptionally(error);
|
||||
return promise;
|
||||
}
|
||||
|
||||
public static @NotNull Promise<Void> start() {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
|
||||
@Deprecated // use resolve()
|
||||
public static <T> @NotNull Promise<T> start(T start) {
|
||||
Promise<T> promise = new Promise<>();
|
||||
promise.complete(start);
|
||||
return promise;
|
||||
}
|
||||
@Nullable PromiseCompletion<T> getCompletion();
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package dev.tommyjs.futur.promise;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public interface PromiseFactory {
|
||||
|
||||
<T> @NotNull Promise<T> resolve(T value);
|
||||
|
||||
<T> @NotNull Promise<T> unresolved();
|
||||
|
||||
<T> @NotNull Promise<T> error(Throwable error);
|
||||
|
||||
@NotNull Promise<Void> start();
|
||||
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package dev.tommyjs.futur.promise;
|
||||
import dev.tommyjs.futur.function.ExceptionalFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -13,8 +15,8 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class Promises {
|
||||
|
||||
public static <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
||||
Promise<Map.Entry<K, V>> promise = new Promise<>();
|
||||
public static <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2, PromiseFactory factory) {
|
||||
Promise<Map.Entry<K, V>> promise = factory.unresolved();
|
||||
p1.addListener(ctx -> {
|
||||
if (ctx.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
@@ -37,13 +39,15 @@ public class Promises {
|
||||
return promise;
|
||||
}
|
||||
|
||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, long timeout, @Nullable BiConsumer<K, Throwable> exceptionHandler) {
|
||||
Map<K, V> map = new HashMap<>();
|
||||
if (promises.isEmpty()) return Promise.resolve(map);
|
||||
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<K, V>> combine(@NotNull Map<K, Promise<V>> promises, long timeout, @Nullable BiConsumer<K, Throwable> exceptionHandler, PromiseFactory factory) {
|
||||
Map<K, V> map = new HashMap<>();
|
||||
ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
Promise<Map<K, V>> promise = new Promise<>();
|
||||
Promise<Map<K, V>> promise = factory.unresolved();
|
||||
for (Map.Entry<K, Promise<V>> entry : promises.entrySet()) {
|
||||
entry.getValue().addListener((ctx) -> {
|
||||
lock.lock();
|
||||
@@ -69,19 +73,35 @@ public class Promises {
|
||||
return promise.timeout(timeout);
|
||||
}
|
||||
|
||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, long timeout, @Nullable BiConsumer<K, Throwable> exceptionHandler) {
|
||||
return combine(promises, timeout, exceptionHandler, obtainFactory(promises.values()));
|
||||
}
|
||||
|
||||
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, boolean strict) {
|
||||
return combine(promises, timeout, strict ? null : (_k, _v) -> {});
|
||||
return combine(promises, timeout, strict, obtainFactory(promises.values()));
|
||||
}
|
||||
|
||||
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) {
|
||||
return combine(promises, timeout, true);
|
||||
return combine(promises, timeout, true, obtainFactory(promises.values()));
|
||||
}
|
||||
|
||||
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 <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises) {
|
||||
return combine(promises, 1500L, true);
|
||||
return combine(promises, obtainFactory(promises.values()));
|
||||
}
|
||||
|
||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout, boolean strict) {
|
||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout, boolean strict, PromiseFactory factory) {
|
||||
AtomicInteger index = new AtomicInteger();
|
||||
return combine(
|
||||
promises.stream()
|
||||
@@ -96,18 +116,28 @@ public class Promises {
|
||||
);
|
||||
}
|
||||
|
||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout) {
|
||||
return combine(promises, timeout, true);
|
||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout, boolean strict) {
|
||||
return combine(promises, timeout, strict, obtainFactory(promises));
|
||||
}
|
||||
|
||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises) {
|
||||
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) {
|
||||
return combine(promises, timeout, obtainFactory(promises));
|
||||
}
|
||||
|
||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, PromiseFactory factory) {
|
||||
return combine(promises, 1500L, true);
|
||||
}
|
||||
|
||||
public static @NotNull Promise<Void> all(@NotNull List<Promise<?>> promises) {
|
||||
if (promises.isEmpty()) return Promise.start();
|
||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises) {
|
||||
return combine(promises, obtainFactory(promises));
|
||||
}
|
||||
|
||||
Promise<Void> promise = new Promise<>();
|
||||
public static @NotNull Promise<Void> all(@NotNull List<Promise<?>> promises, PromiseFactory factory) {
|
||||
Promise<Void> promise = factory.unresolved();
|
||||
for (Promise<?> p : promises) {
|
||||
p.addListener((ctx) -> {
|
||||
if (ctx.isError()) {
|
||||
@@ -121,30 +151,49 @@ public class Promises {
|
||||
return promise;
|
||||
}
|
||||
|
||||
public static @NotNull Promise<Void> all(@NotNull Promise<?>... promises) {
|
||||
return all(Arrays.asList(promises));
|
||||
public static @NotNull Promise<Void> all(@NotNull List<Promise<?>> promises) {
|
||||
PromiseFactory factory;
|
||||
if (promises.isEmpty()) {
|
||||
factory = UnpooledPromiseFactory.INSTANCE;
|
||||
} else {
|
||||
factory = promises.get(0).getFactory();
|
||||
}
|
||||
|
||||
return all(promises, factory);
|
||||
}
|
||||
|
||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper, long timeout, boolean strict) {
|
||||
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 = Promise.resolve(key).thenApplyAsync(mapper);
|
||||
Promise<V> promise = factory.resolve(key).thenApplyAsync(mapper);
|
||||
promises.put(key, promise);
|
||||
}
|
||||
|
||||
return combine(promises, timeout, strict);
|
||||
}
|
||||
|
||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper, long timeout, boolean strict) {
|
||||
return combine(keys, mapper, timeout, strict, UnpooledPromiseFactory.INSTANCE);
|
||||
}
|
||||
|
||||
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) {
|
||||
return combine(keys, mapper, timeout, true);
|
||||
return combine(keys, mapper, timeout, UnpooledPromiseFactory.INSTANCE);
|
||||
}
|
||||
|
||||
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 <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper) {
|
||||
return combine(keys, mapper, 1500L, true);
|
||||
return combine(keys, mapper, UnpooledPromiseFactory.INSTANCE);
|
||||
}
|
||||
|
||||
public static @NotNull Promise<Void> erase(@NotNull Promise<?> p) {
|
||||
Promise<Void> promise = new Promise<>();
|
||||
public static @NotNull Promise<Void> erase(@NotNull Promise<?> p, PromiseFactory factory) {
|
||||
Promise<Void> promise = factory.unresolved();
|
||||
p.addListener(ctx -> {
|
||||
if (ctx.isError()) {
|
||||
//noinspection ConstantConditions
|
||||
@@ -157,8 +206,12 @@ public class Promises {
|
||||
return promise;
|
||||
}
|
||||
|
||||
public static <T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future) {
|
||||
Promise<T> promise = new Promise<>();
|
||||
public static @NotNull Promise<Void> erase(@NotNull Promise<?> p) {
|
||||
return erase(p, p.getFactory());
|
||||
}
|
||||
|
||||
public static <T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future, PromiseFactory factory) {
|
||||
Promise<T> promise = factory.unresolved();
|
||||
future.whenComplete((result, e) -> {
|
||||
if (e != null) {
|
||||
promise.completeExceptionally(e);
|
||||
@@ -170,4 +223,19 @@ public class Promises {
|
||||
return promise;
|
||||
}
|
||||
|
||||
public static <T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future) {
|
||||
return wrap(future, UnpooledPromiseFactory.INSTANCE);
|
||||
}
|
||||
|
||||
public static <T> PromiseFactory obtainFactory(Collection<Promise<T>> promises) {
|
||||
PromiseFactory factory;
|
||||
if (promises.isEmpty()) {
|
||||
factory = UnpooledPromiseFactory.INSTANCE;
|
||||
} else {
|
||||
factory = promises.stream().findFirst().get().getFactory();
|
||||
}
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package dev.tommyjs.futur.promise;
|
||||
|
||||
import dev.tommyjs.futur.scheduler.Scheduler;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class UnpooledPromise<T> extends AbstractPromise<T> {
|
||||
|
||||
@Override
|
||||
protected Scheduler getScheduler() {
|
||||
return UnpooledPromiseFactory.SCHEDULER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Logger getLogger() {
|
||||
return UnpooledPromiseFactory.LOGGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PromiseFactory getFactory() {
|
||||
return UnpooledPromiseFactory.INSTANCE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package dev.tommyjs.futur.promise;
|
||||
|
||||
import dev.tommyjs.futur.scheduler.Scheduler;
|
||||
import dev.tommyjs.futur.scheduler.SingleExecutorScheduler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class UnpooledPromiseFactory implements PromiseFactory {
|
||||
|
||||
public static final @NotNull PromiseFactory INSTANCE;
|
||||
public static final @NotNull Scheduler SCHEDULER;
|
||||
public static final @NotNull Logger LOGGER;
|
||||
|
||||
static {
|
||||
INSTANCE = new UnpooledPromiseFactory();
|
||||
SCHEDULER = new SingleExecutorScheduler(Executors.newSingleThreadScheduledExecutor());
|
||||
LOGGER = LoggerFactory.getLogger(UnpooledPromiseFactory.class);
|
||||
}
|
||||
|
||||
private UnpooledPromiseFactory() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T> Promise<T> resolve(T value) {
|
||||
AbstractPromise<T> promise = new UnpooledPromise<>();
|
||||
promise.setCompletion(new PromiseCompletion<>(value));
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T> Promise<T> unresolved() {
|
||||
return new UnpooledPromise<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T> Promise<T> error(Throwable error) {
|
||||
AbstractPromise<T> promise = new UnpooledPromise<>();
|
||||
promise.completeExceptionally(error);
|
||||
return promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<Void> start() {
|
||||
return resolve(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public interface Scheduler {
|
||||
|
||||
void runRepeatingAsync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
||||
|
||||
default @NotNull Runnable wrapExceptions(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
||||
static @NotNull Runnable wrapExceptions(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
||||
return () -> {
|
||||
try {
|
||||
task.run();
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
package dev.tommyjs.futur.scheduler;
|
||||
|
||||
import dev.tommyjs.futur.trace.ExecutorTrace;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public class Schedulers {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Schedulers.class);
|
||||
|
||||
private static @Nullable Scheduler scheduler;
|
||||
|
||||
public static void runSync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
||||
ensureLoaded();
|
||||
getScheduler().runSync(task, trace);
|
||||
}
|
||||
|
||||
public static void runSync(@NotNull Runnable task) {
|
||||
ensureLoaded();
|
||||
getScheduler().runSync(task, Schedulers.getTrace(task));
|
||||
}
|
||||
|
||||
public static void runDelayedSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
||||
ensureLoaded();
|
||||
getScheduler().runDelayedSync(task, delay, unit, trace);
|
||||
}
|
||||
|
||||
public static void runDelayedSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit) {
|
||||
ensureLoaded();
|
||||
getScheduler().runDelayedSync(task, delay, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
|
||||
public static void runRepeatingSync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
||||
ensureLoaded();
|
||||
getScheduler().runRepeatingSync(task, interval, unit, trace);
|
||||
}
|
||||
|
||||
public static void runRepeatingSync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit) {
|
||||
ensureLoaded();
|
||||
getScheduler().runRepeatingSync(task, interval, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
|
||||
public static void runAsync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
||||
ensureLoaded();
|
||||
getScheduler().runAsync(task, trace);
|
||||
}
|
||||
|
||||
public static void runAsync(@NotNull Runnable task) {
|
||||
ensureLoaded();
|
||||
getScheduler().runAsync(task, Schedulers.getTrace(task));
|
||||
}
|
||||
|
||||
public static void runDelayedAsync(@NotNull Runnable task, long delay, TimeUnit unit, ExecutorTrace trace) {
|
||||
ensureLoaded();
|
||||
getScheduler().runDelayedAsync(task, delay, unit, trace);
|
||||
}
|
||||
|
||||
public static void runDelayedAsync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit) {
|
||||
ensureLoaded();
|
||||
getScheduler().runDelayedAsync(task, delay, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
|
||||
public static void runRepeatingAsync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
||||
ensureLoaded();
|
||||
getScheduler().runRepeatingAsync(task, interval, unit, trace);
|
||||
}
|
||||
|
||||
public static void runRepeatingAsync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit) {
|
||||
ensureLoaded();
|
||||
getScheduler().runRepeatingAsync(task, interval, unit, Schedulers.getTrace(task));
|
||||
}
|
||||
|
||||
public static ExecutorTrace getTrace(@NotNull Object function) {
|
||||
return new ExecutorTrace(function.getClass(), Thread.currentThread().getStackTrace());
|
||||
}
|
||||
|
||||
public static void ensureLoaded() {
|
||||
if (getScheduler() == null) {
|
||||
LOGGER.warn("No scheduler loaded, falling back to default single threaded scheduler");
|
||||
setScheduler(SingleExecutorScheduler.create());
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadDefaultScheduler() {
|
||||
|
||||
}
|
||||
|
||||
public static boolean isLoaded() {
|
||||
return getScheduler() != null;
|
||||
}
|
||||
|
||||
public static @Nullable Scheduler getScheduler() {
|
||||
return scheduler;
|
||||
}
|
||||
|
||||
public static void setScheduler(@NotNull Scheduler scheduler) {
|
||||
Schedulers.scheduler = scheduler;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package dev.tommyjs.futur.scheduler;
|
||||
import dev.tommyjs.futur.trace.ExecutorTrace;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -11,23 +10,23 @@ public class SingleExecutorScheduler implements Scheduler {
|
||||
|
||||
private final ScheduledExecutorService service;
|
||||
|
||||
protected SingleExecutorScheduler(ScheduledExecutorService service) {
|
||||
public SingleExecutorScheduler(ScheduledExecutorService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runSync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
||||
service.submit(wrapExceptions(task, trace));
|
||||
service.submit(Scheduler.wrapExceptions(task, trace));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runDelayedSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
||||
service.schedule(wrapExceptions(task, trace), delay, unit);
|
||||
service.schedule(Scheduler.wrapExceptions(task, trace), delay, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runRepeatingSync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
||||
service.scheduleAtFixedRate(wrapExceptions(task, trace), 0L, interval, unit);
|
||||
service.scheduleAtFixedRate(Scheduler.wrapExceptions(task, trace), 0L, interval, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,8 +44,4 @@ public class SingleExecutorScheduler implements Scheduler {
|
||||
runRepeatingSync(task, interval, unit, trace);
|
||||
}
|
||||
|
||||
public static SingleExecutorScheduler create() {
|
||||
return new SingleExecutorScheduler(Executors.newSingleThreadScheduledExecutor());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package dev.tommyjs.futur.trace;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TraceUtil {
|
||||
|
||||
public static ExecutorTrace getTrace(@NotNull Object function) {
|
||||
return new ExecutorTrace(function.getClass(), Thread.currentThread().getStackTrace());
|
||||
}
|
||||
|
||||
}
|
||||
35
futur-api/src/test/java/dev/tommyjs/test/Test.java
Normal file
35
futur-api/src/test/java/dev/tommyjs/test/Test.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package dev.tommyjs.test;
|
||||
|
||||
import dev.tommyjs.futur.promise.PooledPromiseFactory;
|
||||
import dev.tommyjs.futur.promise.Promise;
|
||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||
import dev.tommyjs.futur.scheduler.Scheduler;
|
||||
import dev.tommyjs.futur.scheduler.SingleExecutorScheduler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Test {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Scheduler scheduler = new SingleExecutorScheduler(Executors.newScheduledThreadPool(4));
|
||||
Logger logger = LoggerFactory.getLogger(Test.class);
|
||||
PromiseFactory factory = new PooledPromiseFactory(scheduler, logger);
|
||||
|
||||
Thread.sleep(2000);
|
||||
|
||||
Promise.start(factory)
|
||||
.thenRunAsync(() -> {
|
||||
System.out.println("HI");
|
||||
})
|
||||
.thenApplyDelayedAsync(_v -> {
|
||||
return "ABC";
|
||||
}, 1L, TimeUnit.SECONDS)
|
||||
.thenConsumeSync(t -> {
|
||||
System.out.println(t);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package dev.tommyjs.futur.reactivestreams;
|
||||
|
||||
import dev.tommyjs.futur.promise.Promise;
|
||||
import dev.tommyjs.futur.promise.AbstractPromise;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
public class ReactiveTransformer {
|
||||
|
||||
public static <T> @NotNull Promise<T> wrapPublisher(@NotNull Publisher<T> publisher) {
|
||||
public static <T> @NotNull AbstractPromise<T> wrapPublisher(@NotNull Publisher<T> publisher) {
|
||||
SingleAccumulatorSubscriber<T> subscriber = SingleAccumulatorSubscriber.create();
|
||||
publisher.subscribe(subscriber);
|
||||
return subscriber.getPromise();
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package dev.tommyjs.futur.reactivestreams;
|
||||
|
||||
import dev.tommyjs.futur.promise.Promise;
|
||||
import dev.tommyjs.futur.promise.AbstractPromise;
|
||||
import org.reactivestreams.Subscriber;
|
||||
import org.reactivestreams.Subscription;
|
||||
|
||||
public class SingleAccumulatorSubscriber<T> implements Subscriber<T> {
|
||||
|
||||
private final Promise<T> promise;
|
||||
private final AbstractPromise<T> promise;
|
||||
|
||||
public SingleAccumulatorSubscriber(Promise<T> promise) {
|
||||
public SingleAccumulatorSubscriber(AbstractPromise<T> promise) {
|
||||
this.promise = promise;
|
||||
}
|
||||
|
||||
@@ -32,16 +32,16 @@ public class SingleAccumulatorSubscriber<T> implements Subscriber<T> {
|
||||
// ignore
|
||||
}
|
||||
|
||||
public Promise<T> getPromise() {
|
||||
public AbstractPromise<T> getPromise() {
|
||||
return promise;
|
||||
}
|
||||
|
||||
public static <T> SingleAccumulatorSubscriber<T> create(Promise<T> promise) {
|
||||
public static <T> SingleAccumulatorSubscriber<T> create(AbstractPromise<T> promise) {
|
||||
return new SingleAccumulatorSubscriber<>(promise);
|
||||
}
|
||||
|
||||
public static <T> SingleAccumulatorSubscriber<T> create() {
|
||||
return create(new Promise<>());
|
||||
return create(new AbstractPromise<>());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package dev.tommyjs.futur.reactor;
|
||||
|
||||
import dev.tommyjs.futur.promise.Promise;
|
||||
import dev.tommyjs.futur.promise.AbstractPromise;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -11,14 +11,14 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class ReactorTransformer {
|
||||
|
||||
public static <T> @NotNull Promise<T> wrapMono(@NotNull Mono<T> mono) {
|
||||
Promise<T> promise = new Promise<>();
|
||||
public static <T> @NotNull AbstractPromise<T> wrapMono(@NotNull Mono<T> mono) {
|
||||
AbstractPromise<T> promise = new AbstractPromise<>();
|
||||
mono.doOnSuccess(promise::complete).doOnError(promise::completeExceptionally).subscribe();
|
||||
return promise;
|
||||
}
|
||||
|
||||
public static <T> @NotNull Promise<@NotNull List<T>> wrapFlux(@NotNull Flux<T> flux) {
|
||||
Promise<List<T>> promise = new Promise<>();
|
||||
public static <T> @NotNull AbstractPromise<@NotNull List<T>> wrapFlux(@NotNull Flux<T> flux) {
|
||||
AbstractPromise<List<T>> promise = new AbstractPromise<>();
|
||||
AtomicReference<List<T>> out = new AtomicReference<>(new ArrayList<>());
|
||||
|
||||
flux.doOnNext(out.get()::add).subscribe();
|
||||
|
||||
Reference in New Issue
Block a user