From 6fb46d40c7fa0ced876eb388a2d8b062ec3f97f5 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Wed, 15 Sep 2010 16:49:05 +0200 Subject: [PATCH] Closing #264, addign JavaAPI to DataFlowVariable --- .../scala/dataflow/DataFlowVariable.scala | 29 +++++++++++++++++++ akka-actor/src/main/scala/util/JavaAPI.scala | 9 +++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/akka-actor/src/main/scala/dataflow/DataFlowVariable.scala b/akka-actor/src/main/scala/dataflow/DataFlowVariable.scala index 787793dc5f..6608f6075b 100644 --- a/akka-actor/src/main/scala/dataflow/DataFlowVariable.scala +++ b/akka-actor/src/main/scala/dataflow/DataFlowVariable.scala @@ -11,6 +11,7 @@ import se.scalablesolutions.akka.actor.{Actor, ActorRef} import se.scalablesolutions.akka.actor.Actor._ import se.scalablesolutions.akka.dispatch.CompletableFuture import se.scalablesolutions.akka.AkkaException +import se.scalablesolutions.akka.util.{ Function, SideEffect } /** * Implements Oz-style dataflow (single assignment) variables. @@ -27,9 +28,22 @@ object DataFlow { */ def thread(body: => Unit): Unit = spawn(body) + /** Executes the supplied SideEffect in another thread + * JavaAPI + */ + def thread(body: SideEffect): Unit = spawn(body.apply) + + /** Executes the supplied function in another thread + */ def thread[A <: AnyRef, R <: AnyRef](body: A => R) = actorOf(new ReactiveEventBasedThread(body)).start + /** Executes the supplied Function in another thread + * JavaAPI + */ + def thread[A <: AnyRef, R <: AnyRef](body: Function[A,R]) = + actorOf(new ReactiveEventBasedThread(body.apply)).start + private class ReactiveEventBasedThread[A <: AnyRef, T <: AnyRef](body: A => T) extends Actor { def receive = { @@ -91,6 +105,11 @@ object DataFlow { "Attempt to change data flow variable (from [" + this.value.get + "] to [" + ref() + "])") } + /** Sets the value of this variable (if unset) with the value of the supplied variable + * JavaAPI + */ + def set(ref: DataFlowVariable[T]) { this << ref } + /** Sets the value of this variable (if unset) */ def <<(value: T) { @@ -99,6 +118,16 @@ object DataFlow { "Attempt to change data flow variable (from [" + this.value.get + "] to [" + value + "])") } + /** Sets the value of this variable (if unset) with the value of the supplied variable + * JavaAPI + */ + def set(value: T) { this << value } + + /** Retrieves the value of variable + * throws a DataFlowVariableException if it times out + */ + def get(): T = this() + /** Retrieves the value of variable * throws a DataFlowVariableException if it times out */ diff --git a/akka-actor/src/main/scala/util/JavaAPI.scala b/akka-actor/src/main/scala/util/JavaAPI.scala index 1d39a0864b..099082595d 100644 --- a/akka-actor/src/main/scala/util/JavaAPI.scala +++ b/akka-actor/src/main/scala/util/JavaAPI.scala @@ -13,4 +13,11 @@ trait Function[T,R] { */ trait Procedure[T] { def apply(param: T): Unit -} \ No newline at end of file +} + +/** + * An executable piece of code that takes no parameters and doesn't return any value + */ +trait SideEffect { + def apply: Unit +}