Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009-2011 Alexander Kerner. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package net.sf.kerner.utils.math;
 
Perform arithmetic operations with number over- and underflow check.

Example:

 TODO example
 

Author(s):
Alexander Kerner
Version:
2011-07-07
 
 public class ArithmeticSavety {
 
     public static final BigInteger BIG_MAX_INT = BigInteger.valueOf(.);
     public static final BigInteger BIG_MIN_INT = BigInteger.valueOf(.);
 
     public static final BigInteger BIG_MAX_LONG = BigInteger.valueOf(.);
     public static final BigInteger BIG_MIN_LONG = BigInteger.valueOf(.);
 
     public static final BigDecimal BIG_MAX_DECIMAL = BigDecimal.valueOf(.);
     public static final BigDecimal BIG_MIN_DECIMAL = BigDecimal.valueOf(.);

    
Check if {@link java.lang.Integer.MIN_VALUE <= b <= java.lang.Integer.MAX_VALUE .

Parameters:
b BigInteger to check if in range
See also:
java.math.BigInteger
 
     public static void rangeCheckInteger(BigInteger b) {
         if (b.compareTo() == 1 || b.compareTo() == -1)
             throw new ArithmeticException("Integer overflow");
     }

    
Check if {@link java.lang.Long.MIN_VALUE <= b <= java.lang.Long.MAX_VALUE .

Parameters:
b BigInteger to check if in range
See also:
java.math.BigInteger
 
     public static void rangeCheckLong(BigInteger b) {
         if (b.compareTo() == 1 || b.compareTo() == -1)
             throw new ArithmeticException("Double overflow");
     }
 
     public static void rangeCheckDouble(BigDecimal b) {
         if (b.compareTo() == 1 || b.compareTo() == -1)
             throw new ArithmeticException("Double overflow");
     }

    
Perform addition of two int values with arithmetic safety. if resulting int would over- or underflow an exception will be thrown.

Parameters:
a first value
b second value
Returns:
a + b
 
     public static int addInt(int aint b) {
         final BigInteger bb = BigInteger.valueOf(a).add(BigInteger.valueOf(b));
         rangeCheckInteger(bb);
         return bb.intValue();
     }

    
Perform addition of two long values with arithmetic safety. if resulting long would over- or underflow an exception will be thrown.

Parameters:
a first value
b second value
Returns:
a + b
    public static long addLong(long along b) {
        final BigInteger bb = BigInteger.valueOf(a).add(BigInteger.valueOf(b));
        rangeCheckLong(bb);
        return bb.longValue();
    }

    
Perform multiplication of two long values with arithmetic safety. if resulting long would over- or underflow an exception will be thrown.

Parameters:
a first value
b second value
Returns:
a * b
    public static int multiply(int aint b) {
        final BigInteger bb = BigInteger.valueOf(a).multiply(BigInteger.valueOf(b));
        rangeCheckInteger(bb);
        return bb.intValue();
    }

    
Perform multiplication of two long values with arithmetic safety. if resulting long would over- or underflow an exception will be thrown.

Parameters:
a first value
b second value
Returns:
a * b
    public static long multiply(long along b) {
        final BigInteger bb = BigInteger.valueOf(a).multiply(BigInteger.valueOf(b));
        rangeCheckLong(bb);
        return bb.longValue();
    }
    public static double multiply(double adouble b) {
        final BigDecimal bb = BigDecimal.valueOf(a).multiply(BigDecimal.valueOf(b));
        rangeCheckDouble(bb);
        return bb.doubleValue();
    }

    
Increment a BigInteger with arithmetic safety.

Parameters:
integer BigInteger to increment
Returns:
incremented BigInteger
    public static Integer increment(Integer integer) {
        return Integer.valueOf(addInt(integer.intValue(), 1));
    }

    
Add one java.lang.Integer to another java.lang.Integer with arithmetic safety.

Parameters:
integer1 first value
integer2 second value
Returns:
a + b
    public static Integer add(Integer integer1Integer integer2) {
        return Integer.valueOf(addInt(integer1.intValue(), integer2.intValue()));
    }
New to GrepCode? Check out our FAQ X