9ec98ede1297411f357fb7a2bfb8e2a437af247d
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / components / ManualSwitch.java
1 package net.mograsim.logic.core.components;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6
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;
13
14 /**
15  * This class models a simple on/off (ONE/ZERO) switch for user interaction.
16  *
17  * @author Christian Femers
18  *
19  */
20 public class ManualSwitch extends Component implements LogicObservable
21 {
22         private Collection<LogicObserver> observers;
23         private ReadWriteEnd output;
24
25         public ManualSwitch(Timeline timeline, ReadWriteEnd output)
26         {
27                 super(timeline);
28                 observers = new ArrayList<>();
29                 if (output.length() != 1)
30                         throw new IllegalArgumentException("Switch output can be only a single wire");
31                 this.output = output;
32         }
33
34         public void switchOn()
35         {
36                 setState(true);
37         }
38
39         public void switchOff()
40         {
41                 setState(false);
42         }
43
44         public void toggle()
45         {
46                 setState(!isOn());
47         }
48
49         public void setState(boolean isOn)
50         {
51                 setToValueOf(isOn ? Bit.ONE : Bit.ZERO);
52         }
53
54         public void setToValueOf(Bit bit)
55         {
56                 if (!bit.isBinary())
57                         throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit);
58                 if (bit == output.getInputValue())
59                         return;
60                 output.feedSignals(bit);
61                 notifyObservers();
62         }
63
64         public boolean isOn()
65         {
66                 return output.getInputValue() == Bit.ONE;
67         }
68
69         public Bit getValue()
70         {
71                 return output.getInputValue();
72         }
73
74         @Override
75         public List<ReadEnd> getAllInputs()
76         {
77                 return List.of();
78         }
79
80         @Override
81         public List<ReadWriteEnd> getAllOutputs()
82         {
83                 return List.of(output);
84         }
85
86         @Override
87         public void registerObserver(LogicObserver ob)
88         {
89                 observers.add(ob);
90         }
91
92         @Override
93         public void deregisterObserver(LogicObserver ob)
94         {
95                 observers.remove(ob);
96         }
97
98         @Override
99         public void notifyObservers()
100         {
101                 observers.forEach(ob -> ob.update(this));
102         }
103
104 }