Coverage Summary for Class: Internal (com.google.common.util.concurrent)

Class Class, % Method, % Line, %
Internal 0% (0/1) 0% (0/2) 0% (0/4)


1 /* 2  * Copyright (C) 2019 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.util.concurrent; 16  17 import com.google.common.annotations.GwtIncompatible; 18 import java.time.Duration; 19  20 /** This class is for {@code com.google.common.util.concurrent} use only! */ 21 @GwtIncompatible // java.time.Duration 22 @ElementTypesAreNonnullByDefault 23 final class Internal { 24  25  /** 26  * Returns the number of nanoseconds of the given duration without throwing or overflowing. 27  * 28  * <p>Instead of throwing {@link ArithmeticException}, this method silently saturates to either 29  * {@link Long#MAX_VALUE} or {@link Long#MIN_VALUE}. This behavior can be useful when decomposing 30  * a duration in order to call a legacy API which requires a {@code long, TimeUnit} pair. 31  */ 32  static long toNanosSaturated(Duration duration) { 33  // Using a try/catch seems lazy, but the catch block will rarely get invoked (except for 34  // durations longer than approximately +/- 292 years). 35  try { 36  return duration.toNanos(); 37  } catch (ArithmeticException tooBig) { 38  return duration.isNegative() ? Long.MIN_VALUE : Long.MAX_VALUE; 39  } 40  } 41  42  private Internal() {} 43 }