org.h2.util
Class MathUtils

java.lang.Object
  extended by org.h2.util.MathUtils

public class MathUtils
extends java.lang.Object

This is a utility class with mathematical helper functions.


Method Summary
static void checkPowerOf2(int len)
          Check if a value is a power of two.
static int convertLongToInt(long l)
          Convert a long value to an int value.
static byte decodeByte(java.lang.String s)
          Parse a string to a byte.
static int decodeInt(java.lang.String s)
          Parse a string to an int.
static long decodeLong(java.lang.String s)
          Parse a string to a long.
static short decodeShort(java.lang.String s)
          Parse a string to a short.
static int nextPowerOf2(int x)
          Get the value that is equal or higher than this value, and that is a power of two.
static int roundUp(int x, int blockSizePowerOf2)
          Round the value up to the next block size.
static long roundUpLong(long x, long blockSizePowerOf2)
          Round the value up to the next block size.
static long scaleUp50Percent(long start, long min, long blockSize, long maxIncrease)
          Increase the value by about 50%.
static java.math.BigDecimal setScale(java.math.BigDecimal bd, int scale)
          Set the scale of a BigDecimal value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

roundUp

public static int roundUp(int x,
                          int blockSizePowerOf2)
Round the value up to the next block size. The block size must be a power of two. As an example, using the block size of 8, the following rounding operations are done: 0 stays 0; values 1..8 results in 8, 9..16 results in 16, and so on.

Parameters:
x - the value to be rounded
blockSizePowerOf2 - the block size
Returns:
the rounded value

roundUpLong

public static long roundUpLong(long x,
                               long blockSizePowerOf2)
Round the value up to the next block size. The block size must be a power of two. As an example, using the block size of 8, the following rounding operations are done: 0 stays 0; values 1..8 results in 8, 9..16 results in 16, and so on.

Parameters:
x - the value to be rounded
blockSizePowerOf2 - the block size
Returns:
the rounded value

checkPowerOf2

public static void checkPowerOf2(int len)
Check if a value is a power of two.

Parameters:
len - the value to check
Throws:
java.lang.RuntimeException - if it is not a power of two

nextPowerOf2

public static int nextPowerOf2(int x)
Get the value that is equal or higher than this value, and that is a power of two.

Parameters:
x - the original value
Returns:
the next power of two value

scaleUp50Percent

public static long scaleUp50Percent(long start,
                                    long min,
                                    long blockSize,
                                    long maxIncrease)
Increase the value by about 50%. The method is used to increase the file size in larger steps.

Parameters:
start - the smallest possible returned value
min - the current value
blockSize - the block size
maxIncrease - the maximum increment
Returns:
the new value

setScale

public static java.math.BigDecimal setScale(java.math.BigDecimal bd,
                                            int scale)
                                     throws java.sql.SQLException
Set the scale of a BigDecimal value.

Parameters:
bd - the BigDecimal value
scale - the new scale
Returns:
the scaled value
Throws:
java.sql.SQLException

decodeByte

public static byte decodeByte(java.lang.String s)
Parse a string to a byte. This method uses the decode method to support decimal, hexadecimal and octal values.

Parameters:
s - the string to parse
Returns:
the value

decodeShort

public static short decodeShort(java.lang.String s)
Parse a string to a short. This method uses the decode method to support decimal, hexadecimal and octal values.

Parameters:
s - the string to parse
Returns:
the value

decodeInt

public static int decodeInt(java.lang.String s)
Parse a string to an int. This method uses the decode method to support decimal, hexadecimal and octal values.

Parameters:
s - the string to parse
Returns:
the value

decodeLong

public static long decodeLong(java.lang.String s)
Parse a string to a long. This method uses the decode method to support decimal, hexadecimal and octal values.

Parameters:
s - the string to parse
Returns:
the value

convertLongToInt

public static int convertLongToInt(long l)
Convert a long value to an int value. Values larger than the biggest int value is converted to the biggest int value, and values smaller than the smallest int value are converted to the smallest int value.

Parameters:
l - the value to convert
Returns:
the converted int value