Created
December 4, 2017 10:13
-
-
Save 0xjac/6bcaccb180ed5bc204e9fea2414a2f5b to your computer and use it in GitHub Desktop.
Revisions
-
0xjac created this gist
Dec 4, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ import java.util.concurrent.ThreadLocalRandom; import java.util.function.Supplier; public abstract class Bool { public static final Bool TRUE = new True(); public static final Bool FALSE = new False(); public abstract <T> T ifTrueIfFalse(Supplier<T> fTrue, Supplier<T> fFalse); private static class True extends Bool { public <T> T ifTrueIfFalse(Supplier<T> fTrue, Supplier<T> fFalse) { return fTrue.get(); } } private static class False extends Bool { public <T> T ifTrueIfFalse(Supplier<T> fTrue, Supplier<T> fFalse) { return fFalse.get(); } } public static void main(String[] args) { System.out.printf("TRUE: %s\n", Bool.TRUE.ifTrueIfFalse(() -> "yes", () -> "oops")); System.out.printf("FALSE: %s\n", Bool.FALSE.ifTrueIfFalse(() -> "yes", () -> "oops")); Bool condition = ThreadLocalRandom.current().nextInt(0, 2) == 0 ? Bool.FALSE : Bool.TRUE; System.out.printf("%s: %s\n", condition.getClass().getName(), condition.ifTrueIfFalse(() -> "yes", () -> "oops")); } }