Skip to content

Commit 84bd014

Browse files
klueverejona86
authored andcommitted
context: Remove mention of "epoch" from Ticker.nanoTime() javadocs, plus other minor touchups
In Java, when people hear "epoch", they think "unix epoch". cl/747082451
1 parent 65d0bb8 commit 84bd014

File tree

1 file changed

+17
-32
lines changed

1 file changed

+17
-32
lines changed

api/src/context/java/io/grpc/Deadline.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package io.grpc;
1818

19-
import java.util.Arrays;
19+
import static java.util.Objects.requireNonNull;
20+
2021
import java.util.Locale;
22+
import java.util.Objects;
2123
import java.util.concurrent.ScheduledExecutorService;
2224
import java.util.concurrent.ScheduledFuture;
2325
import java.util.concurrent.TimeUnit;
@@ -33,7 +35,7 @@
3335
* passed to the various components unambiguously.
3436
*/
3537
public final class Deadline implements Comparable<Deadline> {
36-
private static final SystemTicker SYSTEM_TICKER = new SystemTicker();
38+
private static final Ticker SYSTEM_TICKER = new SystemTicker();
3739
// nanoTime has a range of just under 300 years. Only allow up to 100 years in the past or future
3840
// to prevent wraparound as long as process runs for less than ~100 years.
3941
private static final long MAX_OFFSET = TimeUnit.DAYS.toNanos(100 * 365);
@@ -91,7 +93,7 @@ public static Deadline after(long duration, TimeUnit units) {
9193
* @since 1.24.0
9294
*/
9395
public static Deadline after(long duration, TimeUnit units, Ticker ticker) {
94-
checkNotNull(units, "units");
96+
requireNonNull(units, "units");
9597
return new Deadline(ticker, units.toNanos(duration), true);
9698
}
9799

@@ -191,8 +193,8 @@ public long timeRemaining(TimeUnit unit) {
191193
* @return {@link ScheduledFuture} which can be used to cancel execution of the task
192194
*/
193195
public ScheduledFuture<?> runOnExpiration(Runnable task, ScheduledExecutorService scheduler) {
194-
checkNotNull(task, "task");
195-
checkNotNull(scheduler, "scheduler");
196+
requireNonNull(task, "task");
197+
requireNonNull(scheduler, "scheduler");
196198
return scheduler.schedule(task, deadlineNanos - ticker.nanoTime(), TimeUnit.NANOSECONDS);
197199
}
198200

@@ -225,37 +227,27 @@ public String toString() {
225227
@Override
226228
public int compareTo(Deadline that) {
227229
checkTicker(that);
228-
long diff = this.deadlineNanos - that.deadlineNanos;
229-
if (diff < 0) {
230-
return -1;
231-
} else if (diff > 0) {
232-
return 1;
233-
}
234-
return 0;
230+
return Long.compare(this.deadlineNanos, that.deadlineNanos);
235231
}
236232

237233
@Override
238234
public int hashCode() {
239-
return Arrays.asList(this.ticker, this.deadlineNanos).hashCode();
235+
return Objects.hash(this.ticker, this.deadlineNanos);
240236
}
241237

242238
@Override
243-
public boolean equals(final Object o) {
244-
if (o == this) {
239+
public boolean equals(final Object object) {
240+
if (object == this) {
245241
return true;
246242
}
247-
if (!(o instanceof Deadline)) {
248-
return false;
249-
}
250-
251-
final Deadline other = (Deadline) o;
252-
if (this.ticker == null ? other.ticker != null : this.ticker != other.ticker) {
243+
if (!(object instanceof Deadline)) {
253244
return false;
254245
}
255-
if (this.deadlineNanos != other.deadlineNanos) {
246+
final Deadline that = (Deadline) object;
247+
if (this.ticker == null ? that.ticker != null : this.ticker != that.ticker) {
256248
return false;
257249
}
258-
return true;
250+
return this.deadlineNanos == that.deadlineNanos;
259251
}
260252

261253
/**
@@ -275,24 +267,17 @@ public boolean equals(final Object o) {
275267
* @since 1.24.0
276268
*/
277269
public abstract static class Ticker {
278-
/** Returns the number of nanoseconds since this source's epoch. */
270+
/** Returns the number of nanoseconds elapsed since this ticker's reference point in time. */
279271
public abstract long nanoTime();
280272
}
281273

282-
private static class SystemTicker extends Ticker {
274+
private static final class SystemTicker extends Ticker {
283275
@Override
284276
public long nanoTime() {
285277
return System.nanoTime();
286278
}
287279
}
288280

289-
private static <T> T checkNotNull(T reference, Object errorMessage) {
290-
if (reference == null) {
291-
throw new NullPointerException(String.valueOf(errorMessage));
292-
}
293-
return reference;
294-
}
295-
296281
private void checkTicker(Deadline other) {
297282
if (ticker != other.ticker) {
298283
throw new AssertionError(

0 commit comments

Comments
 (0)