• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
12  * under the License.
13  */
14 
15 package com.google.common.math;
16 
17 import com.google.common.annotations.GwtCompatible;
18 
19 import java.math.BigInteger;
20 
21 import javax.annotation.Nullable;
22 
23 /**
24  * A collection of preconditions for math functions.
25  *
26  * @author Louis Wasserman
27  */
28 @GwtCompatible
29 final class MathPreconditions {
checkPositive(@ullable String role, int x)30   static int checkPositive(@Nullable String role, int x) {
31     if (x <= 0) {
32       throw new IllegalArgumentException(role + " (" + x + ") must be > 0");
33     }
34     return x;
35   }
36 
checkPositive(@ullable String role, long x)37   static long checkPositive(@Nullable String role, long x) {
38     if (x <= 0) {
39       throw new IllegalArgumentException(role + " (" + x + ") must be > 0");
40     }
41     return x;
42   }
43 
checkPositive(@ullable String role, BigInteger x)44   static BigInteger checkPositive(@Nullable String role, BigInteger x) {
45     if (x.signum() <= 0) {
46       throw new IllegalArgumentException(role + " (" + x + ") must be > 0");
47     }
48     return x;
49   }
50 
checkNonNegative(@ullable String role, int x)51   static int checkNonNegative(@Nullable String role, int x) {
52     if (x < 0) {
53       throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
54     }
55     return x;
56   }
57 
checkNonNegative(@ullable String role, long x)58   static long checkNonNegative(@Nullable String role, long x) {
59     if (x < 0) {
60       throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
61     }
62     return x;
63   }
64 
checkNonNegative(@ullable String role, BigInteger x)65   static BigInteger checkNonNegative(@Nullable String role, BigInteger x) {
66     if (x.signum() < 0) {
67       throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
68     }
69     return x;
70   }
71 
checkNonNegative(@ullable String role, double x)72   static double checkNonNegative(@Nullable String role, double x) {
73     if (!(x >= 0)) { // not x < 0, to work with NaN.
74       throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
75     }
76     return x;
77   }
78 
checkRoundingUnnecessary(boolean condition)79   static void checkRoundingUnnecessary(boolean condition) {
80     if (!condition) {
81       throw new ArithmeticException("mode was UNNECESSARY, but rounding was necessary");
82     }
83   }
84 
checkInRange(boolean condition)85   static void checkInRange(boolean condition) {
86     if (!condition) {
87       throw new ArithmeticException("not in range");
88     }
89   }
90 
checkNoOverflow(boolean condition)91   static void checkNoOverflow(boolean condition) {
92     if (!condition) {
93       throw new ArithmeticException("overflow");
94     }
95   }
96 
MathPreconditions()97   private MathPreconditions() {}
98 }
99