Class 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. Promised extends Downstream, which represents the write side of async values. To supply the promised value, simply call exactly one of the methods inherited from Downstream.

    A “promised” can have zero or more listeners, but can only be fulfilled once. All of the methods inherited from Downstream will throw an Promised.AlreadySuppliedException if 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
    • Constructor Detail

      • Promised

        public Promised()
    • 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:
        success in interface Downstream<T>
        Parameters:
        value - the upstream value
      • error

        public void error​(Throwable throwable)
        Signals the unsuccessful production of the upstream value.
        Specified by:
        error in interface Downstream<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:
        complete in interface Downstream<T>
      • accept

        public void accept​(ExecResult<? extends T> result)
        Signals this downstream, based on the given result.
        Specified by:
        accept in interface Downstream<T>
        Parameters:
        result - the result to signal