mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-17 23:16:01 +00:00
add toFuture method
This commit is contained in:
@@ -10,10 +10,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@@ -438,4 +435,16 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
return completion.get();
|
return completion.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull CompletableFuture<T> toFuture() {
|
||||||
|
CompletableFuture<T> future = new CompletableFuture<>();
|
||||||
|
this.addDirectListener(future::complete, future::completeExceptionally);
|
||||||
|
future.whenComplete((res, e) -> {
|
||||||
|
if (e instanceof CancellationException) {
|
||||||
|
this.cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.CancellationException;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
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;
|
||||||
@@ -128,4 +129,6 @@ public interface Promise<T> {
|
|||||||
|
|
||||||
boolean isCompleted();
|
boolean isCompleted();
|
||||||
|
|
||||||
|
@NotNull CompletableFuture<T> toFuture();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,21 @@ public final class PromiseTests {
|
|||||||
assert !finished.get();
|
assert !finished.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToFuture() throws InterruptedException {
|
||||||
|
assert pfac.resolve(true).toFuture().getNow(false);
|
||||||
|
assert pfac.error(new Exception("Test")).toFuture().isCompletedExceptionally();
|
||||||
|
|
||||||
|
var finished = new AtomicBoolean();
|
||||||
|
pfac.start()
|
||||||
|
.thenRunDelayedAsync(() -> finished.set(true), 50, TimeUnit.MILLISECONDS)
|
||||||
|
.toFuture()
|
||||||
|
.cancel(true);
|
||||||
|
|
||||||
|
Thread.sleep(100L);
|
||||||
|
assert !finished.get();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCombineUtil() throws TimeoutException {
|
public void testCombineUtil() throws TimeoutException {
|
||||||
pfac.all(
|
pfac.all(
|
||||||
|
|||||||
Reference in New Issue
Block a user