1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 package androidx.leanback.util;
15 
16 import androidx.annotation.RestrictTo;
17 
18 /**
19  * Math Utilities for leanback library.
20  * @hide
21  */
22 @RestrictTo(RestrictTo.Scope.LIBRARY)
23 public final class MathUtil {
24 
MathUtil()25     private MathUtil() {
26         // Prevent construction of this util class
27     }
28 
29     /**
30      * Convert long to int safely. Similar with Math.toIntExact() in Java 8.
31      * @param numLong Number of type long to convert.
32      * @return int version of input.
33      * @throws ArithmeticException If input overflows int.
34      */
safeLongToInt(long numLong)35     public static int safeLongToInt(long numLong) {
36         if ((int) numLong != numLong) {
37             throw new ArithmeticException("Input overflows int.\n");
38         }
39         return (int) numLong;
40     }
41 }
42