Last active
November 5, 2016 13:59
-
-
Save pascal08/4a170d1dc485146b4c19372332da12b8 to your computer and use it in GitHub Desktop.
How to use boolean logic without using native booleans in 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
interface Conditional { | |
Boolean implies(Boolean b); | |
} | |
interface BiConditional { | |
Boolean equalTo(Boolean b); | |
} | |
interface Disjunction { | |
Boolean or(Boolean b); | |
} | |
interface Conjunction { | |
Boolean and(Boolean b); | |
} | |
interface Negation { | |
Boolean not(); | |
} | |
public abstract class Boolean implements Conditional, BiConditional, Disjunction, Conjunction, Negation { | |
public abstract Boolean implies(Boolean b); | |
public abstract Boolean equalTo(Boolean b); | |
public abstract Boolean or(Boolean b); | |
public abstract Boolean and(Boolean b); | |
public abstract Boolean not(); | |
public abstract String toString(); | |
} | |
public class True extends Boolean { | |
private static True instance; | |
private True() { | |
// | |
} | |
public static Boolean getInstance() { | |
if (instance == null) { | |
// Thread Safe. Might be costly operation in some case | |
synchronized (True.class) { | |
if (instance == null) { | |
instance = new True(); | |
} | |
} | |
} | |
return instance; | |
} | |
public Boolean implies(Boolean b) { | |
return b; | |
} | |
public Boolean equalTo(Boolean b) { | |
return b; | |
} | |
public Boolean or(Boolean b) { | |
return this; | |
} | |
public Boolean not() { | |
return False.getInstance(); | |
} | |
public Boolean and(Boolean b) { | |
return b; | |
} | |
public String toString() { | |
return "true"; | |
} | |
} | |
public class False extends Boolean { | |
private static False instance; | |
private False() { | |
// | |
} | |
public static Boolean getInstance() { | |
if (instance == null) { | |
// Thread Safe. Might be costly operation in some case | |
synchronized (False.class) { | |
if (instance == null) { | |
instance = new False(); | |
} | |
} | |
} | |
return instance; | |
} | |
public Boolean implies(Boolean b) { | |
return True.getInstance(); | |
} | |
public Boolean equalTo(Boolean b) { | |
return b.not(); | |
} | |
public Boolean or(Boolean b) { | |
return b; | |
} | |
public Boolean not() { | |
return True.getInstance(); | |
} | |
public Boolean and(Boolean b) { | |
return this; | |
} | |
public String toString() { | |
return "false"; | |
} | |
} | |
public class Demo { | |
public static void main(String[] args) { | |
Boolean _false = False.getInstance(); | |
Boolean _true = True.getInstance(); | |
System.out.println("toString"); | |
System.out.println(_false.toString()); // "false" | |
System.out.println(_true.toString()); // "true" | |
System.out.println("Implication"); | |
System.out.println(_false.implies(_false).toString()); // "true" | |
System.out.println(_false.implies(_true).toString()); // "true" | |
System.out.println(_true.implies(_false).toString()); // "false" | |
System.out.println(_true.implies(_true).toString()); // "true" | |
System.out.println("Bi-Implication"); | |
System.out.println(_false.equalTo(_false).toString()); // "true" | |
System.out.println(_false.equalTo(_true).toString()); // "false" | |
System.out.println(_true.equalTo(_false).toString()); // "false" | |
System.out.println(_true.equalTo(_true).toString()); // "true" | |
System.out.println("Negation"); | |
System.out.println(_false.not().toString()); // "true" | |
System.out.println(_true.not().toString()); // "false" | |
System.out.println("Disjunction"); | |
System.out.println(_false.or(_false).toString()); // "false" | |
System.out.println(_false.or(_true).toString()); // "true" | |
System.out.println(_true.or(_false).toString()); // "true" | |
System.out.println(_true.or(_true).toString()); // "true" | |
System.out.println("Conjunction"); | |
System.out.println(_false.and(_false).toString()); // "false" | |
System.out.println(_false.and(_true).toString()); // "false" | |
System.out.println(_true.and(_false).toString()); // "false" | |
System.out.println(_true.and(_true).toString()); // "true" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment