1 package net.mograsim.logic.core.components;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import net.mograsim.logic.core.LogicObservable;
8 import net.mograsim.logic.core.LogicObserver;
9 import net.mograsim.logic.core.timeline.Timeline;
10 import net.mograsim.logic.core.types.Bit;
11 import net.mograsim.logic.core.wires.Wire.ReadEnd;
12 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
15 * This class models a simple on/off (ONE/ZERO) switch for user interaction.
17 * @author Christian Femers
20 public class ManualSwitch extends Component implements LogicObservable
22 private Collection<LogicObserver> observers;
23 private ReadWriteEnd output;
26 public ManualSwitch(Timeline timeline, ReadWriteEnd output)
29 observers = new ArrayList<>();
30 if (output.length() != 1)
31 throw new IllegalArgumentException("Switch output can be only a single wire");
35 public void switchOn()
40 public void switchOff()
50 public void setState(boolean isOn)
52 if (this.isOn == isOn)
55 output.feedSignals(getValue());
59 public void setToValueOf(Bit bit)
63 else if (bit == Bit.ZERO)
66 throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit);
76 return isOn ? Bit.ONE : Bit.ZERO;
80 public List<ReadEnd> getAllInputs()
86 public List<ReadWriteEnd> getAllOutputs()
88 return List.of(output);
92 public void registerObserver(LogicObserver ob)
98 public void deregisterObserver(LogicObserver ob)
100 observers.remove(ob);
104 public void notifyObservers()
106 observers.forEach(ob -> ob.update(this));