mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-17 23:16:01 +00:00
cleanup AbstractPromise (still messy)
This commit is contained in:
@@ -12,6 +12,8 @@ import org.slf4j.Logger;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
||||
|
||||
@@ -31,17 +33,40 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
||||
}
|
||||
}
|
||||
|
||||
protected void runCompleter(@NotNull CompletablePromise<?> promise, @NotNull ExceptionalRunnable completer) {
|
||||
protected <V> V supplySafe(@NotNull ExceptionalSupplier<V> supplier, @NotNull Function<Throwable, V> handler) {
|
||||
try {
|
||||
completer.run();
|
||||
} catch (Error e) {
|
||||
promise.completeExceptionally(e);
|
||||
throw e;
|
||||
return supplier.get();
|
||||
} catch (Error error) {
|
||||
// Rethrow error so the Thread can shut down
|
||||
throw error;
|
||||
} catch (Throwable e) {
|
||||
promise.completeExceptionally(e);
|
||||
return handler.apply(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void runSafe(@NotNull ExceptionalRunnable runnable, @NotNull Consumer<Throwable> handler) {
|
||||
try {
|
||||
runnable.run();
|
||||
} catch (Error error) {
|
||||
handler.accept(error);
|
||||
// Rethrow error so the Thread can shut down
|
||||
throw error;
|
||||
} catch (Throwable e) {
|
||||
handler.accept(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void runCompleter(@NotNull CompletablePromise<?> promise, @NotNull ExceptionalRunnable completer) {
|
||||
runSafe(completer, promise::completeExceptionally);
|
||||
}
|
||||
|
||||
protected <V> V useCompletion(Supplier<V> unresolved, Function<T, V> completed, Function<Throwable, V> failed) {
|
||||
PromiseCompletion<T> completion = getCompletion();
|
||||
if (completion == null) return unresolved.get();
|
||||
else if (completion.isSuccess()) return completed.apply(completion.getResult());
|
||||
else return failed.apply(completion.getException());
|
||||
}
|
||||
|
||||
protected <V> @NotNull Runnable createCompleter(T result, @NotNull CompletablePromise<V> promise,
|
||||
@NotNull ExceptionalFunction<T, V> completer) {
|
||||
return () -> {
|
||||
@@ -67,14 +92,7 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
||||
}
|
||||
|
||||
protected void callListenerNow(PromiseListener<T> listener, PromiseCompletion<T> res) {
|
||||
try {
|
||||
listener.handle(res);
|
||||
} catch (Error e) {
|
||||
getLogger().error("Error caught in promise listener", e);
|
||||
throw e;
|
||||
} catch (Throwable e) {
|
||||
getLogger().error("Exception caught in promise listener", e);
|
||||
}
|
||||
runSafe(() -> listener.handle(res), e -> getLogger().error("Exception caught in promise listener", e));
|
||||
}
|
||||
|
||||
protected void callListenerAsyncLastResort(PromiseListener<T> listener, PromiseCompletion<T> completion) {
|
||||
@@ -84,16 +102,27 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
||||
}
|
||||
}
|
||||
|
||||
protected T joinCompletionChecked() throws ExecutionException {
|
||||
PromiseCompletion<T> completion = getCompletion();
|
||||
assert completion != null;
|
||||
if (completion.isSuccess()) return completion.getResult();
|
||||
throw new ExecutionException(completion.getException());
|
||||
}
|
||||
|
||||
protected T joinCompletionUnchecked() {
|
||||
PromiseCompletion<T> completion = getCompletion();
|
||||
assert completion != null;
|
||||
if (completion.isSuccess()) return completion.getResult();
|
||||
throw new CompletionException(completion.getException());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> fork() {
|
||||
PromiseCompletion<T> completion = getCompletion();
|
||||
if (completion == null) {
|
||||
if (isCompleted()) return this;
|
||||
|
||||
CompletablePromise<T> fork = getFactory().unresolved();
|
||||
PromiseUtil.propagateCompletion(this, fork);
|
||||
return fork;
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -119,8 +148,8 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenApply(@NotNull ExceptionalFunction<T, V> task) {
|
||||
PromiseCompletion<T> completion = getCompletion();
|
||||
if (completion == null) {
|
||||
return useCompletion(
|
||||
() -> {
|
||||
CompletablePromise<V> promise = createLinked();
|
||||
addDirectListener(
|
||||
res -> createCompleter(res, promise, task).run(),
|
||||
@@ -128,24 +157,19 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
||||
);
|
||||
|
||||
return promise;
|
||||
} else if (completion.isSuccess()) {
|
||||
try {
|
||||
V result = task.apply(completion.getResult());
|
||||
return getFactory().resolve(result);
|
||||
} catch (Exception e) {
|
||||
return getFactory().error(e);
|
||||
}
|
||||
} else {
|
||||
Throwable ex = completion.getException();
|
||||
assert ex != null;
|
||||
return getFactory().error(ex);
|
||||
}
|
||||
},
|
||||
result -> supplySafe(
|
||||
() -> getFactory().resolve(task.apply(result)),
|
||||
getFactory()::error
|
||||
),
|
||||
getFactory()::error
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> @NotNull Promise<V> thenCompose(@NotNull ExceptionalFunction<T, Promise<V>> task) {
|
||||
PromiseCompletion<T> completion = getCompletion();
|
||||
if (completion == null) {
|
||||
return useCompletion(
|
||||
() -> {
|
||||
CompletablePromise<V> promise = createLinked();
|
||||
thenApply(task).addDirectListener(
|
||||
result -> {
|
||||
@@ -160,27 +184,25 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
||||
);
|
||||
|
||||
return promise;
|
||||
} else if (completion.isSuccess()) {
|
||||
try {
|
||||
Promise<V> result = task.apply(completion.getResult());
|
||||
if (result == null) {
|
||||
},
|
||||
result -> supplySafe(
|
||||
() -> {
|
||||
Promise<V> nested = task.apply(result);
|
||||
if (nested == null) {
|
||||
return getFactory().resolve(null);
|
||||
} else if (result.isCompleted()) {
|
||||
return result;
|
||||
} else if (nested.isCompleted()) {
|
||||
return nested;
|
||||
} else {
|
||||
CompletablePromise<V> promise = createLinked();
|
||||
PromiseUtil.propagateCompletion(result, promise);
|
||||
PromiseUtil.propagateCancel(promise, result);
|
||||
PromiseUtil.propagateCompletion(nested, promise);
|
||||
PromiseUtil.propagateCancel(promise, nested);
|
||||
return promise;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return getFactory().error(e);
|
||||
}
|
||||
} else {
|
||||
Throwable ex = completion.getException();
|
||||
assert ex != null;
|
||||
return getFactory().error(ex);
|
||||
}
|
||||
},
|
||||
getFactory()::error
|
||||
),
|
||||
getFactory()::error
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -451,24 +473,21 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> orDefault(@NotNull ExceptionalFunction<Throwable, T> function) {
|
||||
PromiseCompletion<T> completion = getCompletion();
|
||||
if (completion == null) {
|
||||
return useCompletion(
|
||||
() -> {
|
||||
CompletablePromise<T> promise = createLinked();
|
||||
addDirectListener(promise::complete, e -> runCompleter(promise, () -> promise.complete(function.apply(e))));
|
||||
return promise;
|
||||
} else if (completion.isSuccess()) {
|
||||
return getFactory().resolve(completion.getResult());
|
||||
} else {
|
||||
try {
|
||||
return getFactory().resolve(function.apply(completion.getException()));
|
||||
} catch (Exception e) {
|
||||
return getFactory().error(e);
|
||||
}
|
||||
}
|
||||
},
|
||||
getFactory()::resolve,
|
||||
getFactory()::error
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CompletableFuture<T> toFuture() {
|
||||
return useCompletion(
|
||||
() -> {
|
||||
CompletableFuture<T> future = new CompletableFuture<>();
|
||||
addDirectListener(future::complete, future::completeExceptionally);
|
||||
future.whenComplete((_, e) -> {
|
||||
@@ -478,9 +497,14 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
||||
});
|
||||
|
||||
return future;
|
||||
},
|
||||
CompletableFuture::completedFuture,
|
||||
CompletableFuture::failedFuture
|
||||
);
|
||||
}
|
||||
|
||||
private static class DeferredExecutionException extends ExecutionException {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,12 +42,6 @@ public abstract class BasePromise<T, FS, FA> extends AbstractPromise<T, FS, FA>
|
||||
this.listeners = Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
protected T joinCompletion() throws ExecutionException {
|
||||
PromiseCompletion<T> completion = Objects.requireNonNull(getCompletion());
|
||||
if (completion.isSuccess()) return completion.getResult();
|
||||
throw new ExecutionException(completion.getException());
|
||||
}
|
||||
|
||||
protected void handleCompletion(@NotNull PromiseCompletion<T> cmp) {
|
||||
if (!COMPLETION_HANDLE.compareAndSet(this, null, cmp)) return;
|
||||
sync.releaseShared(1);
|
||||
@@ -99,31 +93,36 @@ public abstract class BasePromise<T, FS, FA> extends AbstractPromise<T, FS, FA>
|
||||
|
||||
@Override
|
||||
public T get() throws InterruptedException, ExecutionException {
|
||||
if (!isCompleted()) {
|
||||
sync.acquireSharedInterruptibly(1);
|
||||
return joinCompletion();
|
||||
}
|
||||
|
||||
return joinCompletionChecked();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(long time, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||
if (!isCompleted()) {
|
||||
boolean success = sync.tryAcquireSharedNanos(1, unit.toNanos(time));
|
||||
if (!success) {
|
||||
throw new TimeoutException("Promise stopped waiting after " + time + " " + unit);
|
||||
}
|
||||
}
|
||||
|
||||
return joinCompletion();
|
||||
return joinCompletionChecked();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T await() {
|
||||
if (!isCompleted()) {
|
||||
try {
|
||||
sync.acquireSharedInterruptibly(1);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
PromiseCompletion<T> completion = Objects.requireNonNull(getCompletion());
|
||||
if (completion.isSuccess()) return completion.getResult();
|
||||
throw new CompletionException(completion.getException());
|
||||
return joinCompletionUnchecked();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,6 +3,7 @@ package dev.tommyjs.futur.promise;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public abstract class CompletedPromise<T, FS, FA> extends AbstractPromise<T, FS, FA> {
|
||||
@@ -28,36 +29,34 @@ public abstract class CompletedPromise<T, FS, FA> extends AbstractPromise<T, FS,
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit) {
|
||||
// Promise is already completed so can't time out
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> maxWaitTime(long time, @NotNull TimeUnit unit) {
|
||||
// Promise is already completed so can't time out
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel(@NotNull CancellationException exception) {
|
||||
// Promise is already completed so can't be cancelled
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
return null;
|
||||
public T get() throws ExecutionException {
|
||||
return joinCompletionChecked();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(long timeout, @NotNull TimeUnit unit) {
|
||||
return null;
|
||||
public T get(long timeout, @NotNull TimeUnit unit) throws ExecutionException {
|
||||
return joinCompletionChecked();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T await() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> fork() {
|
||||
return this;
|
||||
return joinCompletionUnchecked();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user