16
16
17
17
package io .grpc ;
18
18
19
- import java .util .Arrays ;
19
+ import static java .util .Objects .requireNonNull ;
20
+
20
21
import java .util .Locale ;
22
+ import java .util .Objects ;
21
23
import java .util .concurrent .ScheduledExecutorService ;
22
24
import java .util .concurrent .ScheduledFuture ;
23
25
import java .util .concurrent .TimeUnit ;
33
35
* passed to the various components unambiguously.
34
36
*/
35
37
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 ();
37
39
// nanoTime has a range of just under 300 years. Only allow up to 100 years in the past or future
38
40
// to prevent wraparound as long as process runs for less than ~100 years.
39
41
private static final long MAX_OFFSET = TimeUnit .DAYS .toNanos (100 * 365 );
@@ -91,7 +93,7 @@ public static Deadline after(long duration, TimeUnit units) {
91
93
* @since 1.24.0
92
94
*/
93
95
public static Deadline after (long duration , TimeUnit units , Ticker ticker ) {
94
- checkNotNull (units , "units" );
96
+ requireNonNull (units , "units" );
95
97
return new Deadline (ticker , units .toNanos (duration ), true );
96
98
}
97
99
@@ -191,8 +193,8 @@ public long timeRemaining(TimeUnit unit) {
191
193
* @return {@link ScheduledFuture} which can be used to cancel execution of the task
192
194
*/
193
195
public ScheduledFuture <?> runOnExpiration (Runnable task , ScheduledExecutorService scheduler ) {
194
- checkNotNull (task , "task" );
195
- checkNotNull (scheduler , "scheduler" );
196
+ requireNonNull (task , "task" );
197
+ requireNonNull (scheduler , "scheduler" );
196
198
return scheduler .schedule (task , deadlineNanos - ticker .nanoTime (), TimeUnit .NANOSECONDS );
197
199
}
198
200
@@ -225,37 +227,27 @@ public String toString() {
225
227
@ Override
226
228
public int compareTo (Deadline that ) {
227
229
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 );
235
231
}
236
232
237
233
@ Override
238
234
public int hashCode () {
239
- return Arrays . asList (this .ticker , this .deadlineNanos ). hashCode ( );
235
+ return Objects . hash (this .ticker , this .deadlineNanos );
240
236
}
241
237
242
238
@ Override
243
- public boolean equals (final Object o ) {
244
- if (o == this ) {
239
+ public boolean equals (final Object object ) {
240
+ if (object == this ) {
245
241
return true ;
246
242
}
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 )) {
253
244
return false ;
254
245
}
255
- if (this .deadlineNanos != other .deadlineNanos ) {
246
+ final Deadline that = (Deadline ) object ;
247
+ if (this .ticker == null ? that .ticker != null : this .ticker != that .ticker ) {
256
248
return false ;
257
249
}
258
- return true ;
250
+ return this . deadlineNanos == that . deadlineNanos ;
259
251
}
260
252
261
253
/**
@@ -275,24 +267,17 @@ public boolean equals(final Object o) {
275
267
* @since 1.24.0
276
268
*/
277
269
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 . */
279
271
public abstract long nanoTime ();
280
272
}
281
273
282
- private static class SystemTicker extends Ticker {
274
+ private static final class SystemTicker extends Ticker {
283
275
@ Override
284
276
public long nanoTime () {
285
277
return System .nanoTime ();
286
278
}
287
279
}
288
280
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
-
296
281
private void checkTicker (Deadline other ) {
297
282
if (ticker != other .ticker ) {
298
283
throw new AssertionError (
0 commit comments