Merge pull request #28029 from akka/wip-25822-again-patriknw

Add external predicate to matchEquals and matchAny in ReceiverBuilder, #25774
This commit is contained in:
Patrik Nordwall 2019-12-05 09:22:03 +01:00 committed by GitHub
commit 2f80042cd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,10 +4,9 @@
package akka.japi.pf; package akka.japi.pf;
import akka.actor.AbstractActor.Receive;
import scala.PartialFunction; import scala.PartialFunction;
import scala.runtime.BoxedUnit; import scala.runtime.BoxedUnit;
import akka.actor.AbstractActor;
import akka.actor.AbstractActor.Receive;
/** /**
* Used for building a partial function for {@link akka.actor.AbstractActor#createReceive() * Used for building a partial function for {@link akka.actor.AbstractActor#createReceive()
@ -169,7 +168,7 @@ public class ReceiveBuilder {
* List.class</code> and <code>(List&lt;String&gt; list) -> {}</code>. * List.class</code> and <code>(List&lt;String&gt; list) -> {}</code>.
* *
* @param type a type to match the argument against * @param type a type to match the argument against
* @param externalPredicate a external predicate that will be evaluated if the type matches * @param externalPredicate an external predicate that will be evaluated if the type matches
* @param apply an action to apply to the argument if the type matches and the predicate returns * @param apply an action to apply to the argument if the type matches and the predicate returns
* true * true
* @return a builder with the case statement added * @return a builder with the case statement added
@ -240,6 +239,27 @@ public class ReceiveBuilder {
return this; return this;
} }
/**
* Add a new case statement to this builder.
*
* @param object the object to compare equals with
* @param externalPredicate an external predicate that will be evaluated if the object compares
* equal
* @param apply an action to apply to the argument if the object compares equal
* @return a builder with the case statement added
*/
@SuppressWarnings("unchecked")
public <P> ReceiveBuilder matchEquals(
final P object,
final java.util.function.BooleanSupplier externalPredicate,
final FI.UnitApply<P> apply) {
final FI.Predicate predicate = o -> object.equals(o) && externalPredicate.getAsBoolean();
addStatement(new UnitCaseStatement<>(predicate, (FI.UnitApply<Object>) apply));
return this;
}
private static final FI.Predicate ALWAYS_TRUE = (input) -> true;
/** /**
* Add a new case statement to this builder, that matches any argument. * Add a new case statement to this builder, that matches any argument.
* *
@ -247,15 +267,22 @@ public class ReceiveBuilder {
* @return a builder with the case statement added * @return a builder with the case statement added
*/ */
public ReceiveBuilder matchAny(final FI.UnitApply<Object> apply) { public ReceiveBuilder matchAny(final FI.UnitApply<Object> apply) {
addStatement( addStatement(new UnitCaseStatement<>(ALWAYS_TRUE, apply));
new UnitCaseStatement<Object, Object>( return this;
new FI.Predicate() { }
@Override
public boolean defined(Object o) { /**
return true; * Add a new case statement to this builder, that pass the test of the predicate.
} *
}, * @param externalPredicate an external predicate that will always be evaluated.
apply)); * @param apply an action to apply to the argument
* @return a builder with the case statement added
*/
public ReceiveBuilder matchAny(
final java.util.function.BooleanSupplier externalPredicate,
final FI.UnitApply<Object> apply) {
final FI.Predicate predicate = o -> externalPredicate.getAsBoolean();
addStatement(new UnitCaseStatement<>(predicate, apply));
return this; return this;
} }
} }