code style touch up of FSM.scala
This commit is contained in:
parent
b2c63062fd
commit
ec1433c156
2 changed files with 24 additions and 47 deletions
|
|
@ -193,14 +193,12 @@ trait FSM[S, D] extends Listeners {
|
||||||
* @param stateTimeout default state timeout for this state
|
* @param stateTimeout default state timeout for this state
|
||||||
* @param stateFunction partial function describing response to input
|
* @param stateFunction partial function describing response to input
|
||||||
*/
|
*/
|
||||||
protected final def when(stateName: S, stateTimeout: Duration = null)(stateFunction: StateFunction) = {
|
protected final def when(stateName: S, stateTimeout: Duration = null)(stateFunction: StateFunction): Unit =
|
||||||
register(stateName, stateFunction, Option(stateTimeout))
|
register(stateName, stateFunction, Option(stateTimeout))
|
||||||
}
|
|
||||||
|
|
||||||
@deprecated("use the more import-friendly variant taking a Duration", "2.0")
|
@deprecated("use the more import-friendly variant taking a Duration", "2.0")
|
||||||
protected final def when(stateName: S, stateTimeout: Timeout)(stateFunction: StateFunction) = {
|
protected final def when(stateName: S, stateTimeout: Timeout)(stateFunction: StateFunction): Unit =
|
||||||
register(stateName, stateFunction, stateTimeout)
|
register(stateName, stateFunction, stateTimeout)
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set initial state. Call this method from the constructor before the #initialize method.
|
* Set initial state. Call this method from the constructor before the #initialize method.
|
||||||
|
|
@ -211,9 +209,8 @@ trait FSM[S, D] extends Listeners {
|
||||||
*/
|
*/
|
||||||
protected final def startWith(stateName: S,
|
protected final def startWith(stateName: S,
|
||||||
stateData: D,
|
stateData: D,
|
||||||
timeout: Timeout = None) = {
|
timeout: Timeout = None): Unit =
|
||||||
currentState = FSM.State(stateName, stateData, timeout)
|
currentState = FSM.State(stateName, stateData, timeout)
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produce transition to other state. Return this from a state function in
|
* Produce transition to other state. Return this from a state function in
|
||||||
|
|
@ -222,9 +219,7 @@ trait FSM[S, D] extends Listeners {
|
||||||
* @param nextStateName state designator for the next state
|
* @param nextStateName state designator for the next state
|
||||||
* @return state transition descriptor
|
* @return state transition descriptor
|
||||||
*/
|
*/
|
||||||
protected final def goto(nextStateName: S): State = {
|
protected final def goto(nextStateName: S): State = FSM.State(nextStateName, currentState.stateData)
|
||||||
FSM.State(nextStateName, currentState.stateData)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produce "empty" transition descriptor. Return this from a state function
|
* Produce "empty" transition descriptor. Return this from a state function
|
||||||
|
|
@ -232,31 +227,22 @@ trait FSM[S, D] extends Listeners {
|
||||||
*
|
*
|
||||||
* @return descriptor for staying in current state
|
* @return descriptor for staying in current state
|
||||||
*/
|
*/
|
||||||
protected final def stay(): State = {
|
protected final def stay(): State = goto(currentState.stateName) // cannot directly use currentState because of the timeout field
|
||||||
// cannot directly use currentState because of the timeout field
|
|
||||||
goto(currentState.stateName)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produce change descriptor to stop this FSM actor with reason "Normal".
|
* Produce change descriptor to stop this FSM actor with reason "Normal".
|
||||||
*/
|
*/
|
||||||
protected final def stop(): State = {
|
protected final def stop(): State = stop(Normal)
|
||||||
stop(Normal)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produce change descriptor to stop this FSM actor including specified reason.
|
* Produce change descriptor to stop this FSM actor including specified reason.
|
||||||
*/
|
*/
|
||||||
protected final def stop(reason: Reason): State = {
|
protected final def stop(reason: Reason): State = stop(reason, currentState.stateData)
|
||||||
stop(reason, currentState.stateData)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produce change descriptor to stop this FSM actor including specified reason.
|
* Produce change descriptor to stop this FSM actor including specified reason.
|
||||||
*/
|
*/
|
||||||
protected final def stop(reason: Reason, stateData: D): State = {
|
protected final def stop(reason: Reason, stateData: D): State = stay using stateData withStopReason (reason)
|
||||||
stay using stateData withStopReason (reason)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Schedule named timer to deliver message after given delay, possibly repeating.
|
* Schedule named timer to deliver message after given delay, possibly repeating.
|
||||||
|
|
@ -280,12 +266,11 @@ trait FSM[S, D] extends Listeners {
|
||||||
* Cancel named timer, ensuring that the message is not subsequently delivered (no race).
|
* Cancel named timer, ensuring that the message is not subsequently delivered (no race).
|
||||||
* @param name of the timer to cancel
|
* @param name of the timer to cancel
|
||||||
*/
|
*/
|
||||||
protected[akka] def cancelTimer(name: String) = {
|
protected[akka] def cancelTimer(name: String): Unit =
|
||||||
if (timers contains name) {
|
if (timers contains name) {
|
||||||
timers(name).cancel
|
timers(name).cancel
|
||||||
timers -= name
|
timers -= name
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inquire whether the named timer is still active. Returns true unless the
|
* Inquire whether the named timer is still active. Returns true unless the
|
||||||
|
|
@ -298,9 +283,7 @@ trait FSM[S, D] extends Listeners {
|
||||||
* Set state timeout explicitly. This method can safely be used from within a
|
* Set state timeout explicitly. This method can safely be used from within a
|
||||||
* state handler.
|
* state handler.
|
||||||
*/
|
*/
|
||||||
protected final def setStateTimeout(state: S, timeout: Timeout) {
|
protected final def setStateTimeout(state: S, timeout: Timeout): Unit = stateTimeouts(state) = timeout
|
||||||
stateTimeouts(state) = timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This extractor is just convenience for matching a (S, S) pair, including a
|
* This extractor is just convenience for matching a (S, S) pair, including a
|
||||||
|
|
@ -335,9 +318,7 @@ trait FSM[S, D] extends Listeners {
|
||||||
* <b>Multiple handlers may be installed, and every one of them will be
|
* <b>Multiple handlers may be installed, and every one of them will be
|
||||||
* called, not only the first one matching.</b>
|
* called, not only the first one matching.</b>
|
||||||
*/
|
*/
|
||||||
protected final def onTransition(transitionHandler: TransitionHandler) {
|
protected final def onTransition(transitionHandler: TransitionHandler): Unit = transitionEvent :+= transitionHandler
|
||||||
transitionEvent :+= transitionHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience wrapper for using a total function instead of a partial
|
* Convenience wrapper for using a total function instead of a partial
|
||||||
|
|
@ -352,24 +333,20 @@ trait FSM[S, D] extends Listeners {
|
||||||
/**
|
/**
|
||||||
* Set handler which is called upon termination of this FSM actor.
|
* Set handler which is called upon termination of this FSM actor.
|
||||||
*/
|
*/
|
||||||
protected final def onTermination(terminationHandler: PartialFunction[StopEvent[S, D], Unit]) = {
|
protected final def onTermination(terminationHandler: PartialFunction[StopEvent[S, D], Unit]): Unit =
|
||||||
terminateEvent = terminationHandler
|
terminateEvent = terminationHandler
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set handler which is called upon reception of unhandled messages.
|
* Set handler which is called upon reception of unhandled messages.
|
||||||
*/
|
*/
|
||||||
protected final def whenUnhandled(stateFunction: StateFunction) = {
|
protected final def whenUnhandled(stateFunction: StateFunction): Unit =
|
||||||
handleEvent = stateFunction orElse handleEventDefault
|
handleEvent = stateFunction orElse handleEventDefault
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verify existence of initial state and setup timers. This should be the
|
* Verify existence of initial state and setup timers. This should be the
|
||||||
* last call within the constructor.
|
* last call within the constructor.
|
||||||
*/
|
*/
|
||||||
protected final def initialize {
|
protected final def initialize: Unit = makeTransition(currentState)
|
||||||
makeTransition(currentState)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return current state name (i.e. object of type S)
|
* Return current state name (i.e. object of type S)
|
||||||
|
|
@ -412,7 +389,7 @@ trait FSM[S, D] extends Listeners {
|
||||||
private val stateFunctions = mutable.Map[S, StateFunction]()
|
private val stateFunctions = mutable.Map[S, StateFunction]()
|
||||||
private val stateTimeouts = mutable.Map[S, Timeout]()
|
private val stateTimeouts = mutable.Map[S, Timeout]()
|
||||||
|
|
||||||
private def register(name: S, function: StateFunction, timeout: Timeout) {
|
private def register(name: S, function: StateFunction, timeout: Timeout): Unit = {
|
||||||
if (stateFunctions contains name) {
|
if (stateFunctions contains name) {
|
||||||
stateFunctions(name) = stateFunctions(name) orElse function
|
stateFunctions(name) = stateFunctions(name) orElse function
|
||||||
stateTimeouts(name) = timeout orElse stateTimeouts(name)
|
stateTimeouts(name) = timeout orElse stateTimeouts(name)
|
||||||
|
|
@ -492,12 +469,12 @@ trait FSM[S, D] extends Listeners {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def processMsg(value: Any, source: AnyRef) {
|
private def processMsg(value: Any, source: AnyRef): Unit = {
|
||||||
val event = Event(value, currentState.stateData)
|
val event = Event(value, currentState.stateData)
|
||||||
processEvent(event, source)
|
processEvent(event, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
private[akka] def processEvent(event: Event, source: AnyRef) {
|
private[akka] def processEvent(event: Event, source: AnyRef): Unit = {
|
||||||
val stateFunc = stateFunctions(currentState.stateName)
|
val stateFunc = stateFunctions(currentState.stateName)
|
||||||
val nextState = if (stateFunc isDefinedAt event) {
|
val nextState = if (stateFunc isDefinedAt event) {
|
||||||
stateFunc(event)
|
stateFunc(event)
|
||||||
|
|
@ -508,7 +485,7 @@ trait FSM[S, D] extends Listeners {
|
||||||
applyState(nextState)
|
applyState(nextState)
|
||||||
}
|
}
|
||||||
|
|
||||||
private[akka] def applyState(nextState: State) {
|
private[akka] def applyState(nextState: State): Unit = {
|
||||||
nextState.stopReason match {
|
nextState.stopReason match {
|
||||||
case None ⇒ makeTransition(nextState)
|
case None ⇒ makeTransition(nextState)
|
||||||
case _ ⇒
|
case _ ⇒
|
||||||
|
|
@ -518,7 +495,7 @@ trait FSM[S, D] extends Listeners {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private[akka] def makeTransition(nextState: State) {
|
private[akka] def makeTransition(nextState: State): Unit = {
|
||||||
if (!stateFunctions.contains(nextState.stateName)) {
|
if (!stateFunctions.contains(nextState.stateName)) {
|
||||||
terminate(stay withStopReason Failure("Next state %s does not exist".format(nextState.stateName)))
|
terminate(stay withStopReason Failure("Next state %s does not exist".format(nextState.stateName)))
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -539,9 +516,9 @@ trait FSM[S, D] extends Listeners {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override def postStop() { terminate(stay withStopReason Shutdown) }
|
override def postStop(): Unit = { terminate(stay withStopReason Shutdown) }
|
||||||
|
|
||||||
private def terminate(nextState: State) {
|
private def terminate(nextState: State): Unit = {
|
||||||
if (!currentState.stopReason.isDefined) {
|
if (!currentState.stopReason.isDefined) {
|
||||||
val reason = nextState.stopReason.get
|
val reason = nextState.stopReason.get
|
||||||
reason match {
|
reason match {
|
||||||
|
|
@ -598,13 +575,13 @@ trait LoggingFSM[S, D] extends FSM[S, D] { this: Actor ⇒
|
||||||
super.setTimer(name, msg, timeout, repeat)
|
super.setTimer(name, msg, timeout, repeat)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected[akka] abstract override def cancelTimer(name: String) = {
|
protected[akka] abstract override def cancelTimer(name: String): Unit = {
|
||||||
if (debugEvent)
|
if (debugEvent)
|
||||||
log.debug("canceling timer '" + name + "'")
|
log.debug("canceling timer '" + name + "'")
|
||||||
super.cancelTimer(name)
|
super.cancelTimer(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
private[akka] abstract override def processEvent(event: Event, source: AnyRef) {
|
private[akka] abstract override def processEvent(event: Event, source: AnyRef): Unit = {
|
||||||
if (debugEvent) {
|
if (debugEvent) {
|
||||||
val srcstr = source match {
|
val srcstr = source match {
|
||||||
case s: String ⇒ s
|
case s: String ⇒ s
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ abstract class GenericBuncher[A: Manifest, B](val singleTimeout: Duration, val m
|
||||||
case Event(Stop, _) ⇒ stop
|
case Event(Stop, _) ⇒ stop
|
||||||
}
|
}
|
||||||
|
|
||||||
when(Active, stateTimeout = Some(singleTimeout)) {
|
when(Active, stateTimeout = singleTimeout) {
|
||||||
case Event(Msg(m), acc) ⇒
|
case Event(Msg(m), acc) ⇒
|
||||||
stay using merge(acc, m)
|
stay using merge(acc, m)
|
||||||
case Event(StateTimeout, acc) ⇒
|
case Event(StateTimeout, acc) ⇒
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue