Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@0xjac
Created December 4, 2017 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xjac/6bcaccb180ed5bc204e9fea2414a2f5b to your computer and use it in GitHub Desktop.
Save 0xjac/6bcaccb180ed5bc204e9fea2414a2f5b to your computer and use it in GitHub Desktop.
SmallTalk ifTrue ifFalse implementation in Java for my Programming Styles class
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"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment