Merge pull request #16254 from tomekl007/16245-issue
#16245 receive builder could match generic messages, problem with scala ...
This commit is contained in:
commit
7a6dab2319
8 changed files with 257 additions and 144 deletions
|
|
@ -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>(""))));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<X, Y> matcher = Matcher.create(...);
|
||||
*
|
||||
* <p>
|
||||
* Y someY = matcher.match(obj);
|
||||
* </code></pre>
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<X> matcher = UnitMatcher.create(...);
|
||||
*
|
||||
* <p>
|
||||
* matcher.match(obj);
|
||||
* </code></pre>
|
||||
*
|
||||
|
|
|
|||
|
|
@ -30,15 +30,19 @@ 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;
|
||||
}
|
||||
|
||||
|
|
@ -50,11 +54,11 @@ public final class UnitPFBuilder<I> extends AbstractPFBuilder<I, BoxedUnit> {
|
|||
* @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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<String>("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<String>("");
|
||||
GenericMessage<String> nonEmptyGenericMessage = new GenericMessage<String>("a");
|
||||
|
||||
genericTestActor.tell(emptyGenericMessage, getRef());
|
||||
expectNoMsg();
|
||||
|
||||
genericTestActor.tell(nonEmptyGenericMessage, getRef());
|
||||
expectMsgEquals("A");
|
||||
}};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue