mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-18 07:16:45 +00:00
release 2.0.0
This commit is contained in:
@@ -6,7 +6,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "dev.tommyjs"
|
group = "dev.tommyjs"
|
||||||
version = "1.2.0"
|
version = "2.0.0"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package dev.tommyjs.futur.function;
|
package dev.tommyjs.futur.function;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
public interface ExceptionalConsumer<T> {
|
public interface ExceptionalConsumer<T> {
|
||||||
|
|
||||||
void accept(T value) throws Exception;
|
void accept(T value) throws Throwable;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package dev.tommyjs.futur.function;
|
package dev.tommyjs.futur.function;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
public interface ExceptionalFunction<K, V> {
|
public interface ExceptionalFunction<K, V> {
|
||||||
|
|
||||||
V apply(K value) throws Exception;
|
V apply(K value) throws Throwable;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package dev.tommyjs.futur.function;
|
package dev.tommyjs.futur.function;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
public interface ExceptionalRunnable {
|
public interface ExceptionalRunnable {
|
||||||
|
|
||||||
void run() throws Exception;
|
void run() throws Throwable;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package dev.tommyjs.futur.function;
|
package dev.tommyjs.futur.function;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
public interface ExceptionalSupplier<T> {
|
public interface ExceptionalSupplier<T> {
|
||||||
|
|
||||||
T get() throws Exception;
|
T get() throws Throwable;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,26 @@
|
|||||||
package dev.tommyjs.futur.standalone;
|
package dev.tommyjs.futur.impl;
|
||||||
|
|
||||||
import dev.tommyjs.futur.promise.AbstractPromise;
|
import dev.tommyjs.futur.promise.AbstractPromise;
|
||||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
import dev.tommyjs.futur.scheduler.Scheduler;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
public class PooledPromise<T> extends AbstractPromise<T> {
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
private final Scheduler scheduler;
|
public class SimplePromise<T> extends AbstractPromise<T> {
|
||||||
|
|
||||||
|
private final ScheduledExecutorService executor;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private final PromiseFactory factory;
|
private final PromiseFactory factory;
|
||||||
|
|
||||||
public PooledPromise(Scheduler scheduler, Logger logger, PromiseFactory factory) {
|
public SimplePromise(ScheduledExecutorService executor, Logger logger, PromiseFactory factory) {
|
||||||
this.scheduler = scheduler;
|
this.executor = executor;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.factory = factory;
|
this.factory = factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Scheduler getScheduler() {
|
protected ScheduledExecutorService getExecutor() {
|
||||||
return scheduler;
|
return executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -1,44 +1,40 @@
|
|||||||
package dev.tommyjs.futur.standalone;
|
package dev.tommyjs.futur.impl;
|
||||||
|
|
||||||
import dev.tommyjs.futur.promise.AbstractPromise;
|
import dev.tommyjs.futur.promise.AbstractPromise;
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
import dev.tommyjs.futur.scheduler.Scheduler;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
public class PooledPromiseFactory implements PromiseFactory {
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
private final Scheduler scheduler;
|
public class SimplePromiseFactory implements PromiseFactory {
|
||||||
|
|
||||||
|
private final ScheduledExecutorService executor;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
|
||||||
public PooledPromiseFactory(Scheduler scheduler, Logger logger) {
|
public SimplePromiseFactory(ScheduledExecutorService executor, Logger logger) {
|
||||||
this.scheduler = scheduler;
|
this.executor = executor;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull <T> Promise<T> resolve(T value) {
|
public @NotNull <T> Promise<T> resolve(T value) {
|
||||||
AbstractPromise<T> promise = new PooledPromise<>(scheduler, logger, this);
|
AbstractPromise<T> promise = new SimplePromise<>(executor, logger, this);
|
||||||
promise.complete(value);
|
promise.complete(value);
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull <T> Promise<T> unresolved() {
|
public @NotNull <T> Promise<T> unresolved() {
|
||||||
return new PooledPromise<>(scheduler, logger, this);
|
return new SimplePromise<>(executor, logger, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull <T> Promise<T> error(Throwable error) {
|
public @NotNull <T> Promise<T> error(Throwable error) {
|
||||||
AbstractPromise<T> promise = new PooledPromise<>(scheduler, logger, this);
|
AbstractPromise<T> promise = new SimplePromise<>(executor, logger, this);
|
||||||
promise.completeExceptionally(error);
|
promise.completeExceptionally(error);
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Promise<Void> start() {
|
|
||||||
return resolve(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,52 +4,29 @@ 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.Scheduler;
|
|
||||||
import dev.tommyjs.futur.trace.ExecutorTrace;
|
|
||||||
import dev.tommyjs.futur.trace.TraceUtil;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
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 abstract class AbstractPromise<T> implements Promise<T> {
|
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 Collection<PromiseListener<T>> listeners;
|
||||||
private final StackTraceElement[] stackTrace;
|
|
||||||
|
|
||||||
private @Nullable PromiseCompletion<T> completion;
|
private @Nullable PromiseCompletion<T> completion;
|
||||||
|
|
||||||
public AbstractPromise() {
|
public AbstractPromise() {
|
||||||
this.listeners = new ConcurrentLinkedQueue<>();
|
this.listeners = new ConcurrentLinkedQueue<>();
|
||||||
this.completion = null;
|
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 ScheduledExecutorService getExecutor();
|
||||||
|
|
||||||
protected abstract Logger getLogger();
|
protected abstract Logger getLogger();
|
||||||
|
|
||||||
@@ -84,7 +61,7 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
return thenApplySync(result -> {
|
return thenApplySync(result -> {
|
||||||
task.run();
|
task.run();
|
||||||
return null;
|
return null;
|
||||||
}, TraceUtil.getTrace(task));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -92,7 +69,7 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
return thenApplyDelayedSync(result -> {
|
return thenApplyDelayedSync(result -> {
|
||||||
task.run();
|
task.run();
|
||||||
return null;
|
return null;
|
||||||
}, delay, unit, TraceUtil.getTrace(task));
|
}, delay, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -100,7 +77,7 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
return thenApplySync(result -> {
|
return thenApplySync(result -> {
|
||||||
task.accept(result);
|
task.accept(result);
|
||||||
return null;
|
return null;
|
||||||
}, TraceUtil.getTrace(task));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -108,42 +85,21 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
return thenApplyDelayedSync(result -> {
|
return thenApplyDelayedSync(result -> {
|
||||||
task.accept(result);
|
task.accept(result);
|
||||||
return null;
|
return null;
|
||||||
}, delay, unit, TraceUtil.getTrace(task));
|
}, delay, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task) {
|
public <V> @NotNull Promise<V> thenSupplySync(@NotNull ExceptionalSupplier<V> task) {
|
||||||
return thenApplySync(result -> task.get(), TraceUtil.getTrace(task));
|
return thenApplySync(result -> task.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenSupplyDelayedSync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
public <V> @NotNull Promise<V> thenSupplyDelayedSync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
||||||
return thenApplyDelayedSync(result -> task.get(), delay, unit, TraceUtil.getTrace(task));
|
return thenApplyDelayedSync(result -> task.get(), delay, unit);
|
||||||
}
|
|
||||||
|
|
||||||
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
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task) {
|
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();
|
Promise<V> promise = getFactory().unresolved();
|
||||||
addListener(ctx -> {
|
addListener(ctx -> {
|
||||||
if (ctx.isError()) {
|
if (ctx.isError()) {
|
||||||
@@ -153,7 +109,7 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Runnable runnable = createRunnable(ctx, promise, task);
|
Runnable runnable = createRunnable(ctx, promise, task);
|
||||||
getScheduler().runDelayedSync(runnable, delay, unit, trace);
|
getExecutor().submit(runnable);
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
@@ -161,13 +117,25 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
public <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
||||||
return thenApplyDelayedSync(task, delay, unit, TraceUtil.getTrace(task));
|
Promise<V> promise = getFactory().unresolved();
|
||||||
|
addListener(ctx -> {
|
||||||
|
if (ctx.isError()) {
|
||||||
|
//noinspection ConstantConditions
|
||||||
|
promise.completeExceptionally(ctx.getException());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Runnable runnable = createRunnable(ctx, promise, task);
|
||||||
|
getExecutor().schedule(runnable, delay, unit);
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task) {
|
public <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
Promise<V> promise = getFactory().unresolved();
|
||||||
thenApplySync(task, TraceUtil.getTrace(task)).thenConsumeAsync(nestedPromise -> {
|
thenApplySync(task).thenConsumeAsync(nestedPromise -> {
|
||||||
nestedPromise.addListener(ctx1 -> {
|
nestedPromise.addListener(ctx1 -> {
|
||||||
if (ctx1.isError()) {
|
if (ctx1.isError()) {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
@@ -192,7 +160,7 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
return thenApplyAsync(result -> {
|
return thenApplyAsync(result -> {
|
||||||
task.run();
|
task.run();
|
||||||
return null;
|
return null;
|
||||||
}, TraceUtil.getTrace(task));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -200,7 +168,7 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
return thenApplyDelayedAsync(result -> {
|
return thenApplyDelayedAsync(result -> {
|
||||||
task.run();
|
task.run();
|
||||||
return null;
|
return null;
|
||||||
}, delay, unit, TraceUtil.getTrace(task));
|
}, delay, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -208,7 +176,7 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
return thenApplyAsync(result -> {
|
return thenApplyAsync(result -> {
|
||||||
task.accept(result);
|
task.accept(result);
|
||||||
return null;
|
return null;
|
||||||
}, TraceUtil.getTrace(task));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -216,17 +184,17 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
return thenApplyDelayedAsync(result -> {
|
return thenApplyDelayedAsync(result -> {
|
||||||
task.accept(result);
|
task.accept(result);
|
||||||
return null;
|
return null;
|
||||||
}, delay, unit, TraceUtil.getTrace(task));
|
}, delay, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task) {
|
public <V> @NotNull Promise<V> thenSupplyAsync(@NotNull ExceptionalSupplier<V> task) {
|
||||||
return thenApplyAsync(result -> task.get(), TraceUtil.getTrace(task));
|
return thenApplyAsync(result -> task.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenSupplyDelayedAsync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
public <V> @NotNull Promise<V> thenSupplyDelayedAsync(@NotNull ExceptionalSupplier<V> task, long delay, @NotNull TimeUnit unit) {
|
||||||
return thenApplyDelayedAsync(result -> task.get(), delay, unit, TraceUtil.getTrace(task));
|
return thenApplyDelayedAsync(result -> task.get(), delay, unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -237,7 +205,8 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task, @NotNull ExecutorTrace trace) {
|
@Override
|
||||||
|
public <V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
Promise<V> promise = getFactory().unresolved();
|
||||||
addListener(ctx -> {
|
addListener(ctx -> {
|
||||||
if (ctx.isError()) {
|
if (ctx.isError()) {
|
||||||
@@ -247,23 +216,7 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Runnable runnable = createRunnable(ctx, promise, task);
|
Runnable runnable = createRunnable(ctx, promise, task);
|
||||||
getScheduler().runAsync(runnable, trace);
|
getExecutor().submit(runnable);
|
||||||
});
|
|
||||||
|
|
||||||
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;
|
return promise;
|
||||||
@@ -271,18 +224,19 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
public <V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit) {
|
||||||
return thenApplyDelayedAsync(task, delay, unit, TraceUtil.getTrace(task));
|
Promise<V> promise = getFactory().unresolved();
|
||||||
}
|
addListener(ctx -> {
|
||||||
|
Runnable runnable = createRunnable(ctx, promise, task);
|
||||||
|
getExecutor().schedule(runnable, delay, unit);
|
||||||
|
});
|
||||||
|
|
||||||
@Override
|
return promise;
|
||||||
public <V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
|
||||||
return this.thenComposeAsync(task);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
public <V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
||||||
Promise<V> promise = getFactory().unresolved();
|
Promise<V> promise = getFactory().unresolved();
|
||||||
thenApplyAsync(task, TraceUtil.getTrace(task)).thenConsumeAsync(nestedPromise -> {
|
thenApplyAsync(task).thenConsumeAsync(nestedPromise -> {
|
||||||
nestedPromise.addListener(ctx1 -> {
|
nestedPromise.addListener(ctx1 -> {
|
||||||
if (ctx1.isError()) {
|
if (ctx1.isError()) {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
@@ -313,8 +267,8 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
try {
|
try {
|
||||||
V result = task.apply(ctx.getResult());
|
V result = task.apply(ctx.getResult());
|
||||||
promise.complete(result);
|
promise.complete(result);
|
||||||
} catch (Exception e) {
|
} catch (Throwable e) {
|
||||||
promise.completeExceptionally(e, true);
|
promise.completeExceptionally(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -331,14 +285,14 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> addListener(@NotNull PromiseListener<T> listener) {
|
public @NotNull Promise<T> addListener(@NotNull PromiseListener<T> listener) {
|
||||||
if (isCompleted()) {
|
if (isCompleted()) {
|
||||||
getScheduler().runAsync(() -> {
|
getExecutor().submit(() -> {
|
||||||
try {
|
try {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
listener.handle(getCompletion());
|
listener.handle(getCompletion());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
getLogger().error("Exception caught in promise listener", e);
|
getLogger().error("Exception caught in promise listener", e);
|
||||||
}
|
}
|
||||||
}, TraceUtil.getTrace(listener));
|
});
|
||||||
} else {
|
} else {
|
||||||
getListeners().add(listener);
|
getListeners().add(listener);
|
||||||
}
|
}
|
||||||
@@ -348,13 +302,11 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit) {
|
public @NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit) {
|
||||||
Runnable func = () -> {
|
getExecutor().schedule(() -> {
|
||||||
if (!isCompleted()) {
|
if (!isCompleted()) {
|
||||||
completeExceptionally(new TimeoutException("Promise timed out after " + time + " " + unit), true);
|
completeExceptionally(new TimeoutException("Promise timed out after " + time + " " + unit));
|
||||||
}
|
}
|
||||||
};
|
}, time, unit);
|
||||||
|
|
||||||
getScheduler().runDelayedAsync(func, time, unit, TraceUtil.getTrace(func));
|
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -368,19 +320,18 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
if (this.isCompleted()) return;
|
if (this.isCompleted()) return;
|
||||||
setCompletion(ctx);
|
setCompletion(ctx);
|
||||||
|
|
||||||
Runnable func = () -> {
|
getExecutor().submit(() -> {
|
||||||
for (PromiseListener<T> listener : getListeners()) {
|
for (PromiseListener<T> listener : getListeners()) {
|
||||||
if (!ctx.isActive()) return;
|
if (!ctx.isActive()) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
listener.handle(ctx);
|
listener.handle(ctx);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
getLogger().error("Exception caught in promise listener", e);
|
getLogger().error("Exception caught in promise listener", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
getScheduler().runAsync(func, TraceUtil.getTrace(func));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -388,23 +339,9 @@ public abstract class AbstractPromise<T> implements Promise<T> {
|
|||||||
handleCompletion(new PromiseCompletion<>(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
|
@Override
|
||||||
public void completeExceptionally(@NotNull Throwable result) {
|
public void completeExceptionally(@NotNull Throwable result) {
|
||||||
completeExceptionally(result, false);
|
handleCompletion(new PromiseCompletion<>(result));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ 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.trace.ExecutorTrace;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -22,25 +21,12 @@ public interface Promise<T> {
|
|||||||
return factory.error(error);
|
return factory.error(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static <T> @NotNull Promise<T> unresolved(PromiseFactory factory) {
|
||||||
|
return factory.unresolved();
|
||||||
|
}
|
||||||
|
|
||||||
static @NotNull Promise<Void> start(PromiseFactory factory) {
|
static @NotNull Promise<Void> start(PromiseFactory factory) {
|
||||||
return factory.start();
|
return factory.resolve(null);
|
||||||
}
|
|
||||||
|
|
||||||
static <T> @NotNull Promise<T> resolve(T value) {
|
|
||||||
return resolve(value, UnpooledPromiseFactory.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static <T> @NotNull Promise<T> error(Throwable error) {
|
|
||||||
return error(error, UnpooledPromiseFactory.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static @NotNull Promise<Void> start() {
|
|
||||||
return start(UnpooledPromiseFactory.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
static <T> @NotNull Promise<T> start(T start) {
|
|
||||||
return resolve(start);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PromiseFactory getFactory();
|
PromiseFactory getFactory();
|
||||||
@@ -61,8 +47,6 @@ public interface Promise<T> {
|
|||||||
|
|
||||||
<V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task);
|
<V> @NotNull Promise<V> thenApplySync(@NotNull ExceptionalFunction<T, V> task);
|
||||||
|
|
||||||
<V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
|
||||||
|
|
||||||
<V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit);
|
<V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit);
|
||||||
|
|
||||||
<V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task);
|
<V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task);
|
||||||
@@ -83,12 +67,8 @@ public interface Promise<T> {
|
|||||||
|
|
||||||
<V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task);
|
<V> @NotNull Promise<V> thenApplyAsync(@NotNull ExceptionalFunction<T, V> task);
|
||||||
|
|
||||||
<V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
|
||||||
|
|
||||||
<V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit);
|
<V> @NotNull Promise<V> thenApplyDelayedAsync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit);
|
||||||
|
|
||||||
<V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task);
|
|
||||||
|
|
||||||
<V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task);
|
<V> @NotNull Promise<V> thenComposeAsync(@NotNull ExceptionalFunction<T, Promise<V>> task);
|
||||||
|
|
||||||
@NotNull Promise<T> logExceptions();
|
@NotNull Promise<T> logExceptions();
|
||||||
@@ -101,8 +81,6 @@ public interface Promise<T> {
|
|||||||
|
|
||||||
void complete(@Nullable T result);
|
void complete(@Nullable T result);
|
||||||
|
|
||||||
void completeExceptionally(@NotNull Throwable result, boolean appendStacktrace);
|
|
||||||
|
|
||||||
void completeExceptionally(@NotNull Throwable result);
|
void completeExceptionally(@NotNull Throwable result);
|
||||||
|
|
||||||
boolean isCompleted();
|
boolean isCompleted();
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
package dev.tommyjs.futur.promise;
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
|
import dev.tommyjs.futur.impl.SimplePromiseFactory;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
public interface PromiseFactory {
|
public interface PromiseFactory {
|
||||||
|
|
||||||
@@ -10,6 +16,20 @@ public interface PromiseFactory {
|
|||||||
|
|
||||||
<T> @NotNull Promise<T> error(Throwable error);
|
<T> @NotNull Promise<T> error(Throwable error);
|
||||||
|
|
||||||
@NotNull Promise<Void> start();
|
static PromiseFactory create(ScheduledExecutorService executor, Logger logger) {
|
||||||
|
return new SimplePromiseFactory(executor, logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
static PromiseFactory create(ScheduledExecutorService executor) {
|
||||||
|
return create(executor, LoggerFactory.getLogger(SimplePromiseFactory.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
static PromiseFactory create(int threadPoolSize) {
|
||||||
|
return create(Executors.newScheduledThreadPool(threadPoolSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
static PromiseFactory create() {
|
||||||
|
return create(Runtime.getRuntime().availableProcessors());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,41 +71,23 @@ 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) {
|
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);
|
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, obtainFactory(promises.values()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, long timeout, PromiseFactory factory) {
|
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);
|
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, obtainFactory(promises.values()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, PromiseFactory factory) {
|
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Map<K, Promise<V>> promises, PromiseFactory factory) {
|
||||||
return combine(promises, 1500L, true, 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, obtainFactory(promises.values()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout, boolean strict, PromiseFactory factory) {
|
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().collect(Collectors.toMap(s -> index.getAndIncrement(), v -> v)),
|
||||||
.collect(Collectors.toMap(s -> index.getAndIncrement(), v -> v)),
|
timeout, strict, factory
|
||||||
timeout,
|
|
||||||
strict
|
|
||||||
).thenApplySync(v ->
|
).thenApplySync(v ->
|
||||||
v.entrySet().stream()
|
v.entrySet().stream()
|
||||||
.sorted(Map.Entry.comparingByKey())
|
.sorted(Map.Entry.comparingByKey())
|
||||||
@@ -114,24 +96,12 @@ public class Promises {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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, long timeout, PromiseFactory factory) {
|
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises, long timeout, PromiseFactory factory) {
|
||||||
return combine(promises, timeout, true, 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) {
|
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, factory);
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> @NotNull Promise<List<V>> combine(@NotNull List<Promise<V>> promises) {
|
|
||||||
return combine(promises, obtainFactory(promises));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NotNull Promise<Void> all(@NotNull List<Promise<?>> promises, PromiseFactory factory) {
|
public static @NotNull Promise<Void> all(@NotNull List<Promise<?>> promises, PromiseFactory factory) {
|
||||||
@@ -149,17 +119,6 @@ public class Promises {
|
|||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
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, PromiseFactory 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) {
|
||||||
@@ -167,29 +126,17 @@ public class Promises {
|
|||||||
promises.put(key, promise);
|
promises.put(key, promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
return combine(promises, timeout, strict);
|
return combine(promises, timeout, strict, factory);
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper, 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) {
|
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);
|
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, UnpooledPromiseFactory.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper, PromiseFactory factory) {
|
public static <K, V> @NotNull Promise<Map<K, V>> combine(@NotNull Collection<K> keys, @NotNull ExceptionalFunction<K, V> mapper, PromiseFactory factory) {
|
||||||
return combine(keys, mapper, 1500L, true, factory);
|
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, UnpooledPromiseFactory.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static @NotNull Promise<Void> erase(@NotNull Promise<?> p, PromiseFactory factory) {
|
public static @NotNull Promise<Void> erase(@NotNull Promise<?> p, PromiseFactory factory) {
|
||||||
Promise<Void> promise = factory.unresolved();
|
Promise<Void> promise = factory.unresolved();
|
||||||
p.addListener(ctx -> {
|
p.addListener(ctx -> {
|
||||||
@@ -221,19 +168,4 @@ 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,24 @@
|
|||||||
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
|
public class StaticPromise<T> extends AbstractPromise<T> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ScheduledExecutorService getExecutor() {
|
||||||
|
return StaticPromiseFactory.EXECUTOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Logger getLogger() {
|
||||||
|
return StaticPromiseFactory.LOGGER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PromiseFactory getFactory() {
|
||||||
|
return StaticPromiseFactory.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package dev.tommyjs.futur.promise;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
|
public class StaticPromiseFactory implements PromiseFactory {
|
||||||
|
|
||||||
|
public static final @NotNull PromiseFactory INSTANCE;
|
||||||
|
public static final @NotNull ScheduledExecutorService EXECUTOR;
|
||||||
|
public static final @NotNull Logger LOGGER;
|
||||||
|
|
||||||
|
static {
|
||||||
|
INSTANCE = new StaticPromiseFactory();
|
||||||
|
EXECUTOR = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
LOGGER = LoggerFactory.getLogger(StaticPromiseFactory.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private StaticPromiseFactory() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull <T> Promise<T> resolve(T value) {
|
||||||
|
AbstractPromise<T> promise = new StaticPromise<>();
|
||||||
|
promise.setCompletion(new PromiseCompletion<>(value));
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull <T> Promise<T> unresolved() {
|
||||||
|
return new StaticPromise<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull <T> Promise<T> error(Throwable error) {
|
||||||
|
AbstractPromise<T> promise = new StaticPromise<>();
|
||||||
|
promise.completeExceptionally(error);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package dev.tommyjs.futur.scheduler;
|
|
||||||
|
|
||||||
import dev.tommyjs.futur.trace.ExecutorTrace;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
public interface Scheduler {
|
|
||||||
|
|
||||||
Logger LOGGER = LoggerFactory.getLogger(Scheduler.class);
|
|
||||||
|
|
||||||
void runSync(@NotNull Runnable task, @NotNull ExecutorTrace trace);
|
|
||||||
|
|
||||||
void runDelayedSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
|
||||||
|
|
||||||
void runRepeatingSync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
|
||||||
|
|
||||||
void runAsync(@NotNull Runnable task, @NotNull ExecutorTrace trace);
|
|
||||||
|
|
||||||
void runDelayedAsync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
|
||||||
|
|
||||||
void runRepeatingAsync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace);
|
|
||||||
|
|
||||||
static @NotNull Runnable wrapExceptions(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
|
||||||
return () -> {
|
|
||||||
try {
|
|
||||||
task.run();
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.error("Exception in scheduled task: {}", e.getClass().getName());
|
|
||||||
LOGGER.error(trace.toString());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
package dev.tommyjs.futur.scheduler;
|
|
||||||
|
|
||||||
import dev.tommyjs.futur.trace.ExecutorTrace;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
public class SingleExecutorScheduler implements Scheduler {
|
|
||||||
|
|
||||||
private final ScheduledExecutorService service;
|
|
||||||
|
|
||||||
public SingleExecutorScheduler(ScheduledExecutorService service) {
|
|
||||||
this.service = service;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runSync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
|
||||||
service.submit(Scheduler.wrapExceptions(task, trace));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runDelayedSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
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(Scheduler.wrapExceptions(task, trace), 0L, interval, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runAsync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
|
||||||
runSync(task, trace);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runDelayedAsync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
runDelayedSync(task, delay, unit, trace);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runRepeatingAsync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
runRepeatingSync(task, interval, unit, trace);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
package dev.tommyjs.futur.trace;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class ExecutorTrace {
|
|
||||||
|
|
||||||
private final @NotNull Class<?> clazz;
|
|
||||||
private final @NotNull StackTraceElement[] trace;
|
|
||||||
|
|
||||||
public ExecutorTrace(@NotNull Class<?> clazz, @NotNull StackTraceElement[] trace) {
|
|
||||||
this.clazz = clazz;
|
|
||||||
this.trace = trace;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Class<?> getClazz() {
|
|
||||||
return clazz;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull StackTraceElement[] getTrace() {
|
|
||||||
return trace;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return Arrays.stream(trace).map(StackTraceElement::toString).collect(Collectors.joining("\n"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "dev.tommyjs"
|
group = "dev.tommyjs"
|
||||||
version = "1.2.0"
|
version = "2.0.0"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package dev.tommyjs.futur.reactivestreams;
|
|||||||
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
import dev.tommyjs.futur.promise.UnpooledPromiseFactory;
|
import dev.tommyjs.futur.promise.StaticPromiseFactory;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
|
|
||||||
@@ -14,8 +14,4 @@ public class ReactiveTransformer {
|
|||||||
return subscriber.getPromise();
|
return subscriber.getPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> @NotNull Promise<T> wrapPublisher(@NotNull Publisher<T> publisher) {
|
|
||||||
return wrapPublisher(publisher, UnpooledPromiseFactory.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package dev.tommyjs.futur.reactivestreams;
|
|||||||
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
import dev.tommyjs.futur.promise.UnpooledPromiseFactory;
|
import dev.tommyjs.futur.promise.StaticPromiseFactory;
|
||||||
import org.reactivestreams.Subscriber;
|
import org.reactivestreams.Subscriber;
|
||||||
import org.reactivestreams.Subscription;
|
import org.reactivestreams.Subscription;
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ public class SingleAccumulatorSubscriber<T> implements Subscriber<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T> SingleAccumulatorSubscriber<T> create() {
|
public static <T> SingleAccumulatorSubscriber<T> create() {
|
||||||
return create(UnpooledPromiseFactory.INSTANCE);
|
return create(StaticPromiseFactory.INSTANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "dev.tommyjs"
|
group = "dev.tommyjs"
|
||||||
version = "1.2.0"
|
version = "2.0.0"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package dev.tommyjs.futur.reactor;
|
|||||||
|
|
||||||
import dev.tommyjs.futur.promise.Promise;
|
import dev.tommyjs.futur.promise.Promise;
|
||||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||||
import dev.tommyjs.futur.promise.UnpooledPromiseFactory;
|
import dev.tommyjs.futur.promise.StaticPromiseFactory;
|
||||||
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;
|
||||||
@@ -19,10 +19,6 @@ public class ReactorTransformer {
|
|||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> @NotNull Promise<T> wrapMono(@NotNull Mono<T> mono) {
|
|
||||||
return wrapMono(mono, UnpooledPromiseFactory.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> @NotNull Promise<@NotNull List<T>> wrapFlux(@NotNull Flux<T> flux, PromiseFactory factory) {
|
public static <T> @NotNull Promise<@NotNull List<T>> wrapFlux(@NotNull Flux<T> flux, PromiseFactory factory) {
|
||||||
Promise<List<T>> promise = factory.unresolved();
|
Promise<List<T>> promise = factory.unresolved();
|
||||||
AtomicReference<List<T>> out = new AtomicReference<>(new ArrayList<>());
|
AtomicReference<List<T>> out = new AtomicReference<>(new ArrayList<>());
|
||||||
@@ -34,8 +30,4 @@ public class ReactorTransformer {
|
|||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> @NotNull Promise<@NotNull List<T>> wrapFlux(@NotNull Flux<T> flux) {
|
|
||||||
return wrapFlux(flux, UnpooledPromiseFactory.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
42
futur-standalone/.gitignore
vendored
42
futur-standalone/.gitignore
vendored
@@ -1,42 +0,0 @@
|
|||||||
.gradle
|
|
||||||
build/
|
|
||||||
!gradle/wrapper/gradle-wrapper.jar
|
|
||||||
!**/src/main/**/build/
|
|
||||||
!**/src/test/**/build/
|
|
||||||
|
|
||||||
### IntelliJ IDEA ###
|
|
||||||
.idea/modules.xml
|
|
||||||
.idea/jarRepositories.xml
|
|
||||||
.idea/compiler.xml
|
|
||||||
.idea/libraries/
|
|
||||||
*.iws
|
|
||||||
*.iml
|
|
||||||
*.ipr
|
|
||||||
out/
|
|
||||||
!**/src/main/**/out/
|
|
||||||
!**/src/test/**/out/
|
|
||||||
|
|
||||||
### Eclipse ###
|
|
||||||
.apt_generated
|
|
||||||
.classpath
|
|
||||||
.factorypath
|
|
||||||
.project
|
|
||||||
.settings
|
|
||||||
.springBeans
|
|
||||||
.sts4-cache
|
|
||||||
bin/
|
|
||||||
!**/src/main/**/bin/
|
|
||||||
!**/src/test/**/bin/
|
|
||||||
|
|
||||||
### NetBeans ###
|
|
||||||
/nbproject/private/
|
|
||||||
/nbbuild/
|
|
||||||
/dist/
|
|
||||||
/nbdist/
|
|
||||||
/.nb-gradle/
|
|
||||||
|
|
||||||
### VS Code ###
|
|
||||||
.vscode/
|
|
||||||
|
|
||||||
### Mac OS ###
|
|
||||||
.DS_Store
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
|
||||||
|
|
||||||
plugins {
|
|
||||||
id("java")
|
|
||||||
id("com.github.johnrengelman.shadow") version "7.1.2"
|
|
||||||
}
|
|
||||||
|
|
||||||
group = "dev.tommyjs"
|
|
||||||
version = "1.2.0"
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation("org.jetbrains:annotations:24.1.0")
|
|
||||||
implementation("org.slf4j:slf4j-api:2.0.9")
|
|
||||||
compileOnly(project(mapOf("path" to ":futur-api")))
|
|
||||||
testImplementation(platform("org.junit:junit-bom:5.9.1"))
|
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter")
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks {
|
|
||||||
build {
|
|
||||||
dependsOn(shadowJar)
|
|
||||||
}
|
|
||||||
|
|
||||||
withType<ShadowJar> {
|
|
||||||
exclude("META-INF/**")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.test {
|
|
||||||
useJUnitPlatform()
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
package dev.tommyjs.futur.standalone;
|
|
||||||
|
|
||||||
import dev.tommyjs.futur.scheduler.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;
|
|
||||||
|
|
||||||
public class ExclusiveThreadPoolScheduler implements Scheduler {
|
|
||||||
|
|
||||||
private final ScheduledExecutorService executor;
|
|
||||||
|
|
||||||
protected ExclusiveThreadPoolScheduler(ScheduledExecutorService executor) {
|
|
||||||
this.executor = executor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runSync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
|
||||||
throw new UnsupportedOperationException("Sync task invoked on asynchronous environment");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runDelayedSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
throw new UnsupportedOperationException("Sync task invoked on asynchronous environment");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runRepeatingSync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
throw new UnsupportedOperationException("Sync task invoked on asynchronous environment");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runAsync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
|
||||||
executor.submit(Scheduler.wrapExceptions(task, trace));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runDelayedAsync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
executor.schedule(Scheduler.wrapExceptions(task, trace), delay, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runRepeatingAsync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
executor.scheduleAtFixedRate(Scheduler.wrapExceptions(task, trace), 0L, interval, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ScheduledExecutorService getExecutor() {
|
|
||||||
return executor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ExclusiveThreadPoolScheduler create(ScheduledExecutorService executor) {
|
|
||||||
return new ExclusiveThreadPoolScheduler(executor);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ExclusiveThreadPoolScheduler create(int nThreads) {
|
|
||||||
return create(Executors.newScheduledThreadPool(nThreads));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
package dev.tommyjs.futur.standalone;
|
|
||||||
|
|
||||||
import dev.tommyjs.futur.scheduler.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;
|
|
||||||
|
|
||||||
public class ThreadPoolScheduler implements Scheduler {
|
|
||||||
|
|
||||||
private final ScheduledExecutorService syncExecutor;
|
|
||||||
private final ScheduledExecutorService asyncExecutor;
|
|
||||||
|
|
||||||
protected ThreadPoolScheduler(ScheduledExecutorService syncExecutor, ScheduledExecutorService asyncExecutor) {
|
|
||||||
this.syncExecutor = syncExecutor;
|
|
||||||
this.asyncExecutor = asyncExecutor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runSync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
|
||||||
syncExecutor.submit(Scheduler.wrapExceptions(task, trace));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runDelayedSync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
syncExecutor.schedule(Scheduler.wrapExceptions(task, trace), delay, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runRepeatingSync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
syncExecutor.scheduleAtFixedRate(Scheduler.wrapExceptions(task, trace), 0L, interval, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runAsync(@NotNull Runnable task, @NotNull ExecutorTrace trace) {
|
|
||||||
asyncExecutor.submit(Scheduler.wrapExceptions(task, trace));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runDelayedAsync(@NotNull Runnable task, long delay, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
asyncExecutor.schedule(Scheduler.wrapExceptions(task, trace), delay, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runRepeatingAsync(@NotNull Runnable task, long interval, @NotNull TimeUnit unit, @NotNull ExecutorTrace trace) {
|
|
||||||
asyncExecutor.scheduleAtFixedRate(Scheduler.wrapExceptions(task, trace), 0L, interval, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ScheduledExecutorService getSyncExecutor() {
|
|
||||||
return syncExecutor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ScheduledExecutorService getAsyncExecutor() {
|
|
||||||
return asyncExecutor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ThreadPoolScheduler create(int nThreads) {
|
|
||||||
return new ThreadPoolScheduler(Executors.newSingleThreadScheduledExecutor(), Executors.newScheduledThreadPool(nThreads));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user