The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.core / src / net / mograsim / logic / core / components / CoreManualSwitch.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.types.BitVector;
12 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
13 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
14
15 /**
16  * This class models a simple on/off (ONE/ZERO) switch for user interaction.
17  *
18  * @author Christian Femers
19  *
20  */
21 public class CoreManualSwitch extends CoreComponent implements LogicObservable
22 {
23         private Collection<LogicObserver> observers;
24         private ReadWriteEnd output;
25         private BitVector inputValues;
26
27         public CoreManualSwitch(Timeline timeline, ReadWriteEnd output)
28         {
29                 super(timeline);
30                 observers = new ArrayList<>();
31                 this.output = output;
32                 this.inputValues = output.getInputValues();
33         }
34
35         public void switchFullOn()
36         {
37                 setState(BitVector.of(Bit.ONE, output.width()));
38         }
39
40         public void switchFullOff()
41         {
42                 setState(BitVector.of(Bit.ZERO, output.width()));
43         }
44
45         public void toggle()
46         {
47                 if (isFullOn())
48                         switchFullOff();
49                 else
50                         switchFullOn();
51         }
52
53         public void setState(Bit bit)
54         {
55                 setState(BitVector.of(bit));
56         }
57
58         public void setState(BitVector bits)
59         {
60                 if (bits.length() != output.width())
61                         throw new IllegalArgumentException("Incorrect bit vector length");
62                 if (bits.equals(inputValues))
63                         return;
64                 inputValues = bits;
65                 output.feedSignals(bits);
66                 notifyObservers();
67         }
68
69         public boolean isFullOn()
70         {
71                 return BitVector.of(Bit.ONE, output.width()).equals(output.getInputValues());
72         }
73
74         public BitVector getValues()
75         {
76                 return inputValues;
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 }