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=fc6e9432271c96fc69dd183e6b17fb762b4a447b;hpb=07faf07e3acb8b2afdc2bf65a46bc868faaed0f8;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 fc6e9432..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 @@ -1,7 +1,11 @@ package net.mograsim.logic.core.components; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import net.mograsim.logic.core.LogicObservable; +import net.mograsim.logic.core.LogicObserver; import net.mograsim.logic.core.timeline.Timeline; import net.mograsim.logic.core.types.Bit; import net.mograsim.logic.core.wires.Wire.ReadEnd; @@ -13,14 +17,15 @@ import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; * @author Christian Femers * */ -public class ManualSwitch extends Component +public class ManualSwitch extends Component implements LogicObservable { + private Collection observers; private ReadWriteEnd output; - private boolean isOn; public ManualSwitch(Timeline timeline, ReadWriteEnd output) { super(timeline); + observers = new ArrayList<>(); if (output.length() != 1) throw new IllegalArgumentException("Switch output can be only a single wire"); this.output = output; @@ -38,25 +43,32 @@ public class ManualSwitch extends Component public void toggle() { - setState(!isOn); + setState(!isOn()); } public void setState(boolean isOn) { - if (this.isOn == isOn) + setToValueOf(isOn ? Bit.ONE : Bit.ZERO); + } + + public void setToValueOf(Bit bit) + { + if (!bit.isBinary()) + throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit); + if (bit == output.getInputValue()) return; - this.isOn = isOn; - output.feedSignals(getValue()); + 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 @@ -71,4 +83,22 @@ public class ManualSwitch extends Component return List.of(output); } + @Override + public void registerObserver(LogicObserver ob) + { + observers.add(ob); + } + + @Override + public void deregisterObserver(LogicObserver ob) + { + observers.remove(ob); + } + + @Override + public void notifyObservers() + { + observers.forEach(ob -> ob.update(this)); + } + }