1616
1717package io .grpc ;
1818
19- import java .util .Arrays ;
19+ import static java .util .Objects .requireNonNull ;
20+
2021import java .util .Locale ;
22+ import java .util .Objects ;
2123import java .util .concurrent .ScheduledExecutorService ;
2224import java .util .concurrent .ScheduledFuture ;
2325import java .util .concurrent .TimeUnit ;
3335 * passed to the various components unambiguously.
3436 */
3537public 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