Class EventHandlerBuilderByState<S extends State,​State,​Event>


  • public final class EventHandlerBuilderByState<S extends State,​State,​Event>
    extends java.lang.Object
    • Constructor Detail

      • EventHandlerBuilderByState

        public EventHandlerBuilderByState​(java.lang.Class<S> stateClass,
                                          java.util.function.Predicate<S> statePredicate)
    • Method Detail

      • builder

        public static <S extends State,​State,​Event> EventHandlerBuilderByState<S,​State,​Event> builder​(java.lang.Class<S> stateClass)
        Parameters:
        stateClass - The handlers defined by this builder are used when the state is an instance of the stateClass
        Returns:
        A new, mutable, EventHandlerBuilderByState
      • builder

        public static <State,​Event> EventHandlerBuilderByState<State,​State,​Event> builder​(java.util.function.Predicate<State> statePredicate)
        Parameters:
        statePredicate - The handlers defined by this builder are used when the statePredicate is true, useful for example when state type is an Optional
        Returns:
        A new, mutable, EventHandlerBuilderByState
      • onEvent

        public <E extends EventEventHandlerBuilderByState<S,​State,​Event> onEvent​(java.lang.Class<E> eventClass,
                                                                                               java.util.function.BiFunction<S,​E,​State> handler)
        Match any event which is an instance of E or a subtype of E.

        Note: event handlers are selected in the order they are added. Once a matching is found, it's selected for handling the event 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 event handlers.

      • onEvent

        public <E extends EventEventHandlerBuilderByState<S,​State,​Event> onEvent​(java.lang.Class<E> eventClass,
                                                                                               java.util.function.Function<E,​State> handler)
        Match any event which is an instance of E or a subtype of E.

        Use this when then State is not needed in the handler, otherwise there is an overloaded method that pass the state in a BiFunction.

        Note: event handlers are selected in the order they are added. Once a matching is found, it's selected for handling the event 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 event handlers.

      • onEvent

        public <E extends EventEventHandlerBuilderByState<S,​State,​Event> onEvent​(java.lang.Class<E> eventClass,
                                                                                               java.util.function.Supplier<State> handler)
        Match any event which is an instance of E or a subtype of E.

        Use this when then State and the Event are not needed in the handler.

        Note: event handlers are selected in the order they are added. Once a matching is found, it's selected for handling the event 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 event handlers.

      • onAnyEvent

        public EventHandler<State,​Event> onAnyEvent​(java.util.function.BiFunction<State,​Event,​State> handler)
        Match any event.

        Note: event handlers are selected in the order they are added. Once a matching is found, it's selected for handling the event 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 event handlers.

        Extra care should be taken when using onAnyEvent(java.util.function.BiFunction<State,Event,State>) as it will match any event. This method builds and returns the event handler since this will not let through any states to subsequent match statements.

        Returns:
        An EventHandler from the appended states.
      • onAnyEvent

        public EventHandler<State,​Event> onAnyEvent​(java.util.function.Function<Event,​State> handler)
        Match any event.

        Use this when then State is not needed in the handler, otherwise there is an overloaded method that pass the state in a BiFunction.

        Note: event handlers are selected in the order they are added. Once a matching is found, it's selected for handling the event 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 event handlers.

        Extra care should be taken when using onAnyEvent(java.util.function.BiFunction<State,Event,State>) as it will match any event. This method builds and returns the event handler since this will not let through any states to subsequent match statements.

        Returns:
        An EventHandler from the appended states.
      • build

        public EventHandler<State,​Event> build()
        Builds and returns a handler from the appended states. The returned EventHandler will throw a MatchError if applied to an event that has no defined case.