|
17 | 17 | package com.example.time.testing; |
18 | 18 |
|
19 | 19 | import com.example.time.Clock; |
| 20 | +import java.time.Duration; |
| 21 | +import java.time.Instant; |
20 | 22 | import java.util.concurrent.atomic.AtomicLong; |
21 | | -import org.joda.time.Instant; |
22 | | -import org.joda.time.ReadableDuration; |
23 | | -import org.joda.time.ReadableInstant; |
24 | 23 |
|
25 | 24 | /** |
26 | 25 | * A Clock that returns a fixed Instant value as the current clock time. The fixed Instant is |
|
35 | 34 | */ |
36 | 35 | public class FakeClock implements Clock { |
37 | 36 |
|
38 | | - private static final Instant DEFAULT_TIME = new Instant(1000000000L); |
| 37 | + private static final Instant DEFAULT_TIME = Instant.ofEpochMilli(1000000000L); |
39 | 38 | private final long baseTimeMs; |
40 | 39 | private final AtomicLong fakeNowMs; |
41 | 40 | private volatile long autoIncrementStepMs; |
@@ -50,8 +49,8 @@ public FakeClock() { |
50 | 49 | /** |
51 | 50 | * Creates a FakeClock instance initialized to the given time. |
52 | 51 | */ |
53 | | - public FakeClock(ReadableInstant now) { |
54 | | - baseTimeMs = now.getMillis(); |
| 52 | + public FakeClock(Instant now) { |
| 53 | + baseTimeMs = now.toEpochMilli(); |
55 | 54 | fakeNowMs = new AtomicLong(baseTimeMs); |
56 | 55 | } |
57 | 56 |
|
@@ -60,8 +59,8 @@ public FakeClock(ReadableInstant now) { |
60 | 59 | * |
61 | 60 | * @return this |
62 | 61 | */ |
63 | | - public FakeClock setNow(ReadableInstant now) { |
64 | | - fakeNowMs.set(now.getMillis()); |
| 62 | + public FakeClock setNow(Instant now) { |
| 63 | + fakeNowMs.set(now.toEpochMilli()); |
65 | 64 | return this; |
66 | 65 | } |
67 | 66 |
|
@@ -75,7 +74,7 @@ public Instant now() { |
75 | 74 | * behavior of {@link #now()} is the same as this method. |
76 | 75 | */ |
77 | 76 | public Instant peek() { |
78 | | - return new Instant(fakeNowMs.get()); |
| 77 | + return Instant.ofEpochMilli(fakeNowMs.get()); |
79 | 78 | } |
80 | 79 |
|
81 | 80 | /** |
@@ -95,8 +94,8 @@ public FakeClock resetTime() { |
95 | 94 | * @param duration the duration to increment the clock time by |
96 | 95 | * @return this |
97 | 96 | */ |
98 | | - public FakeClock incrementTime(ReadableDuration duration) { |
99 | | - incrementTime(duration.getMillis()); |
| 97 | + public FakeClock incrementTime(Duration duration) { |
| 98 | + incrementTime(duration.toMillis()); |
100 | 99 | return this; |
101 | 100 | } |
102 | 101 |
|
@@ -117,8 +116,8 @@ public FakeClock incrementTime(long durationMs) { |
117 | 116 | * @param duration the duration to decrement the clock time by |
118 | 117 | * @return this |
119 | 118 | */ |
120 | | - public FakeClock decrementTime(ReadableDuration duration) { |
121 | | - incrementTime(-duration.getMillis()); |
| 119 | + public FakeClock decrementTime(Duration duration) { |
| 120 | + incrementTime(-duration.toMillis()); |
122 | 121 | return this; |
123 | 122 | } |
124 | 123 |
|
@@ -140,8 +139,8 @@ public FakeClock decrementTime(long durationMs) { |
140 | 139 | * @param autoIncrementStep the new auto increment duration |
141 | 140 | * @return this |
142 | 141 | */ |
143 | | - public FakeClock setAutoIncrementStep(ReadableDuration autoIncrementStep) { |
144 | | - setAutoIncrementStep(autoIncrementStep.getMillis()); |
| 142 | + public FakeClock setAutoIncrementStep(Duration autoIncrementStep) { |
| 143 | + setAutoIncrementStep(autoIncrementStep.toMillis()); |
145 | 144 | return this; |
146 | 145 | } |
147 | 146 |
|
@@ -165,7 +164,7 @@ public FakeClock setAutoIncrementStep(long autoIncrementStepMs) { |
165 | 164 | * @see AtomicLong#addAndGet |
166 | 165 | */ |
167 | 166 | protected final Instant addAndGet(long durationMs) { |
168 | | - return new Instant(fakeNowMs.addAndGet(durationMs)); |
| 167 | + return Instant.ofEpochMilli(fakeNowMs.addAndGet(durationMs)); |
169 | 168 | } |
170 | 169 |
|
171 | 170 | /** |
@@ -176,6 +175,6 @@ protected final Instant addAndGet(long durationMs) { |
176 | 175 | * @see AtomicLong#getAndAdd |
177 | 176 | */ |
178 | 177 | protected final Instant getAndAdd(long durationMs) { |
179 | | - return new Instant(fakeNowMs.getAndAdd(durationMs)); |
| 178 | + return Instant.ofEpochMilli(fakeNowMs.getAndAdd(durationMs)); |
180 | 179 | } |
181 | 180 | } |