using java.util.concurrent; using java.util.function; using Torch.Utils; namespace Maintenance.Extensions; public static class CompletableFutureExtensions { [ReflectedStaticMethod(Type = typeof(Consumer), Name = "andThen")] private static readonly Func AndThenDefault = null!; public static Task ToTask(this CompletableFuture completableFuture) { var taskCompletionSource = new TaskCompletionSource(); completableFuture.thenAccept(new TaskConsumer(taskCompletionSource)); return taskCompletionSource.Task; } private sealed class TaskConsumer(TaskCompletionSource completionSource) : Consumer { public void accept(object t) { completionSource.SetResult((T)t); } public Consumer andThen(Consumer after) => AndThenDefault(this, after); } }