2013-12-11 14:53:41 +01:00
|
|
|
/**
|
2016-02-23 12:58:39 +01:00
|
|
|
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
|
2013-12-11 14:53:41 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.japi.pf;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used for building a partial function for {@link akka.actor.Actor#receive() Actor.receive()}.
|
2014-11-20 16:57:49 +01:00
|
|
|
*
|
2013-12-11 14:53:41 +01:00
|
|
|
* There is both a match on type only, and a match on type and predicate.
|
2014-11-20 16:57:49 +01:00
|
|
|
*
|
2014-02-21 12:43:30 +01:00
|
|
|
* Inside an actor you can use it like this with Java 8 to define your receive method.
|
2015-05-05 16:44:49 +02:00
|
|
|
* <p>
|
2014-02-21 12:43:30 +01:00
|
|
|
* Example:
|
2015-05-05 16:44:49 +02:00
|
|
|
* </p>
|
2013-12-11 14:53:41 +01:00
|
|
|
* <pre>
|
2015-05-05 16:44:49 +02:00
|
|
|
* @Override
|
2014-11-20 16:57:49 +01:00
|
|
|
* public Actor() {
|
|
|
|
|
* receive(ReceiveBuilder.
|
2015-05-05 16:44:49 +02:00
|
|
|
* match(Double.class, d -> {
|
2014-11-20 16:57:49 +01:00
|
|
|
* sender().tell(d.isNaN() ? 0 : d, self());
|
|
|
|
|
* }).
|
2015-05-05 16:44:49 +02:00
|
|
|
* match(Integer.class, i -> {
|
2014-11-20 16:57:49 +01:00
|
|
|
* sender().tell(i * 10, self());
|
|
|
|
|
* }).
|
2015-05-05 16:44:49 +02:00
|
|
|
* match(String.class, s -> s.startsWith("foo"), s -> {
|
2014-11-20 16:57:49 +01:00
|
|
|
* sender().tell(s.toUpperCase(), self());
|
|
|
|
|
* }).build()
|
|
|
|
|
* );
|
2013-12-11 14:53:41 +01:00
|
|
|
* }
|
|
|
|
|
* </pre>
|
2014-11-20 16:57:49 +01:00
|
|
|
*
|
2014-02-24 12:11:02 +01:00
|
|
|
* This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
|
2013-12-11 14:53:41 +01:00
|
|
|
*/
|
|
|
|
|
public class ReceiveBuilder {
|
|
|
|
|
private ReceiveBuilder() {
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-23 22:35:18 +02:00
|
|
|
/**
|
|
|
|
|
* Return a new {@link UnitPFBuilder} with no case statements. They can be added later as the returned {@link
|
|
|
|
|
* UnitPFBuilder} is a mutable object.
|
|
|
|
|
*
|
|
|
|
|
* @return a builder with no case statements
|
|
|
|
|
*/
|
|
|
|
|
public static UnitPFBuilder<Object> create() {
|
|
|
|
|
return new UnitPFBuilder<>();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 14:53:41 +01:00
|
|
|
/**
|
|
|
|
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
|
|
|
|
*
|
2014-11-08 13:55:38 +01:00
|
|
|
* @param type a type to match the argument against
|
|
|
|
|
* @param apply an action to apply to the argument if the type matches
|
|
|
|
|
* @return a builder with the case statement added
|
2013-12-11 14:53:41 +01:00
|
|
|
*/
|
2014-11-08 13:55:38 +01:00
|
|
|
public static <P> UnitPFBuilder<Object> match(final Class<? extends P> type, FI.UnitApply<? extends P> apply) {
|
2013-12-11 14:53:41 +01:00
|
|
|
return UnitMatch.match(type, apply);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
|
|
|
|
*
|
2014-11-08 13:55:38 +01:00
|
|
|
* @param type a type to match the argument against
|
|
|
|
|
* @param predicate a predicate that will be evaluated on the argument if the type matches
|
|
|
|
|
* @param apply an action to apply to the argument if the type matches and the predicate returns true
|
|
|
|
|
* @return a builder with the case statement added
|
2013-12-11 14:53:41 +01:00
|
|
|
*/
|
2014-11-08 13:55:38 +01:00
|
|
|
public static <P> UnitPFBuilder<Object> match(final Class<? extends P> type,
|
|
|
|
|
FI.TypedPredicate<? extends P> predicate,
|
|
|
|
|
FI.UnitApply<? extends P> apply) {
|
2013-12-11 14:53:41 +01:00
|
|
|
return UnitMatch.match(type, predicate, apply);
|
|
|
|
|
}
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
|
|
|
|
*
|
2014-11-08 13:55:38 +01:00
|
|
|
* @param object the object to compare equals with
|
2014-02-21 12:43:30 +01:00
|
|
|
* @param apply an action to apply to the argument if the object compares equal
|
|
|
|
|
* @return a builder with the case statement added
|
|
|
|
|
*/
|
|
|
|
|
public static <P> UnitPFBuilder<Object> matchEquals(P object, FI.UnitApply<P> apply) {
|
|
|
|
|
return UnitMatch.matchEquals(object, apply);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-13 10:29:06 +01:00
|
|
|
/**
|
|
|
|
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
|
|
|
|
*
|
2014-11-08 13:55:38 +01:00
|
|
|
* @param object the object to compare equals with
|
|
|
|
|
* @param predicate a predicate that will be evaluated on the argument if the object compares equal
|
|
|
|
|
* @param apply an action to apply to the argument if the object compares equal
|
2014-03-13 10:29:06 +01:00
|
|
|
* @return a builder with the case statement added
|
|
|
|
|
*/
|
|
|
|
|
public static <P> UnitPFBuilder<Object> matchEquals(P object,
|
|
|
|
|
FI.TypedPredicate<P> predicate,
|
|
|
|
|
FI.UnitApply<P> apply) {
|
|
|
|
|
return UnitMatch.matchEquals(object, predicate, apply);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-21 12:43:30 +01:00
|
|
|
/**
|
|
|
|
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
|
|
|
|
*
|
2014-11-08 13:55:38 +01:00
|
|
|
* @param apply an action to apply to the argument
|
|
|
|
|
* @return a builder with the case statement added
|
2014-02-21 12:43:30 +01:00
|
|
|
*/
|
|
|
|
|
public static UnitPFBuilder<Object> matchAny(FI.UnitApply<Object> apply) {
|
|
|
|
|
return UnitMatch.matchAny(apply);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 14:53:41 +01:00
|
|
|
}
|