Created
November 22, 2013 21:49
-
-
Save davidhoyt/7607459 to your computer and use it in GitHub Desktop.
Converts from a scala.concurrent.Future to a java.util.concurrent.Future
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util._ | |
import scala.concurrent._ | |
import java.util.concurrent.{Callable, FutureTask} | |
object Utils { | |
def toJavaFuture[TScalaType, TJavaType](f: Future[TScalaType])(map: (TScalaType => TJavaType))(implicit executor: ExecutionContext): java.util.concurrent.Future[TJavaType] = { | |
val applied = f.map(map) | |
val task = new FutureTask[TJavaType](new Callable[TJavaType] { | |
def call(): TJavaType = applied.value.get match { | |
case Success(v) => v | |
case Failure(t) => throw new ExecutionException(s"Failure during execution", t) | |
case _ => throw new ExecutionException(s"Failure during execution", null) | |
} | |
}) | |
applied.onComplete { | |
case _ => | |
task.run() | |
} | |
task | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment