Skip to content

Commit

Permalink
Merge pull request #4 from georgevangelou/generics
Browse files Browse the repository at this point in the history
API v2
  • Loading branch information
georgevangelou authored Jan 7, 2022
2 parents 6bcb69f + 9570f1e commit 18f5c2b
Show file tree
Hide file tree
Showing 17 changed files with 573 additions and 49 deletions.
Binary file added diagram/Boolean_Circuit_v2.eapx
Binary file not shown.
Binary file added diagram/Boolean_Circuit_v2.ldb
Binary file not shown.
Binary file added diagram/Starter Class Diagram_v2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added jar_files/boolean_circuit_2022.01.07.1730.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>guava</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/circuitry/elements/API.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package circuitry.elements;

/**
*
* Created on: 2022-01-06
*/
public abstract class API {
public static final int VERSION = 1;
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/circuitry/elements/And.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
package circuitry.elements;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.tuple.Pair;

/**
*
* Created on: 2022-01-06
*/
public class And extends Calculatable {
private final Calculatable a;
private final Calculatable b;
class And<T> extends Calculatable<T> {
private final Calculatable<T> a;
private final Calculatable<T> b;
private final Class<T> classOfInputs;


protected And(final Calculatable a, final Calculatable b) {
protected And(final Calculatable<T> a, final Calculatable<T> b, final Class<T> classOfInputs) {
Preconditions.checkNotNull(a, "Gate input should not be null");
Preconditions.checkNotNull(b, "Gate input should not be null");
Preconditions.checkNotNull(classOfInputs, "Gate input should not be null");

this.a = a;
this.b = b;
this.classOfInputs = classOfInputs;
}


@Override
protected boolean calculateInner() {
return a.calculate() && b.calculate();
protected Pair<Boolean, T> calculateInner() {
if (this.classOfInputs == Double.class) {
Double value = (Double) ((Double) a.calculate().getRight() * ((Double) b.calculate().getRight()));
return Pair.of(false, (T) value);
} else if (this.classOfInputs == Boolean.class) {
Boolean value = (Boolean) ((Boolean) a.calculate().getRight() && ((Boolean) b.calculate().getRight()));
return Pair.of(true, (T) value);
} else {
throw new RuntimeException();
}
}
}
14 changes: 8 additions & 6 deletions src/main/java/circuitry/elements/Calculatable.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package circuitry.elements;

import org.apache.commons.lang3.tuple.Pair;

/**
*
* Created on: 2022-01-06
*/
public abstract class Calculatable {
public abstract class Calculatable<T> {

public final boolean calculate() {
protected final Pair<Boolean, T> calculate() {
try {
return calculateInner();
} catch (final NullPointerException npe) {
System.out.println("Null object found.");
return false;
System.err.println("Null object found.");
throw new NullPointerException("Null object found.");
}
}


protected abstract boolean calculateInner();
protected abstract Pair<Boolean, T> calculateInner();
}
20 changes: 17 additions & 3 deletions src/main/java/circuitry/elements/Circuit.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package circuitry.elements;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.tuple.Pair;

/**
*
* Created on: 2022-01-06
*/
public final class Circuit {
private final Calculatable calculatable;
private final Calculatable<?> calculatable;


protected Circuit(final Calculatable calculatable) {
protected Circuit(final Calculatable<?> calculatable) {
Preconditions.checkNotNull(calculatable, "Circuit input should not be null");

this.calculatable = calculatable;
Expand All @@ -22,6 +23,19 @@ public void hello() {


public boolean calculate() {
Object output = this.calculatable.calculate().getRight();

if (output instanceof Double) {
return ((Double) output) >= 0.5;
} else if (output instanceof Boolean) {
return (Boolean) output;
} else {
throw new RuntimeException("Unexpected type of output");
}
}


public Pair<Boolean, ?> calculatePairOutput() {
return this.calculatable.calculate();
}
}
82 changes: 72 additions & 10 deletions src/main/java/circuitry/elements/ElementFactory.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package circuitry.elements;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.tuple.Pair;

import java.util.InputMismatchException;

/**
*
* Created on: 2022-01-06
*/
public final class ElementFactory extends API {
public ElementFactory(int version) {
Expand All @@ -13,12 +16,10 @@ public ElementFactory(int 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) {
public final Circuit createCircuit(final Calculatable<?> a) {
Preconditions.checkNotNull(a, "Circuit input should not be null");

return new Circuit(a);
Expand All @@ -30,25 +31,86 @@ public final InputBoolean createInputBoolean(final boolean value) {
}


public final Calculatable createAndGateWithInputs(final Calculatable a, final Calculatable b) {
public final Calculatable<Boolean> createAndGateWithInputs(final Calculatable<Boolean> a, final Calculatable<Boolean> b) {
Preconditions.checkNotNull(a, "Gate input should not be null");
Preconditions.checkNotNull(b, "Gate input should not be null");

return new And<Boolean>(a, b, Boolean.class);
}


public final Calculatable<Boolean> createOrGateWithInputs(final Calculatable<Boolean> a, final Calculatable<Boolean> b) {
Preconditions.checkNotNull(a, "Gate input should not be null");
Preconditions.checkNotNull(b, "Gate input should not be null");

return new And(a, b);
return new Or<Boolean>(a, b, Boolean.class);
}


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

return new Not<Boolean>(a, Boolean.class);
}


public final InputPair<?> createInputPair(final boolean valueIsBoolean, final Object value) {
if (((value.getClass() == Double.class) && !valueIsBoolean && ((Double) value >= 0) && ((Double) value <= 1))
|| ((value.getClass() == Boolean.class) && valueIsBoolean)) {
return new InputPair<>(Pair.of(valueIsBoolean, value));
}
throw new InputMismatchException("Wrong input pair");
}


public final Calculatable createOrGateWithInputs(final Calculatable a, final Calculatable b) {
public final Calculatable<?> createDualModeAndGateWithInputs(final Calculatable<?> a, final Calculatable<?> b) {
Preconditions.checkNotNull(a, "Gate input should not be null");
Preconditions.checkNotNull(b, "Gate input should not be null");

if (a.calculate().getRight() instanceof Boolean) {
return new And<Boolean>((Calculatable<Boolean>) a, (Calculatable<Boolean>) b, Boolean.class);
} else if (a.calculate().getRight() instanceof Double) {
return new And<Double>((Calculatable<Double>) a, (Calculatable<Double>) b, Double.class);
} else {
throw new InputMismatchException("Wrong matching");
}
}

public final Calculatable<?> createDualModeOrGateWithInputs(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);
if (a.calculate().getRight() instanceof Boolean) {
return new Or<Boolean>((Calculatable<Boolean>) a, (Calculatable<Boolean>) b, Boolean.class);
} else if (a.calculate().getRight() instanceof Double) {
return new Or<Double>((Calculatable<Double>) a, (Calculatable<Double>) b, Double.class);
} else {
throw new InputMismatchException("Wrong matching");
}
}


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

return new Not(a);
if (a.calculate().getRight() instanceof Boolean) {
return new Not<Boolean>((Calculatable<Boolean>) a, Boolean.class);
} else if (a.calculate().getRight() instanceof Double) {
return new Not<Double>((Calculatable<Double>) a, Double.class);
} else {
throw new InputMismatchException("Wrong matching");
}
}


public final Calculatable<Boolean> createGteGateWithInputs(final Calculatable<?> a, final Calculatable<?> b) {
Preconditions.checkNotNull(a, "Gate input should not be null");
Preconditions.checkNotNull(b, "Gate input should not be null");

if (a.calculate().getRight() instanceof Double && b.calculate().getRight() instanceof Double) {
return new Gte<>((Calculatable<Double>) a, (Calculatable<Double>) b);
} else {
throw new InputMismatchException("Wrong matching");
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/circuitry/elements/Gte.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package circuitry.elements;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.tuple.Pair;

/**
* Created on: 2022-01-07
*/
class Gte<T> extends Calculatable<T> {
private final Calculatable<Double> a;
private final Calculatable<Double> b;
private final Class<Double> classOfInputs = Double.class;


protected Gte(final Calculatable<Double> a, final Calculatable<Double> b) {
Preconditions.checkNotNull(a, "Gate input should not be null");
Preconditions.checkNotNull(b, "Gate input should not be null");
Preconditions.checkNotNull(classOfInputs, "Gate input should not be null");

this.a = a;
this.b = b;
}


@Override
protected Pair<Boolean, T> calculateInner() {
if (this.classOfInputs == Double.class) {
Boolean value = ((Double) a.calculate().getRight() >= ((Double) b.calculate().getRight()));
return Pair.of(true, (T) value);
} else {
throw new RuntimeException();
}
}
}
13 changes: 6 additions & 7 deletions src/main/java/circuitry/elements/InputBoolean.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package circuitry.elements;

import org.apache.commons.lang3.tuple.Pair;

/**
*
* Created on: 2022-01-06
*/
public class InputBoolean extends Calculatable {
public final class InputBoolean extends Calculatable<Boolean> {
private boolean value;


protected InputBoolean(final boolean value) {
this.value = value;
}


@Override
protected boolean calculateInner() {
return this.value;
protected Pair<Boolean, Boolean> calculateInner() {
return Pair.of(true, this.value);
}


public void set(final boolean value) {
this.value = value;
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/circuitry/elements/InputPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package circuitry.elements;

import org.apache.commons.lang3.tuple.Pair;

import java.util.InputMismatchException;

/**
* Created on: 2022-01-07
*/
public final class InputPair<T> extends Calculatable<T> {
private Pair<Boolean, ?> pair;

protected InputPair(Pair<Boolean, T> pair) {
this.pair = pair;
}


@Override
protected Pair<Boolean, T> calculateInner() {
try {
return (Pair<Boolean, T>) this.pair;
} catch (ClassCastException classCastException) {
System.err.println("Class cast exception");
throw classCastException;
}
}


public void set(final InputPair<?> updatedInput) {
if (this.pair.getLeft() != updatedInput.pair.getLeft()) {
throw new InputMismatchException("Input signal should not change type (boolean/double)");
}
this.pair = updatedInput.pair;
}
}
25 changes: 19 additions & 6 deletions src/main/java/circuitry/elements/Not.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package circuitry.elements;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.tuple.Pair;

/**
*
* Created on: 2022-01-06
*/
public class Not extends Calculatable {
private final Calculatable a;
class Not<T> extends Calculatable<T> {
private final Calculatable<T> a;
private final Class<T> classOfInputs;


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

this.a = a;
this.classOfInputs = classOfInputs;
}


@Override
protected boolean calculateInner() {
return !a.calculate();
protected Pair<Boolean, T> calculateInner() {
if (this.classOfInputs == Double.class) {
double ad = (Double) a.calculate().getRight();
Double value = (1 - ad);
return Pair.of(false, (T) value);
} else if (this.classOfInputs == Boolean.class) {
Boolean value = !((Boolean) ((Boolean) a.calculate().getRight()));
return Pair.of(true, (T) value);
} else {
throw new RuntimeException();
}
}
}
Loading

0 comments on commit 18f5c2b

Please sign in to comment.