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 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 byte[] generateAlternativeSeed()
          Generate a seed value, using as much unpredictable data as possible.
static int nextPowerOf2(int x)
          Get the value that is equal or higher than this value, and that is a power of two.
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 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.
 
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

generateAlternativeSeed

public static byte[] generateAlternativeSeed()
Generate a seed value, using as much unpredictable data as possible.

Returns:
the seed

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

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

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