Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor updates #3

Merged
merged 4 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
Binary file added jar_files/boolean_circuit_2022.01.07.1010.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions src/main/java/circuitry/elements/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 4 additions & 2 deletions src/main/java/circuitry/elements/And.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -18,6 +18,8 @@ protected And(final Calculatable a, final Calculatable b) {
this.b = b;
}


@Override
protected boolean calculateInner() {
return a.calculate() && b.calculate();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/circuitry/elements/Calculatable.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public final boolean calculate() {
}
}

protected abstract boolean calculateInner();

protected abstract boolean calculateInner();
}
6 changes: 4 additions & 2 deletions src/main/java/circuitry/elements/Circuit.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package circuitry.elements;

import circuitry.elements.Calculatable;
import com.google.common.base.Preconditions;

/**
Expand All @@ -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();
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/circuitry/elements/ElementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,42 @@ 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");

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");

return new Or(a, b);
}


public final Calculatable createNotGateWithInput(final Calculatable a) {
Preconditions.checkNotNull(a, "Gate input should not be null");

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/circuitry/elements/InputBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/circuitry/elements/Not.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
*/
public class Not extends Calculatable {
private final Calculatable a;



protected Not(final Calculatable a) {
Preconditions.checkNotNull(a, "Gate input should not be null");

this.a = a;
}


@Override
protected boolean calculateInner() {
return !a.calculate();
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/circuitry/elements/Or.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -17,6 +18,8 @@ protected Or(final Calculatable a, final Calculatable b) {
this.b = b;
}


@Override
protected boolean calculateInner() {
return a.calculate() || b.calculate();
}
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/Testing.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -39,6 +41,7 @@ public void testAndGate() {
System.out.println("Success (testAndGate)!");
}


@Test
public void testOrGate() {
final InputBoolean input1 = factory.createInputBoolean(false);
Expand Down Expand Up @@ -110,6 +113,7 @@ public void testIfCircuitIsFailSafe1() {
}
}


@Test
public void testIfCircuitIsFailSafe2() {
try {
Expand All @@ -125,6 +129,7 @@ public void testIfCircuitIsFailSafe2() {
}
}


@Test
public void testIfCircuitIsFailSafe3() {
try {
Expand Down