KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > MathUtils


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 import java.math.BigDecimal JavaDoc;
8 import java.sql.SQLException JavaDoc;
9
10 import org.h2.engine.Constants;
11 import org.h2.message.Message;
12
13 /**
14  * @author Thomas
15  */

16
17 public class MathUtils {
18     // with blockSizePowerOf2 8: 0 > 0; 1..8 > 8, 9..16 > 16, ...
19
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 JavaDoc setScale(BigDecimal JavaDoc bd, int scale) throws SQLException JavaDoc {
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 JavaDoc s) {
47         return Byte.decode(s).byteValue();
48     }
49
50     public static short decodeShort(String JavaDoc s) {
51         return Short.decode(s).shortValue();
52     }
53
54     public static int decodeInt(String JavaDoc s) {
55         return Integer.decode(s).intValue();
56     }
57     
58     public static long decodeLong(String JavaDoc 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