X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fcomponents%2FManualSwitch.java;h=9ec98ede1297411f357fb7a2bfb8e2a437af247d;hb=c6ad3a18e8a437d7b131b203267fe2723708641f;hp=83842ddae1909768736880c42d7194313d62101d;hpb=b37ba7609a925cc945bbac0f6ead619d07912238;p=Mograsim.git diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java index 83842dda..9ec98ede 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java @@ -21,7 +21,6 @@ public class ManualSwitch extends Component implements LogicObservable { private Collection observers; private ReadWriteEnd output; - private boolean isOn; public ManualSwitch(Timeline timeline, ReadWriteEnd output) { @@ -44,36 +43,32 @@ public class ManualSwitch extends Component implements LogicObservable public void toggle() { - setState(!isOn); + setState(!isOn()); } public void setState(boolean isOn) { - if (this.isOn == isOn) - return; - this.isOn = isOn; - output.feedSignals(getValue()); - notifyObservers(); + setToValueOf(isOn ? Bit.ONE : Bit.ZERO); } public void setToValueOf(Bit bit) { - if (bit == Bit.ONE) - switchOn(); - else if (bit == Bit.ZERO) - switchOff(); - else + if (!bit.isBinary()) throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit); + if (bit == output.getInputValue()) + return; + output.feedSignals(bit); + notifyObservers(); } public boolean isOn() { - return isOn; + return output.getInputValue() == Bit.ONE; } public Bit getValue() { - return isOn ? Bit.ONE : Bit.ZERO; + return output.getInputValue(); } @Override