object FutureConverters
This class contains static methods which convert between Java CompletionStage and Scala Future. This is useful when mediating between Scala and Java libraries with asynchronous APIs.
Note that the bridge is implemented at the read-only side of asynchronous
handles, namely scala.concurrent.Future instead of scala.concurrent.Promise
and CompletionStage instead of CompletableFuture. This is intentional, as
the semantics of bridging the write-handles would be prone to race
conditions; if both ends (CompletableFuture and Promise) are completed
independently at the same time, they may contain different values afterwards.
For this reason, toCompletableFuture()
is not supported on the
created CompletionStages.
Example usage:
import java.util.concurrent.CompletionStage; import scala.concurrent.Future; import static scala.concurrent.java8.FutureConverters.*; final CompletionStage<String> cs = ... // from an async Java API final Future<String> f = toScala(cs); ... final Future<Integer> f2 = ... // from an async Scala API final CompletionStage<Integer> cs2 = toJava(f2);
- Alphabetic
- By Inheritance
- FutureConverters
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- final class CompletionStageOps[T] extends AnyVal
- final class FutureOps[T] extends AnyVal
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- implicit def CompletionStageOps[T](cs: CompletionStage[T]): CompletionStageOps[T]
- implicit def FutureOps[T](f: Future[T]): FutureOps[T]
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def failedPromise[T](ex: Throwable): Promise[T]
Construct an already fulfilled
scala.concurrent.Promise
which holds the given failure.Construct an already fulfilled
scala.concurrent.Promise
which holds the given failure.- returns
the fulfilled Promise
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def fromExecutor(e: Executor): ExecutionContextExecutor
Creates an ExecutionContext from a given Executor, using the default reporter for uncaught exceptions which will just call
.printStackTrace()
.Creates an ExecutionContext from a given Executor, using the default reporter for uncaught exceptions which will just call
.printStackTrace()
.- e
an Executor
- returns
an ExecutionContext backed by the given Executor
- def fromExecutor(e: Executor, reporter: Consumer[Throwable]): ExecutionContextExecutor
Creates an ExecutionContext from a given Executor, using the given Consumer for reporting errors.
Creates an ExecutionContext from a given Executor, using the given Consumer for reporting errors. The latter can be created as in the following example:
final ExecutionContext ec = Converter.fromExecutor(es, thr -> thr.printStackTrace());
- e
an Executor
- reporter
a Consumer for reporting errors during execution
- returns
an ExecutionContext backed by the given Executor
- def fromExecutorService(e: ExecutorService): ExecutionContextExecutorService
Creates an ExecutionContext from a given ExecutorService, using the default reporter for uncaught exceptions which will just call
.printStackTrace()
.Creates an ExecutionContext from a given ExecutorService, using the default reporter for uncaught exceptions which will just call
.printStackTrace()
.- e
an ExecutorService
- returns
an ExecutionContext backed by the given ExecutorService
- def fromExecutorService(e: ExecutorService, reporter: Consumer[Throwable]): ExecutionContextExecutorService
Creates an ExecutionContext from a given ExecutorService, using the given Consumer for reporting errors.
Creates an ExecutionContext from a given ExecutorService, using the given Consumer for reporting errors. The latter can be created as in the following example:
final ExecutionContext ec = Converter.fromExecutorService(es, thr -> thr.printStackTrace());
- e
an ExecutorService
- reporter
a Consumer for reporting errors during execution
- returns
an ExecutionContext backed by the given ExecutorService
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def globalExecutionContext: ExecutionContext
Return the global ExecutionContext for Scala Futures.
Return the global ExecutionContext for Scala Futures.
- returns
the ExecutionContext
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def keptPromise[T](v: T): Promise[T]
Construct an already fulfilled
scala.concurrent.Promise
which holds the given value.Construct an already fulfilled
scala.concurrent.Promise
which holds the given value.- returns
the fulfilled Promise
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def promise[T](): Promise[T]
Construct an empty
scala.concurrent.Promise
.Construct an empty
scala.concurrent.Promise
.- returns
a Promise which is not yet completed
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toJava[T](f: Future[T]): CompletionStage[T]
Returns a CompletionStage that will be completed with the same value or exception as the given Scala Future when that completes.
Returns a CompletionStage that will be completed with the same value or exception as the given Scala Future when that completes. Since the Future is a read-only representation, this CompletionStage does not support the
toCompletableFuture
method. The semantics of Scala Future demand that all callbacks are invoked asynchronously by default, therefore the returned CompletionStage routes all calls to synchronous transformations to their asynchronous counterparts, i.e.thenRun
will internally callthenRunAsync
.- f
The Scala Future which may eventually supply the completion for the returned CompletionStage
- returns
a CompletionStage that runs all callbacks asynchronously and does not support the CompletableFuture interface
- def toScala[T](cs: CompletionStage[T]): Future[T]
Returns a Scala Future that will be completed with the same value or exception as the given CompletionStage when that completes.
Returns a Scala Future that will be completed with the same value or exception as the given CompletionStage when that completes. Transformations of the returned Future are executed asynchronously as specified by the ExecutionContext that is given to the combinator methods.
- cs
The CompletionStage which may eventually supply the completion for the returned Scala Future
- returns
a Scala Future that represents the CompletionStage's completion
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()