1 5 package org.h2.util; 6 7 import java.math.BigDecimal ; 8 import java.sql.SQLException ; 9 10 import org.h2.engine.Constants; 11 import org.h2.message.Message; 12 13 16 17 public class MathUtils { 18 public static int roundUp(int x, int blockSizePowerOf2) { 20 return (x + blockSizePowerOf2 - 1) & (-blockSizePowerOf2); 21 } 22 23 public static void checkPowerOf2(int len) { 24 if((len & (len-1)) != 0 && len > 0) { 25 throw Message.getInternalError("not a power of 2: " + len); 26 } 27 } 28 29 public static long scaleUp50Percent(long start, long min, long blockSize) { 30 while(start < min) { 31 start += start / 2; 32 start += start % blockSize; 33 } 34 return start; 35 } 36 37 public static BigDecimal setScale(BigDecimal bd, int scale) throws SQLException { 38 if(scale > Constants.BIGDECIMAL_SCALE_MAX) { 39 throw Message.getInvalidValueException(""+scale, "scale"); 40 } else if(scale < 0) { 41 throw Message.getInvalidValueException(""+scale, "scale"); 42 } 43 return bd.setScale(scale, BigDecimal.ROUND_HALF_UP); 44 } 45 46 public static byte decodeByte(String s) { 47 return Byte.decode(s).byteValue(); 48 } 49 50 public static short decodeShort(String s) { 51 return Short.decode(s).shortValue(); 52 } 53 54 public static int decodeInt(String s) { 55 return Integer.decode(s).intValue(); 56 } 57 58 public static long decodeLong(String s) { 59 return Long.decode(s).longValue(); 60 } 61 62 public static int convertLongToInt(long l) { 63 if(l<=Integer.MIN_VALUE) { 64 return Integer.MIN_VALUE; 65 } else if(l>=Integer.MAX_VALUE) { 66 return Integer.MAX_VALUE; 67 } else { 68 return (int) l; 69 } 70 } 71 72 } 73 | Popular Tags |