-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from georgevangelou/generics
API v2
- Loading branch information
Showing
17 changed files
with
573 additions
and
49 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
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
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
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
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(); | ||
} | ||
} | ||
} |
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
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(); | ||
} |
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
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
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
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(); | ||
} | ||
} | ||
} |
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
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
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; | ||
} | ||
} |
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
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.