com.hazelcast.util
Class QuickMath

java.lang.Object
  extended by com.hazelcast.util.QuickMath

public final class QuickMath
extends Object

The class QuickMath contains methods to perform optimized mathematical operations. Methods are allowed to put additional constraints on the range of input values if required for efficiency. Methods are not required to perform validation of input arguments, but they have to indicate the constraints in theirs contract.


Method Summary
static String bytesToHex(byte[] in)
           
static int compareIntegers(int i1, int i2)
          Compares two integers
static int compareLongs(long l1, long l2)
          Compares two longs
static int divideByAndCeilToInt(double d, int k)
          Divide d by k and return the smallest integer greater than or equal to the result.
static long divideByAndCeilToLong(double d, int k)
          Divide d by k and return the smallest integer greater than or equal to the result.
static int divideByAndRoundToInt(double d, int k)
          Divide d by k and return the int value closest to the result.
static long divideByAndRoundToLong(double d, int k)
          Divide d by k and return the long value closest to the result.
static boolean isPowerOfTwo(long x)
          Return true if input argument is power of two.
static int log2(int value)
          Return the log 2 result for this int.
static int log2(long value)
          Return the log 2 result for this long.
static int modPowerOfTwo(int a, int b)
          Computes the remainder of the division of a by b.
static long modPowerOfTwo(long a, int b)
          Computes the remainder of the division of a by b.
static int nextPowerOfTwo(int value)
          Returns the next power of two that is larger than the specified int value.
static long nextPowerOfTwo(long value)
          Returns the next power of two that is larger than the specified long value.
static int normalize(int value, int factor)
          Divide value by factor, take the smallest integer greater than or equal to the result, multiply that integer by factor, and return it.
static long normalize(long value, int factor)
          Divide value by factor, take the smallest integer greater than or equal to the result, multiply that integer by factor, and return it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isPowerOfTwo

public static boolean isPowerOfTwo(long x)
Return true if input argument is power of two. Input has to be a a positive integer.

Result is undefined for zero or negative integers.

Parameters:
x - test x to see if it is a power of two
Returns:
true if x is power of two

modPowerOfTwo

public static int modPowerOfTwo(int a,
                                int b)
Computes the remainder of the division of a by b. a has to be a non-negative integer and b has to be a power of two, otherwise the result is undefined.

Parameters:
a - divide a by b. a must be a non-negative integer
b - divide a by b. b must be a power of two
Returns:
remainder of the division of a by b.

modPowerOfTwo

public static long modPowerOfTwo(long a,
                                 int b)
Computes the remainder of the division of a by b. a has to be a non-negative integer and b has to be a power of two, otherwise the result is undefined.

Parameters:
a - divide a by b. a must be a non-negative integer
b - divide a by b. b must be a power of two
Returns:
remainder of the division of a by b.

nextPowerOfTwo

public static int nextPowerOfTwo(int value)
Returns the next power of two that is larger than the specified int value.

Parameters:
value - the int value
Returns:
the next power of two that is larger than the specified int value.

nextPowerOfTwo

public static long nextPowerOfTwo(long value)
Returns the next power of two that is larger than the specified long value.

Parameters:
value - the long value
Returns:
the next power of two that is larger than the specified long value

log2

public static int log2(int value)
Return the log 2 result for this int.

Parameters:
value - the int value
Returns:
the log 2 result for value

log2

public static int log2(long value)
Return the log 2 result for this long.

Parameters:
value - the long value
Returns:
the log 2 result for value

divideByAndCeilToInt

public static int divideByAndCeilToInt(double d,
                                       int k)
Divide d by k and return the smallest integer greater than or equal to the result.

Parameters:
d - divide d by k
k - divide d by k
Returns:
the smallest integer greater than or equal to the result

divideByAndCeilToLong

public static long divideByAndCeilToLong(double d,
                                         int k)
Divide d by k and return the smallest integer greater than or equal to the result.

Parameters:
d - divide d by k
k - divide d by k
Returns:
the smallest integer greater than or equal to the result

divideByAndRoundToInt

public static int divideByAndRoundToInt(double d,
                                        int k)
Divide d by k and return the int value closest to the result.

Parameters:
d - divide d by k
k - divide d by k
Returns:
the int value closest to the result

divideByAndRoundToLong

public static long divideByAndRoundToLong(double d,
                                          int k)
Divide d by k and return the long value closest to the result.

Parameters:
d - divide d by k
k - divide d by k
Returns:
the long value closest to the result

normalize

public static int normalize(int value,
                            int factor)
Divide value by factor, take the smallest integer greater than or equal to the result, multiply that integer by factor, and return it.

Parameters:
value - normalize this value by factor
factor - normalize this value by factor
Returns:
the result of value being normalized by factor

normalize

public static long normalize(long value,
                             int factor)
Divide value by factor, take the smallest integer greater than or equal to the result, multiply that integer by factor, and return it.

Parameters:
value - normalize this value by factor
factor - normalize this value by factor
Returns:
the result of value being normalized by factor

bytesToHex

public static String bytesToHex(byte[] in)

compareIntegers

public static int compareIntegers(int i1,
                                  int i2)
Compares two integers

Parameters:
i1 - First number to compare with second one
i2 - Second number to compare with first one
Returns:
+1 if i1 > i2, -1 if i2 > i1, 0 if i1 and i2 are equals

compareLongs

public static int compareLongs(long l1,
                               long l2)
Compares two longs

Parameters:
l1 - First number to compare with second one
l2 - Second number to compare with first one
Returns:
+1 if l1 > l2, -1 if l2 > l1, 0 if l1 and l2 are equals


Copyright © 2015 Hazelcast, Inc.. All Rights Reserved.