=typ #22736 expose Behavior.Same and friends

This commit is contained in:
Konrad `ktoso` Malawski 2017-04-21 20:05:43 +09:00
parent 4b12ce2376
commit 6c40da71dd
4 changed files with 46 additions and 19 deletions

View file

@ -192,7 +192,7 @@ public abstract class Actor {
* to reuse the previous behavior, including the hint that the message has not
* been handled. This hint may be used by composite behaviors that delegate
* (partial) handling to other behaviors.
*
*
* @return pseudo-behavior marking unhandled
*/
static public <T> Behavior<T> unhandled() {
@ -405,7 +405,7 @@ public abstract class Actor {
* By default, this method returns `unhandled`.
*/
public Behavior<T> onSignal(Signal msg) throws Exception {
return Actor.unhandled();
return unhandled();
}
}

View file

@ -79,6 +79,39 @@ abstract class ExtensibleBehavior[T] extends Behavior[T] {
object Behavior {
/**
* Return this behavior from message processing in order to advise the
* system to reuse the previous behavior. This is provided in order to
* avoid the allocation overhead of recreating the current behavior where
* that is not necessary.
*/
def same[T]: Behavior[T] = SameBehavior.asInstanceOf[Behavior[T]]
/**
* Return this behavior from message processing in order to advise the
* system to reuse the previous behavior, including the hint that the
* message has not been handled. This hint may be used by composite
* behaviors that delegate (partial) handling to other behaviors.
*/
def unhandled[T]: Behavior[T] = UnhandledBehavior.asInstanceOf[Behavior[T]]
/**
* Return this behavior from message processing to signal that this actor
* shall terminate voluntarily. If this actor has created child actors then
* these will be stopped as part of the shutdown procedure. The PostStop
* signal that results from stopping this actor will NOT be passed to the
* current behavior, it will be effectively ignored.
*/
def stopped[T]: Behavior[T] = StoppedBehavior.asInstanceOf[Behavior[T]]
/**
* A behavior that treats every incoming message as unhandled.
*/
def empty[T]: Behavior[T] = EmptyBehavior.asInstanceOf[Behavior[T]]
/**
* A behavior that ignores every incoming message and returns same.
*/
def ignore[T]: Behavior[T] = IgnoreBehavior.asInstanceOf[Behavior[T]]
/**
* INTERNAL API.
*/

View file

@ -7,7 +7,7 @@ package patterns
import scala.reflect.ClassTag
import scala.util.control.NonFatal
import akka.event.Logging
import akka.typed.Behavior._
import akka.typed.scaladsl.Actor
import akka.typed.scaladsl.Actor._
/**
@ -22,7 +22,7 @@ final case class Restarter[T, Thr <: Throwable: ClassTag](initialBehavior: Behav
private def restart(ctx: ActorContext[T], startedBehavior: Behavior[T]): Behavior[T] = {
try Behavior.interpretSignal(startedBehavior, ctx, PreRestart) catch { case NonFatal(_) }
// no need to canonicalize, it's done in the calling methods
validateAsInitial(undefer(initialBehavior, ctx))
Behavior.validateAsInitial(Behavior.undefer(initialBehavior, ctx))
}
private def canonical(b: Behavior[T], ctx: ActorContext[T]): Behavior[T] =
@ -32,8 +32,8 @@ final case class Restarter[T, Thr <: Throwable: ClassTag](initialBehavior: Behav
else if (b eq behavior) Same
else {
b match {
case d: DeferredBehavior[_] canonical(Behavior.undefer(d, ctx), ctx)
case b Restarter[T, Thr](initialBehavior, resume)(b)
case d: Behavior.DeferredBehavior[_] canonical(Behavior.undefer(d, ctx), ctx)
case b Restarter[T, Thr](initialBehavior, resume)(b)
}
}
@ -79,12 +79,12 @@ final case class MutableRestarter[T, Thr <: Throwable: ClassTag](initialBehavior
private[this] var current: Behavior[T] = _
private def startCurrent(ctx: ActorContext[T]) = {
// we need to undefer it if needed the first time we have access to a context
if (current eq null) current = validateAsInitial(undefer(initialBehavior, ctx))
if (current eq null) current = Behavior.validateAsInitial(Behavior.undefer(initialBehavior, ctx))
}
private def restart(ctx: ActorContext[T]): Behavior[T] = {
try Behavior.interpretSignal(current, ctx, PreRestart) catch { case NonFatal(_) }
validateAsInitial(undefer(initialBehavior, ctx))
Behavior.validateAsInitial(Behavior.undefer(initialBehavior, ctx))
}
override def management(ctx: ActorContext[T], signal: Signal): Behavior[T] = {

View file

@ -287,7 +287,7 @@ object Actor {
* avoid the allocation overhead of recreating the current behavior where
* that is not necessary.
*/
def Same[T]: Behavior[T] = SameBehavior.asInstanceOf[Behavior[T]]
def Same[T]: Behavior[T] = Behavior.same
/**
* Return this behavior from message processing in order to advise the
@ -295,13 +295,7 @@ object Actor {
* message has not been handled. This hint may be used by composite
* behaviors that delegate (partial) handling to other behaviors.
*/
def Unhandled[T]: Behavior[T] = UnhandledBehavior.asInstanceOf[Behavior[T]]
/*
* TODO write a Behavior that waits for all child actors to stop and then
* runs some cleanup before stopping. The factory for this behavior should
* stop and watch all children to get the process started.
*/
def Unhandled[T]: Behavior[T] = Behavior.unhandled
/**
* Return this behavior from message processing to signal that this actor
@ -310,17 +304,17 @@ object Actor {
* signal that results from stopping this actor will NOT be passed to the
* current behavior, it will be effectively ignored.
*/
def Stopped[T]: Behavior[T] = StoppedBehavior.asInstanceOf[Behavior[T]]
def Stopped[T]: Behavior[T] = Behavior.stopped
/**
* A behavior that treats every incoming message as unhandled.
*/
def Empty[T]: Behavior[T] = EmptyBehavior.asInstanceOf[Behavior[T]]
def Empty[T]: Behavior[T] = Behavior.empty
/**
* A behavior that ignores every incoming message and returns same.
*/
def Ignore[T]: Behavior[T] = IgnoreBehavior.asInstanceOf[Behavior[T]]
def Ignore[T]: Behavior[T] = Behavior.ignore
/**
* Construct an actor behavior that can react to both incoming messages and