Skip to content

Instantly share code, notes, and snippets.

@0xjac
Created December 4, 2017 10:13
Show Gist options
  • Save 0xjac/6bcaccb180ed5bc204e9fea2414a2f5b to your computer and use it in GitHub Desktop.
Save 0xjac/6bcaccb180ed5bc204e9fea2414a2f5b to your computer and use it in GitHub Desktop.

Revisions

  1. 0xjac created this gist Dec 4, 2017.
    35 changes: 35 additions & 0 deletions Bool.java
    Original 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"));
    }

    }