Package ratpack.exec.util
Class Promised<T>
- java.lang.Object
-
- ratpack.exec.util.Promised<T>
-
- Type Parameters:
T- the type of value that is promised
- All Implemented Interfaces:
Downstream<T>
public final class Promised<T> extends Object implements Downstream<T>
A logical value that will be available later, that promises can be created for.A “promised” can be used by the producer of a value to notify an interested parties when the value becomes available. Zero or more promises can be created for the promised value via the
promise()method.PromisedextendsDownstream, which represents the write side of async values. To supply the promised value, simply call exactly one of the methods inherited fromDownstream.A “promised” can have zero or more listeners, but can only be fulfilled once. All of the methods inherited from
Downstreamwill throw anPromised.AlreadySuppliedExceptionif a value, error or completion have already been signalled for this topic.import ratpack.test.exec.ExecHarness; import ratpack.exec.Execution; import ratpack.exec.util.Promised; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.CountDownLatch; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static void main(String... args) throws Exception { AtomicReference<Long> ref = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); ExecHarness.runSingle(e -> { Promised<Long> topic = new Promised<>(); // create a listener Execution.fork().start(e1 -> topic.promise().then(v -> { ref.set(v); latch.countDown(); }) ); // fulfill the topic, notifying listeners topic.success(1l); }); latch.await(); assertEquals(1l, ref.get().longValue()); } }- Since:
- 1.2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPromised.AlreadySuppliedExceptionThrown if an attempt is made to supply the value/result after it has already been supplied.
-
Constructor Summary
Constructors Constructor Description Promised()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaccept(ExecResult<? extends T> result)Signals this downstream, based on the given result.voidcomplete()Signals that the upstream will not be providing a value, as it has terminated.voiderror(Throwable throwable)Signals the unsuccessful production of the upstream value.Promise<T>promise()Creates a new promise for the eventual value.voidsuccess(T value)Signals the successful production of the upstream value.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ratpack.exec.Downstream
accept, accept, accept, completionHandler, onComplete, onError, onSuccess
-
-
-
-
Method Detail
-
promise
public Promise<T> promise()
Creates a new promise for the eventual value.- Returns:
- a new promise for the eventual value
-
success
public void success(T value)
Signals the successful production of the upstream value.- Specified by:
successin interfaceDownstream<T>- Parameters:
value- the upstream value
-
error
public void error(Throwable throwable)
Signals the unsuccessful production of the upstream value.- Specified by:
errorin interfaceDownstream<T>- Parameters:
throwable- what went wrong
-
complete
public void complete()
Signals that the upstream will not be providing a value, as it has terminated.- Specified by:
completein interfaceDownstream<T>
-
accept
public void accept(ExecResult<? extends T> result)
Signals this downstream, based on the given result.- Specified by:
acceptin interfaceDownstream<T>- Parameters:
result- the result to signal
-
-