P - The parameter typeR - The result typepublic class Choice<P,R> extends Object implements Function<P,R>
Typically used to implement multiple choices, e.g. based on an enum:
public enum Signum implements Function<Integer, Integer> {
POSITIVE(any -> 1),
NON_ZERO(Choice.on(Condition.IS_POSITIVE).apply(POSITIVE).orReply(-1)),
ANY(Choice.on(Condition.IS_ZERO).reply(0).orApply(NON_ZERO));
private final Function<Integer, Integer> backing;
Signum(final Function<Integer, Integer> backing) {
this.backing = backing;
}
public static int map(final int input) {
return ANY.apply(input);
}
@Override
public Integer apply(final Integer input) {
return backing.apply(input);
}
interface Condition extends Predicate<Integer> {
Condition IS_ZERO = input -> input == 0;
Condition IS_POSITIVE = input -> input > 0;
}
}
| Modifier and Type | Class and Description |
|---|---|
static class |
Choice.Condition<P>
|
static interface |
Choice.Consequence<P,R>
Represents the preliminary stage of a
Choice that is only missing a negative result. |
| Modifier and Type | Method and Description |
|---|---|
R |
apply(P parameter) |
static <P> Choice.Condition<P> |
on(Predicate<P> condition)
|
Copyright © 2021. All rights reserved.