diff --git a/.gitignore b/.gitignore index f2dbad6..e633363 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ *.zip *.tar.gz *.rar +/out # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* diff --git a/jar_files/boolean_circuit_2022.01.06.1730..jar b/jar_files/boolean_circuit_2022.01.06.1730.jar similarity index 100% rename from jar_files/boolean_circuit_2022.01.06.1730..jar rename to jar_files/boolean_circuit_2022.01.06.1730.jar diff --git a/jar_files/boolean_circuit_2022.01.07.1010.jar b/jar_files/boolean_circuit_2022.01.07.1010.jar new file mode 100644 index 0000000..05f3d52 Binary files /dev/null and b/jar_files/boolean_circuit_2022.01.07.1010.jar differ diff --git a/src/main/java/circuitry/elements/API.java b/src/main/java/circuitry/elements/API.java index acc9ce9..5efa36d 100644 --- a/src/main/java/circuitry/elements/API.java +++ b/src/main/java/circuitry/elements/API.java @@ -6,11 +6,13 @@ public abstract class API { public static final int VERSION = 1; + protected API() { // System.err.println("Initializing version " + VERSION); init(API.VERSION); // System.err.println("Properly initialized: " + this); } + protected abstract void init(int version) throws IllegalStateException; } diff --git a/src/main/java/circuitry/elements/And.java b/src/main/java/circuitry/elements/And.java index 07ec528..dc96755 100644 --- a/src/main/java/circuitry/elements/And.java +++ b/src/main/java/circuitry/elements/And.java @@ -8,8 +8,8 @@ public class And extends Calculatable { private final Calculatable a; private final Calculatable b; - - + + protected And(final Calculatable a, final Calculatable b) { Preconditions.checkNotNull(a, "Gate input should not be null"); Preconditions.checkNotNull(b, "Gate input should not be null"); @@ -18,6 +18,8 @@ protected And(final Calculatable a, final Calculatable b) { this.b = b; } + + @Override protected boolean calculateInner() { return a.calculate() && b.calculate(); } diff --git a/src/main/java/circuitry/elements/Calculatable.java b/src/main/java/circuitry/elements/Calculatable.java index 8d1c4ac..3031b9f 100644 --- a/src/main/java/circuitry/elements/Calculatable.java +++ b/src/main/java/circuitry/elements/Calculatable.java @@ -14,6 +14,6 @@ public final boolean calculate() { } } - protected abstract boolean calculateInner(); + protected abstract boolean calculateInner(); } diff --git a/src/main/java/circuitry/elements/Circuit.java b/src/main/java/circuitry/elements/Circuit.java index 580ca3f..0e655a8 100644 --- a/src/main/java/circuitry/elements/Circuit.java +++ b/src/main/java/circuitry/elements/Circuit.java @@ -1,6 +1,5 @@ package circuitry.elements; -import circuitry.elements.Calculatable; import com.google.common.base.Preconditions; /** @@ -9,16 +8,19 @@ public final class Circuit { private final Calculatable calculatable; + protected Circuit(final Calculatable calculatable) { Preconditions.checkNotNull(calculatable, "Circuit input should not be null"); - + this.calculatable = calculatable; } + public void hello() { System.out.println("Hello world"); } + public boolean calculate() { return this.calculatable.calculate(); } diff --git a/src/main/java/circuitry/elements/ElementFactory.java b/src/main/java/circuitry/elements/ElementFactory.java index b98ea51..76e34b7 100644 --- a/src/main/java/circuitry/elements/ElementFactory.java +++ b/src/main/java/circuitry/elements/ElementFactory.java @@ -10,22 +10,26 @@ public ElementFactory(int version) { init(version); } + @Override protected void init(int version) throws IllegalStateException { if (version != API.VERSION) throw new IllegalStateException("API version error!"); } + public final Circuit createCircuit(final Calculatable a) { Preconditions.checkNotNull(a, "Circuit input should not be null"); return new Circuit(a); } + public final InputBoolean createInputBoolean(final boolean value) { return new InputBoolean(value); } + public final Calculatable createAndGateWithInputs(final Calculatable a, final Calculatable b) { Preconditions.checkNotNull(a, "Gate input should not be null"); Preconditions.checkNotNull(b, "Gate input should not be null"); @@ -33,6 +37,7 @@ public final Calculatable createAndGateWithInputs(final Calculatable a, final Ca return new And(a, b); } + public final Calculatable createOrGateWithInputs(final Calculatable a, final Calculatable b) { Preconditions.checkNotNull(a, "Gate input should not be null"); Preconditions.checkNotNull(b, "Gate input should not be null"); @@ -40,6 +45,7 @@ public final Calculatable createOrGateWithInputs(final Calculatable a, final Cal return new Or(a, b); } + public final Calculatable createNotGateWithInput(final Calculatable a) { Preconditions.checkNotNull(a, "Gate input should not be null"); diff --git a/src/main/java/circuitry/elements/InputBoolean.java b/src/main/java/circuitry/elements/InputBoolean.java index 600a46a..729fb6e 100644 --- a/src/main/java/circuitry/elements/InputBoolean.java +++ b/src/main/java/circuitry/elements/InputBoolean.java @@ -3,17 +3,21 @@ /** * */ -public class InputBoolean extends Calculatable{ +public class InputBoolean extends Calculatable { private boolean value; - + + protected InputBoolean(final boolean value) { this.value = value; } + + @Override protected boolean calculateInner() { return this.value; } - + + public void set(final boolean value) { this.value = value; } diff --git a/src/main/java/circuitry/elements/Not.java b/src/main/java/circuitry/elements/Not.java index 65790c6..ad18e50 100644 --- a/src/main/java/circuitry/elements/Not.java +++ b/src/main/java/circuitry/elements/Not.java @@ -7,7 +7,8 @@ */ public class Not extends Calculatable { private final Calculatable a; - + + protected Not(final Calculatable a) { Preconditions.checkNotNull(a, "Gate input should not be null"); @@ -15,6 +16,7 @@ protected Not(final Calculatable a) { } + @Override protected boolean calculateInner() { return !a.calculate(); } diff --git a/src/main/java/circuitry/elements/Or.java b/src/main/java/circuitry/elements/Or.java index 0d2a0dd..aa6ed9a 100644 --- a/src/main/java/circuitry/elements/Or.java +++ b/src/main/java/circuitry/elements/Or.java @@ -5,10 +5,11 @@ /** * */ -public class Or extends Calculatable{ +public class Or extends Calculatable { private final Calculatable a; private final Calculatable b; - + + protected Or(final Calculatable a, final Calculatable b) { Preconditions.checkNotNull(a, "Gate input should not be null"); Preconditions.checkNotNull(b, "Gate input should not be null"); @@ -17,6 +18,8 @@ protected Or(final Calculatable a, final Calculatable b) { this.b = b; } + + @Override protected boolean calculateInner() { return a.calculate() || b.calculate(); } diff --git a/src/test/java/Testing.java b/src/test/java/Testing.java index 218a715..c303b2f 100644 --- a/src/test/java/Testing.java +++ b/src/test/java/Testing.java @@ -8,15 +8,17 @@ * */ public class Testing { - private ElementFactory factory = new ElementFactory(API.VERSION); + private final ElementFactory factory = new ElementFactory(API.VERSION); + @Test - public void testCreation() { + public void testCircuitCreation() { Circuit circuit = factory.createCircuit(factory.createInputBoolean(false)); circuit.hello(); System.out.println("Success (testCreation)!"); } + @Test public void testAndGate() { final InputBoolean input1 = factory.createInputBoolean(false); @@ -39,6 +41,7 @@ public void testAndGate() { System.out.println("Success (testAndGate)!"); } + @Test public void testOrGate() { final InputBoolean input1 = factory.createInputBoolean(false); @@ -110,6 +113,7 @@ public void testIfCircuitIsFailSafe1() { } } + @Test public void testIfCircuitIsFailSafe2() { try { @@ -125,6 +129,7 @@ public void testIfCircuitIsFailSafe2() { } } + @Test public void testIfCircuitIsFailSafe3() { try {