Skip to content

Instantly share code, notes, and snippets.

@naosim
Created November 4, 2015 11:38
Show Gist options
  • Save naosim/6d6994258283595547a4 to your computer and use it in GitHub Desktop.
Save naosim/6d6994258283595547a4 to your computer and use it in GitHub Desktop.
SwitchCreator.java
public class SwitchCreator {
private final SwitchType switchType;
public SwitchCreator(SwitchType switchType) {
this.switchType = switchType;
}
public <T, R> R create(
T arg,
Predicate<T> leftFilter, Function<T, R> leftAction,
Predicate<T> rightFilter, Function<T, R> rightAction,
Function<T, R> elseAction
) {
if (this.switchType == SwitchType.left && leftFilter.test(arg)) {
return leftAction.apply(arg);
} else if (this.switchType == SwitchType.right && rightFilter.test(arg)) {
return rightAction.apply(arg);
} else {
return elseAction.apply(arg);
}
}
public <T, R> R create(
T arg,
Function<T, R> leftAction,
Function<T, R> rightAction,
Function<T, R> elseAction
) {
return create(
arg,
(m) -> true, leftAction,
(m) -> true, rightAction,
elseAction
);
}
public enum SwitchType {
left, right, other;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment