seperate scheduler and virtual executor promise chain methods

This commit is contained in:
WhatCats
2025-05-28 13:23:23 +02:00
parent a630984cb0
commit fea0575392
21 changed files with 595 additions and 231 deletions

View File

@@ -1,5 +1,6 @@
package dev.tommyjs.futur.promise;
import dev.tommyjs.futur.executor.PromiseScheduler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -13,7 +14,7 @@ import java.util.concurrent.*;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
@SuppressWarnings({"FieldMayBeFinal"})
public abstract class BasePromise<T, FS, FA> extends AbstractPromise<T, FS, FA> implements CompletablePromise<T> {
public abstract class BasePromise<T> extends AbstractPromise<T> implements CompletablePromise<T> {
private static final VarHandle COMPLETION_HANDLE;
private static final VarHandle LISTENERS_HANDLE;
@@ -48,10 +49,10 @@ public abstract class BasePromise<T, FS, FA> extends AbstractPromise<T, FS, FA>
callListeners(cmp);
}
protected Promise<T> completeExceptionallyDelayed(Throwable e, long delay, TimeUnit unit) {
protected <F> Promise<T> completeExceptionallyDelayed(Throwable e, long delay, TimeUnit unit, PromiseScheduler<F> scheduler) {
runCompleter(this, () -> {
FA future = getFactory().getAsyncExecutor().run(() -> completeExceptionally(e), delay, unit);
addDirectListener(_ -> getFactory().getAsyncExecutor().cancel(future));
F future = scheduler.schedule(() -> completeExceptionally(e), delay, unit);
addDirectListener(_ -> scheduler.cancel(future));
});
return this;
@@ -125,16 +126,21 @@ public abstract class BasePromise<T, FS, FA> extends AbstractPromise<T, FS, FA>
return joinCompletionUnchecked();
}
@Override
public T getNow() {
return joinCompletionUnchecked();
}
@Override
public @NotNull Promise<T> timeout(long time, @NotNull TimeUnit unit) {
Exception e = new CancellationException("Promise timed out after " + time + " " + unit.toString().toLowerCase());
return completeExceptionallyDelayed(e, time, unit);
return completeExceptionallyDelayed(e, time, unit, PromiseScheduler.getDefault());
}
@Override
public @NotNull Promise<T> maxWaitTime(long time, @NotNull TimeUnit unit) {
Exception e = new TimeoutException("Promise stopped waiting after " + time + " " + unit.toString().toLowerCase());
return completeExceptionallyDelayed(e, time, unit);
return completeExceptionallyDelayed(e, time, unit, PromiseScheduler.getDefault());
}
@Override
@@ -162,6 +168,40 @@ public abstract class BasePromise<T, FS, FA> extends AbstractPromise<T, FS, FA>
return completion;
}
@Override
public @NotNull CompletableFuture<T> toFuture() {
return useCompletion(
() -> {
CompletableFuture<T> future = new CompletableFuture<>();
addDirectListener(future::complete, future::completeExceptionally);
future.whenComplete((result, error) -> {
if (error == null) {
complete(result);
} else {
completeExceptionally(error);
}
});
return future;
},
CompletableFuture::completedFuture,
CompletableFuture::failedFuture
);
}
@Override
public @NotNull CompletionStage<T> toCompletionStage() {
return useCompletion(
() -> {
CompletableFuture<T> future = new CompletableFuture<>();
addDirectListener(future::complete, future::completeExceptionally);
return future;
},
CompletableFuture::completedStage,
CompletableFuture::failedStage
);
}
private static final class Sync extends AbstractQueuedSynchronizer {
private Sync() {