From 58359e00c63fe707b690bac8ffb49b9564e3c1a3 Mon Sep 17 00:00:00 2001 From: Derek Williams Date: Sat, 12 Feb 2011 11:01:08 -0700 Subject: [PATCH] Add method on Future to await and return the result. Works like resultWithin, but does not need an explicit timeout. --- akka-actor/src/main/scala/akka/dispatch/Future.scala | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/akka-actor/src/main/scala/akka/dispatch/Future.scala b/akka-actor/src/main/scala/akka/dispatch/Future.scala index 0d3bfc9a31..ab0e064c4a 100644 --- a/akka-actor/src/main/scala/akka/dispatch/Future.scala +++ b/akka-actor/src/main/scala/akka/dispatch/Future.scala @@ -146,6 +146,8 @@ sealed trait Future[T] { def result: Option[T] = value flatMap (_.right.toOption) + def awaitResult: Option[Either[Throwable, T]] + /** * Returns the result of the Future if one is available within the specified time, * if the time left on the future is less than the specified time, the time left on the future will be used instead @@ -209,6 +211,14 @@ class DefaultCompletableFuture[T](timeout: Long) extends CompletableFuture[T] { } } + def awaitResult: Option[Either[Throwable, T]] = try { + _lock.lock + awaitUnsafe(timeoutInNanos - (currentTimeInNanos - _startTimeInNanos)) + _value + } finally { + _lock.unlock + } + def resultWithin(time: Long, unit: TimeUnit): Option[Either[Throwable, T]] = try { _lock.lock awaitUnsafe(unit.toNanos(time).min(timeoutInNanos - (currentTimeInNanos - _startTimeInNanos)))