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 package org.spf4j.base;
33
34 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35 import java.util.concurrent.TimeoutException;
36 import javax.annotation.Nonnegative;
37
38
39
40
41 @SuppressFBWarnings("FCCD_FIND_CLASS_CIRCULAR_DEPENDENCY")
42 public final class TimeoutDeadline {
43
44 private final long timeoutNanos;
45
46 private final long deadlineNanos;
47
48 public TimeoutDeadline(@Nonnegative final long timeoutNanos, final long deadlineNanos) {
49 this.timeoutNanos = timeoutNanos;
50 this.deadlineNanos = deadlineNanos;
51 }
52
53 public static TimeoutDeadline of(final long timeoutNanos, final long deadlineNanos)
54 throws TimeoutException {
55 if (timeoutNanos < 0) {
56 throw new TimeoutException("Timeout exceeded by " + (-timeoutNanos) + " ns, deadline: "
57 + Timing.getCurrentTiming().fromNanoTimeToInstant(deadlineNanos));
58 }
59 return new TimeoutDeadline(timeoutNanos, deadlineNanos);
60 }
61
62
63 public long getTimeoutNanos() {
64 return timeoutNanos;
65 }
66
67 public long getDeadlineNanos() {
68 return deadlineNanos;
69 }
70
71 @Override
72 public int hashCode() {
73 int hash = 3;
74 hash = 23 * hash + (int) (this.timeoutNanos ^ (this.timeoutNanos >>> 32));
75 return 23 * hash + (int) (this.deadlineNanos ^ (this.deadlineNanos >>> 32));
76 }
77
78 @Override
79 public boolean equals(final Object obj) {
80 if (this == obj) {
81 return true;
82 }
83 if (obj == null) {
84 return false;
85 }
86 if (getClass() != obj.getClass()) {
87 return false;
88 }
89 final TimeoutDeadline other = (TimeoutDeadline) obj;
90 if (this.timeoutNanos != other.timeoutNanos) {
91 return false;
92 }
93 return this.deadlineNanos == other.deadlineNanos;
94 }
95
96 @Override
97 public String toString() {
98 return "TimeoutDeadline{" + "timeoutNanos=" + timeoutNanos + ", deadlineNanos=" + deadlineNanos + '}';
99 }
100
101
102 }