Coverage Summary for Class: MathPreconditions (com.google.common.math)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| MathPreconditions | 100% (1/1) | 25% (3/12) | 17.6% (6/34) |
1 /* 2 * Copyright (C) 2011 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.math; 16 17 import com.google.common.annotations.GwtCompatible; 18 import com.google.errorprone.annotations.CanIgnoreReturnValue; 19 import java.math.BigInteger; 20 import java.math.RoundingMode; 21 22 /** 23 * A collection of preconditions for math functions. 24 * 25 * @author Louis Wasserman 26 */ 27 @GwtCompatible 28 @CanIgnoreReturnValue 29 @ElementTypesAreNonnullByDefault 30 final class MathPreconditions { 31 static int checkPositive(String role, int x) { 32 if (x <= 0) { 33 throw new IllegalArgumentException(role + " (" + x + ") must be > 0"); 34 } 35 return x; 36 } 37 38 static long checkPositive(String role, long x) { 39 if (x <= 0) { 40 throw new IllegalArgumentException(role + " (" + x + ") must be > 0"); 41 } 42 return x; 43 } 44 45 static BigInteger checkPositive(String role, BigInteger x) { 46 if (x.signum() <= 0) { 47 throw new IllegalArgumentException(role + " (" + x + ") must be > 0"); 48 } 49 return x; 50 } 51 52 static int checkNonNegative(String role, int x) { 53 if (x < 0) { 54 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 55 } 56 return x; 57 } 58 59 static long checkNonNegative(String role, long x) { 60 if (x < 0) { 61 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 62 } 63 return x; 64 } 65 66 static BigInteger checkNonNegative(String role, BigInteger x) { 67 if (x.signum() < 0) { 68 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 69 } 70 return x; 71 } 72 73 static double checkNonNegative(String role, double x) { 74 if (!(x >= 0)) { // not x < 0, to work with NaN. 75 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 76 } 77 return x; 78 } 79 80 static void checkRoundingUnnecessary(boolean condition) { 81 if (!condition) { 82 throw new ArithmeticException("mode was UNNECESSARY, but rounding was necessary"); 83 } 84 } 85 86 static void checkInRangeForRoundingInputs(boolean condition, double input, RoundingMode mode) { 87 if (!condition) { 88 throw new ArithmeticException( 89 "rounded value is out of range for input " + input + " and rounding mode " + mode); 90 } 91 } 92 93 static void checkNoOverflow(boolean condition, String methodName, int a, int b) { 94 if (!condition) { 95 throw new ArithmeticException("overflow: " + methodName + "(" + a + ", " + b + ")"); 96 } 97 } 98 99 static void checkNoOverflow(boolean condition, String methodName, long a, long b) { 100 if (!condition) { 101 throw new ArithmeticException("overflow: " + methodName + "(" + a + ", " + b + ")"); 102 } 103 } 104 105 private MathPreconditions() {} 106 }