Switch order of javadsl EventHandler type params (#25354)

Closes #25188
This commit is contained in:
Christopher Batey 2018-07-24 08:47:45 +01:00 committed by GitHub
parent 339fc3d48e
commit d8a47b6700
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 29 deletions

View file

@ -15,46 +15,46 @@ import akka.util.OptionVal
* Used with [[EventHandlerBuilder]] to setup the behavior of a [[PersistentBehavior]] * Used with [[EventHandlerBuilder]] to setup the behavior of a [[PersistentBehavior]]
*/ */
@FunctionalInterface @FunctionalInterface
trait EventHandler[Event, State] { trait EventHandler[State, Event] {
def apply(state: State, event: Event): State def apply(state: State, event: Event): State
} }
object EventHandlerBuilder { object EventHandlerBuilder {
def builder[Event, State >: Null](): EventHandlerBuilder[Event, State] = def builder[State >: Null, Event](): EventHandlerBuilder[State, Event] =
new EventHandlerBuilder[Event, State]() new EventHandlerBuilder[State, Event]()
/** /**
* INTERNAL API * INTERNAL API
*/ */
@InternalApi private final case class EventHandlerCase[Event, State]( @InternalApi private final case class EventHandlerCase[State, Event](
eventPredicate: Event Boolean,
statePredicate: State Boolean, statePredicate: State Boolean,
eventPredicate: Event Boolean,
handler: BiFunction[State, Event, State]) handler: BiFunction[State, Event, State])
} }
final class EventHandlerBuilder[Event, State >: Null]() { final class EventHandlerBuilder[State >: Null, Event]() {
import EventHandlerBuilder.EventHandlerCase 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 = { private def addCase(eventPredicate: Event Boolean, handler: BiFunction[State, Event, State]): Unit = {
cases = EventHandlerCase[Event, State](predicate, _ true, handler) :: cases cases = EventHandlerCase[State, Event](_ true, eventPredicate, handler) :: cases
} }
/** /**
* Match any event which is an instance of `E` or a subtype of `E` * 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]]) addCase(e eventClass.isAssignableFrom(e.getClass), biFunction.asInstanceOf[BiFunction[State, Event, State]])
this this
} }
def matchEvent[E <: Event, S <: State](eventClass: Class[E], stateClass: Class[S], 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]( cases = EventHandlerCase[State, Event](
eventPredicate = e eventClass.isAssignableFrom(e.getClass),
statePredicate = s stateClass.isAssignableFrom(s.getClass), statePredicate = s stateClass.isAssignableFrom(s.getClass),
eventPredicate = e eventClass.isAssignableFrom(e.getClass),
biFunction.asInstanceOf[BiFunction[State, Event, State]]) :: cases biFunction.asInstanceOf[BiFunction[State, Event, State]]) :: cases
this 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 * 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]]) addCase(_ true, biFunction.asInstanceOf[BiFunction[State, Event, State]])
build() 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 * Compose this builder with another builder. The handlers in this builder will be tried first followed
* by the handlers in `other`. * by the handlers in `other`.
*/ */
def orElse(other: EventHandlerBuilder[Event, State]): EventHandlerBuilder[Event, State] = { def orElse(other: EventHandlerBuilder[State, Event]): EventHandlerBuilder[State, Event] = {
val newBuilder = new EventHandlerBuilder[Event, State] val newBuilder = new EventHandlerBuilder[State, Event]
// problem with overloaded constructor with `cases` as parameter // problem with overloaded constructor with `cases` as parameter
newBuilder.cases = other.cases ::: cases newBuilder.cases = other.cases ::: cases
newBuilder newBuilder
@ -86,10 +86,10 @@ final class EventHandlerBuilder[Event, State >: Null]() {
* *
* The builder is reset to empty after build has been called. * 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 val builtCases = cases.reverse.toArray
new EventHandler[Event, State] { new EventHandler[State, Event] {
def apply(state: State, event: Event): State = { def apply(state: State, event: Event): State = {
var result: OptionVal[State] = OptionVal.None var result: OptionVal[State] = OptionVal.None
var idx = 0 var idx = 0

View file

@ -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; * For that reason it is strongly discouraged to perform side-effects in this handler;
* Side effects should be executed in `andThen` or `recoveryCompleted` blocks. * 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` * @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 * @return A new, mutable, event handler builder
*/ */
protected final def eventHandlerBuilder(): EventHandlerBuilder[Event, State] = protected final def eventHandlerBuilder(): EventHandlerBuilder[State, Event] =
EventHandlerBuilder.builder[Event, State]() EventHandlerBuilder.builder[State, Event]()
/** /**
* The `callback` function is called to notify the actor that the recovery process * The `callback` function is called to notify the actor that the recovery process

View file

@ -103,7 +103,7 @@ public class PersistentActorCompileOnlyTest {
//#event-handler //#event-handler
@Override @Override
public EventHandler<SimpleEvent, SimpleState> eventHandler() { public EventHandler<SimpleState, SimpleEvent> eventHandler() {
return (state, event) -> state.addEvent(event); return (state, event) -> state.addEvent(event);
} }
//#event-handler //#event-handler
@ -164,7 +164,7 @@ public class PersistentActorCompileOnlyTest {
} }
@Override @Override
public EventHandler<MyEvent, ExampleState> eventHandler() { public EventHandler<ExampleState, MyEvent> eventHandler() {
return eventHandlerBuilder() return eventHandlerBuilder()
.matchEvent(Evt.class, (state, event) -> { .matchEvent(Evt.class, (state, event) -> {
state.events.add(event.data); state.events.add(event.data);
@ -278,7 +278,7 @@ public class PersistentActorCompileOnlyTest {
} }
@Override @Override
public EventHandler<Event, EventsInFlight> eventHandler() { public EventHandler<EventsInFlight, Event> eventHandler() {
return eventHandlerBuilder() return eventHandlerBuilder()
.matchEvent(IntentRecord.class, (state, event) -> { .matchEvent(IntentRecord.class, (state, event) -> {
int nextCorrelationId = event.correlationId; int nextCorrelationId = event.correlationId;

View file

@ -285,7 +285,7 @@ public class PersistentActorJavaDslTest extends JUnitSuite {
} }
@Override @Override
public EventHandler<Incremented, State> eventHandler() { public EventHandler<State, Incremented> eventHandler() {
return eventHandlerBuilder() return eventHandlerBuilder()
.matchEvent(Incremented.class, (state, event) -> { .matchEvent(Incremented.class, (state, event) -> {
List<Integer> newHistory = new ArrayList<>(state.history); List<Integer> newHistory = new ArrayList<>(state.history);

View file

@ -43,7 +43,7 @@ public class BasicPersistentBehaviorsTest {
} }
@Override @Override
public EventHandler<Event, State> eventHandler() { public EventHandler<State, Event> eventHandler() {
return (state, event) -> { return (state, event) -> {
throw new RuntimeException("TODO: process the event return the next state"); throw new RuntimeException("TODO: process the event return the next state");
}; };

View file

@ -222,7 +222,7 @@ public class InDepthPersistentBehaviorTest {
//#event-handler //#event-handler
@Override @Override
public EventHandler<BlogEvent, BlogState> eventHandler() { public EventHandler<BlogState, BlogEvent> eventHandler() {
return eventHandlerBuilder() return eventHandlerBuilder()
.matchEvent(PostAdded.class, (state, event) -> .matchEvent(PostAdded.class, (state, event) ->
new DraftState(event.content, false)) new DraftState(event.content, false))

View file

@ -113,7 +113,7 @@ public class MovieWatchList extends PersistentBehavior<MovieWatchList.Command, M
} }
@Override @Override
public EventHandler<Event, MovieList> eventHandler() { public EventHandler<MovieList, Event> eventHandler() {
return eventHandlerBuilder() return eventHandlerBuilder()
.matchEvent(MovieAdded.class, (state, event) -> state.add(event.movieId)) .matchEvent(MovieAdded.class, (state, event) -> state.add(event.movieId))
.matchEvent(MovieRemoved.class, (state, event) -> state.remove(event.movieId)) .matchEvent(MovieRemoved.class, (state, event) -> state.remove(event.movieId))

View file

@ -165,7 +165,7 @@ public class OptionalBlogState {
} }
@Override @Override
public EventHandler<BlogEvent, Optional<BlogState>> eventHandler() { public EventHandler<Optional<BlogState>, BlogEvent> eventHandler() {
return eventHandlerBuilder() return eventHandlerBuilder()
.matchEvent(PostAdded.class, (state, event) -> .matchEvent(PostAdded.class, (state, event) ->
Optional.of(new BlogState(event.content, false))) Optional.of(new BlogState(event.content, false)))