Reformatted everything. Eclipse built-in Linewrapping/Comments 140 chars
[Mograsim.git] / era.mi / src / era / mi / logic / components / ManualSwitch.java
1 package era.mi.logic.components;
2
3 import java.util.List;
4
5 import era.mi.logic.Bit;
6 import era.mi.logic.wires.WireArray;
7 import era.mi.logic.wires.WireArray.WireArrayInput;
8
9 /**
10  * This class models a simple on/off (ONE/ZERO) switch for user interaction.
11  *
12  * @author Christian Femers
13  *
14  */
15 public class ManualSwitch implements Component {
16         private WireArray output;
17         private WireArrayInput outputI;
18         private boolean isOn;
19
20         public ManualSwitch(WireArray output) {
21                 if (output.length != 1)
22                         throw new IllegalArgumentException("Switch output can be only a single wire");
23                 this.output = output;
24                 this.outputI = output.createInput();
25         }
26
27         public void switchOn() {
28                 setState(true);
29         }
30
31         public void switchOff() {
32                 setState(false);
33         }
34
35         public void toggle() {
36                 setState(!isOn);
37         }
38
39         public void setState(boolean isOn) {
40                 if (this.isOn == isOn)
41                         return;
42                 this.isOn = isOn;
43                 outputI.feedSignals(getValue());
44         }
45
46         public boolean isOn() {
47                 return isOn;
48         }
49
50         public Bit getValue() {
51                 return isOn ? Bit.ONE : Bit.ZERO;
52         }
53
54         @Override
55         public List<WireArray> getAllInputs() {
56                 return List.of();
57         }
58
59         @Override
60         public List<WireArray> getAllOutputs() {
61                 return List.of(output);
62         }
63
64 }