Package org.apache.pekko.japi.pf
Class ReceiveBuilder
java.lang.Object
org.apache.pekko.japi.pf.ReceiveBuilder
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddStatement(scala.PartialFunction<Object, scala.runtime.BoxedUnit> statement) build()Build aPartialFunctionfrom this builder.static ReceiveBuildercreate()Return a newReceiveBuilderwith no case statements.<P> ReceiveBuildermatch(Class<P> type, BooleanSupplier externalPredicate, Procedure<P> apply) Add a new case statement to this builder.<P> ReceiveBuilderAdd a new case statement to this builder.<P> ReceiveBuilderAdd a new case statement to this builder.matchAny(BooleanSupplier externalPredicate, Procedure<Object> apply) Add a new case statement to this builder, that pass the test of the predicate.Add a new case statement to this builder, that matches any argument.<P> ReceiveBuildermatchEquals(P object, BooleanSupplier externalPredicate, Procedure<P> apply) Add a new case statement to this builder.<P> ReceiveBuildermatchEquals(P object, Predicate<P> predicate, Procedure<P> apply) Add a new case statement to this builder.<P> ReceiveBuildermatchEquals(P object, Procedure<P> apply) Add a new case statement to this builder.<P> ReceiveBuildermatchUnchecked(Class<?> type, BooleanSupplier externalPredicate, Procedure<P> apply) Add a new case statement to this builder without compile time type check.<P> ReceiveBuildermatchUnchecked(Class<?> type, Predicate<?> predicate, Procedure<P> apply) Add a new case statement to this builder without compile time type check.matchUnchecked(Class<?> type, Procedure<?> apply) Add a new case statement to this builder without compile time type check.
-
Constructor Details
-
ReceiveBuilder
public ReceiveBuilder()
-
-
Method Details
-
addStatement
-
build
Build aPartialFunctionfrom this builder. After this call the builder will be reset.- Returns:
- a PartialFunction for this builder.
-
create
Return a newReceiveBuilderwith no case statements. They can be added later as the returnedReceiveBuilderis a mutable object.- Returns:
- a builder with no case statements
-
match
Add a new case statement to this builder.- Parameters:
type- a type to match the argument againstapply- an action to apply to the argument if the type matches- Returns:
- a builder with the case statement added
-
matchUnchecked
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.classand(List<String> list) -> {}.- Parameters:
type- a type to match the argument againstapply- an action to apply to the argument if the type matches- Returns:
- a builder with the case statement added
-
match
Add a new case statement to this builder.- Parameters:
type- a type to match the argument againstpredicate- a predicate that will be evaluated on the argument if the type matchesapply- 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 againstexternalPredicate- a external predicate that will be evaluated if the type matchesapply- 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
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.classand(List<String> list) -> {}.- Parameters:
type- a type to match the argument againstpredicate- a predicate that will be evaluated on the argument if the type matchesapply- 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.classand(List<String> list) -> {}.- Parameters:
type- a type to match the argument againstexternalPredicate- an external predicate that will be evaluated if the type matchesapply- 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
Add a new case statement to this builder.- Parameters:
object- the object to compare equals withapply- an action to apply to the argument if the object compares equal- Returns:
- a builder with the case statement added
-
matchEquals
Add a new case statement to this builder.- Parameters:
object- the object to compare equals withpredicate- a predicate that will be evaluated on the argument if the object compares equalapply- 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 withexternalPredicate- an external predicate that will be evaluated if the object compares equalapply- an action to apply to the argument if the object compares equal- Returns:
- a builder with the case statement added
-
matchAny
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
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
-