Created
September 25, 2015 02:10
-
-
Save reynoldscem/828b2b62fea5f44f64fb to your computer and use it in GitHub Desktop.
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
import java.lang.IllegalArgumentException; | |
import java.lang.IllegalStateException; | |
public class Truthiness { | |
private static final int MAX_RECURSIVE_TRUTH_CHECKS = 1000; | |
public static boolean isTrue(boolean condition) { | |
return isTrue(condition, 0); | |
} | |
private static boolean isTrue(boolean condition, int checkCount) { | |
if ((checkCount >= MAX_RECURSIVE_TRUTH_CHECKS) == true) { | |
throw new IllegalArgumentException("Must not be called with (checkCount >= MAX_RECURSIVE_TRUTH_CHECKS == true)."); | |
} | |
boolean truthiness; | |
if (condition == true) { | |
truthiness = true; | |
} else if (condition == false) { | |
truthiness = false; | |
} else { | |
throw new IllegalArgumentException("Condition neither true nor false."); | |
} | |
checkCount = checkCount + 1; | |
// Baremetal truth check to avoid stack overflow | |
if ((checkCount >= MAX_RECURSIVE_TRUTH_CHECKS) == true) { | |
return truthiness; | |
} else if ((checkCount >= MAX_RECURSIVE_TRUTH_CHECKS) == false) { | |
return isTrue(condition && truthiness, checkCount); | |
} | |
throw new IllegalStateException("The very essence of truthhood and falsity has been defiled."); | |
} | |
public static void main(String[] args) { | |
if (isTrue(true)) { | |
System.out.println("True"); | |
} else { | |
System.out.println("False"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment