Permalink
Browse files

Fixes and clean ups for futures based on review by @viktorklang

  • Loading branch information...
1 parent db3056f commit af71b10083ed8f91b9735e363651a64149a5ca89 @phaller phaller committed Apr 12, 2012
@@ -10,18 +10,23 @@ package scala.concurrent
-import scala.annotation.implicitNotFound
import scala.concurrent.util.Duration
trait Awaitable[+T] {
+ /**
+ * Should throw [[scala.concurrent.TimeoutException]] if it times out
+ * This method should not be called directly.
+ */
+ @throws(classOf[TimeoutException])
def ready(atMost: Duration)(implicit permit: CanAwait): this.type
/**
- * Throws exceptions if cannot produce a T within the specified time
+ * Throws exceptions if it cannot produce a T within the specified time.
* This method should not be called directly.
*/
+ @throws(classOf[Exception])
def result(atMost: Duration)(implicit permit: CanAwait): T
}
@@ -82,7 +82,6 @@ import scala.collection.generic.CanBuildFrom
* }}}
*/
trait Future[+T] extends Awaitable[T] {
-self =>
/* Callbacks */
@@ -132,7 +131,7 @@ self =>
/** Creates a new promise.
*/
- def newPromise[S]: Promise[S]
+ protected def newPromise[S]: Promise[S]
/** Returns whether the future has already been completed with
* a value or an exception.
@@ -168,14 +167,11 @@ self =>
* and throws a corresponding exception if the original future fails.
*/
def failed: Future[Throwable] = {
- def noSuchElem(v: T) =
- new NoSuchElementException("Future.failed not completed with a throwable. Instead completed with: " + v)
-
val p = newPromise[Throwable]
onComplete {
case Left(t) => p success t
- case Right(v) => p failure noSuchElem(v)
+ case Right(v) => p failure (new NoSuchElementException("Future.failed not completed with a throwable. Instead completed with: " + v))
@viktorklang

viktorklang Aug 16, 2012

I think we should not add the "v" to the String as it may generate a huge String.

}
p.future
@@ -411,21 +407,16 @@ self =>
* {{{
* val f = future { sys.error("failed") }
* val g = future { 5 }
- * val h = f orElse g
+ * val h = f fallbackTo g
* await(h, 0) // evaluates to 5
* }}}
*/
def fallbackTo[U >: T](that: Future[U]): Future[U] = {
val p = newPromise[U]
-
onComplete {
- case Left(t) => that onComplete {
- case Left(_) => p failure t
- case Right(v) => p success v
- }
- case Right(v) => p success v
+ case r @ Right(_) p complete r
+ case _ p completeWith that
}
-
p.future
}
@@ -497,14 +488,14 @@ self =>
* }}}
*/
def either[U >: T](that: Future[U]): Future[U] = {
- val p = self.newPromise[U]
+ val p = newPromise[U]
val completePromise: PartialFunction[Either[Throwable, U], _] = {
case Left(t) => p tryFailure t
case Right(v) => p trySuccess v
}
- self onComplete completePromise
+ this onComplete completePromise
that onComplete completePromise
p.future
@@ -84,7 +84,7 @@ private[scala] class ExecutionContextImpl(es: AnyRef) extends ExecutionContext w
}
-object ExecutionContextImpl {
+private[concurrent] object ExecutionContextImpl {
private[concurrent] def currentExecutionContext: ThreadLocal[ExecutionContext] = new ThreadLocal[ExecutionContext] {
override protected def initialValue = null
@@ -31,16 +31,6 @@ package concurrent {
def result[T](awaitable: Awaitable[T], atMost: Duration): T = awaitable.result(atMost)
}
- /** A timeout exception.
- *
- * Futures are failed with a timeout exception when their timeout expires.
- *
- * Each timeout exception contains an origin future which originally timed out.
- */
- class FutureTimeoutException(origin: Future[_], message: String) extends TimeoutException(message) {
- def this(origin: Future[_]) = this(origin, "Future timed out.")
- }
-
final class DurationOps private[concurrent] (x: Int) {
// TODO ADD OTHERS
def ns = util.Duration.fromNanos(0)

0 comments on commit af71b10

Please sign in to comment.