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);
|
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>(""))));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,13 +22,13 @@ public class Match<I, R> extends AbstractMatch<I, R> {
|
||||||
* Convenience function to create a {@link PFBuilder} with the first
|
* Convenience function to create a {@link PFBuilder} with the first
|
||||||
* case statement added.
|
* case statement added.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @param type a type to match the argument against
|
||||||
* @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
|
||||||
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,15 +36,15 @@ public class Match<I, R> extends AbstractMatch<I, R> {
|
||||||
* Convenience function to create a {@link PFBuilder} with the first
|
* Convenience function to create a {@link PFBuilder} with the first
|
||||||
* case statement added.
|
* case statement added.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @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 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
|
* @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
|
||||||
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,7 +52,7 @@ public class Match<I, R> extends AbstractMatch<I, R> {
|
||||||
* Convenience function to create a {@link PFBuilder} with the first
|
* Convenience function to create a {@link PFBuilder} with the first
|
||||||
* case statement added.
|
* case statement added.
|
||||||
*
|
*
|
||||||
* @param object the object to compare equals with
|
* @param object the object to compare equals with
|
||||||
* @param apply an action to apply to the argument 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
|
* @return a builder with the case statement added
|
||||||
* @see PFBuilder#matchEquals(Object, FI.Apply)
|
* @see PFBuilder#matchEquals(Object, FI.Apply)
|
||||||
|
|
@ -66,7 +66,7 @@ public class Match<I, R> extends AbstractMatch<I, R> {
|
||||||
* Convenience function to create a {@link PFBuilder} with the first
|
* Convenience function to create a {@link PFBuilder} with the first
|
||||||
* case statement added.
|
* case statement added.
|
||||||
*
|
*
|
||||||
* @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
|
||||||
* @see PFBuilder#matchAny(FI.Apply)
|
* @see PFBuilder#matchAny(FI.Apply)
|
||||||
*/
|
*/
|
||||||
|
|
@ -77,8 +77,8 @@ public class Match<I, R> extends AbstractMatch<I, R> {
|
||||||
/**
|
/**
|
||||||
* Create a {@link Match} from the builder.
|
* Create a {@link Match} from the builder.
|
||||||
*
|
*
|
||||||
* @param builder a builder representing the partial function
|
* @param builder a builder representing the partial function
|
||||||
* @return a {@link Match} that can be reused
|
* @return a {@link Match} that can be reused
|
||||||
*/
|
*/
|
||||||
public static final <F, T> Match<F, T> create(PFBuilder<F, T> builder) {
|
public static final <F, T> Match<F, T> create(PFBuilder<F, T> builder) {
|
||||||
return new Match<F, T>(builder.build());
|
return new Match<F, T>(builder.build());
|
||||||
|
|
@ -90,16 +90,16 @@ 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<X, Y> matcher = Matcher.create(...);
|
* Matcher<X, Y> matcher = Matcher.create(...);
|
||||||
*
|
* <p>
|
||||||
* Y someY = matcher.match(obj);
|
* Y someY = matcher.match(obj);
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
*
|
*
|
||||||
* @param i the argument to apply the match to
|
* @param i the argument to apply the match to
|
||||||
* @return the result of the application
|
* @return the result of the application
|
||||||
* @throws MatchError if there is no match
|
* @throws MatchError if there is no match
|
||||||
*/
|
*/
|
||||||
public R match(I i) throws MatchError {
|
public R match(I i) throws MatchError {
|
||||||
return statements.apply(i);
|
return statements.apply(i);
|
||||||
|
|
|
||||||
|
|
@ -23,52 +23,56 @@ public final class PFBuilder<I, R> extends AbstractPFBuilder<I, R> {
|
||||||
/**
|
/**
|
||||||
* Add a new case statement to this builder.
|
* Add a new case statement to this builder.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @param type a type to match the argument against
|
||||||
* @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() {
|
|
||||||
@Override
|
FI.Predicate predicate = new FI.Predicate() {
|
||||||
public boolean defined(Object o) {
|
@Override
|
||||||
return type.isInstance(o);
|
public boolean defined(Object o) {
|
||||||
}
|
return type.isInstance(o);
|
||||||
}, apply));
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
addStatement(new CaseStatement<I, P, R>(predicate, (FI.Apply<P, R>) apply));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new case statement to this builder.
|
* Add a new case statement to this builder.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @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 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
|
* @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))
|
||||||
return false;
|
return false;
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new case statement to this builder.
|
* Add a new case statement to this builder.
|
||||||
*
|
*
|
||||||
* @param object the object to compare equals with
|
* @param object the object to compare equals with
|
||||||
* @param apply an action to apply to the argument 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
|
* @return a builder with the case statement added
|
||||||
*/
|
*/
|
||||||
|
|
@ -86,8 +90,9 @@ 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
|
*
|
||||||
* @return a builder with the case statement added
|
* @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) {
|
public PFBuilder<I, R> matchAny(final FI.Apply<Object, R> apply) {
|
||||||
addStatement(new CaseStatement<I, Object, R>(
|
addStatement(new CaseStatement<I, Object, R>(
|
||||||
|
|
|
||||||
|
|
@ -38,32 +38,32 @@ public class ReceiveBuilder {
|
||||||
/**
|
/**
|
||||||
* Return a new {@link UnitPFBuilder} with a case statement added.
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @param type a type to match the argument against
|
||||||
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a new {@link UnitPFBuilder} with a case statement added.
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @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 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
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a new {@link UnitPFBuilder} with a case statement added.
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
||||||
*
|
*
|
||||||
* @param object the object to compare equals with
|
* @param object the object to compare equals with
|
||||||
* @param apply an action to apply to the argument 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
|
* @return a builder with the case statement added
|
||||||
*/
|
*/
|
||||||
|
|
@ -74,9 +74,9 @@ public class ReceiveBuilder {
|
||||||
/**
|
/**
|
||||||
* Return a new {@link UnitPFBuilder} with a case statement added.
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
||||||
*
|
*
|
||||||
* @param object the object to compare equals with
|
* @param object the object to compare equals with
|
||||||
* @param predicate a predicate that will be evaluated on the argument if the object compares equal
|
* @param predicate a predicate that will be evaluated on the argument if the object compares equal
|
||||||
* @param apply an action to apply to the argument 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
|
* @return a builder with the case statement added
|
||||||
*/
|
*/
|
||||||
public static <P> UnitPFBuilder<Object> matchEquals(P object,
|
public static <P> UnitPFBuilder<Object> matchEquals(P object,
|
||||||
|
|
@ -88,8 +88,8 @@ public class ReceiveBuilder {
|
||||||
/**
|
/**
|
||||||
* Return a new {@link UnitPFBuilder} with a case statement added.
|
* Return a new {@link UnitPFBuilder} with a case statement added.
|
||||||
*
|
*
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
public static UnitPFBuilder<Object> matchAny(FI.UnitApply<Object> apply) {
|
public static UnitPFBuilder<Object> matchAny(FI.UnitApply<Object> apply) {
|
||||||
return UnitMatch.matchAny(apply);
|
return UnitMatch.matchAny(apply);
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,11 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
|
||||||
* case statement added.
|
* case statement added.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @param type a type to match the argument against
|
||||||
* @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
|
||||||
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,15 +37,15 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
|
||||||
* Convenience function to create a {@link UnitPFBuilder} with the first
|
* Convenience function to create a {@link UnitPFBuilder} with the first
|
||||||
* case statement added.
|
* case statement added.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @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 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 and predicate matches
|
* @param apply an action to apply to the argument if the type and predicate matches
|
||||||
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,7 +53,7 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
|
||||||
* Convenience function to create a {@link UnitPFBuilder} with the first
|
* Convenience function to create a {@link UnitPFBuilder} with the first
|
||||||
* case statement added.
|
* case statement added.
|
||||||
*
|
*
|
||||||
* @param object the object to compare equals with
|
* @param object the object to compare equals with
|
||||||
* @param apply an action to apply to the argument 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
|
* @return a builder with the case statement added
|
||||||
* @see UnitPFBuilder#matchEquals(Object, FI.UnitApply)
|
* @see UnitPFBuilder#matchEquals(Object, FI.UnitApply)
|
||||||
|
|
@ -67,9 +67,9 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
|
||||||
* Convenience function to create a {@link UnitPFBuilder} with the first
|
* Convenience function to create a {@link UnitPFBuilder} with the first
|
||||||
* case statement added.
|
* case statement added.
|
||||||
*
|
*
|
||||||
* @param object the object to compare equals with
|
* @param object the object to compare equals with
|
||||||
* @param predicate a predicate that will be evaluated on the argument the object compares equal
|
* @param predicate a predicate that will be evaluated on the argument the object compares equal
|
||||||
* @param apply an action to apply to the argument 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
|
* @return a builder with the case statement added
|
||||||
* @see UnitPFBuilder#matchEquals(Object, FI.UnitApply)
|
* @see UnitPFBuilder#matchEquals(Object, FI.UnitApply)
|
||||||
*/
|
*/
|
||||||
|
|
@ -83,7 +83,7 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
|
||||||
* Convenience function to create a {@link UnitPFBuilder} with the first
|
* Convenience function to create a {@link UnitPFBuilder} with the first
|
||||||
* case statement added.
|
* case statement added.
|
||||||
*
|
*
|
||||||
* @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
|
||||||
* @see UnitPFBuilder#matchAny(FI.UnitApply)
|
* @see UnitPFBuilder#matchAny(FI.UnitApply)
|
||||||
*/
|
*/
|
||||||
|
|
@ -94,7 +94,7 @@ public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {
|
||||||
/**
|
/**
|
||||||
* Create a {@link UnitMatch} from the builder.
|
* Create a {@link UnitMatch} from the builder.
|
||||||
*
|
*
|
||||||
* @param builder a builder representing the partial function
|
* @param builder a builder representing the partial function
|
||||||
* @return a {@link UnitMatch} that can be reused
|
* @return a {@link UnitMatch} that can be reused
|
||||||
*/
|
*/
|
||||||
public static <F> UnitMatch<F> create(UnitPFBuilder<F> builder) {
|
public static <F> UnitMatch<F> create(UnitPFBuilder<F> builder) {
|
||||||
|
|
@ -107,15 +107,15 @@ 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<X> matcher = UnitMatcher.create(...);
|
* UnitMatcher<X> matcher = UnitMatcher.create(...);
|
||||||
*
|
* <p>
|
||||||
* matcher.match(obj);
|
* matcher.match(obj);
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
*
|
*
|
||||||
* @param i the argument to apply the match to
|
* @param i the argument to apply the match to
|
||||||
* @throws scala.MatchError if there is no match
|
* @throws scala.MatchError if there is no match
|
||||||
*/
|
*/
|
||||||
public void match(I i) throws MatchError {
|
public void match(I i) throws MatchError {
|
||||||
statements.apply(i);
|
statements.apply(i);
|
||||||
|
|
|
||||||
|
|
@ -26,53 +26,60 @@ public final class UnitPFBuilder<I> extends AbstractPFBuilder<I, BoxedUnit> {
|
||||||
/**
|
/**
|
||||||
* Add a new case statement to this builder.
|
* Add a new case statement to this builder.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @param type a type to match the argument against
|
||||||
* @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() {
|
|
||||||
@Override
|
FI.Predicate predicate = new FI.Predicate() {
|
||||||
public boolean defined(Object o) {
|
@Override
|
||||||
return type.isInstance(o);
|
public boolean defined(Object o) {
|
||||||
}
|
return type.isInstance(o);
|
||||||
}, apply));
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
addStatement(new UnitCaseStatement<I, P>(predicate, (FI.UnitApply<P>) apply));
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new case statement to this builder.
|
* Add a new case statement to this builder.
|
||||||
*
|
*
|
||||||
* @param type a type to match the argument against
|
* @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 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
|
* @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))
|
||||||
return false;
|
return false;
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new case statement to this builder.
|
* Add a new case statement to this builder.
|
||||||
*
|
*
|
||||||
* @param object the object to compare equals with
|
* @param object the object to compare equals with
|
||||||
* @param apply an action to apply to the argument 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
|
* @return a builder with the case statement added
|
||||||
*/
|
*/
|
||||||
|
|
@ -83,17 +90,17 @@ public final class UnitPFBuilder<I> extends AbstractPFBuilder<I, BoxedUnit> {
|
||||||
@Override
|
@Override
|
||||||
public boolean defined(Object o) {
|
public boolean defined(Object o) {
|
||||||
return object.equals(o);
|
return object.equals(o);
|
||||||
}
|
}
|
||||||
}, apply));
|
}, apply));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new case statement to this builder.
|
* Add a new case statement to this builder.
|
||||||
*
|
*
|
||||||
* @param object the object to compare equals with
|
* @param object the object to compare equals with
|
||||||
* @param predicate a predicate that will be evaluated on the argument if the object compares equal
|
* @param predicate a predicate that will be evaluated on the argument if the object compares equal
|
||||||
* @param apply an action to apply to the argument 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
|
* @return a builder with the case statement added
|
||||||
*/
|
*/
|
||||||
public <P> UnitPFBuilder<I> matchEquals(final P object,
|
public <P> UnitPFBuilder<I> matchEquals(final P object,
|
||||||
|
|
@ -117,8 +124,9 @@ 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
|
*
|
||||||
* @return a builder with the case statement added
|
* @param apply an action to apply to the argument
|
||||||
|
* @return a builder with the case statement added
|
||||||
*/
|
*/
|
||||||
public UnitPFBuilder<I> matchAny(final FI.UnitApply<Object> apply) {
|
public UnitPFBuilder<I> matchAny(final FI.UnitApply<Object> apply) {
|
||||||
addStatement(new UnitCaseStatement<I, Object>(
|
addStatement(new UnitCaseStatement<I, Object>(
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
@ -24,4 +24,4 @@ private[pf] class UnitCaseStatement[F, P](predicate: Predicate, apply: UnitApply
|
||||||
override def isDefinedAt(o: F) = predicate.defined(o)
|
override def isDefinedAt(o: F) = predicate.defined(o)
|
||||||
|
|
||||||
override def apply(o: F) = apply.apply(o.asInstanceOf[P])
|
override def apply(o: F) = apply.apply(o.asInstanceOf[P])
|
||||||
}
|
}
|
||||||
|
|
@ -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 {
|
||||||
|
|
||||||
|
|
@ -27,7 +30,7 @@ public class InitializationDocTest {
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void afterClass() throws Exception {
|
public static void afterClass() throws Exception {
|
||||||
Await.ready(system.terminate(), Duration.create("5 seconds"));
|
Await.ready(system.terminate(), Duration.create("5 seconds"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class MessageInitExample extends AbstractActor {
|
public static class MessageInitExample extends AbstractActor {
|
||||||
|
|
@ -36,18 +39,49 @@ public class InitializationDocTest {
|
||||||
public MessageInitExample() {
|
public MessageInitExample() {
|
||||||
//#messageInit
|
//#messageInit
|
||||||
receive(ReceiveBuilder.
|
receive(ReceiveBuilder.
|
||||||
matchEquals("init", m1 -> {
|
matchEquals("init", m1 -> {
|
||||||
initializeMe = "Up and running";
|
initializeMe = "Up and running";
|
||||||
context().become(ReceiveBuilder.
|
context().become(ReceiveBuilder.
|
||||||
matchEquals("U OK?", m2 -> {
|
matchEquals("U OK?", m2 -> {
|
||||||
sender().tell(initializeMe, self());
|
sender().tell(initializeMe, self());
|
||||||
}).build());
|
}).build());
|
||||||
}).build()
|
|
||||||
//#messageInit
|
}).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
|
@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");
|
||||||
|
}};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue