Switch order of javadsl EventHandler type params (#25354)
Closes #25188
This commit is contained in:
parent
339fc3d48e
commit
d8a47b6700
8 changed files with 29 additions and 29 deletions
|
|
@ -15,46 +15,46 @@ import akka.util.OptionVal
|
|||
* Used with [[EventHandlerBuilder]] to setup the behavior of a [[PersistentBehavior]]
|
||||
*/
|
||||
@FunctionalInterface
|
||||
trait EventHandler[Event, State] {
|
||||
trait EventHandler[State, Event] {
|
||||
def apply(state: State, event: Event): State
|
||||
}
|
||||
|
||||
object EventHandlerBuilder {
|
||||
def builder[Event, State >: Null](): EventHandlerBuilder[Event, State] =
|
||||
new EventHandlerBuilder[Event, State]()
|
||||
def builder[State >: Null, Event](): EventHandlerBuilder[State, Event] =
|
||||
new EventHandlerBuilder[State, Event]()
|
||||
|
||||
/**
|
||||
* INTERNAL API
|
||||
*/
|
||||
@InternalApi private final case class EventHandlerCase[Event, State](
|
||||
eventPredicate: Event ⇒ Boolean,
|
||||
@InternalApi private final case class EventHandlerCase[State, Event](
|
||||
statePredicate: State ⇒ Boolean,
|
||||
eventPredicate: Event ⇒ Boolean,
|
||||
handler: BiFunction[State, Event, State])
|
||||
}
|
||||
|
||||
final class EventHandlerBuilder[Event, State >: Null]() {
|
||||
final class EventHandlerBuilder[State >: Null, Event]() {
|
||||
import EventHandlerBuilder.EventHandlerCase
|
||||
|
||||
private var cases: List[EventHandlerCase[Event, State]] = Nil
|
||||
private var cases: List[EventHandlerCase[State, Event]] = Nil
|
||||
|
||||
private def addCase(predicate: Event ⇒ Boolean, handler: BiFunction[State, Event, State]): Unit = {
|
||||
cases = EventHandlerCase[Event, State](predicate, _ ⇒ true, handler) :: cases
|
||||
private def addCase(eventPredicate: Event ⇒ Boolean, handler: BiFunction[State, Event, State]): Unit = {
|
||||
cases = EventHandlerCase[State, Event](_ ⇒ true, eventPredicate, handler) :: cases
|
||||
}
|
||||
|
||||
/**
|
||||
* Match any event which is an instance of `E` or a subtype of `E`
|
||||
*/
|
||||
def matchEvent[E <: Event](eventClass: Class[E], biFunction: BiFunction[State, E, State]): EventHandlerBuilder[Event, State] = {
|
||||
def matchEvent[E <: Event](eventClass: Class[E], biFunction: BiFunction[State, E, State]): EventHandlerBuilder[State, Event] = {
|
||||
addCase(e ⇒ eventClass.isAssignableFrom(e.getClass), biFunction.asInstanceOf[BiFunction[State, Event, State]])
|
||||
this
|
||||
}
|
||||
|
||||
def matchEvent[E <: Event, S <: State](eventClass: Class[E], stateClass: Class[S],
|
||||
biFunction: BiFunction[S, E, State]): EventHandlerBuilder[Event, State] = {
|
||||
biFunction: BiFunction[S, E, State]): EventHandlerBuilder[State, Event] = {
|
||||
|
||||
cases = EventHandlerCase[Event, State](
|
||||
eventPredicate = e ⇒ eventClass.isAssignableFrom(e.getClass),
|
||||
cases = EventHandlerCase[State, Event](
|
||||
statePredicate = s ⇒ stateClass.isAssignableFrom(s.getClass),
|
||||
eventPredicate = e ⇒ eventClass.isAssignableFrom(e.getClass),
|
||||
biFunction.asInstanceOf[BiFunction[State, Event, State]]) :: cases
|
||||
this
|
||||
}
|
||||
|
|
@ -64,7 +64,7 @@ final class EventHandlerBuilder[Event, State >: Null]() {
|
|||
*
|
||||
* Builds and returns the handler since this will not let through any states to subsequent match statements
|
||||
*/
|
||||
def matchAny(biFunction: BiFunction[State, Event, State]): EventHandler[Event, State] = {
|
||||
def matchAny(biFunction: BiFunction[State, Event, State]): EventHandler[State, Event] = {
|
||||
addCase(_ ⇒ true, biFunction.asInstanceOf[BiFunction[State, Event, State]])
|
||||
build()
|
||||
}
|
||||
|
|
@ -73,8 +73,8 @@ final class EventHandlerBuilder[Event, State >: Null]() {
|
|||
* Compose this builder with another builder. The handlers in this builder will be tried first followed
|
||||
* by the handlers in `other`.
|
||||
*/
|
||||
def orElse(other: EventHandlerBuilder[Event, State]): EventHandlerBuilder[Event, State] = {
|
||||
val newBuilder = new EventHandlerBuilder[Event, State]
|
||||
def orElse(other: EventHandlerBuilder[State, Event]): EventHandlerBuilder[State, Event] = {
|
||||
val newBuilder = new EventHandlerBuilder[State, Event]
|
||||
// problem with overloaded constructor with `cases` as parameter
|
||||
newBuilder.cases = other.cases ::: cases
|
||||
newBuilder
|
||||
|
|
@ -86,10 +86,10 @@ final class EventHandlerBuilder[Event, State >: Null]() {
|
|||
*
|
||||
* The builder is reset to empty after build has been called.
|
||||
*/
|
||||
def build(): EventHandler[Event, State] = {
|
||||
def build(): EventHandler[State, Event] = {
|
||||
val builtCases = cases.reverse.toArray
|
||||
|
||||
new EventHandler[Event, State] {
|
||||
new EventHandler[State, Event] {
|
||||
def apply(state: State, event: Event): State = {
|
||||
var result: OptionVal[State] = OptionVal.None
|
||||
var idx = 0
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ abstract class PersistentBehavior[Command, Event, State >: Null] private (val pe
|
|||
* For that reason it is strongly discouraged to perform side-effects in this handler;
|
||||
* Side effects should be executed in `andThen` or `recoveryCompleted` blocks.
|
||||
*/
|
||||
protected def eventHandler(): EventHandler[Event, State]
|
||||
protected def eventHandler(): EventHandler[State, Event]
|
||||
|
||||
/**
|
||||
* @param stateClass The handlers defined by this builder are used when the state is an instance of the `stateClass`
|
||||
|
|
@ -85,8 +85,8 @@ abstract class PersistentBehavior[Command, Event, State >: Null] private (val pe
|
|||
/**
|
||||
* @return A new, mutable, event handler builder
|
||||
*/
|
||||
protected final def eventHandlerBuilder(): EventHandlerBuilder[Event, State] =
|
||||
EventHandlerBuilder.builder[Event, State]()
|
||||
protected final def eventHandlerBuilder(): EventHandlerBuilder[State, Event] =
|
||||
EventHandlerBuilder.builder[State, Event]()
|
||||
|
||||
/**
|
||||
* The `callback` function is called to notify the actor that the recovery process
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ public class PersistentActorCompileOnlyTest {
|
|||
|
||||
//#event-handler
|
||||
@Override
|
||||
public EventHandler<SimpleEvent, SimpleState> eventHandler() {
|
||||
public EventHandler<SimpleState, SimpleEvent> eventHandler() {
|
||||
return (state, event) -> state.addEvent(event);
|
||||
}
|
||||
//#event-handler
|
||||
|
|
@ -164,7 +164,7 @@ public class PersistentActorCompileOnlyTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public EventHandler<MyEvent, ExampleState> eventHandler() {
|
||||
public EventHandler<ExampleState, MyEvent> eventHandler() {
|
||||
return eventHandlerBuilder()
|
||||
.matchEvent(Evt.class, (state, event) -> {
|
||||
state.events.add(event.data);
|
||||
|
|
@ -278,7 +278,7 @@ public class PersistentActorCompileOnlyTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public EventHandler<Event, EventsInFlight> eventHandler() {
|
||||
public EventHandler<EventsInFlight, Event> eventHandler() {
|
||||
return eventHandlerBuilder()
|
||||
.matchEvent(IntentRecord.class, (state, event) -> {
|
||||
int nextCorrelationId = event.correlationId;
|
||||
|
|
|
|||
|
|
@ -285,7 +285,7 @@ public class PersistentActorJavaDslTest extends JUnitSuite {
|
|||
}
|
||||
|
||||
@Override
|
||||
public EventHandler<Incremented, State> eventHandler() {
|
||||
public EventHandler<State, Incremented> eventHandler() {
|
||||
return eventHandlerBuilder()
|
||||
.matchEvent(Incremented.class, (state, event) -> {
|
||||
List<Integer> newHistory = new ArrayList<>(state.history);
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ public class BasicPersistentBehaviorsTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public EventHandler<Event, State> eventHandler() {
|
||||
public EventHandler<State, Event> eventHandler() {
|
||||
return (state, event) -> {
|
||||
throw new RuntimeException("TODO: process the event return the next state");
|
||||
};
|
||||
|
|
|
|||
|
|
@ -222,7 +222,7 @@ public class InDepthPersistentBehaviorTest {
|
|||
|
||||
//#event-handler
|
||||
@Override
|
||||
public EventHandler<BlogEvent, BlogState> eventHandler() {
|
||||
public EventHandler<BlogState, BlogEvent> eventHandler() {
|
||||
return eventHandlerBuilder()
|
||||
.matchEvent(PostAdded.class, (state, event) ->
|
||||
new DraftState(event.content, false))
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ public class MovieWatchList extends PersistentBehavior<MovieWatchList.Command, M
|
|||
}
|
||||
|
||||
@Override
|
||||
public EventHandler<Event, MovieList> eventHandler() {
|
||||
public EventHandler<MovieList, Event> eventHandler() {
|
||||
return eventHandlerBuilder()
|
||||
.matchEvent(MovieAdded.class, (state, event) -> state.add(event.movieId))
|
||||
.matchEvent(MovieRemoved.class, (state, event) -> state.remove(event.movieId))
|
||||
|
|
|
|||
|
|
@ -165,7 +165,7 @@ public class OptionalBlogState {
|
|||
}
|
||||
|
||||
@Override
|
||||
public EventHandler<BlogEvent, Optional<BlogState>> eventHandler() {
|
||||
public EventHandler<Optional<BlogState>, BlogEvent> eventHandler() {
|
||||
return eventHandlerBuilder()
|
||||
.matchEvent(PostAdded.class, (state, event) ->
|
||||
Optional.of(new BlogState(event.content, false)))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue