-
Notifications
You must be signed in to change notification settings - Fork 743
/
Copy pathBridge.java
41 lines (32 loc) · 994 Bytes
/
Bridge.java
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
package nextstep.ladder.model;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
public class Bridge {
private final boolean value;
private static final Map<Boolean, Bridge> cacheBridge = new HashMap<>();
static {
cacheBridge.put(Boolean.TRUE, new Bridge(true));
cacheBridge.put(Boolean.FALSE, new Bridge(false));
}
private Bridge(boolean value) {
this.value = value;
}
public static Bridge of(boolean value) {
return cacheBridge.get(value);
}
public boolean canCrossBridge() {
return value;
}
public Bridge next() {
if (this.value) {
return of(false);
}
return of(ThreadLocalRandom.current().nextBoolean());
}
public void compareToNextBridge(Bridge next) {
if (this.value && next.value) {
throw new IllegalArgumentException("연속해서 true인 Bridge가 있습니다.");
}
}
}