Merge pull request #16254 from tomekl007/16245-issue

#16245 receive builder could match generic messages, problem with scala ...
This commit is contained in:
Konrad Malawski 2014-11-21 15:31:56 +01:00
commit 7a6dab2319
8 changed files with 257 additions and 144 deletions

View file

@ -38,4 +38,44 @@ public class MatchBuilderTest {
exception.expect(MatchError.class); exception.expect(MatchError.class);
assertFalse("A string should throw a MatchError", new Double(4711).equals(pf.match("4711"))); 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

@ -27,8 +27,8 @@ public class Match<I, R> extends AbstractMatch<I, R> {
* @return a builder with the case statement added * @return a builder with the case statement added
* @see PFBuilder#match(Class, FI.Apply) * @see PFBuilder#match(Class, FI.Apply)
*/ */
public static final <F, T, P> PFBuilder<F, T> match(final Class<P> type, public static <F, T, P> PFBuilder<F, T> match(final Class<? extends P> type,
final FI.Apply<P, T> apply) { final FI.Apply<? extends P, T> apply) {
return new PFBuilder<F, T>().match(type, 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 * @return a builder with the case statement added
* @see PFBuilder#match(Class, FI.TypedPredicate, FI.Apply) * @see PFBuilder#match(Class, FI.TypedPredicate, FI.Apply)
*/ */
public static <F, T, P> PFBuilder<F, T> match(final Class<P> type, public static <F, T, P> PFBuilder<F, T> match(final Class<? extends P> type,
final FI.TypedPredicate<P> predicate, final FI.TypedPredicate<? extends P> predicate,
final FI.Apply<P, T> apply) { final FI.Apply<? extends P, T> apply) {
return new PFBuilder<F, T>().match(type, predicate, 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. * Convenience function to make the Java code more readable.
* * <p>
* <pre><code> * <pre><code>
* Matcher&lt;X, Y&gt; matcher = Matcher.create(...); * Matcher&lt;X, Y&gt; matcher = Matcher.create(...);
* * <p>
* Y someY = matcher.match(obj); * Y someY = matcher.match(obj);
* </code></pre> * </code></pre>
* *

View file

@ -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 * @param apply an action to apply to the argument if the type matches
* @return a builder with the case statement added * @return a builder with the case statement added
*/ */
public <P> PFBuilder<I, R> match(final Class<P> type, FI.Apply<P, R> apply) { @SuppressWarnings("unchecked")
addStatement(new CaseStatement<I, P, R>( public <P> PFBuilder<I, R> match(final Class<? extends P> type, FI.Apply<? extends P, R> apply) {
new FI.Predicate() {
FI.Predicate predicate = new FI.Predicate() {
@Override @Override
public boolean defined(Object o) { public boolean defined(Object o) {
return type.isInstance(o); return type.isInstance(o);
} }
}, apply)); };
addStatement(new CaseStatement<I, P, R>(predicate, (FI.Apply<P, R>) apply));
return this; 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 * @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 * @return a builder with the case statement added
*/ */
public <P> PFBuilder<I, R> match(final Class<P> type, @SuppressWarnings("unchecked")
final FI.TypedPredicate<P> predicate, public <P> PFBuilder<I, R> match(final Class<? extends P> type,
final FI.Apply<P, R> apply) { final FI.TypedPredicate<? extends P> predicate,
addStatement(new CaseStatement<I, P, R>( final FI.Apply<? extends P, R> apply) {
new FI.Predicate() { FI.Predicate fiPredicate = new FI.Predicate() {
@Override @Override
public boolean defined(Object o) { public boolean defined(Object o) {
if (!type.isInstance(o)) if (!type.isInstance(o))
@ -58,10 +61,11 @@ public final class PFBuilder<I, R> extends AbstractPFBuilder<I, R> {
else { else {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
P p = (P) o; 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; 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. * Add a new case statement to this builder, that matches any argument.
*
* @param apply an action to apply to the argument * @param apply an action to apply to the argument
* @return a builder with the case statement added * @return a builder with the case statement added
*/ */

View file

@ -42,7 +42,7 @@ public class ReceiveBuilder {
* @param apply an action to apply to the argument if the type matches * @param apply an action to apply to the argument if the type matches
* @return a builder with the case statement added * @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); 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 * @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 * @return a builder with the case statement added
*/ */
public static <P> UnitPFBuilder<Object> match(final Class<P> type, public static <P> UnitPFBuilder<Object> match(final Class<? extends P> type,
FI.TypedPredicate<P> predicate, FI.TypedPredicate<? extends P> predicate,
FI.UnitApply<P> apply) { FI.UnitApply<? extends P> apply) {
return UnitMatch.match(type, predicate, apply); return UnitMatch.match(type, predicate, apply);
} }

View file

@ -29,7 +29,7 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
* @return a builder with the case statement added * @return a builder with the case statement added
* @see UnitPFBuilder#match(Class, FI.UnitApply) * @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); 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 * @return a builder with the case statement added
* @see UnitPFBuilder#match(Class, FI.TypedPredicate, FI.UnitApply) * @see UnitPFBuilder#match(Class, FI.TypedPredicate, FI.UnitApply)
*/ */
public static <F, P> UnitPFBuilder<F> match(final Class<P> type, public static <F, P> UnitPFBuilder<F> match(final Class<? extends P> type,
final FI.TypedPredicate<P> predicate, final FI.TypedPredicate<? extends P> predicate,
final FI.UnitApply<P> apply) { final FI.UnitApply<? extends P> apply) {
return new UnitPFBuilder<F>().match(type, predicate, 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. * Convenience function to make the Java code more readable.
* * <p>
* <pre><code> * <pre><code>
* UnitMatcher&lt;X&gt; matcher = UnitMatcher.create(...); * UnitMatcher&lt;X&gt; matcher = UnitMatcher.create(...);
* * <p>
* matcher.match(obj); * matcher.match(obj);
* </code></pre> * </code></pre>
* *

View file

@ -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 * @param apply an action to apply to the argument if the type matches
* @return a builder with the case statement added * @return a builder with the case statement added
*/ */
public <P> UnitPFBuilder<I> match(final Class<P> type, @SuppressWarnings("unchecked")
final FI.UnitApply<P> apply) { public <P> UnitPFBuilder<I> match(final Class<? extends P> type,
addStatement(new UnitCaseStatement<I, P>( final FI.UnitApply<? extends P> apply) {
new FI.Predicate() {
FI.Predicate predicate = new FI.Predicate() {
@Override @Override
public boolean defined(Object o) { public boolean defined(Object o) {
return type.isInstance(o); return type.isInstance(o);
} }
}, apply)); };
addStatement(new UnitCaseStatement<I, P>(predicate, (FI.UnitApply<P>) apply));
return this; 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 * @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 * @return a builder with the case statement added
*/ */
public <P> UnitPFBuilder<I> match(final Class<P> type, @SuppressWarnings("unchecked")
final FI.TypedPredicate<P> predicate, public <P> UnitPFBuilder<I> match(final Class<? extends P> type,
final FI.UnitApply<P> apply) { final FI.TypedPredicate<? extends P> predicate,
addStatement(new UnitCaseStatement<I, P>( final FI.UnitApply<? extends P> apply) {
new FI.Predicate() { FI.Predicate fiPredicate = new FI.Predicate() {
@Override @Override
public boolean defined(Object o) { public boolean defined(Object o) {
if (!type.isInstance(o)) if (!type.isInstance(o))
@ -62,10 +66,13 @@ public final class UnitPFBuilder<I> extends AbstractPFBuilder<I, BoxedUnit> {
else { else {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
P p = (P) o; 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; 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. * Add a new case statement to this builder, that matches any argument.
*
* @param apply an action to apply to the argument * @param apply an action to apply to the argument
* @return a builder with the case statement added * @return a builder with the case statement added
*/ */

View file

@ -4,13 +4,13 @@
package akka.japi.pf package akka.japi.pf
import FI.{ UnitApply, Apply, Predicate } import FI.{UnitApply, Apply, Predicate}
private[pf] object CaseStatement { private[pf] object CaseStatement {
def empty[F, T](): PartialFunction[F, T] = PartialFunction.empty 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] { extends PartialFunction[F, T] {
override def isDefinedAt(o: F) = predicate.defined(o) 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> * 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.japi.pf.ReceiveBuilder;
import akka.testkit.JavaTestKit; import akka.testkit.JavaTestKit;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import scala.concurrent.duration.Duration;
import scala.concurrent.Await; import scala.concurrent.Await;
import scala.concurrent.duration.Duration;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class InitializationDocTest { public class InitializationDocTest {
@ -42,12 +45,43 @@ public class InitializationDocTest {
matchEquals("U OK?", m2 -> { matchEquals("U OK?", m2 -> {
sender().tell(initializeMe, self()); sender().tell(initializeMe, self());
}).build()); }).build());
}).build() }).build()
//#messageInit //#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 @Test
public void testIt() { public void testIt() {
@ -63,4 +97,30 @@ public class InitializationDocTest {
expectMsgEquals("Up and running"); 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");
}};
}
} }