+act #16245 receive builder works with generic messages

UnitPFBuilder use CaseStatements as previously, because apply is cast to exact type ( not ? extends T )
PFBuilder.match working with generic messages
This commit is contained in:
tomekl007 2014-11-08 13:55:38 +01:00
parent c183989ea2
commit 7cd4a68cee
8 changed files with 289 additions and 176 deletions

View file

@ -38,4 +38,44 @@ public class MatchBuilderTest {
exception.expect(MatchError.class);
assertFalse("A string should throw a MatchError", new Double(4711).equals(pf.match("4711")));
}
static class GenericClass<T> {
T val;
public GenericClass(T val) {
this.val = val;
}
}
@Test
public void shouldHandleMatchOnGenericClass() {
Match<Object, String> pf = Match.create(Match.match(GenericClass.class, new FI.Apply<GenericClass<String>, String>() {
@Override
public String apply(GenericClass<String> stringGenericClass) {
return stringGenericClass.val;
}
}));
assertTrue("String value should be extract from GenericMessage", "A".equals(pf.match(new GenericClass<String>("A"))));
}
@Test
public void shouldHandleMatchWithPredicateOnGenericClass() {
Match<Object, String> pf = Match.create(Match.match(GenericClass.class, new FI.TypedPredicate<GenericClass<String>>() {
@Override
public boolean defined(GenericClass<String> genericClass) {
return !genericClass.val.isEmpty();
}
}, new FI.Apply<GenericClass<String>, String>() {
@Override
public String apply(GenericClass<String> stringGenericClass) {
return stringGenericClass.val;
}
}));
exception.expect(MatchError.class);
assertTrue("empty GenericMessage should throw match error", "".equals(pf.match(new GenericClass<String>(""))));
}
}

View file

@ -13,7 +13,7 @@ import scala.PartialFunction;
*
* @param <I> the input type, that this PartialFunction will be applied to
* @param <R> the return type, that the results of the application will have
*
* <p>
* This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
*/
public class Match<I, R> extends AbstractMatch<I, R> {
@ -27,8 +27,8 @@ public class Match<I, R> extends AbstractMatch<I, R> {
* @return a builder with the case statement added
* @see PFBuilder#match(Class, FI.Apply)
*/
public static final <F, T, P> PFBuilder<F, T> match(final Class<P> type,
final FI.Apply<P, T> apply) {
public static <F, T, P> PFBuilder<F, T> match(final Class<? extends P> type,
final FI.Apply<? extends P, T> apply) {
return new PFBuilder<F, T>().match(type, apply);
}
@ -42,9 +42,9 @@ public class Match<I, R> extends AbstractMatch<I, R> {
* @return a builder with the case statement added
* @see PFBuilder#match(Class, FI.TypedPredicate, FI.Apply)
*/
public static <F, T, P> PFBuilder<F, T> match(final Class<P> type,
final FI.TypedPredicate<P> predicate,
final FI.Apply<P, T> apply) {
public static <F, T, P> PFBuilder<F, T> match(final Class<? extends P> type,
final FI.TypedPredicate<? extends P> predicate,
final FI.Apply<? extends P, T> apply) {
return new PFBuilder<F, T>().match(type, predicate, apply);
}
@ -90,10 +90,10 @@ public class Match<I, R> extends AbstractMatch<I, R> {
/**
* Convenience function to make the Java code more readable.
*
* <p>
* <pre><code>
* Matcher&lt;X, Y&gt; matcher = Matcher.create(...);
*
* <p>
* Y someY = matcher.match(obj);
* </code></pre>
*

View file

@ -9,7 +9,7 @@ package akka.japi.pf;
*
* @param <I> the input type, that this PartialFunction will be applied to
* @param <R> the return type, that the results of the application will have
*
* <p>
* This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
*/
public final class PFBuilder<I, R> extends AbstractPFBuilder<I, R> {
@ -27,14 +27,17 @@ public final class PFBuilder<I, R> extends AbstractPFBuilder<I, R> {
* @param apply an action to apply to the argument if the type matches
* @return a builder with the case statement added
*/
public <P> PFBuilder<I, R> match(final Class<P> type, FI.Apply<P, R> apply) {
addStatement(new CaseStatement<I, P, R>(
new FI.Predicate() {
@SuppressWarnings("unchecked")
public <P> PFBuilder<I, R> match(final Class<? extends P> type, FI.Apply<? extends P, R> apply) {
FI.Predicate predicate = new FI.Predicate() {
@Override
public boolean defined(Object o) {
return type.isInstance(o);
}
}, apply));
};
addStatement(new CaseStatement<I, P, R>(predicate, (FI.Apply<P, R>) apply));
return this;
}
@ -46,11 +49,11 @@ public final class PFBuilder<I, R> extends AbstractPFBuilder<I, R> {
* @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
*/
public <P> PFBuilder<I, R> match(final Class<P> type,
final FI.TypedPredicate<P> predicate,
final FI.Apply<P, R> apply) {
addStatement(new CaseStatement<I, P, R>(
new FI.Predicate() {
@SuppressWarnings("unchecked")
public <P> PFBuilder<I, R> match(final Class<? extends P> type,
final FI.TypedPredicate<? extends P> predicate,
final FI.Apply<? extends P, R> apply) {
FI.Predicate fiPredicate = new FI.Predicate() {
@Override
public boolean defined(Object o) {
if (!type.isInstance(o))
@ -58,10 +61,11 @@ public final class PFBuilder<I, R> extends AbstractPFBuilder<I, R> {
else {
@SuppressWarnings("unchecked")
P p = (P) o;
return predicate.defined(p);
return ((FI.TypedPredicate<P>) predicate).defined(p);
}
}
}, apply));
};
addStatement(new CaseStatement<I, P, R>(fiPredicate, (FI.Apply<P, R>) apply));
return this;
}
@ -86,6 +90,7 @@ public final class PFBuilder<I, R> extends AbstractPFBuilder<I, R> {
/**
* Add a new case statement to this builder, that matches any argument.
*
* @param apply an action to apply to the argument
* @return a builder with the case statement added
*/

View file

@ -6,15 +6,15 @@ package akka.japi.pf;
/**
* Used for building a partial function for {@link akka.actor.Actor#receive() Actor.receive()}.
*
* <p>
* There is both a match on type only, and a match on type and predicate.
*
* <p>
* Inside an actor you can use it like this with Java 8 to define your receive method.
* <p/>
* <p>
* Example:
* <pre>
* @Override
* public Actor() {
*
* @Override public Actor() {
* receive(ReceiveBuilder.
* match(Double.class, d -> {
* sender().tell(d.isNaN() ? 0 : d, self());
@ -28,7 +28,7 @@ package akka.japi.pf;
* );
* }
* </pre>
*
* <p>
* This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
*/
public class ReceiveBuilder {
@ -42,7 +42,7 @@ public class ReceiveBuilder {
* @param apply an action to apply to the argument if the type matches
* @return a builder with the case statement added
*/
public static <P> UnitPFBuilder<Object> match(final Class<P> type, FI.UnitApply<P> apply) {
public static <P> UnitPFBuilder<Object> match(final Class<? extends P> type, FI.UnitApply<? extends P> apply) {
return UnitMatch.match(type, apply);
}
@ -54,9 +54,9 @@ public class ReceiveBuilder {
* @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
*/
public static <P> UnitPFBuilder<Object> match(final Class<P> type,
FI.TypedPredicate<P> predicate,
FI.UnitApply<P> apply) {
public static <P> UnitPFBuilder<Object> match(final Class<? extends P> type,
FI.TypedPredicate<? extends P> predicate,
FI.UnitApply<? extends P> apply) {
return UnitMatch.match(type, predicate, apply);
}

View file

@ -15,7 +15,7 @@ import scala.runtime.BoxedUnit;
* void methods to {@link scala.runtime.BoxedUnit}.
*
* @param <I> the input type, that this PartialFunction will be applied to
*
* <p>
* This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
*/
public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
@ -29,7 +29,7 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
* @return a builder with the case statement added
* @see UnitPFBuilder#match(Class, FI.UnitApply)
*/
public static final <F, P> UnitPFBuilder<F> match(final Class<P> type, FI.UnitApply<P> apply) {
public static <F, P> UnitPFBuilder<F> match(final Class<? extends P> type, FI.UnitApply<? extends P> apply) {
return new UnitPFBuilder<F>().match(type, apply);
}
@ -43,9 +43,9 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
* @return a builder with the case statement added
* @see UnitPFBuilder#match(Class, FI.TypedPredicate, FI.UnitApply)
*/
public static <F, P> UnitPFBuilder<F> match(final Class<P> type,
final FI.TypedPredicate<P> predicate,
final FI.UnitApply<P> apply) {
public static <F, P> UnitPFBuilder<F> match(final Class<? extends P> type,
final FI.TypedPredicate<? extends P> predicate,
final FI.UnitApply<? extends P> apply) {
return new UnitPFBuilder<F>().match(type, predicate, apply);
}
@ -107,10 +107,10 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
/**
* Convenience function to make the Java code more readable.
*
* <p>
* <pre><code>
* UnitMatcher&lt;X&gt; matcher = UnitMatcher.create(...);
*
* <p>
* matcher.match(obj);
* </code></pre>
*

View file

@ -12,7 +12,7 @@ import scala.runtime.BoxedUnit;
* void methods to {@link scala.runtime.BoxedUnit}.
*
* @param <I> the input type, that this PartialFunction to be applied to
*
* <p>
* This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
*/
public final class UnitPFBuilder<I> extends AbstractPFBuilder<I, BoxedUnit> {
@ -30,31 +30,35 @@ public final class UnitPFBuilder<I> extends AbstractPFBuilder<I, BoxedUnit> {
* @param apply an action to apply to the argument if the type matches
* @return a builder with the case statement added
*/
public <P> UnitPFBuilder<I> match(final Class<P> type,
final FI.UnitApply<P> apply) {
addStatement(new UnitCaseStatement<I, P>(
new FI.Predicate() {
@SuppressWarnings("unchecked")
public <P> UnitPFBuilder<I> match(final Class<? extends P> type,
final FI.UnitApply<? extends P> apply) {
FI.Predicate predicate = new FI.Predicate() {
@Override
public boolean defined(Object o) {
return type.isInstance(o);
}
}, apply));
};
addStatement(new UnitCaseStatement<I, P>(predicate, (FI.UnitApply<P>) apply));
return this;
}
/**
* Add a new case statement to this builder.
* Add a new case statement to this b uilder.
*
* @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
*/
public <P> UnitPFBuilder<I> match(final Class<P> type,
final FI.TypedPredicate<P> predicate,
final FI.UnitApply<P> apply) {
addStatement(new UnitCaseStatement<I, P>(
new FI.Predicate() {
@SuppressWarnings("unchecked")
public <P> UnitPFBuilder<I> match(final Class<? extends P> type,
final FI.TypedPredicate<? extends P> predicate,
final FI.UnitApply<? extends P> apply) {
FI.Predicate fiPredicate = new FI.Predicate() {
@Override
public boolean defined(Object o) {
if (!type.isInstance(o))
@ -62,10 +66,13 @@ public final class UnitPFBuilder<I> extends AbstractPFBuilder<I, BoxedUnit> {
else {
@SuppressWarnings("unchecked")
P p = (P) o;
return predicate.defined(p);
return ((FI.TypedPredicate<P>) predicate).defined(p);
}
}
}, apply));
};
addStatement(new UnitCaseStatement<I, P>(fiPredicate, (FI.UnitApply<P>) apply));
return this;
}
@ -117,6 +124,7 @@ public final class UnitPFBuilder<I> extends AbstractPFBuilder<I, BoxedUnit> {
/**
* Add a new case statement to this builder, that matches any argument.
*
* @param apply an action to apply to the argument
* @return a builder with the case statement added
*/

View file

@ -4,13 +4,13 @@
package akka.japi.pf
import FI.{ UnitApply, Apply, Predicate }
import FI.{UnitApply, Apply, Predicate}
private[pf] object CaseStatement {
def empty[F, T](): PartialFunction[F, T] = PartialFunction.empty
}
private[pf] class CaseStatement[F, P, T](predicate: Predicate, apply: Apply[P, T])
private[pf] class CaseStatement[-F, +P, T](predicate: Predicate, apply: Apply[P, T])
extends PartialFunction[F, T] {
override def isDefinedAt(o: F) = predicate.defined(o)

View file

@ -1,20 +1,23 @@
package docs.actor;
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package docs.actor;
import akka.actor.*;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.japi.pf.FI;
import akka.japi.pf.ReceiveBuilder;
import akka.testkit.JavaTestKit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import scala.concurrent.duration.Duration;
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class InitializationDocTest {
@ -42,12 +45,43 @@ public class InitializationDocTest {
matchEquals("U OK?", m2 -> {
sender().tell(initializeMe, self());
}).build());
}).build()
//#messageInit
);
}
}
public class GenericMessage<T> {
T value;
public GenericMessage(T value) {
this.value = value;
}
}
public static class GenericActor extends AbstractActor {
public GenericActor() {
receive(ReceiveBuilder.match(GenericMessage.class, (GenericMessage<String> msg) -> {
GenericMessage<String> message = msg;
sender().tell(message.value.toUpperCase(), self());
}).build());
}
}
static class GenericActorWithPredicate extends AbstractActor {
public GenericActorWithPredicate() {
FI.TypedPredicate<GenericMessage<String>> typedPredicate = s -> !s.value.isEmpty();
receive(ReceiveBuilder.match(GenericMessage.class, typedPredicate, (GenericMessage<String> msg) -> {
sender().tell(msg.value.toUpperCase(), self());
}).build());
}
}
@Test
public void testIt() {
@ -63,4 +97,30 @@ public class InitializationDocTest {
expectMsgEquals("Up and running");
}};
}
@Test
public void testGenericActor() {
new JavaTestKit(system) {{
ActorRef genericTestActor = system.actorOf(Props.create(GenericActor.class), "genericActor");
GenericMessage<String> genericMessage = new GenericMessage<>("a");
genericTestActor.tell(genericMessage, getRef());
expectMsgEquals("A");
}};
}
@Test
public void actorShouldNotRespondForEmptyMessage() {
new JavaTestKit(system) {{
ActorRef genericTestActor = system.actorOf(Props.create(GenericActorWithPredicate.class), "genericActorWithPredicate");
GenericMessage<String> emptyGenericMessage = new GenericMessage<>("");
GenericMessage<String> nonEmptyGenericMessage = new GenericMessage<>("a");
genericTestActor.tell(emptyGenericMessage, getRef());
expectNoMsg();
genericTestActor.tell(nonEmptyGenericMessage, getRef());
expectMsgEquals("A");
}};
}
}