forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrainDelay.java
More file actions
35 lines (28 loc) · 967 Bytes
/
Copy pathTrainDelay.java
File metadata and controls
35 lines (28 loc) · 967 Bytes
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
package modern.challenge;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class TrainDelay implements Delayed {
private final String id;
private final String to;
private final long departAt;
public TrainDelay(String id, String to, long delayOfDeparture) {
this.id = id;
this.to = to;
this.departAt = System.currentTimeMillis() + delayOfDeparture;
}
// when we try to consume a train
// this method decides if it is expired or not
@Override
public long getDelay(TimeUnit tu) {
long diff = departAt - System.currentTimeMillis();
return tu.convert(diff, TimeUnit.MILLISECONDS);
}
@Override
public int compareTo(Delayed t) {
return Long.compare(departAt, ((TrainDelay) t).departAt);
}
@Override
public String toString() {
return "TrainDelay{" + "id=" + id + ", to=" + to + ", departAt=" + departAt + '}';
}
}