forked from dispatch/classic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFutures.scala
39 lines (37 loc) · 1.34 KB
/
Futures.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package dispatch.futures
import java.util.concurrent.{Executors,Callable,ExecutorService}
object Futures extends AvailableFutures {
/** Structural type coinciding with scala.actors.Future */
type Future[T] = Function0[T] {
def isSet: Boolean
}
}
object DefaultFuture extends JucFuture {
lazy val futureExecutor = Executors.newCachedThreadPool
}
trait AvailableFutures {
/** @return values of futures that have completed their processing */
def available[T](fs: Iterable[Futures.Future[T]]) = fs filter { _.isSet } map { _() } toList
}
/** Interface to futures functionality */
trait Futures {
def future[T](result: => T): Futures.Future[T]
}
/** A java.util.concurrent Future accessor, executor undefined */
trait JucFuture extends Futures {
def future[T](result: => T) = new JucFuture(result)
/** Implement to customize the java.util.concurrent Executor, defaults to Executors.newCachedThreadPool */
val futureExecutor: ExecutorService
/** Wraps java.util.concurrent.Future */
class JucFuture[T](f: => T) extends Function0[T] {
val delegate = futureExecutor.submit(new Callable[T]{
def call = f
})
def isSet = delegate.isDone
def apply() = delegate.get()
}
}
/** Future accessor using a scala.actors future */
object ActorsFuture extends Futures {
def future[T](result: => T) = scala.actors.Futures.future(result)
}