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;
25 public ManualSwitch(Timeline timeline, ReadWriteEnd output)
28 observers = new ArrayList<>();
29 if (output.length() != 1)
30 throw new IllegalArgumentException("Switch output can be only a single wire");
34 public void switchOn()
39 public void switchOff()
49 public void setState(boolean isOn)
51 setToValueOf(isOn ? Bit.ONE : Bit.ZERO);
54 public void setToValueOf(Bit bit)
57 throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit);
58 if (bit == output.getInputValue())
60 output.feedSignals(bit);
66 return output.getInputValue() == Bit.ONE;
71 return output.getInputValue();
75 public List<ReadEnd> getAllInputs()
81 public List<ReadWriteEnd> getAllOutputs()
83 return List.of(output);
87 public void registerObserver(LogicObserver ob)
93 public void deregisterObserver(LogicObserver ob)
99 public void notifyObservers()
101 observers.forEach(ob -> ob.update(this));