Created
April 29, 2021 02:17
-
-
Save paul-brebner/cf9324acf565c4d243181d0c5cc2f437 to your computer and use it in GitHub Desktop.
Demo Three-Valued-Logic (TVL) for PostgreSQL boolean to/from Java conversion and logical operators.
This file contains 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
// Demo of PostgreSQL to Java Three-Valued-Logic and conversion functions | |
// Paul Brebner, April 2021 | |
public enum TVL { | |
TRUE, | |
FALSE, | |
UNKNOWN; | |
// Note this function won't work with PreparedStatements only Statements | |
public String toPGBoolString() | |
{ | |
if (this == TVL.TRUE) | |
return "t"; | |
else if (this == TVL.FALSE) | |
return "f"; | |
else return null; | |
} | |
// given a String return TVL enum | |
// note that String is null if SQL returns NULL! | |
public TVL fromString(String x) | |
{ | |
if (x == null) | |
return TVL.UNKNOWN; | |
else | |
if (x.contentEquals("t")) | |
return TVL.TRUE; | |
else // if (x.contentEquals("f")) | |
return TVL.FALSE; | |
} | |
// TVL Logical Operators | |
/* | |
* T = TRUE | |
* F = FALSE | |
* U = UNKNOWN | |
* | |
* AND truth table: | |
* x y x AND y | |
* --------------- | |
* T T T | |
* T F F | |
* T U U | |
* F T F | |
* F F F | |
* F U F | |
* U T U | |
* U F F | |
* U U U | |
* | |
* X = any value | |
* simplifies to: | |
* | |
* AND truth table: | |
* x y x AND y | |
* --------------- | |
* T T T | |
* T F F | |
* T U U | |
* F X F | |
* U F F | |
* U X U | |
* | |
*/ | |
public TVL and(TVL y) | |
{ | |
if (this == TVL.TRUE) | |
return y; | |
else if (this == TVL.FALSE) | |
return TVL.FALSE; | |
else // this == TVL.UNKNOWN | |
return (y == TVL.FALSE) ? TVL.FALSE : TVL.UNKNOWN; | |
} | |
/* | |
* T = TRUE | |
* F = FALSE | |
* U = UNKNOWN | |
* | |
* OR truth table: | |
* x y x OR y | |
* -------------- | |
* T T T | |
* T F T | |
* T U T | |
* F T T | |
* F F F | |
* F U U | |
* U T T | |
* U F U | |
* U U U | |
* | |
* simplifies to: | |
* OR truth table: | |
* x y x OR y | |
* -------------- | |
* T X T | |
* F T T | |
* F F F | |
* F U U | |
* U T T | |
* U X U | |
* | |
*/ | |
public TVL or(TVL y) | |
{ | |
if (this == TVL.TRUE) | |
return TRUE; | |
else if (this == TVL.FALSE) | |
return y; | |
else // if this == UNKNOWN | |
return (y == TRUE) ? TRUE : UNKNOWN; | |
} | |
/* | |
* T = TRUE | |
* F = FALSE | |
* U = UNKNOWN | |
* | |
* NOT truth table: | |
* x NOT x | |
* --------- | |
* T F | |
* F T | |
* U U | |
*/ | |
public TVL not() | |
{ | |
if (this == TVL.TRUE) | |
return FALSE; | |
else if (this == TVL.FALSE) | |
return TRUE; | |
else // if this == UNKNOWN | |
return UNKNOWN; | |
} | |
public static void main(String[] args) { | |
System.out.println("Three Valued Logic AND Truth Table"); | |
for(TVL x:TVL.values()) | |
for(TVL y:TVL.values()) | |
System.out.println(x + " AND " + y + " = " + x.and(y)); | |
System.out.println("Three Valued Logic OR Truth Table"); | |
for(TVL x:TVL.values()) | |
for(TVL y:TVL.values()) | |
System.out.println(x + " OR " + y + " = " + x.or(y)); | |
System.out.println("Three Valued Logic NOT Truth Table"); | |
for(TVL x:TVL.values()) | |
System.out.println("NOT " + x + " = " + x.not()); | |
for(TVL x:TVL.values()) | |
System.out.println("to PG Bool " + x + " = " + x.toPGBoolString()); | |
for(TVL x:TVL.values()) | |
System.out.println("from PG Bool " + x.toPGBoolString() + " = " + x.fromString(x.toPGBoolString())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment