forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChessClock.java
More file actions
138 lines (112 loc) · 3.78 KB
/
Copy pathChessClock.java
File metadata and controls
138 lines (112 loc) · 3.78 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package modern.challenge;
import java.io.Serializable;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Objects;
public class ChessClock extends Clock implements Serializable {
public enum Player {
LEFT, RIGHT
}
private static final long serialVersionUID = 1L;
private Instant instantStart;
private Instant instantLeft;
private Instant instantRight;
private long timeLeft;
private long timeRight;
private Player player;
public ChessClock(Player player) {
this.player = player;
}
public Instant gameStart() {
if (this.instantStart == null) {
this.timeLeft = 0;
this.timeRight = 0;
this.instantStart = Instant.now();
this.instantLeft = instantStart;
this.instantRight = instantStart;
return instantStart;
}
throw new IllegalStateException("Game already started. Stop it and try again.");
}
public Instant gameEnd() {
if (this.instantStart != null) {
instantStart = null;
return Instant.now();
}
throw new IllegalStateException("Game was not started.");
}
@Override
public ZoneId getZone() {
return ZoneOffset.UTC;
}
@Override
public Clock withZone(ZoneId zone) {
throw new UnsupportedOperationException("The ChessClock works only in UTC time zone");
}
@Override
public Instant instant() {
if (this.instantStart != null) {
if (player == Player.LEFT) {
player = Player.RIGHT;
long secondsLeft = Instant.now().getEpochSecond() - instantRight.getEpochSecond();
instantLeft = instantLeft.plusSeconds(secondsLeft - timeLeft);
timeLeft = secondsLeft;
return instantLeft;
} else {
player = Player.LEFT;
long secondsRight = Instant.now().getEpochSecond() - instantLeft.getEpochSecond();
instantRight = instantRight.plusSeconds(secondsRight - timeRight);
timeRight = secondsRight;
return instantRight;
}
}
throw new IllegalStateException("Game was not started.");
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + Objects.hashCode(this.instantStart);
hash = 97 * hash + Objects.hashCode(this.instantLeft);
hash = 97 * hash + Objects.hashCode(this.instantRight);
hash = 97 * hash + (int) (this.timeLeft ^ (this.timeLeft >>> 32));
hash = 97 * hash + (int) (this.timeRight ^ (this.timeRight >>> 32));
hash = 97 * hash + Objects.hashCode(this.player);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ChessClock other = (ChessClock) obj;
if (this.timeLeft != other.timeLeft) {
return false;
}
if (this.timeRight != other.timeRight) {
return false;
}
if (!Objects.equals(this.instantStart, other.instantStart)) {
return false;
}
if (!Objects.equals(this.instantLeft, other.instantLeft)) {
return false;
}
if (!Objects.equals(this.instantRight, other.instantRight)) {
return false;
}
return this.player == other.player;
}
@Override
public String toString() {
return "ChessClock{" + "Left move =" + instantLeft
+ ", Right move =" + instantRight + ", player=" + player + '}';
}
}