!act #18996 PFBuilder matchAny should take declared input type I

Currently PFBuilder.matchAny takes a lambda with Object as input argument.
This loses type information for partial functions that are not akka
receive functions, e.g. exception handlers (PF<Exception,RouteResult).
With this change, the fallback handler still has the guaranteed type I
visible on its lambda.
This commit is contained in:
Jan Ypma 2015-11-23 13:16:39 +01:00
parent aad2c4ca35
commit 9c1c3f3b2c
4 changed files with 28 additions and 4 deletions

View file

@ -0,0 +1,24 @@
/**
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.japi.pf;
import org.junit.Test;
import scala.PartialFunction;
import static org.junit.Assert.*;
public class PFBuilderTest {
@Test
public void pfbuilder_matchAny_should_infer_declared_input_type_for_lambda() {
PartialFunction<String,Integer> pf = new PFBuilder<String,Integer>()
.matchEquals("hello", s -> 1)
.matchAny(s -> Integer.valueOf(s))
.build();
assertTrue(pf.isDefinedAt("hello"));
assertTrue(pf.isDefinedAt("42"));
assertEquals(42, pf.apply("42").intValue());
}
}

View file

@ -65,7 +65,7 @@ public class DeciderBuilder {
* @param apply an action to apply to the argument
* @return a builder with the case statement added
*/
public static PFBuilder<Throwable, Directive> matchAny(FI.Apply<Object, Directive> apply) {
public static PFBuilder<Throwable, Directive> matchAny(FI.Apply<Throwable, Directive> apply) {
return Match.matchAny(apply);
}
}

View file

@ -70,7 +70,7 @@ public class Match<I, R> extends AbstractMatch<I, R> {
* @return a builder with the case statement added
* @see PFBuilder#matchAny(FI.Apply)
*/
public static <F, T> PFBuilder<F, T> matchAny(final FI.Apply<Object, T> apply) {
public static <F, T> PFBuilder<F, T> matchAny(final FI.Apply<F, T> apply) {
return new PFBuilder<F, T>().matchAny(apply);
}

View file

@ -94,8 +94,8 @@ public final class PFBuilder<I, R> extends AbstractPFBuilder<I, R> {
* @param apply an action to apply to the argument
* @return a builder with the case statement added
*/
public PFBuilder<I, R> matchAny(final FI.Apply<Object, R> apply) {
addStatement(new CaseStatement<I, Object, R>(
public PFBuilder<I, R> matchAny(final FI.Apply<I, R> apply) {
addStatement(new CaseStatement<I, I, R>(
new FI.Predicate() {
@Override
public boolean defined(Object o) {