Completely changed the structure and switched to Eclipse Plugin.
[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         private boolean isOn;
25
26         public ManualSwitch(Timeline timeline, ReadWriteEnd output)
27         {
28                 super(timeline);
29                 observers = new ArrayList<>();
30                 if (output.length() != 1)
31                         throw new IllegalArgumentException("Switch output can be only a single wire");
32                 this.output = output;
33         }
34
35         public void switchOn()
36         {
37                 setState(true);
38         }
39
40         public void switchOff()
41         {
42                 setState(false);
43         }
44
45         public void toggle()
46         {
47                 setState(!isOn);
48         }
49
50         public void setState(boolean isOn)
51         {
52                 if (this.isOn == isOn)
53                         return;
54                 this.isOn = isOn;
55                 output.feedSignals(getValue());
56                 notifyObservers();
57         }
58
59         public void setToValueOf(Bit bit)
60         {
61                 if (bit == Bit.ONE)
62                         switchOn();
63                 else if (bit == Bit.ZERO)
64                         switchOff();
65                 else
66                         throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit);
67         }
68
69         public boolean isOn()
70         {
71                 return isOn;
72         }
73
74         public Bit getValue()
75         {
76                 return isOn ? Bit.ONE : Bit.ZERO;
77         }
78
79         @Override
80         public List<ReadEnd> getAllInputs()
81         {
82                 return List.of();
83         }
84
85         @Override
86         public List<ReadWriteEnd> getAllOutputs()
87         {
88                 return List.of(output);
89         }
90
91         @Override
92         public void registerObserver(LogicObserver ob)
93         {
94                 observers.add(ob);
95         }
96
97         @Override
98         public void deregisterObserver(LogicObserver ob)
99         {
100                 observers.remove(ob);
101         }
102
103         @Override
104         public void notifyObservers()
105         {
106                 observers.forEach(ob -> ob.update(this));
107         }
108
109 }