1/* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.common.math; 18 19import static com.google.common.base.Preconditions.checkNotNull; 20 21import com.google.common.annotations.GwtCompatible; 22 23import java.math.BigInteger; 24 25/** 26 * A collection of preconditions for math functions. 27 * 28 * @author Louis Wasserman 29 */ 30@GwtCompatible 31final class MathPreconditions { 32 static int checkPositive(String role, int x) { 33 if (x <= 0) { 34 throw new IllegalArgumentException(role + " (" + x + ") must be > 0"); 35 } 36 return x; 37 } 38 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 static BigInteger checkPositive(String role, BigInteger x) { 47 if (x.signum() <= 0) { 48 throw new IllegalArgumentException(role + " (" + x + ") must be > 0"); 49 } 50 return x; 51 } 52 53 static int checkNonNegative(String role, int x) { 54 if (x < 0) { 55 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 56 } 57 return x; 58 } 59 60 static long checkNonNegative(String role, long x) { 61 if (x < 0) { 62 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 63 } 64 return x; 65 } 66 67 static BigInteger checkNonNegative(String role, BigInteger x) { 68 if (checkNotNull(x).signum() < 0) { 69 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0"); 70 } 71 return x; 72 } 73 74 static void checkRoundingUnnecessary(boolean condition) { 75 if (!condition) { 76 throw new ArithmeticException("mode was UNNECESSARY, but rounding was necessary"); 77 } 78 } 79 80 static void checkInRange(boolean condition) { 81 if (!condition) { 82 throw new ArithmeticException("not in range"); 83 } 84 } 85 86 static void checkNoOverflow(boolean condition) { 87 if (!condition) { 88 throw new ArithmeticException("overflow"); 89 } 90 } 91 92 private MathPreconditions() {} 93} 94