package types; public class BooleanSupport { interface Operation { boolean op (boolean op1, boolean op2); } interface Formatter { String format (boolean value); } public static class And implements Operation { public boolean op (boolean value1, boolean value2) { return value1 & value2; } } public static class Or implements Operation { public boolean op (boolean value1, boolean value2) { return value1 | value2; } } public static class Xor implements Operation { public boolean op (boolean value1, boolean value2) { return value1 ^ value2; } } public static class SimpleFormatter implements Formatter { public String format (boolean value) { return value ? "T" : "F"; } } public static class DefaultFormatter implements Formatter { public String format (boolean value) { return value + ""; } } public static class Table { private static final String spaces = " "; private static final String dashes = "----------"; private Operation operation; private Formatter formatter; private String title; private int size1; private int size2; public Table (Operation operation, String title, Formatter formatter) { this.operation = operation; this.formatter = formatter; this.title = title; int lengthOfTrue = formatter.format (true).length (); int lengthOfFalse = formatter.format (false).length (); size2 = Math.max (lengthOfTrue, lengthOfFalse) + 2; size1 = Math.max (title.length () + 2, size2); } public String toString () { return line () + row (title, formatter.format (true), formatter.format (false)) + line () + row (formatter.format (true) , formatter.format (operation.op (true, true)) , formatter.format (operation.op (true, false))) + line () + row (formatter.format (false) , formatter.format (operation.op (false, true)) , formatter.format (operation.op (false, false))) + line () + "\n"; } private String row (String text1, String text2, String text3) { return row (text1, text2, text3, spaces, ":"); } private String line () { return row ("-", "-", "-", dashes, "+"); } private String row (String text1, String text2, String text3 , String fillWith, String separator) { return format (text1, size1, fillWith) + separator + format (text2, size2, fillWith) + separator + format (text3, size2, fillWith) + "|\n"; } private String format (String text, int size, String fillWith) { text += fillWith.substring (0, (size - text.length ()) / 2); return fillWith.substring (0, size - text.length ()) + text; } } }