Created
November 4, 2015 11:38
-
-
Save naosim/6d6994258283595547a4 to your computer and use it in GitHub Desktop.
SwitchCreator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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