Skip to content

Instantly share code, notes, and snippets.

@davidhoyt
Created November 22, 2013 21:49
Show Gist options
  • Save davidhoyt/7607459 to your computer and use it in GitHub Desktop.
Save davidhoyt/7607459 to your computer and use it in GitHub Desktop.
Converts from a scala.concurrent.Future to a java.util.concurrent.Future
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