2009-05-25 14:48:43 +02:00
|
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2009-05-25 14:48:43 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
2010-10-26 12:49:25 +02:00
|
|
|
|
package akka.dispatch
|
2009-05-25 14:48:43 +02:00
|
|
|
|
|
2012-07-04 15:25:30 +02:00
|
|
|
|
import scala.runtime.{ BoxedUnit, AbstractPartialFunction }
|
2012-07-22 21:40:09 +02:00
|
|
|
|
import akka.japi.{ Function ⇒ JFunc, Option ⇒ JOption, Procedure }
|
|
|
|
|
|
import scala.concurrent.{ Future, Promise, ExecutionContext, ExecutionContextExecutor, ExecutionContextExecutorService }
|
2011-05-18 17:25:30 +02:00
|
|
|
|
import java.lang.{ Iterable ⇒ JIterable }
|
|
|
|
|
|
import java.util.{ LinkedList ⇒ JLinkedList }
|
2012-07-22 21:40:09 +02:00
|
|
|
|
import java.util.concurrent.{ Executor, ExecutorService, ExecutionException, Callable, TimeoutException }
|
2012-08-20 15:21:44 +02:00
|
|
|
|
import scala.util.{ Try, Success, Failure }
|
2012-07-22 21:40:09 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* ExecutionContexts is the Java API for ExecutionContexts
|
|
|
|
|
|
*/
|
|
|
|
|
|
object ExecutionContexts {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns a new ExecutionContextExecutor which will delegate execution to the underlying Executor,
|
|
|
|
|
|
* and which will use the default error reporter.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param executor the Executor which will be used for the ExecutionContext
|
|
|
|
|
|
* @return a new ExecutionContext
|
|
|
|
|
|
*/
|
|
|
|
|
|
def fromExecutor(executor: Executor): ExecutionContextExecutor =
|
|
|
|
|
|
ExecutionContext.fromExecutor(executor)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns a new ExecutionContextExecutor which will delegate execution to the underlying Executor,
|
|
|
|
|
|
* and which will use the provided error reporter.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param executor the Executor which will be used for the ExecutionContext
|
|
|
|
|
|
* @param errorReporter a Procedure that will log any exceptions passed to it
|
|
|
|
|
|
* @return a new ExecutionContext
|
|
|
|
|
|
*/
|
|
|
|
|
|
def fromExecutor(executor: Executor, errorReporter: Procedure[Throwable]): ExecutionContextExecutor =
|
|
|
|
|
|
ExecutionContext.fromExecutor(executor, errorReporter.apply)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns a new ExecutionContextExecutorService which will delegate execution to the underlying ExecutorService,
|
|
|
|
|
|
* and which will use the default error reporter.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param executor the ExecutorService which will be used for the ExecutionContext
|
|
|
|
|
|
* @return a new ExecutionContext
|
|
|
|
|
|
*/
|
|
|
|
|
|
def fromExecutorService(executorService: ExecutorService): ExecutionContextExecutorService =
|
|
|
|
|
|
ExecutionContext.fromExecutorService(executorService)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns a new ExecutionContextExecutorService which will delegate execution to the underlying ExecutorService,
|
|
|
|
|
|
* and which will use the provided error reporter.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param executor the ExecutorService which will be used for the ExecutionContext
|
|
|
|
|
|
* @param errorReporter a Procedure that will log any exceptions passed to it
|
|
|
|
|
|
* @return a new ExecutionContext
|
|
|
|
|
|
*/
|
|
|
|
|
|
def fromExecutorService(executorService: ExecutorService, errorReporter: Procedure[Throwable]): ExecutionContextExecutorService =
|
|
|
|
|
|
ExecutionContext.fromExecutorService(executorService, errorReporter.apply)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @return a reference to the global ExecutionContext
|
|
|
|
|
|
*/
|
|
|
|
|
|
def global(): ExecutionContext = ExecutionContext.global
|
|
|
|
|
|
}
|
2011-12-11 00:40:52 +01:00
|
|
|
|
|
2011-12-15 16:56:53 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Futures is the Java API for Futures and Promises
|
|
|
|
|
|
*/
|
2011-12-12 14:39:10 +01:00
|
|
|
|
object Futures {
|
2011-02-28 16:29:03 -07:00
|
|
|
|
|
2010-11-12 14:04:06 +01:00
|
|
|
|
/**
|
2011-03-22 17:20:35 +01:00
|
|
|
|
* Java API, equivalent to Future.apply
|
2010-11-12 14:04:06 +01:00
|
|
|
|
*/
|
2012-07-22 13:38:12 +02:00
|
|
|
|
def future[T](body: Callable[T], executor: ExecutionContext): Future[T] = Future(body.call)(executor)
|
2011-06-18 23:23:47 -06:00
|
|
|
|
|
2010-10-26 16:40:09 +02:00
|
|
|
|
/**
|
2011-12-11 00:40:52 +01:00
|
|
|
|
* Java API, equivalent to Promise.apply
|
2010-10-26 16:40:09 +02:00
|
|
|
|
*/
|
2012-07-22 13:38:12 +02:00
|
|
|
|
def promise[T](): Promise[T] = Promise[T]()
|
2010-11-12 12:54:48 +01:00
|
|
|
|
|
2011-12-15 16:56:53 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Java API, creates an already completed Promise with the specified exception
|
|
|
|
|
|
*/
|
2012-07-22 13:38:12 +02:00
|
|
|
|
def failed[T](exception: Throwable): Future[T] = Future.failed(exception)
|
2011-12-15 16:56:53 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Java API, Creates an already completed Promise with the specified result
|
|
|
|
|
|
*/
|
2012-07-22 13:38:12 +02:00
|
|
|
|
def successful[T](result: T): Future[T] = Future.successful(result)
|
2011-12-15 16:56:53 +01:00
|
|
|
|
|
2011-08-26 17:25:18 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Java API.
|
|
|
|
|
|
* Returns a Future that will hold the optional result of the first Future with a result that matches the predicate
|
|
|
|
|
|
*/
|
2011-12-30 13:48:31 +01:00
|
|
|
|
def find[T <: AnyRef](futures: JIterable[Future[T]], predicate: JFunc[T, java.lang.Boolean], executor: ExecutionContext): Future[JOption[T]] = {
|
2012-07-17 17:21:08 +02:00
|
|
|
|
implicit val ec = executor
|
2011-12-30 13:48:31 +01:00
|
|
|
|
Future.find[T]((scala.collection.JavaConversions.iterableAsScalaIterable(futures)))(predicate.apply(_))(executor).map(JOption.fromScalaOption(_))
|
2011-08-26 17:25:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-03-22 22:12:16 +01:00
|
|
|
|
/**
|
2011-03-30 21:19:26 +02:00
|
|
|
|
* Java API.
|
2011-03-22 22:12:16 +01:00
|
|
|
|
* Returns a Future to the result of the first future in the list that is completed
|
|
|
|
|
|
*/
|
2011-12-30 13:48:31 +01:00
|
|
|
|
def firstCompletedOf[T <: AnyRef](futures: JIterable[Future[T]], executor: ExecutionContext): Future[T] =
|
|
|
|
|
|
Future.firstCompletedOf(scala.collection.JavaConversions.iterableAsScalaIterable(futures))(executor)
|
2011-10-06 21:19:46 +02:00
|
|
|
|
|
2011-03-22 22:12:16 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Java API
|
2011-12-27 12:02:55 +01:00
|
|
|
|
* A non-blocking fold over the specified futures, with the start value of the given zero.
|
2011-03-22 22:12:16 +01:00
|
|
|
|
* The fold is performed on the thread where the last future is completed,
|
|
|
|
|
|
* the result will be the first failure of any of the futures, or any failure in the actual fold,
|
|
|
|
|
|
* or the result of the fold.
|
|
|
|
|
|
*/
|
2011-12-30 13:48:31 +01:00
|
|
|
|
def fold[T <: AnyRef, R <: AnyRef](zero: R, futures: JIterable[Future[T]], fun: akka.japi.Function2[R, T, R], executor: ExecutionContext): Future[R] =
|
2012-01-19 17:41:53 +01:00
|
|
|
|
Future.fold(scala.collection.JavaConversions.iterableAsScalaIterable(futures))(zero)(fun.apply)(executor)
|
2011-06-18 23:23:47 -06:00
|
|
|
|
|
2011-03-22 22:12:16 +01:00
|
|
|
|
/**
|
2011-03-30 21:19:26 +02:00
|
|
|
|
* Java API.
|
2012-07-13 12:21:55 +02:00
|
|
|
|
* Reduces the results of the supplied futures and binary function.
|
2011-03-22 22:12:16 +01:00
|
|
|
|
*/
|
2012-01-19 17:41:53 +01:00
|
|
|
|
def reduce[T <: AnyRef, R >: T](futures: JIterable[Future[T]], fun: akka.japi.Function2[R, T, R], executor: ExecutionContext): Future[R] =
|
|
|
|
|
|
Future.reduce[T, R](scala.collection.JavaConversions.iterableAsScalaIterable(futures))(fun.apply)(executor)
|
2011-10-06 21:19:46 +02:00
|
|
|
|
|
2011-04-15 13:09:53 -06:00
|
|
|
|
/**
|
|
|
|
|
|
* Java API.
|
2011-12-14 01:24:55 +01:00
|
|
|
|
* Simple version of Future.traverse. Transforms a JIterable[Future[A]] into a Future[JIterable[A]].
|
2011-04-15 13:09:53 -06:00
|
|
|
|
* Useful for reducing many Futures into a single Future.
|
|
|
|
|
|
*/
|
2011-12-30 13:48:31 +01:00
|
|
|
|
def sequence[A](in: JIterable[Future[A]], executor: ExecutionContext): Future[JIterable[A]] = {
|
|
|
|
|
|
implicit val d = executor
|
2012-08-02 12:15:29 +02:00
|
|
|
|
scala.collection.JavaConversions.iterableAsScalaIterable(in).foldLeft(Future(new JLinkedList[A]())) { (fr, fa) ⇒
|
|
|
|
|
|
for (r ← fr; a ← fa) yield { r add a; r }
|
|
|
|
|
|
}
|
2011-10-06 21:19:46 +02:00
|
|
|
|
}
|
2011-01-24 16:37:08 +01:00
|
|
|
|
|
2011-03-30 21:19:26 +02:00
|
|
|
|
/**
|
2011-04-15 13:09:53 -06:00
|
|
|
|
* Java API.
|
2011-12-14 01:24:55 +01:00
|
|
|
|
* Transforms a JIterable[A] into a Future[JIterable[B]] using the provided Function A ⇒ Future[B].
|
2011-04-03 10:40:06 -06:00
|
|
|
|
* This is useful for performing a parallel map. For example, to apply a function to all items of a list
|
2011-04-15 13:09:53 -06:00
|
|
|
|
* in parallel.
|
2011-03-30 21:19:26 +02:00
|
|
|
|
*/
|
2011-12-30 13:48:31 +01:00
|
|
|
|
def traverse[A, B](in: JIterable[A], fn: JFunc[A, Future[B]], executor: ExecutionContext): Future[JIterable[B]] = {
|
|
|
|
|
|
implicit val d = executor
|
2011-05-18 17:25:30 +02:00
|
|
|
|
scala.collection.JavaConversions.iterableAsScalaIterable(in).foldLeft(Future(new JLinkedList[B]())) { (fr, a) ⇒
|
2011-04-15 13:09:53 -06:00
|
|
|
|
val fb = fn(a)
|
2011-12-11 00:40:52 +01:00
|
|
|
|
for (r ← fr; b ← fb) yield { r add b; r }
|
2011-04-15 13:09:53 -06:00
|
|
|
|
}
|
2011-10-06 21:19:46 +02:00
|
|
|
|
}
|
2011-03-18 17:26:53 +01:00
|
|
|
|
}
|
2012-01-26 15:11:49 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This class contains bridge classes between Scala and Java.
|
|
|
|
|
|
* Internal use only.
|
|
|
|
|
|
*/
|
2012-01-26 14:15:25 +01:00
|
|
|
|
object japi {
|
|
|
|
|
|
@deprecated("Do not use this directly, use subclasses of this", "2.0")
|
2012-06-25 13:56:11 +02:00
|
|
|
|
class CallbackBridge[-T] extends AbstractPartialFunction[T, BoxedUnit] {
|
2012-01-26 14:15:25 +01:00
|
|
|
|
override final def isDefinedAt(t: T): Boolean = true
|
2012-02-24 15:03:36 +01:00
|
|
|
|
override final def apply(t: T): BoxedUnit = {
|
|
|
|
|
|
internal(t)
|
|
|
|
|
|
BoxedUnit.UNIT
|
|
|
|
|
|
}
|
2012-01-26 14:15:25 +01:00
|
|
|
|
protected def internal(result: T): Unit = ()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@deprecated("Do not use this directly, use 'Recover'", "2.0")
|
2012-06-25 13:56:11 +02:00
|
|
|
|
class RecoverBridge[+T] extends AbstractPartialFunction[Throwable, T] {
|
2012-01-26 14:15:25 +01:00
|
|
|
|
override final def isDefinedAt(t: Throwable): Boolean = true
|
|
|
|
|
|
override final def apply(t: Throwable): T = internal(t)
|
|
|
|
|
|
protected def internal(result: Throwable): T = null.asInstanceOf[T]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@deprecated("Do not use this directly, use subclasses of this", "2.0")
|
|
|
|
|
|
class BooleanFunctionBridge[-T] extends scala.Function1[T, Boolean] {
|
|
|
|
|
|
override final def apply(t: T): Boolean = internal(t)
|
|
|
|
|
|
protected def internal(result: T): Boolean = false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@deprecated("Do not use this directly, use subclasses of this", "2.0")
|
2012-02-24 15:03:36 +01:00
|
|
|
|
class UnitFunctionBridge[-T] extends (T ⇒ BoxedUnit) {
|
2012-06-25 13:56:11 +02:00
|
|
|
|
final def apply$mcLJ$sp(l: Long): BoxedUnit = { internal(l.asInstanceOf[T]); BoxedUnit.UNIT }
|
|
|
|
|
|
final def apply$mcLI$sp(i: Int): BoxedUnit = { internal(i.asInstanceOf[T]); BoxedUnit.UNIT }
|
|
|
|
|
|
final def apply$mcLF$sp(f: Float): BoxedUnit = { internal(f.asInstanceOf[T]); BoxedUnit.UNIT }
|
|
|
|
|
|
final def apply$mcLD$sp(d: Double): BoxedUnit = { internal(d.asInstanceOf[T]); BoxedUnit.UNIT }
|
|
|
|
|
|
override final def apply(t: T): BoxedUnit = { internal(t); BoxedUnit.UNIT }
|
2012-01-26 14:15:25 +01:00
|
|
|
|
protected def internal(result: T): Unit = ()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-26 15:11:49 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Callback for when a Future is completed successfully
|
|
|
|
|
|
* SAM (Single Abstract Method) class
|
|
|
|
|
|
*
|
|
|
|
|
|
* Java API
|
|
|
|
|
|
*/
|
2012-01-26 14:15:25 +01:00
|
|
|
|
abstract class OnSuccess[-T] extends japi.CallbackBridge[T] {
|
|
|
|
|
|
protected final override def internal(result: T) = onSuccess(result)
|
2012-01-26 15:11:49 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This method will be invoked once when/if a Future that this callback is registered on
|
|
|
|
|
|
* becomes successfully completed
|
|
|
|
|
|
*/
|
2012-04-24 17:56:18 +02:00
|
|
|
|
@throws(classOf[Throwable])
|
2012-01-26 14:15:25 +01:00
|
|
|
|
def onSuccess(result: T): Unit
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-26 15:11:49 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Callback for when a Future is completed with a failure
|
|
|
|
|
|
* SAM (Single Abstract Method) class
|
|
|
|
|
|
*
|
|
|
|
|
|
* Java API
|
|
|
|
|
|
*/
|
2012-01-26 14:15:25 +01:00
|
|
|
|
abstract class OnFailure extends japi.CallbackBridge[Throwable] {
|
|
|
|
|
|
protected final override def internal(failure: Throwable) = onFailure(failure)
|
2012-01-26 15:11:49 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This method will be invoked once when/if a Future that this callback is registered on
|
|
|
|
|
|
* becomes completed with a failure
|
|
|
|
|
|
*/
|
2012-04-24 17:56:18 +02:00
|
|
|
|
@throws(classOf[Throwable])
|
2012-01-26 14:15:25 +01:00
|
|
|
|
def onFailure(failure: Throwable): Unit
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-26 15:11:49 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Callback for when a Future is completed with either failure or a success
|
|
|
|
|
|
* SAM (Single Abstract Method) class
|
|
|
|
|
|
*
|
|
|
|
|
|
* Java API
|
|
|
|
|
|
*/
|
2012-08-20 15:21:44 +02:00
|
|
|
|
abstract class OnComplete[-T] extends japi.CallbackBridge[Try[T]] {
|
|
|
|
|
|
protected final override def internal(value: Try[T]): Unit = value match {
|
|
|
|
|
|
case Failure(t) ⇒ onComplete(t, null.asInstanceOf[T])
|
|
|
|
|
|
case Success(r) ⇒ onComplete(null, r)
|
2012-01-26 14:15:25 +01:00
|
|
|
|
}
|
2012-01-26 15:11:49 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This method will be invoked once when/if a Future that this callback is registered on
|
|
|
|
|
|
* becomes completed with a failure or a success.
|
|
|
|
|
|
* In the case of success then "failure" will be null, and in the case of failure the "success" will be null.
|
|
|
|
|
|
*/
|
2012-04-24 17:56:18 +02:00
|
|
|
|
@throws(classOf[Throwable])
|
2012-01-26 14:15:25 +01:00
|
|
|
|
def onComplete(failure: Throwable, success: T): Unit
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-26 15:11:49 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Callback for the Future.recover operation that conditionally turns failures into successes.
|
|
|
|
|
|
*
|
|
|
|
|
|
* SAM (Single Abstract Method) class
|
|
|
|
|
|
*
|
|
|
|
|
|
* Java API
|
|
|
|
|
|
*/
|
2012-01-26 14:15:25 +01:00
|
|
|
|
abstract class Recover[+T] extends japi.RecoverBridge[T] {
|
|
|
|
|
|
protected final override def internal(result: Throwable): T = recover(result)
|
2012-01-26 15:11:49 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This method will be invoked once when/if the Future this recover callback is registered on
|
|
|
|
|
|
* becomes completed with a failure.
|
|
|
|
|
|
*
|
2012-02-06 16:59:09 +01:00
|
|
|
|
* @return a successful value for the passed in failure
|
2012-01-26 15:11:49 +01:00
|
|
|
|
* @throws the passed in failure to propagate it.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Java API
|
|
|
|
|
|
*/
|
|
|
|
|
|
@throws(classOf[Throwable])
|
2012-01-26 14:15:25 +01:00
|
|
|
|
def recover(failure: Throwable): T
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-26 15:11:49 +01:00
|
|
|
|
/**
|
2012-02-26 21:53:17 +01:00
|
|
|
|
* <i><b>Java API (not recommended):</b></i>
|
2012-01-26 15:11:49 +01:00
|
|
|
|
* Callback for the Future.filter operation that creates a new Future which will
|
|
|
|
|
|
* conditionally contain the success of another Future.
|
|
|
|
|
|
*
|
2012-02-26 21:53:17 +01:00
|
|
|
|
* Unfortunately it is not possible to express the type of a Scala filter in
|
|
|
|
|
|
* Java: Function1[T, Boolean], where “Boolean” is the primitive type. It is
|
|
|
|
|
|
* possible to use `Future.filter` by constructing such a function indirectly:
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
2012-02-27 11:08:02 +01:00
|
|
|
|
* import static akka.dispatch.Filter.filterOf;
|
2012-02-26 21:53:17 +01:00
|
|
|
|
* Future<String> f = ...;
|
2012-02-27 11:08:02 +01:00
|
|
|
|
* f.filter(filterOf(new Function<String, Boolean>() {
|
2012-02-26 21:53:17 +01:00
|
|
|
|
* @Override
|
2012-02-27 11:08:02 +01:00
|
|
|
|
* public Boolean apply(String s) {
|
2012-02-26 21:53:17 +01:00
|
|
|
|
* ...
|
|
|
|
|
|
* }
|
|
|
|
|
|
* }));
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* However, `Future.filter` exists mainly to support Scala’s for-comprehensions,
|
|
|
|
|
|
* thus Java users should prefer `Future.map`, translating non-matching values
|
|
|
|
|
|
* to failure cases.
|
2012-01-26 15:11:49 +01:00
|
|
|
|
*/
|
2012-02-25 19:34:59 +01:00
|
|
|
|
object Filter {
|
2012-02-27 11:08:02 +01:00
|
|
|
|
def filterOf[T](f: akka.japi.Function[T, java.lang.Boolean]): (T ⇒ Boolean) =
|
|
|
|
|
|
new Function1[T, Boolean] { def apply(result: T): Boolean = f(result).booleanValue() }
|
2012-02-25 19:34:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-26 15:11:49 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Callback for the Future.foreach operation that will be invoked if the Future that this callback
|
|
|
|
|
|
* is registered on becomes completed with a success. This method is essentially the same operation
|
|
|
|
|
|
* as onSuccess.
|
|
|
|
|
|
*
|
|
|
|
|
|
* SAM (Single Abstract Method) class
|
|
|
|
|
|
* Java API
|
|
|
|
|
|
*/
|
2012-01-26 14:15:25 +01:00
|
|
|
|
abstract class Foreach[-T] extends japi.UnitFunctionBridge[T] {
|
|
|
|
|
|
override final def internal(t: T): Unit = each(t)
|
2012-01-26 15:11:49 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This method will be invoked once when/if a Future that this callback is registered on
|
|
|
|
|
|
* becomes successfully completed
|
|
|
|
|
|
*/
|
2012-04-24 17:56:18 +02:00
|
|
|
|
@throws(classOf[Throwable])
|
2012-01-26 14:15:25 +01:00
|
|
|
|
def each(result: T): Unit
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-26 15:11:49 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Callback for the Future.map and Future.flatMap operations that will be invoked
|
|
|
|
|
|
* if the Future that this callback is registered on becomes completed with a success.
|
|
|
|
|
|
* This callback is the equivalent of an akka.japi.Function
|
|
|
|
|
|
*
|
2012-04-24 17:56:18 +02:00
|
|
|
|
* Override "apply" normally, or "checkedApply" if you need to throw checked exceptions.
|
|
|
|
|
|
*
|
2012-01-26 15:11:49 +01:00
|
|
|
|
* SAM (Single Abstract Method) class
|
|
|
|
|
|
*
|
|
|
|
|
|
* Java API
|
|
|
|
|
|
*/
|
2012-04-24 17:56:18 +02:00
|
|
|
|
abstract class Mapper[-T, +R] extends scala.runtime.AbstractFunction1[T, R] {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Override this method to perform the map operation, by default delegates to "checkedApply"
|
|
|
|
|
|
* which by default throws an UnsupportedOperationException.
|
|
|
|
|
|
*/
|
|
|
|
|
|
def apply(parameter: T): R = checkedApply(parameter)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Override this method if you need to throw checked exceptions
|
|
|
|
|
|
*
|
|
|
|
|
|
* @throws UnsupportedOperation by default
|
|
|
|
|
|
*/
|
|
|
|
|
|
@throws(classOf[Throwable])
|
|
|
|
|
|
def checkedApply(parameter: T): R = throw new UnsupportedOperationException("Mapper.checkedApply has not been implemented")
|
|
|
|
|
|
}
|