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 @ElementTypesAreNonnullByDefault 29 final class MathPreconditions { 30 @CanIgnoreReturnValue checkPositive(String role, int x)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 @CanIgnoreReturnValue checkPositive(String role, long x)39 static long checkPositive(String role, long x) { 40 if (x <= 0) { 41 throw new IllegalArgumentException(role + " (" + x + ") must be > 0"); 42 } 43 return x; 44 } 45 46 @CanIgnoreReturnValue checkPositive(String role, BigInteger x)47 static BigInteger checkPositive(String role, BigInteger x) { 48 if (x.signum() <= 0) { 49 throw new IllegalArgumentException(role + " (" + x + ") must be > 0"); 50 } 51 return x; 52 } 53 54 @CanIgnoreReturnValue checkNonNegative(String role, int x)55 static int checkNonNegative(String role, int x) { 56 if (x < 0) { 57 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 58 } 59 return x; 60 } 61 62 @CanIgnoreReturnValue checkNonNegative(String role, long x)63 static long checkNonNegative(String role, long x) { 64 if (x < 0) { 65 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 66 } 67 return x; 68 } 69 70 @CanIgnoreReturnValue checkNonNegative(String role, BigInteger x)71 static BigInteger checkNonNegative(String role, BigInteger x) { 72 if (x.signum() < 0) { 73 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 74 } 75 return x; 76 } 77 78 @CanIgnoreReturnValue checkNonNegative(String role, double x)79 static double checkNonNegative(String role, double x) { 80 if (!(x >= 0)) { // not x < 0, to work with NaN. 81 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 82 } 83 return x; 84 } 85 checkRoundingUnnecessary(boolean condition)86 static void checkRoundingUnnecessary(boolean condition) { 87 if (!condition) { 88 throw new ArithmeticException("mode was UNNECESSARY, but rounding was necessary"); 89 } 90 } 91 checkInRangeForRoundingInputs(boolean condition, double input, RoundingMode mode)92 static void checkInRangeForRoundingInputs(boolean condition, double input, RoundingMode mode) { 93 if (!condition) { 94 throw new ArithmeticException( 95 "rounded value is out of range for input " + input + " and rounding mode " + mode); 96 } 97 } 98 checkNoOverflow(boolean condition, String methodName, int a, int b)99 static void checkNoOverflow(boolean condition, String methodName, int a, int b) { 100 if (!condition) { 101 throw new ArithmeticException("overflow: " + methodName + "(" + a + ", " + b + ")"); 102 } 103 } 104 checkNoOverflow(boolean condition, String methodName, long a, long b)105 static void checkNoOverflow(boolean condition, String methodName, long a, long b) { 106 if (!condition) { 107 throw new ArithmeticException("overflow: " + methodName + "(" + a + ", " + b + ")"); 108 } 109 } 110 MathPreconditions()111 private MathPreconditions() {} 112 } 113