mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-18 07:16:45 +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.ExceptionalFunction;
|
||||||
import dev.tommyjs.futur.function.ExceptionalRunnable;
|
import dev.tommyjs.futur.function.ExceptionalRunnable;
|
||||||
import dev.tommyjs.futur.function.ExceptionalSupplier;
|
import dev.tommyjs.futur.function.ExceptionalSupplier;
|
||||||
import dev.tommyjs.futur.scheduler.Schedulers;
|
|
||||||
import dev.tommyjs.futur.trace.ExecutorTrace;
|
import dev.tommyjs.futur.trace.ExecutorTrace;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
public class Promise<T> {
|
public interface Promise<T> {
|
||||||
|
|
||||||
private static final String PACKAGE;
|
static <T> @NotNull Promise<T> resolve(T value, PromiseFactory factory) {
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(Promise.class);
|
return factory.resolve(value);
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task) {
|
|
||||||
return thenApplySync(result -> {
|
|
||||||
task.run();
|
|
||||||
return null;
|
|
||||||
}, Schedulers.getTrace(task));
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Promise<Void> thenConsumeSync(@NotNull ExceptionalConsumer<T> task) {
|
|
||||||
return thenApplySync(result -> {
|
|
||||||
task.accept(result);
|
|
||||||
return null;
|
|
||||||
}, Schedulers.getTrace(task));
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task) {
|
|
||||||
return thenApplySync(result -> task.get(), Schedulers.getTrace(task));
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
Runnable runnable = createRunnable(ctx, promise, task);
|
|
||||||
Schedulers.runSync(runnable, trace);
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task) {
|
|
||||||
return thenApplySync(task, Schedulers.getTrace(task));
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
Runnable runnable = createRunnable(ctx, promise, task);
|
|
||||||
Schedulers.runDelayedSync(runnable, delay, unit, trace);
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
|
||||||
return thenApplyDelayedSync(task, delay, unit, Schedulers.getTrace(task));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task) {
|
static <T> @NotNull Promise<T> error(Throwable error, PromiseFactory factory) {
|
||||||
Promise<V> promise = new Promise<>();
|
return factory.error(error);
|
||||||
thenApplySync(task, Schedulers.getTrace(task)).thenConsumeAsync(nestedPromise -> {
|
|
||||||
nestedPromise.addListener(ctx1 -> {
|
|
||||||
if (ctx1.isError()) {
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
promise.completeExceptionally(ctx1.getException());
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
promise.complete(ctx1.getResult());
|
static @NotNull Promise<Void> start(PromiseFactory factory) {
|
||||||
});
|
return factory.start();
|
||||||
}).addListener(ctx2 -> {
|
|
||||||
if (ctx2.isError()) {
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
promise.completeExceptionally(ctx2.getException());
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
static <T> @NotNull Promise<T> resolve(T value) {
|
||||||
|
return resolve(value, UnpooledPromiseFactory.INSTANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task) {
|
static <T> @NotNull Promise<T> error(Throwable error) {
|
||||||
return thenApplyAsync(result -> {
|
return error(error, UnpooledPromiseFactory.INSTANCE);
|
||||||
task.run();
|
|
||||||
return null;
|
|
||||||
}, Schedulers.getTrace(task));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Promise<Void> thenRunDelayedAsync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit) {
|
static @NotNull Promise<Void> start() {
|
||||||
return thenApplyDelayedAsync(result -> {
|
return start(UnpooledPromiseFactory.INSTANCE);
|
||||||
task.run();
|
|
||||||
return null;
|
|
||||||
}, delay, unit, Schedulers.getTrace(task));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Promise<Void> thenConsumeAsync(@NotNull ExceptionalConsumer<T> task) {
|
@Deprecated
|
||||||
return thenApplyAsync(result -> {
|
static <T> @NotNull Promise<T> start(T start) {
|
||||||
task.accept(result);
|
return resolve(start);
|
||||||
return null;
|
|
||||||
}, Schedulers.getTrace(task));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Promise<Void> thenConsumeDelayedAsync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit) {
|
PromiseFactory getFactory();
|
||||||
return thenApplyDelayedAsync(result -> {
|
|
||||||
task.accept(result);
|
|
||||||
return null;
|
|
||||||
}, delay, unit, Schedulers.getTrace(task));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated(forRemoval = true)
|
|
||||||
public @NotNull Promise<Void> thenConsumerDelayedAsync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit) {
|
|
||||||
return thenConsumeDelayedAsync(task, delay, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task) {
|
T join(long interval, long timeout) throws TimeoutException;
|
||||||
return thenApplyAsync(result -> task.get(), Schedulers.getTrace(task));
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Promise<T> thenPopulateReference(@NotNull AtomicReference<T> reference) {
|
|
||||||
return thenApplyAsync((result) -> {
|
|
||||||
reference.set(result);
|
|
||||||
return result;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task) {
|
@NotNull Promise<Void> thenRunSync(@NotNull ExceptionalRunnable task);
|
||||||
return thenApplyAsync(task, Schedulers.getTrace(task));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
@NotNull Promise<Void> thenRunDelayedSync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit);
|
||||||
Promise<V> promise = new Promise<>();
|
|
||||||
addListener(ctx -> {
|
|
||||||
Runnable runnable = createRunnable(ctx, promise, task);
|
|
||||||
Schedulers.runDelayedAsync(runnable, delay, unit, trace);
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
@NotNull Promise<Void> thenConsumeSync(@NotNull ExceptionalConsumer<T> task);
|
||||||
}
|
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
@NotNull Promise<Void> thenConsumeDelayedSync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit);
|
||||||
return thenApplyDelayedAsync(task, delay, unit, Schedulers.getTrace(task));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
<V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task);
|
||||||
return this.thenComposeAsync(task);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
<V> @NotNull Promise<V> thenSupplyDelayedSync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
promise.complete(ctx1.getResult());
|
<V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task);
|
||||||
});
|
|
||||||
}).addListener(ctx2 -> {
|
|
||||||
if (ctx2.isError()) {
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
promise.completeExceptionally(ctx2.getException());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
<V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
||||||
}
|
|
||||||
|
|
||||||
private <V> @NotNull Runnable createRunnable(@NotNull PromiseCompletion<T> ctx, @NotNull Promise<V> promise, @NotNull ExceptionalFunction<T, V> task) {
|
<V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit);
|
||||||
return () -> {
|
|
||||||
if (ctx.isError()) {
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
promise.completeExceptionally(ctx.getException());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
<V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task);
|
||||||
V result = task.apply(ctx.getResult());
|
|
||||||
promise.complete(result);
|
|
||||||
} catch (Exception e) {
|
|
||||||
promise.completeExceptionally(e, true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Promise<T> logExceptions() {
|
@NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task);
|
||||||
return addListener(ctx -> {
|
|
||||||
if (ctx.isError()) {
|
|
||||||
LOGGER.error("Exception caught in promise pipeline", ctx.getException());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Promise<T> addListener(@NotNull PromiseListener<T> listener) {
|
@NotNull Promise<Void> thenRunDelayedAsync(@NotNull ExceptionalRunnable task, long delay, @NotNull TimeUnit unit);
|
||||||
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;
|
@NotNull Promise<Void> thenConsumeAsync(@NotNull ExceptionalConsumer<T> task);
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit) {
|
@NotNull Promise<Void> thenConsumeDelayedAsync(@NotNull ExceptionalConsumer<T> task, long delay, @NotNull TimeUnit unit);
|
||||||
Schedulers.runDelayedAsync(() -> {
|
|
||||||
if (!isCompleted()) {
|
|
||||||
completeExceptionally(new TimeoutException("Promise timed out after " + time + " " + unit), true);
|
|
||||||
}
|
|
||||||
}, time, unit);
|
|
||||||
|
|
||||||
return this;
|
<V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task);
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Promise<T> timeout(long ms) {
|
<V> @NotNull Promise<V> thenSupplyDelayedAsync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit);
|
||||||
return timeout(ms, TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void handleCompletion(@NotNull PromiseCompletion<T> ctx) {
|
@NotNull Promise<T> thenPopulateReference(@NotNull AtomicReference<T> reference);
|
||||||
if (this.isCompleted()) return;
|
|
||||||
setCompletion(ctx);
|
|
||||||
|
|
||||||
Schedulers.runAsync(() -> {
|
<V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task);
|
||||||
for (PromiseListener<T> listener : getListeners()) {
|
|
||||||
if (!ctx.isActive()) return;
|
|
||||||
|
|
||||||
try {
|
<V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
||||||
listener.handle(ctx);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.error("Exception caught in promise listener", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void complete(@Nullable T result) {
|
<V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit);
|
||||||
handleCompletion(new PromiseCompletion<>(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void completeExceptionally(@NotNull Throwable result, boolean appendStacktrace) {
|
<V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task);
|
||||||
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));
|
<V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task);
|
||||||
}
|
|
||||||
|
|
||||||
public void completeExceptionally(@NotNull Throwable result) {
|
@NotNull Promise<T> logExceptions();
|
||||||
completeExceptionally(result, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isCompleted() {
|
@NotNull Promise<T> addListener(@NotNull PromiseListener<T> listener);
|
||||||
return getCompletion() != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Collection<PromiseListener<T>> getListeners() {
|
@NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit);
|
||||||
return listeners;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @Nullable PromiseCompletion<T> getCompletion() {
|
@NotNull Promise<T> timeout(long ms);
|
||||||
return completion;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setCompletion(@NotNull PromiseCompletion<T> completion) {
|
void complete(@Nullable T result);
|
||||||
this.completion = completion;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> @NotNull Promise<T> resolve(T value) {
|
void completeExceptionally(@NotNull Throwable result, boolean appendStacktrace);
|
||||||
Promise<T> promise = new Promise<>();
|
|
||||||
promise.setCompletion(new PromiseCompletion<>(value));
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> @NotNull Promise<T> error(Throwable error) {
|
void completeExceptionally(@NotNull Throwable result);
|
||||||
Promise<T> promise = new Promise<>();
|
|
||||||
promise.completeExceptionally(error);
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> start() {
|
boolean isCompleted();
|
||||||
return Promise.resolve(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated // use resolve()
|
@Nullable PromiseCompletion<T> getCompletion();
|
||||||
public static <T> @NotNull Promise<T> start(T start) {
|
|
||||||
Promise<T> promise = new Promise<>();
|
|
||||||
promise.complete(start);
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 dev.tommyjs.futur.function.ExceptionalFunction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -13,8 +15,8 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
public class Promises {
|
public class Promises {
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
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 = new Promise<>();
|
Promise<Map.Entry<K, V>> promise = factory.unresolved();
|
||||||
p1.addListener(ctx -> {
|
p1.addListener(ctx -> {
|
||||||
if (ctx.isError()) {
|
if (ctx.isError()) {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
@@ -37,13 +39,15 @@ public class Promises {
|
|||||||
return promise;
|
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) {
|
public static <K, V> @NotNull Promise<Map.Entry<K, V>> combine(@NotNull Promise<K> p1, @NotNull Promise<V> p2) {
|
||||||
Map<K, V> map = new HashMap<>();
|
return combine(p1, p2, p1.getFactory());
|
||||||
if (promises.isEmpty()) return Promise.resolve(map);
|
}
|
||||||
|
|
||||||
|
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();
|
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()) {
|
for (Map.Entry<K, Promise<V>> entry : promises.entrySet()) {
|
||||||
entry.getValue().addListener((ctx) -> {
|
entry.getValue().addListener((ctx) -> {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
@@ -69,19 +73,35 @@ public class Promises {
|
|||||||
return promise.timeout(timeout);
|
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) {
|
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) {
|
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) {
|
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();
|
AtomicInteger index = new AtomicInteger();
|
||||||
return combine(
|
return combine(
|
||||||
promises.stream()
|
promises.stream()
|
||||||
@@ -96,18 +116,28 @@ public class Promises {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout) {
|
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout, boolean strict) {
|
||||||
return combine(promises, timeout, true);
|
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);
|
return combine(promises, 1500L, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NotNull Promise<Void> all(@NotNull List<Promise<?>> promises) {
|
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises) {
|
||||||
if (promises.isEmpty()) return Promise.start();
|
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) {
|
for (Promise<?> p : promises) {
|
||||||
p.addListener((ctx) -> {
|
p.addListener((ctx) -> {
|
||||||
if (ctx.isError()) {
|
if (ctx.isError()) {
|
||||||
@@ -121,30 +151,49 @@ public class Promises {
|
|||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NotNull Promise<Void> all(@NotNull Promise<?>... promises) {
|
public static @NotNull Promise<Void> all(@NotNull List<Promise<?>> promises) {
|
||||||
return all(Arrays.asList(promises));
|
PromiseFactory factory;
|
||||||
|
if (promises.isEmpty()) {
|
||||||
|
factory = UnpooledPromiseFactory.INSTANCE;
|
||||||
|
} else {
|
||||||
|
factory = promises.get(0).getFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper, long timeout, boolean strict) {
|
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, PromiseFactory factory) {
|
||||||
Map<K, Promise<V>> promises = new HashMap<>();
|
Map<K, Promise<V>> promises = new HashMap<>();
|
||||||
for (K key : keys) {
|
for (K key : keys) {
|
||||||
Promise<V> promise = Promise.resolve(key).thenApplyAsync(mapper);
|
Promise<V> promise = factory.resolve(key).thenApplyAsync(mapper);
|
||||||
promises.put(key, promise);
|
promises.put(key, promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
return combine(promises, timeout, strict);
|
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) {
|
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) {
|
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) {
|
public static @NotNull Promise<Void> erase(@NotNull Promise<?> p, PromiseFactory factory) {
|
||||||
Promise<Void> promise = new Promise<>();
|
Promise<Void> promise = factory.unresolved();
|
||||||
p.addListener(ctx -> {
|
p.addListener(ctx -> {
|
||||||
if (ctx.isError()) {
|
if (ctx.isError()) {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
@@ -157,8 +206,12 @@ public class Promises {
|
|||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> @NotNull Promise<T> wrap(@NotNull CompletableFuture<T> future) {
|
public static @NotNull Promise<Void> erase(@NotNull Promise<?> p) {
|
||||||
Promise<T> promise = new Promise<>();
|
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) -> {
|
future.whenComplete((result, e) -> {
|
||||||
if (e != null) {
|
if (e != null) {
|
||||||
promise.completeExceptionally(e);
|
promise.completeExceptionally(e);
|
||||||
@@ -170,4 +223,19 @@ public class Promises {
|
|||||||
return promise;
|
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);
|
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 () -> {
|
return () -> {
|
||||||
try {
|
try {
|
||||||
task.run();
|
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 dev.tommyjs.futur.trace.ExecutorTrace;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@@ -11,23 +10,23 @@ public class SingleExecutorScheduler implements Scheduler {
|
|||||||
|
|
||||||
private final ScheduledExecutorService service;
|
private final ScheduledExecutorService service;
|
||||||
|
|
||||||
protected SingleExecutorScheduler(ScheduledExecutorService service) {
|
public SingleExecutorScheduler(ScheduledExecutorService service) {
|
||||||
this.service = service;
|
this.service = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void runSync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
public void runSync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
||||||
service.submit(wrapExceptions(task, trace));
|
service.submit(Scheduler.wrapExceptions(task, trace));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void runDelayedSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
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
|
@Override
|
||||||
public void runRepeatingSync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
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
|
@Override
|
||||||
@@ -45,8 +44,4 @@ public class SingleExecutorScheduler implements Scheduler {
|
|||||||
runRepeatingSync(task, interval, unit, trace);
|
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;
|
package dev.tommyjs.futur.reactivestreams;
|
||||||
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
import dev.tommyjs.futur.promise.AbstractPromise;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
|
|
||||||
public class ReactiveTransformer {
|
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();
|
SingleAccumulatorSubscriber<T> subscriber = SingleAccumulatorSubscriber.create();
|
||||||
publisher.subscribe(subscriber);
|
publisher.subscribe(subscriber);
|
||||||
return subscriber.getPromise();
|
return subscriber.getPromise();
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
package dev.tommyjs.futur.reactivestreams;
|
package dev.tommyjs.futur.reactivestreams;
|
||||||
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
import dev.tommyjs.futur.promise.AbstractPromise;
|
||||||
import org.reactivestreams.Subscriber;
|
import org.reactivestreams.Subscriber;
|
||||||
import org.reactivestreams.Subscription;
|
import org.reactivestreams.Subscription;
|
||||||
|
|
||||||
public class SingleAccumulatorSubscriber<T> implements Subscriber<T> {
|
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;
|
this.promise = promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,16 +32,16 @@ public class SingleAccumulatorSubscriber<T> implements Subscriber<T> {
|
|||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
public Promise<T> getPromise() {
|
public AbstractPromise<T> getPromise() {
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> SingleAccumulatorSubscriber<T> create(Promise<T> promise) {
|
public static <T> SingleAccumulatorSubscriber<T> create(AbstractPromise<T> promise) {
|
||||||
return new SingleAccumulatorSubscriber<>(promise);
|
return new SingleAccumulatorSubscriber<>(promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> SingleAccumulatorSubscriber<T> create() {
|
public static <T> SingleAccumulatorSubscriber<T> create() {
|
||||||
return create(new Promise<>());
|
return create(new AbstractPromise<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package dev.tommyjs.futur.reactor;
|
package dev.tommyjs.futur.reactor;
|
||||||
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
import dev.tommyjs.futur.promise.AbstractPromise;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
@@ -11,14 +11,14 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
|
|
||||||
public class ReactorTransformer {
|
public class ReactorTransformer {
|
||||||
|
|
||||||
public static <T> @NotNull Promise<T> wrapMono(@NotNull Mono<T> mono) {
|
public static <T> @NotNull AbstractPromise<T> wrapMono(@NotNull Mono<T> mono) {
|
||||||
Promise<T> promise = new Promise<>();
|
AbstractPromise<T> promise = new AbstractPromise<>();
|
||||||
mono.doOnSuccess(promise::complete).doOnError(promise::completeExceptionally).subscribe();
|
mono.doOnSuccess(promise::complete).doOnError(promise::completeExceptionally).subscribe();
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> @NotNull Promise<@NotNull List<T>> wrapFlux(@NotNull Flux<T> flux) {
|
public static <T> @NotNull AbstractPromise<@NotNull List<T>> wrapFlux(@NotNull Flux<T> flux) {
|
||||||
Promise<List<T>> promise = new Promise<>();
|
AbstractPromise<List<T>> promise = new AbstractPromise<>();
|
||||||
AtomicReference<List<T>> out = new AtomicReference<>(new ArrayList<>());
|
AtomicReference<List<T>> out = new AtomicReference<>(new ArrayList<>());
|
||||||
|
|
||||||
flux.doOnNext(out.get()::add).subscribe();
|
flux.doOnNext(out.get()::add).subscribe();
|
||||||
|
|||||||
Reference in New Issue
Block a user