org.megam.util

Promise

class Promise[A] extends Future[A] with Responder[A]

A writeable org.megam.util.Future that supports merging. Callbacks (responders) of Promises are scheduled with org.megam.concurrent.Scheduler.

Implementation details

A Promise is in one of five states: Waiting, Interruptible, Interrupted, Done and Linked where Interruptible and Interrupted are variants of Waiting to deal with future interrupts. Promises are concurrency-safe, using lock-free operations throughout. Callback dispatch is scheduled with org.megam.concurrent.Scheduler.

Waiters are stored as a org.megam.util.Promise.K. Ks (mnemonic: continuation) specifies a depth. This is used to implement Promise chaining: a callback with depth d is invoked only after all callbacks with depth < d have already been invoked.

Promise.become merges two promises: they are declared equivalent. become merges the states of the two promises, and links one to the other. Thus promises support the analog to tail-call elimination: no space leak is incurred from flatMap in the tail position since intermediate promises are merged into the root promise.

A number of optimizations are employed to conserve space: we pay particular heed to the JVM's object representation, in particular for OpenJDK (HotSpot) version 7 running on 64-bit architectures with compressed OOPS. See comments on org.megam.util.Promise.State for details.

Linear Supertypes
Responder[A], Future[A], Awaitable[A], AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Promise
  2. Responder
  3. Future
  4. Awaitable
  5. AnyRef
  6. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Promise(result: Try[A])

  2. new Promise(handleInterrupt: PartialFunction[Throwable, Unit])

  3. new Promise()

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. def addEventListener(listener: FutureEventListener[_ >: A]): Future[A]

    Register a FutureEventListener to be invoked when the computation completes.

    Register a FutureEventListener to be invoked when the computation completes. This method is typically used by Java programs because it avoids the use of small Function objects.

    Compare this method to transformedBy. The difference is that addEventListener is used to perform a simple action when a computation completes, such as recording data in a log-file. It analogous to a void method in Java: it has side-effects and no return value. transformedBy, on the other hand, is used to transform values from one type to another, or to chain a series of asynchronous calls and return the result. It is analogous to methods in Java that have a return-type. Note that transformedBy and addEventListener are not mutually exclusive and may be profitably combined.

    Definition Classes
    Future
  7. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  8. def become(other: Future[A]): Unit

    Become the other promise.

    Become the other promise. become declares an equivalence relation: this and other are the same.

    By becoming other, its waitlists are now merged into this's, and this becomes canonical. The same is true of interrupt handlers: other's interrupt handler becomes active, but is stored canonically by this - further references are forwarded. Note that this must be unsatisfied at the time of the call, and not race with any other setters. become is a form of satisfying the promise.

    This has the combined effect of compressing the other into this, effectively providing a form of tail-call elimination when used in recursion constructs. transform (and thus any other combinator) use this to compress Futures, freeing them from space leaks when used with recursive constructions.

    Note: do not use become with cyclic graphs of futures: the behavior of racing a.become(b) with b.become(a) is undefined (where a and b may resolve as such transitively).

  9. def before[B](f: ⇒ Future[B])(implicit ev: <:<[Promise.this.type, Future[Unit]]): Future[B]

    Sequentially compose this with f.

    Sequentially compose this with f. This is as flatMap, but discards the result of this. Note that this applies only Unit-valued Futures -- i.e. side-effects..

    Definition Classes
    Future
  10. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  11. final def compress(): Promise[A]

    Attributes
    protected
  12. final def continue(k: K[A]): Unit

    Attributes
    protected[org.megam.util]
    Definition Classes
    PromiseResponder
    Annotations
    @tailrec()
  13. def delayed(howlong: Duration)(implicit timer: Timer): Future[A]

    Delay the completion of this Future for at least howlong from now.

    Delay the completion of this Future for at least howlong from now.

    Definition Classes
    Future
  14. final def depth: Short

    Attributes
    protected[org.megam.util]
    Definition Classes
    PromiseResponder
  15. final def detach(k: K[A]): Boolean

    Attributes
    protected
    Annotations
    @tailrec()
  16. def ensure(f: ⇒ Unit): Future[A]

    Invoked regardless of whether the computation completed successfully or unsuccessfully.

    Invoked regardless of whether the computation completed successfully or unsuccessfully. Implemented in terms of respond so that subclasses control evaluation order. Returns a chained Future.

    Definition Classes
    Future
  17. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  18. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  19. def filter(p: (A) ⇒ Boolean): Future[A]

    Definition Classes
    Future
  20. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  21. def flatMap[B](f: (A) ⇒ Future[B]): Future[B]

    If this, the original future, succeeds, run f on the result.

    If this, the original future, succeeds, run f on the result.

    The returned result is a Future that is satisfied when the original future and the callback, f, are done. If the original future fails, this one will also fail, without executing f.

    Definition Classes
    Future
    See also

    map()

  22. def flatten[B](implicit ev: <:<[A, Future[B]]): Future[B]

    Converts a Future[Future[B]] into a Future[B]

    Converts a Future[Future[B]] into a Future[B]

    Definition Classes
    Future
  23. def foreach(k: (A) ⇒ Unit): Future[A]

    Invoke the callback only if the Future returns successfully.

    Invoke the callback only if the Future returns successfully. Useful for Scala for comprehensions. Use onSuccess instead of this method for more readable code.

    Definition Classes
    Future
  24. final def forwardInterruptsTo(other: Future[_]): Unit

    Forward interrupts to another future.

    Forward interrupts to another future.

    other

    the Future to which interrupts are forwarded.

    Annotations
    @tailrec()
  25. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  26. def handle[B >: A](rescueException: PartialFunction[Throwable, B]): Future[B]

    Definition Classes
    Future
  27. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  28. def isDefined: Boolean

    Is the result of the Future available yet?

    Is the result of the Future available yet?

    Definition Classes
    PromiseFuture
  29. def isDone(implicit ev: <:<[Promise.this.type, Future[Unit]]): Boolean

    Checks whether a Unit-typed Future is done.

    Checks whether a Unit-typed Future is done. By convention, futures of type Future[Unit] are used for signalling.

    Definition Classes
    Future
  30. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  31. def isInterrupted: Option[Throwable]

    Returns this promise's interrupt if it is interrupted.

  32. def isReady(implicit permit: CanAwait): Boolean

    Is this Awaitable ready? In other words: would calling Awaitable.ready block?

    Is this Awaitable ready? In other words: would calling Awaitable.ready block?

    Definition Classes
    PromiseAwaitable
  33. def join[B](other: Future[B]): Future[(A, B)]

    Combines two Futures into one Future of the Tuple of the two results.

    Combines two Futures into one Future of the Tuple of the two results.

    Definition Classes
    Future
  34. def liftToTry: Future[Try[A]]

    Returns the result of the computation as a Future[Try[A]].

    Returns the result of the computation as a Future[Try[A]].

    Definition Classes
    Future
  35. final def link(target: Promise[A]): Unit

    Attributes
    protected
    Annotations
    @tailrec()
  36. def map[B](f: (A) ⇒ B): Future[B]

    If this, the original future, succeeds, run f on the result.

    If this, the original future, succeeds, run f on the result.

    The returned result is a Future that is satisfied when the original future and the callback, f, are done. If the original future fails, this one will also fail, without executing f.

    Definition Classes
    Future
    See also

    flatMap()

  37. def mask(pred: PartialFunction[Throwable, Boolean]): Future[A]

    Returns an identical future except that it ignores interrupts which match a predicate

    Returns an identical future except that it ignores interrupts which match a predicate

    Definition Classes
    Future
  38. def masked: Future[A]

    Returns an identical future that ignores all interrupts

    Returns an identical future that ignores all interrupts

    Definition Classes
    Future
  39. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  40. final def notify(): Unit

    Definition Classes
    AnyRef
  41. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  42. def onFailure(rescueException: (Throwable) ⇒ Unit): Future[A]

    Invoke the function on the error, if the computation was unsuccessful.

    Invoke the function on the error, if the computation was unsuccessful. Returns a chained Future as in respond.

    returns

    chained Future

    Definition Classes
    Future
  43. def onSuccess(f: (A) ⇒ Unit): Future[A]

    Invoke the function on the result, if the computation was successful.

    Invoke the function on the result, if the computation was successful. Returns a chained Future as in respond.

    returns

    chained Future

    Definition Classes
    Future
  44. def or[U >: A](other: Future[U]): Future[U]

    A synonym for select(): Choose the first Future to be satisfied.

    A synonym for select(): Choose the first Future to be satisfied.

    Definition Classes
    Future
  45. final def parent: Promise[A]

    Attributes
    protected
    Definition Classes
    PromiseResponder
  46. def poll: Option[Try[A]]

    Polls for an available result.

    Polls for an available result. If the Future has been satisfied, returns Some(result), otherwise None.

    Definition Classes
    PromiseFuture
  47. def proxyTo[B >: A](other: Promise[B]): Unit

    Send updates from this Future to the other.

    Send updates from this Future to the other. other must not yet be satisfied.

    Definition Classes
    Future
  48. final def raise(intr: Throwable): Unit

    Raise the given throwable as an interrupt.

    Raise the given throwable as an interrupt. Interrupts are one-shot and latest-interrupt wins. That is, the last interrupt to have been raised is delivered exactly once to the Promise responsible for making progress on the future (multiple such promises may be involved in flatMap chains).

    Raising an interrupt does not alter the externally observable state of the Future. They are used to signal to the producer of the future's value that the result is no longer desired (for whatever reason given in the passed Throwable).

    Definition Classes
    PromiseFuture
    Annotations
    @tailrec()
  49. def raiseWithin(timer: Timer, timeout: Duration, exc: Throwable): Future[A]

    Returns a new Future that will error if this Future does not return in time.

    Returns a new Future that will error if this Future does not return in time.

    Note: On timeout, the underlying future is interrupted.

    Definition Classes
    Future
  50. def raiseWithin(timeout: Duration, exc: Throwable)(implicit timer: Timer): Future[A]

    Same as the other raiseWithin, but with an implicit timer.

    Same as the other raiseWithin, but with an implicit timer. Sometimes this is more convenient.

    Note: On timeout, the underlying future is interrupted.

    Definition Classes
    Future
  51. def raiseWithin(timeout: Duration)(implicit timer: Timer): Future[A]

    Same as the other raiseWithin, but with an implicit timer.

    Same as the other raiseWithin, but with an implicit timer. Sometimes this is more convenient.

    Note: On timeout, the underlying future is interrupted.

    Definition Classes
    Future
  52. def ready(timeout: Duration)(implicit permit: CanAwait): Promise.this.type

    Support for Await.ready.

    Support for Await.ready. The use of the implicit permit is an access control mechanism: only Await.ready may call this method.

    Definition Classes
    PromiseAwaitable
    Annotations
    @throws( classOf[TimeoutException] ) @throws( classOf[InterruptedException] )
  53. def rescue[B >: A](rescueException: PartialFunction[Throwable, Future[B]]): Future[B]

    Definition Classes
    Future
  54. def respond(k: (Try[A]) ⇒ Unit): Future[A]

    Note: exceptions in responds are monitored.

    Note: exceptions in responds are monitored. That is, if the computation {{k}} throws a raw (ie. not encoded in a Future) exception, it is handled by the current monitor, see {{org.megam.util.Monitor}} for details.

    returns

    a chained Future[A]

    Definition Classes
    ResponderFuture
  55. def result(timeout: Duration)(implicit permit: CanAwait): A

    Support for Await.result.

    Support for Await.result. The use of the implicit permit is an access control mechanism: only Await.result may call this method.

    Definition Classes
    PromiseAwaitable
    Annotations
    @throws( classOf[Exception] )
  56. def select[U >: A](other: Future[U]): Future[U]

    Choose the first Future to be satisfied.

    Choose the first Future to be satisfied.

    other

    another Future

    returns

    a new Future whose result is that of the first of this and other to return

    Definition Classes
    Future
  57. def setDone()(implicit ev: <:<[Promise.this.type, Promise[Unit]]): Boolean

    Sets a Unit-typed future.

    Sets a Unit-typed future. By convention, futures of type Future[Unit] are used for signalling.

  58. def setException(throwable: Throwable): Unit

    Populate the Promise with the given exception.

    Populate the Promise with the given exception.

    Exceptions thrown
    ImmutableResult

    if the Promise is already populated

  59. final def setInterruptHandler(f: PartialFunction[Throwable, Unit]): Unit

    (Re)sets the interrupt handler.

    (Re)sets the interrupt handler. There is only one active interrupt handler.

    f

    the new interrupt handler

    Annotations
    @tailrec()
  60. def setValue(result: A): Unit

    Populate the Promise with the given result.

    Populate the Promise with the given result.

    Exceptions thrown
    ImmutableResult

    if the Promise is already populated

  61. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  62. def toJavaFuture: java.util.concurrent.Future[_ <: A]

    Convert a Twitter Future to a Java native Future.

    Convert a Twitter Future to a Java native Future. This should match the semantics of a Java Future as closely as possible to avoid issues with the way another API might use them. See:

    http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html#cancel(boolean)

    Definition Classes
    Future
  63. def toOffer: Offer[Try[A]]

    An offer for this future.

    An offer for this future. The offer is activated when the future is satisfied.

    Definition Classes
    Future
  64. def toString(): String

    Definition Classes
    Promise → AnyRef → Any
  65. def transform[B](f: (Try[A]) ⇒ Future[B]): Future[B]

    Definition Classes
    ResponderFuture
  66. def transformedBy[B](transformer: FutureTransformer[A, B]): Future[B]

    Transform the Future[A] into a Future[B] using the FutureTransformer.

    Transform the Future[A] into a Future[B] using the FutureTransformer. The FutureTransformer handles both success (Return) and failure (Throw) values by implementing map/flatMap and handle/rescue. This method is typically used by Java programs because it avoids the use of small Function objects.

    Compare this method to addEventListener. The difference is that addEventListener is used to perform a simple action when a computation completes, such as recording data in a log-file. It analogous to a void method in Java: it has side-effects and no return value. transformedBy, on the other hand, is used to transform values from one type to another, or to chain a series of asynchronous calls and return the result. It is analogous to methods in Java that have a return-type. Note that transformedBy and addEventListener are not mutually exclusive and may be profitably combined.

    Note: The FutureTransformer must implement either flatMap or map and may optionally implement handle. Failing to implement a method will result in a run-time (AbstractMethod) error.

    Definition Classes
    Future
  67. def unit: Future[Unit]

    Convert this Future[A] to a Future[Unit] by discarding the result.

    Convert this Future[A] to a Future[Unit] by discarding the result.

    Definition Classes
    Future
  68. def update(result: Try[A]): Unit

    Populate the Promise with the given Try.

    Populate the Promise with the given Try. The try can either be a value or an exception. setValue and setException are generally more readable methods to use.

    Exceptions thrown
    ImmutableResult

    if the Promise is already populated

  69. final def updateIfEmpty(result: Try[A]): Boolean

    Populate the Promise with the given Try.

    Populate the Promise with the given Try. The try can either be a value or an exception. setValue and setException are generally more readable methods to use.

    returns

    true only if the result is updated, false if it was already set.

    Annotations
    @tailrec()
  70. def voided: Future[Void]

    Convert this Future[A] to a Future[Void] by discarding the result.

    Convert this Future[A] to a Future[Void] by discarding the result.

    Definition Classes
    Future
  71. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  72. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  73. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  74. def willEqual[B](that: Future[B]): Promise[Boolean]

    Returns a Future[Boolean] indicating whether two Futures are equivalent.

    Returns a Future[Boolean] indicating whether two Futures are equivalent. Note that Future.exception(e).willEqual(Future.exception(e)) == Future.value(true).

    Definition Classes
    Future
  75. def withFilter(p: (A) ⇒ Boolean): Future[A]

    Definition Classes
    Future
  76. def within(timer: Timer, timeout: Duration, exc: ⇒ Throwable): Future[A]

    Returns a new Future that will error if this Future does not return in time.

    Returns a new Future that will error if this Future does not return in time.

    Note: On timeout, the underlying future is not interrupted.

    timer

    to run timeout on.

    timeout

    indicates how long you are willing to wait for the result to be available.

    exc

    exception to throw.

    Definition Classes
    Future
  77. def within(timer: Timer, timeout: Duration): Future[A]

    Returns a new Future that will error if this Future does not return in time.

    Returns a new Future that will error if this Future does not return in time.

    Note: On timeout, the underlying future is not interrupted.

    Definition Classes
    Future
  78. def within(timeout: Duration)(implicit timer: Timer): Future[A]

    Same as the other within, but with an implicit timer.

    Same as the other within, but with an implicit timer. Sometimes this is more convenient.

    Note: On timeout, the underlying future is not interrupted.

    Definition Classes
    Future

