Package akka.japi.pf

Class ReceiveBuilder


  • public class ReceiveBuilder
    extends java.lang.Object
    Used for building a partial function for AbstractActor.createReceive().

    There is both a match on type only, and a match on type and predicate.

    Inside an actor you can use it like this:

    Example:

     @Override
     public Receive createReceive() {
       return receiveBuilder()
         .match(Double.class, d -> {
           getSender().tell(d.isNaN() ? 0 : d, self());
         })
         .match(Integer.class, i -> {
           getSender().tell(i * 10, self());
         })
         .match(String.class, s -> s.startsWith("foo"), s -> {
           getSender().tell(s.toUpperCase(), self());
         })
         .build()
       );
     }
     
    • Constructor Detail

      • ReceiveBuilder

        public ReceiveBuilder()
    • Method Detail

      • addStatement

        protected void addStatement​(scala.PartialFunction<java.lang.Object,​scala.runtime.BoxedUnit> statement)
      • build

        public AbstractActor.Receive build()
        Build a PartialFunction from this builder. After this call the builder will be reset.
        Returns:
        a PartialFunction for this builder.
      • create

        public static ReceiveBuilder create()
        Return a new ReceiveBuilder with no case statements. They can be added later as the returned ReceiveBuilder is a mutable object.
        Returns:
        a builder with no case statements
      • match

        public <P> ReceiveBuilder match​(java.lang.Class<P> type,
                                        FI.UnitApply<P> apply)
        Add a new case statement to this builder.
        Parameters:
        type - a type to match the argument against
        apply - an action to apply to the argument if the type matches
        Returns:
        a builder with the case statement added
      • matchUnchecked

        public ReceiveBuilder matchUnchecked​(java.lang.Class<?> type,
                                             FI.UnitApply<?> apply)
        Add a new case statement to this builder without compile time type check. Should normally not be used, but when matching on class with generic type argument it can be useful, e.g. List.class and (List<String> list) -> {}.
        Parameters:
        type - a type to match the argument against
        apply - an action to apply to the argument if the type matches
        Returns:
        a builder with the case statement added
      • match

        public <P> ReceiveBuilder match​(java.lang.Class<P> type,
                                        FI.TypedPredicate<P> predicate,
                                        FI.UnitApply<P> apply)
        Add a new case statement to this builder.
        Parameters:
        type - a type to match the argument against
        predicate - a predicate that will be evaluated on the argument if the type matches
        apply - an action to apply to the argument if the type matches and the predicate returns true
        Returns:
        a builder with the case statement added
      • match

        public <P> ReceiveBuilder match​(java.lang.Class<P> type,
                                        java.util.function.BooleanSupplier externalPredicate,
                                        FI.UnitApply<P> apply)
        Add a new case statement to this builder.
        Parameters:
        type - a type to match the argument against
        externalPredicate - a external predicate that will be evaluated if the type matches
        apply - an action to apply to the argument if the type matches and the predicate returns true
        Returns:
        a builder with the case statement added
      • matchUnchecked

        public <P> ReceiveBuilder matchUnchecked​(java.lang.Class<?> type,
                                                 FI.TypedPredicate<?> predicate,
                                                 FI.UnitApply<P> apply)
        Add a new case statement to this builder without compile time type check. Should normally not be used, but when matching on class with generic type argument it can be useful, e.g. List.class and (List<String> list) -> {}.
        Parameters:
        type - a type to match the argument against
        predicate - a predicate that will be evaluated on the argument if the type matches
        apply - an action to apply to the argument if the type matches and the predicate returns true
        Returns:
        a builder with the case statement added
      • matchUnchecked

        public <P> ReceiveBuilder matchUnchecked​(java.lang.Class<?> type,
                                                 java.util.function.BooleanSupplier externalPredicate,
                                                 FI.UnitApply<P> apply)
        Add a new case statement to this builder without compile time type check. Should normally not be used, but when matching on class with generic type argument it can be useful, e.g. List.class and (List<String> list) -> {}.
        Parameters:
        type - a type to match the argument against
        externalPredicate - an external predicate that will be evaluated if the type matches
        apply - an action to apply to the argument if the type matches and the predicate returns true
        Returns:
        a builder with the case statement added
      • matchEquals

        public <P> ReceiveBuilder matchEquals​(P object,
                                              FI.UnitApply<P> apply)
        Add a new case statement to this builder.
        Parameters:
        object - the object to compare equals with
        apply - an action to apply to the argument if the object compares equal
        Returns:
        a builder with the case statement added
      • matchEquals

        public <P> ReceiveBuilder matchEquals​(P object,
                                              FI.TypedPredicate<P> predicate,
                                              FI.UnitApply<P> apply)
        Add a new case statement to this builder.
        Parameters:
        object - the object to compare equals with
        predicate - a predicate that will be evaluated on the argument if the object compares equal
        apply - an action to apply to the argument if the object compares equal
        Returns:
        a builder with the case statement added
      • matchEquals

        public <P> ReceiveBuilder matchEquals​(P object,
                                              java.util.function.BooleanSupplier externalPredicate,
                                              FI.UnitApply<P> apply)
        Add a new case statement to this builder.
        Parameters:
        object - the object to compare equals with
        externalPredicate - an external predicate that will be evaluated if the object compares equal
        apply - an action to apply to the argument if the object compares equal
        Returns:
        a builder with the case statement added
      • matchAny

        public ReceiveBuilder matchAny​(FI.UnitApply<java.lang.Object> apply)
        Add a new case statement to this builder, that matches any argument.
        Parameters:
        apply - an action to apply to the argument
        Returns:
        a builder with the case statement added
      • matchAny

        public ReceiveBuilder matchAny​(java.util.function.BooleanSupplier externalPredicate,
                                       FI.UnitApply<java.lang.Object> apply)
        Add a new case statement to this builder, that pass the test of the predicate.
        Parameters:
        externalPredicate - an external predicate that will always be evaluated.
        apply - an action to apply to the argument
        Returns:
        a builder with the case statement added