-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFanSpeed.java
More file actions
47 lines (35 loc) · 1.07 KB
/
FanSpeed.java
File metadata and controls
47 lines (35 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package behavioral.command;
import java.util.Objects;
public class FanSpeed {
private final static String[] SPEEDS = {"LOW", "MEDIUM", "HIGH"};
private static FanSpeed fanSpeed;
private Integer currentSpeed;
private FanSpeed() {
this.currentSpeed = 1;
}
public static FanSpeed getInstance() {
if (Objects.isNull(fanSpeed)) {
synchronized (FanSpeed.class) {
if (Objects.isNull(fanSpeed)) {
fanSpeed = new FanSpeed();
}
}
}
return fanSpeed;
}
public String getCurrentSpeed() {
return SPEEDS[this.currentSpeed];
}
public String next() {
this.currentSpeed = Math.floorMod(this.currentSpeed + 1, SPEEDS.length);
return getCurrentSpeed();
}
public String previous() {
this.currentSpeed = Math.floorMod(this.currentSpeed - 1, SPEEDS.length);
return getCurrentSpeed();
}
@Override
public String toString() {
return "Fan speed is set on '" + getCurrentSpeed() + "'";
}
}