Class ReceiveBuilder

java.lang.Object
org.apache.pekko.japi.pf.ReceiveBuilder

public class ReceiveBuilder extends 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 Details

    • ReceiveBuilder

      public ReceiveBuilder()
  • Method Details

    • addStatement

      protected void addStatement(scala.PartialFunction<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(Class<P> type, Procedure<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(Class<?> type, Procedure<?> 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(Class<P> type, Predicate<P> predicate, Procedure<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(Class<P> type, BooleanSupplier externalPredicate, Procedure<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(Class<?> type, Predicate<?> predicate, Procedure<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(Class<?> type, BooleanSupplier externalPredicate, Procedure<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, Procedure<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, Predicate<P> predicate, Procedure<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, BooleanSupplier externalPredicate, Procedure<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(Procedure<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(BooleanSupplier externalPredicate, Procedure<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