From 88ee18c3f73f495a7a1eb093aa84ec246eb2c27d Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 18 Feb 2019 08:45:01 +0100 Subject: [PATCH] fix more with reply types in persistence-typed examples --- .../javadsl/CommandHandlerWithReply.scala | 42 +++++++++---------- ...ountExampleWithCommandHandlersInState.java | 14 ++++--- ...ccountExampleWithEventHandlersInState.java | 14 ++++--- .../typed/AccountExampleWithMutableState.java | 14 ++++--- .../typed/AccountExampleWithNullState.java | 14 ++++--- 5 files changed, 53 insertions(+), 45 deletions(-) diff --git a/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/CommandHandlerWithReply.scala b/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/CommandHandlerWithReply.scala index f539e3ed48..884f1e6fbc 100644 --- a/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/CommandHandlerWithReply.scala +++ b/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/CommandHandlerWithReply.scala @@ -210,11 +210,11 @@ final class CommandHandlerWithReplyBuilderByState[Command, Event, S <: State, St /** * Matches any command which the given `predicate` returns true for. * - * Note: command handlers are matched in the order they are added. Once a matching is found, it's selected for handling the command + * Note: command handlers are selected in the order they are added. Once a matching is found, it's selected for handling the command * and no further lookup is done. Therefore you must make sure that their matching conditions don't overlap, * otherwise you risk to 'shadow' part of your command handlers. */ - def matchCommand(predicate: Predicate[Command], handler: BiFunction[S, Command, ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { + def onCommand(predicate: Predicate[Command], handler: BiFunction[S, Command, ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { addCase(cmd ⇒ predicate.test(cmd), handler) this } @@ -225,11 +225,11 @@ final class CommandHandlerWithReplyBuilderByState[Command, Event, S <: State, St * Use this when the `State` is not needed in the `handler`, otherwise there is an overloaded method that pass * the state in a `BiFunction`. * - * Note: command handlers are matched in the order they are added. Once a matching is found, it's selected for handling the command + * Note: command handlers are selected in the order they are added. Once a matching is found, it's selected for handling the command * and no further lookup is done. Therefore you must make sure that their matching conditions don't overlap, * otherwise you risk to 'shadow' part of your command handlers. */ - def matchCommand(predicate: Predicate[Command], handler: JFunction[Command, ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { + def onCommand(predicate: Predicate[Command], handler: JFunction[Command, ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { addCase(cmd ⇒ predicate.test(cmd), new BiFunction[S, Command, ReplyEffect[Event, State]] { override def apply(state: S, cmd: Command): ReplyEffect[Event, State] = handler(cmd) }) @@ -239,11 +239,11 @@ final class CommandHandlerWithReplyBuilderByState[Command, Event, S <: State, St /** * Matches commands that are of the given `commandClass` or subclass thereof * - * Note: command handlers are matched in the order they are added. Once a matching is found, it's selected for handling the command + * Note: command handlers are selected in the order they are added. Once a matching is found, it's selected for handling the command * and no further lookup is done. Therefore you must make sure that their matching conditions don't overlap, * otherwise you risk to 'shadow' part of your command handlers. */ - def matchCommand[C <: Command](commandClass: Class[C], handler: BiFunction[S, C, ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { + def onCommand[C <: Command](commandClass: Class[C], handler: BiFunction[S, C, ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { addCase(cmd ⇒ commandClass.isAssignableFrom(cmd.getClass), handler.asInstanceOf[BiFunction[S, Command, ReplyEffect[Event, State]]]) this } @@ -254,12 +254,12 @@ final class CommandHandlerWithReplyBuilderByState[Command, Event, S <: State, St * Use this when the `State` is not needed in the `handler`, otherwise there is an overloaded method that pass * the state in a `BiFunction`. * - * Note: command handlers are matched in the order they are added. Once a matching is found, it's selected for handling the command + * Note: command handlers are selected in the order they are added. Once a matching is found, it's selected for handling the command * and no further lookup is done. Therefore you must make sure that their matching conditions don't overlap, * otherwise you risk to 'shadow' part of your command handlers. */ - def matchCommand[C <: Command](commandClass: Class[C], handler: JFunction[C, ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { - matchCommand[C](commandClass, new BiFunction[S, C, ReplyEffect[Event, State]] { + def onCommand[C <: Command](commandClass: Class[C], handler: JFunction[C, ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { + onCommand[C](commandClass, new BiFunction[S, C, ReplyEffect[Event, State]] { override def apply(state: S, cmd: C): ReplyEffect[Event, State] = handler(cmd) }) } @@ -269,12 +269,12 @@ final class CommandHandlerWithReplyBuilderByState[Command, Event, S <: State, St * * Use this when you just need to initialize the `State` without using any data from the command. * - * Note: command handlers are matched in the order they are added. Once a matching is found, it's selected for handling the command + * Note: command handlers are selected in the order they are added. Once a matching is found, it's selected for handling the command * and no further lookup is done. Therefore you must make sure that their matching conditions don't overlap, * otherwise you risk to 'shadow' part of your command handlers. */ - def matchCommand[C <: Command](commandClass: Class[C], handler: Supplier[ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { - matchCommand[C](commandClass, new BiFunction[S, C, ReplyEffect[Event, State]] { + def onCommand[C <: Command](commandClass: Class[C], handler: Supplier[ReplyEffect[Event, State]]): CommandHandlerWithReplyBuilderByState[Command, Event, S, State] = { + onCommand[C](commandClass, new BiFunction[S, C, ReplyEffect[Event, State]] { override def apply(state: S, cmd: C): ReplyEffect[Event, State] = handler.get() }) } @@ -285,16 +285,16 @@ final class CommandHandlerWithReplyBuilderByState[Command, Event, S <: State, St * Use this to declare a command handler that will match any command. This is particular useful when encoding * a finite state machine in which the final state is not supposed to handle any new command. * - * Note: command handlers are matched in the order they are added. Once a matching is found, it's selected for handling the command + * Note: command handlers are selected in the order they are added. Once a matching is found, it's selected for handling the command * and no further lookup is done. Therefore you must make sure that their matching conditions don't overlap, * otherwise you risk to 'shadow' part of your command handlers. * - * Extra care should be taken when using [[matchAny]] as it will match any command. + * Extra care should be taken when using [[onAnyCommand]] as it will match any command. * This method builds and returns the command handler since this will not let through any states to subsequent match statements. * * @return A CommandHandlerWithReply from the appended states. */ - def matchAny(handler: BiFunction[S, Command, ReplyEffect[Event, State]]): CommandHandlerWithReply[Command, Event, State] = { + def onAnyCommand(handler: BiFunction[S, Command, ReplyEffect[Event, State]]): CommandHandlerWithReply[Command, Event, State] = { addCase(_ ⇒ true, handler) build() } @@ -307,16 +307,16 @@ final class CommandHandlerWithReplyBuilderByState[Command, Event, S <: State, St * * Use this when you just need to return an [[ReplyEffect]] without using any data from the state. * - * Note: command handlers are matched in the order they are added. Once a matching is found, it's selected for handling the command + * Note: command handlers are selected in the order they are added. Once a matching is found, it's selected for handling the command * and no further lookup is done. Therefore you must make sure that their matching conditions don't overlap, * otherwise you risk to 'shadow' part of your command handlers. * - * Extra care should be taken when using [[matchAny]] as it will match any command. + * Extra care should be taken when using [[onAnyCommand]] as it will match any command. * This method builds and returns the command handler since this will not let through any states to subsequent match statements. * * @return A CommandHandlerWithReply from the appended states. */ - def matchAny(handler: JFunction[Command, ReplyEffect[Event, State]]): CommandHandlerWithReply[Command, Event, State] = { + def onAnyCommand(handler: JFunction[Command, ReplyEffect[Event, State]]): CommandHandlerWithReply[Command, Event, State] = { addCase(_ ⇒ true, new BiFunction[S, Command, ReplyEffect[Event, State]] { override def apply(state: S, cmd: Command): ReplyEffect[Event, State] = handler(cmd) }) @@ -330,16 +330,16 @@ final class CommandHandlerWithReplyBuilderByState[Command, Event, S <: State, St * * Use this when you just need to return an [[ReplyEffect]] without using any data from the command or from the state. * - * Note: command handlers are matched in the order they are added. Once a matching is found, it's selected for handling the command + * Note: command handlers are selected in the order they are added. Once a matching is found, it's selected for handling the command * and no further lookup is done. Therefore you must make sure that their matching conditions don't overlap, * otherwise you risk to 'shadow' part of your command handlers. * - * Extra care should be taken when using [[matchAny]] as it will match any command. + * Extra care should be taken when using [[onAnyCommand]] as it will match any command. * This method builds and returns the command handler since this will not let through any states to subsequent match statements. * * @return A CommandHandlerWithReply from the appended states. */ - def matchAny(handler: Supplier[ReplyEffect[Event, State]]): CommandHandlerWithReply[Command, Event, State] = { + def onAnyCommand(handler: Supplier[ReplyEffect[Event, State]]): CommandHandlerWithReply[Command, Event, State] = { addCase(_ ⇒ true, new BiFunction[S, Command, ReplyEffect[Event, State]] { override def apply(state: S, cmd: Command): ReplyEffect[Event, State] = handler.get() }) diff --git a/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithCommandHandlersInState.java b/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithCommandHandlersInState.java index 6557305481..572a6bb084 100644 --- a/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithCommandHandlersInState.java +++ b/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithCommandHandlersInState.java @@ -10,8 +10,8 @@ import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; import akka.persistence.typed.ExpectingReply; import akka.persistence.typed.PersistenceId; -import akka.persistence.typed.javadsl.CommandHandler; -import akka.persistence.typed.javadsl.CommandHandlerBuilder; +import akka.persistence.typed.javadsl.CommandHandlerWithReply; +import akka.persistence.typed.javadsl.CommandHandlerWithReplyBuilder; import akka.persistence.typed.javadsl.EventHandler; import akka.persistence.typed.javadsl.EventHandlerBuilder; import akka.persistence.typed.javadsl.EventSourcedBehaviorWithEnforcedReplies; @@ -240,9 +240,9 @@ public interface AccountExampleWithCommandHandlersInState { } @Override - public CommandHandler commandHandler() { - CommandHandlerBuilder builder = - newCommandHandlerBuilder(); + public CommandHandlerWithReply commandHandler() { + CommandHandlerWithReplyBuilder builder = + newCommandHandlerWithReplyBuilder(); builder .forStateType(EmptyAccount.class) @@ -255,7 +255,9 @@ public interface AccountExampleWithCommandHandlersInState { .onCommand(GetBalance.class, OpenedAccount::getBalance) .onCommand(CloseAccount.class, OpenedAccount::closeAccount); - builder.forStateType(ClosedAccount.class).onAnyCommand(() -> Effect().unhandled()); + builder + .forStateType(ClosedAccount.class) + .onAnyCommand(() -> Effect().unhandled().thenNoReply()); return builder.build(); } diff --git a/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithEventHandlersInState.java b/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithEventHandlersInState.java index 897b4a5695..c2d531251f 100644 --- a/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithEventHandlersInState.java +++ b/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithEventHandlersInState.java @@ -10,8 +10,8 @@ import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; import akka.persistence.typed.ExpectingReply; import akka.persistence.typed.PersistenceId; -import akka.persistence.typed.javadsl.CommandHandler; -import akka.persistence.typed.javadsl.CommandHandlerBuilder; +import akka.persistence.typed.javadsl.CommandHandlerWithReply; +import akka.persistence.typed.javadsl.CommandHandlerWithReplyBuilder; import akka.persistence.typed.javadsl.ReplyEffect; import akka.persistence.typed.javadsl.EventHandler; import akka.persistence.typed.javadsl.EventHandlerBuilder; @@ -209,9 +209,9 @@ public interface AccountExampleWithEventHandlersInState { } @Override - public CommandHandler commandHandler() { - CommandHandlerBuilder builder = - newCommandHandlerBuilder(); + public CommandHandlerWithReply commandHandler() { + CommandHandlerWithReplyBuilder builder = + newCommandHandlerWithReplyBuilder(); builder.forStateType(EmptyAccount.class).onCommand(CreateAccount.class, this::createAccount); @@ -222,7 +222,9 @@ public interface AccountExampleWithEventHandlersInState { .onCommand(GetBalance.class, this::getBalance) .onCommand(CloseAccount.class, this::closeAccount); - builder.forStateType(ClosedAccount.class).onAnyCommand(() -> Effect().unhandled()); + builder + .forStateType(ClosedAccount.class) + .onAnyCommand(() -> Effect().unhandled().thenNoReply()); return builder.build(); } diff --git a/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithMutableState.java b/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithMutableState.java index ee43ca9468..523fe2b518 100644 --- a/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithMutableState.java +++ b/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithMutableState.java @@ -10,8 +10,8 @@ import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; import akka.persistence.typed.ExpectingReply; import akka.persistence.typed.PersistenceId; -import akka.persistence.typed.javadsl.CommandHandler; -import akka.persistence.typed.javadsl.CommandHandlerBuilder; +import akka.persistence.typed.javadsl.CommandHandlerWithReply; +import akka.persistence.typed.javadsl.CommandHandlerWithReplyBuilder; import akka.persistence.typed.javadsl.EventHandler; import akka.persistence.typed.javadsl.EventHandlerBuilder; import akka.persistence.typed.javadsl.EventSourcedBehaviorWithEnforcedReplies; @@ -203,9 +203,9 @@ public interface AccountExampleWithMutableState { } @Override - public CommandHandler commandHandler() { - CommandHandlerBuilder builder = - newCommandHandlerBuilder(); + public CommandHandlerWithReply commandHandler() { + CommandHandlerWithReplyBuilder builder = + newCommandHandlerWithReplyBuilder(); builder.forStateType(EmptyAccount.class).onCommand(CreateAccount.class, this::createAccount); @@ -216,7 +216,9 @@ public interface AccountExampleWithMutableState { .onCommand(GetBalance.class, this::getBalance) .onCommand(CloseAccount.class, this::closeAccount); - builder.forStateType(ClosedAccount.class).onAnyCommand(() -> Effect().unhandled()); + builder + .forStateType(ClosedAccount.class) + .onAnyCommand(() -> Effect().unhandled().thenNoReply()); return builder.build(); } diff --git a/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithNullState.java b/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithNullState.java index 41d2719b0a..d443036860 100644 --- a/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithNullState.java +++ b/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/AccountExampleWithNullState.java @@ -10,8 +10,8 @@ import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; import akka.persistence.typed.ExpectingReply; import akka.persistence.typed.PersistenceId; -import akka.persistence.typed.javadsl.CommandHandler; -import akka.persistence.typed.javadsl.CommandHandlerBuilder; +import akka.persistence.typed.javadsl.CommandHandlerWithReply; +import akka.persistence.typed.javadsl.CommandHandlerWithReplyBuilder; import akka.persistence.typed.javadsl.EventHandler; import akka.persistence.typed.javadsl.EventHandlerBuilder; import akka.persistence.typed.javadsl.EventSourcedBehaviorWithEnforcedReplies; @@ -201,9 +201,9 @@ public interface AccountExampleWithNullState { } @Override - public CommandHandler commandHandler() { - CommandHandlerBuilder builder = - newCommandHandlerBuilder(); + public CommandHandlerWithReply commandHandler() { + CommandHandlerWithReplyBuilder builder = + newCommandHandlerWithReplyBuilder(); builder.forNullState().onCommand(CreateAccount.class, this::createAccount); @@ -214,7 +214,9 @@ public interface AccountExampleWithNullState { .onCommand(GetBalance.class, this::getBalance) .onCommand(CloseAccount.class, this::closeAccount); - builder.forStateType(ClosedAccount.class).onAnyCommand(() -> Effect().unhandled()); + builder + .forStateType(ClosedAccount.class) + .onAnyCommand(() -> Effect().unhandled().thenNoReply()); return builder.build(); }