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 compareInt(int a, int b)
          Compare two values.
static int compareLong(long a, long b)
          Compare two values.
static int convertLongToInt(long l)
          Convert a long value to an int value.
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 precision(java.math.BigDecimal x)
          Compatibility for BigDecimal.precision() which is not available in Java 1.4.
static void randomBytes(byte[] bytes)
          Get a number of pseudo random bytes.
static int randomInt(int lowerThan)
          Get a pseudo random int value between 0 (including and the given value (excluding).
static int reverseInt(int x)
          Reverse the bits in a 32 bit integer.
static long reverseLong(long x)
          Reverse the bits in a 64 bit long.
static int roundUpInt(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 byte[] secureRandomBytes(int len)
          Get a number of cryptographically secure pseudo random bytes.
static int secureRandomInt(int lowerThan)
          Get a cryptographically secure pseudo random int value between 0 (including and the given value (excluding).
static long secureRandomLong()
          Get a cryptographically secure pseudo random long value.
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

roundUpInt

public static int roundUpInt(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

setScale

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

Parameters:
bd - the BigDecimal value
scale - the new scale
Returns:
the scaled 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

reverseInt

public static int reverseInt(int x)
Reverse the bits in a 32 bit integer. This code is also available in Java 5 using Integer.reverse, however not available yet in Retrotranslator. The code was taken from http://www.hackersdelight.org - reverse.c

Parameters:
x - the original value
Returns:
the value with reversed bits

reverseLong

public static long reverseLong(long x)
Reverse the bits in a 64 bit long. This code is also available in Java 5 using Long.reverse, however not available yet in Retrotranslator.

Parameters:
x - the original value
Returns:
the value with reversed bits

precision

public static int precision(java.math.BigDecimal x)
Compatibility for BigDecimal.precision() which is not available in Java 1.4.

Parameters:
x - the value
Returns:
the precision

compareInt

public static int compareInt(int a,
                             int b)
Compare two values. Returns -1 if the first value is smaller, 1 if bigger, and 0 if equal.

Parameters:
a - the first value
b - the second value
Returns:
the result

compareLong

public static int compareLong(long a,
                              long b)
Compare two values. Returns -1 if the first value is smaller, 1 if bigger, and 0 if equal.

Parameters:
a - the first value
b - the second value
Returns:
the result

secureRandomLong

public static long secureRandomLong()
Get a cryptographically secure pseudo random long value.

Returns:
the random long value

randomBytes

public static void randomBytes(byte[] bytes)
Get a number of pseudo random bytes.

Parameters:
bytes - the target array

secureRandomBytes

public static byte[] secureRandomBytes(int len)
Get a number of cryptographically secure pseudo random bytes.

Parameters:
len - the number of bytes
Returns:
the random bytes

randomInt

public static int randomInt(int lowerThan)
Get a pseudo random int value between 0 (including and the given value (excluding). The value is not cryptographically secure.

Parameters:
lowerThan - the value returned will be lower than this value
Returns:
the random long value

secureRandomInt

public static int secureRandomInt(int lowerThan)
Get a cryptographically secure pseudo random int value between 0 (including and the given value (excluding).

Parameters:
lowerThan - the value returned will be lower than this value
Returns:
the random long value