Deprecated Value Members

  1. def apply(timeout: Duration): A

    Block, but only as long as the given Timeout.

    Block, but only as long as the given Timeout.

    Definition Classes
    Future
    Annotations
    @deprecated
    Deprecated

    (Since version 6.2.x) Use Await.result

  2. def apply(): A

    Block indefinitely, wait for the result of the Future to be available.

    Block indefinitely, wait for the result of the Future to be available.

    Definition Classes
    Future
    Annotations
    @deprecated
    Deprecated

    (Since version 6.2.x) Use Await.result

  3. def cancel(): Unit

    Definition Classes
    Future
    Annotations
    @deprecated
    Deprecated

    (Since version 6.0.0) Provided for API compatibility; use raise() instead.

  4. final def get(timeout: Duration): Try[A]

    Demands that the result of the future be available within timeout.

    Demands that the result of the future be available within timeout. The result is a Return[_] or Throw[_] depending upon whether the computation finished in time.

    Definition Classes
    Future
    Annotations
    @deprecated
    Deprecated

    (Since version 6.2.x) Use Await.result

  5. def get(): A

    Alias for apply().

    Alias for apply().

    Definition Classes
    Future
    Annotations
    @deprecated
    Deprecated

    (Since version 6.2.x) Use Await.result

  6. def isReturn: Boolean

    Definition Classes
    Future
    Annotations
    @deprecated
    Deprecated

    (Since version 6.2.x) Use Await.result

  7. def isThrow: Boolean

    Definition Classes
    Future
    Annotations
    @deprecated
    Deprecated

    (Since version 6.2.x) Use Await.result

  8. def void: Future[Void]

    Definition Classes
    Future
    Annotations
    @deprecated
    Deprecated

    (Since version 5.x) 'void' is a reserved word in javac.

Inherited from Responder[A]

Inherited from Future[A]

Inherited from Awaitable[A]

Inherited from AnyRef

Inherited from Any

Ungrouped