Skip to content

Commit d393a25

Browse files
committed
PR fixes
1 parent 5ef637f commit d393a25

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

driver-core/src/main/com/mongodb/internal/time/TimePoint.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public Timeout asTimeout() {
129129
* timeout {@link #isInfinite()} before calling.
130130
*/
131131
private long remaining(final TimeUnit unit) {
132-
if (nanos == null) {
132+
if (isInfinite()) {
133133
throw new AssertionError("Infinite TimePoints have infinite remaining time");
134134
}
135135
long remaining = nanos - currentNanos();
@@ -146,7 +146,7 @@ private long remaining(final TimeUnit unit) {
146146
* @see #durationSince(TimePoint)
147147
*/
148148
public Duration elapsed() {
149-
if (nanos == null) {
149+
if (isInfinite()) {
150150
throw new AssertionError("No time can elapse since an infinite TimePoint");
151151
}
152152
return Duration.ofNanos(currentNanos() - nanos);
@@ -160,13 +160,13 @@ public Duration elapsed() {
160160
* @see #elapsed()
161161
*/
162162
Duration durationSince(final TimePoint t) {
163-
if (nanos == null) {
163+
if (this.isInfinite()) {
164164
throw new AssertionError("this timepoint is infinite, with no duration since");
165165
}
166-
if (t.nanos == null) {
166+
if (t.isInfinite()) {
167167
throw new AssertionError("the other timepoint is infinite, with no duration until");
168168
}
169-
return Duration.ofNanos(nanos - t.nanos);
169+
return Duration.ofNanos(nanos - assertNotNull(t.nanos));
170170
}
171171

172172
/**
@@ -189,7 +189,7 @@ public TimePoint timeoutAfterOrInfiniteIfNegative(final long timeoutValue, final
189189
* @param duration A duration that may also be {@linkplain Duration#isNegative() negative}.
190190
*/
191191
TimePoint add(final Duration duration) {
192-
if (nanos == null) {
192+
if (isInfinite()) {
193193
throw new AssertionError("No time can be added to an infinite TimePoint");
194194
}
195195
long durationNanos = duration.toNanos();
@@ -205,12 +205,12 @@ TimePoint add(final Duration duration) {
205205
public int compareTo(final TimePoint t) {
206206
if (Objects.equals(nanos, t.nanos)) {
207207
return 0;
208-
} else if (nanos == null) {
208+
} else if (this.isInfinite()) {
209209
return 1;
210-
} else if (t.nanos == null) {
210+
} else if (t.isInfinite()) {
211211
return -1;
212212
}
213-
return Long.signum(nanos - t.nanos);
213+
return Long.signum(nanos - assertNotNull(t.nanos));
214214
}
215215

216216
@Override
@@ -232,9 +232,9 @@ public int hashCode() {
232232

233233
@Override
234234
public String toString() {
235-
long remainingMs = nanos == null
236-
? -1
237-
: TimeUnit.MILLISECONDS.convert(currentNanos() - nanos, NANOSECONDS);
235+
String remainingMs = isInfinite()
236+
? "infinite"
237+
: "" + TimeUnit.MILLISECONDS.convert(currentNanos() - nanos, NANOSECONDS);
238238
return "TimePoint{"
239239
+ "nanos=" + nanos
240240
+ "remainingMs=" + remainingMs

driver-core/src/main/com/mongodb/internal/time/Timeout.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.mongodb.internal.time;
1717

1818
import com.mongodb.MongoInterruptedException;
19+
import com.mongodb.assertions.Assertions;
1920
import com.mongodb.internal.function.CheckedConsumer;
2021
import com.mongodb.internal.function.CheckedFunction;
2122
import com.mongodb.internal.function.CheckedRunnable;
@@ -86,14 +87,17 @@ static Timeout expiresIn(final long duration, final TimeUnit unit, final ZeroDur
8687
// TODO (CSOT) confirm that all usages in final PR always supply a non-negative duration
8788
if (duration < 0) {
8889
throw new AssertionError("Timeouts must not be in the past");
89-
}
90-
91-
if (zeroDurationIs == ZeroDurationIs.INFINITE) {
92-
if (duration == 0) {
93-
return Timeout.infinite();
90+
} else if (duration == 0) {
91+
switch (zeroDurationIs) {
92+
case INFINITE:
93+
return Timeout.infinite();
94+
case EXPIRED:
95+
return TimePoint.now();
96+
default:
97+
throw Assertions.fail("Unknown enum value");
9498
}
95-
return expiresIn(duration, unit, ZeroDurationIs.EXPIRED);
9699
} else {
100+
// duration will never be negative
97101
return TimePoint.now().timeoutAfterOrInfiniteIfNegative(duration, unit);
98102
}
99103
}

0 commit comments

Comments
 (0)