ariba.util.core
Class MathUtil

java.lang.Object
  extended by ariba.util.core.MathUtil

public final class MathUtil
extends java.lang.Object

Math Utilities. These are helper functions for dealing with math.


Method Summary
static int bound(int value, int lower, int upper)
          Bounds value to be within lower and upper, inclusively.
static int forceEven(int n)
          Returns the specified number with its least significant bit zeroed.
static int forceOdd(int n)
          Returns the specified number with its least significant bit set.
static long fromBase36(java.lang.String s)
          Convert a string representation of a base36 number into a long.
static long fromBase36(java.lang.String s, int offset)
          Convert a string representation of a base36 number into a long.
static long fromBase64(java.lang.String s)
          Convert a string representation of a base64 number into a long.
static long fromBase64(java.lang.String s, int offset)
          Convert a string representation of a base64 number into a long.
static long fromBase64(java.lang.String s, int beginIndex, int endIndex)
          Convert a string representation of a base64 number into a long.
static long getGCD(long a, long b)
          Calculate the greatest common denominator for two longs.
static long getLCM(long a, long b)
          Calculate the least common multiple for two longs.
static int log2(long x)
          Calculate the log (base 2) of a specified long.
static int modulo10(double d, boolean round)
          Returns the integer result of (d mod 10).
static int normalHashCode(char[] val)
          Private method that calculates the hash code for the char[] val according to the java spec.
static java.util.List permutations(int n, int r)
          Returns a List of Lists, where each sub-List is a list of Integers representing one permutation of r numbers between 0 and n.
static long power2(int x)
          Calculate 2 raised to a specified power.
static double powerOfTen(int power)
          Calculate 10 raised to a specified power.
static java.math.BigDecimal setScale(java.math.BigDecimal value, int scale)
          Adjust the scale of the given BigDecimal to the specified scale.
static java.lang.Double setScale(java.lang.Double value, int scale)
           
static int sgn(int a, int b)
          Returns the value of the sgn mathematical function (the signum function) on the operands a and b.
static int sgn(long a, long b)
          Returns the value of the sgn mathematical function (the signum function) on the operands a and b.
static int sunHashCode(char[] val)
          Private method that calculates the hash code for the char[] val according to the sun's unique implementation which they say will be standard in 1.2 (love those standards)
static java.lang.String toBase36(long num)
          Converts the given long into a base 36 string.
static int toBase36(long num, char[] chr, int off)
          Converts the given long into a base 36 string.
static java.lang.String toBase64(long num)
          Converts the given long into a base 64 string.
static int toBase64(long num, char[] chr, int off)
          Converts the given long into a base 64 string.
static long unsignedUniqueId(java.lang.String s)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

log2

public static int log2(long x)
Calculate the log (base 2) of a specified long. This can be useful for calculating a bit index from a bit mask.

Parameters:
x - a long to calculate the log of
Returns:
floor(lg(x)), that is, the greatest integer less than or equal to log base 2 of x.

power2

public static long power2(int x)
Calculate 2 raised to a specified power.

Parameters:
x - the power to raise 2 to, must be greater or equal to 0
Returns:
2 raised to the power of x.

forceEven

public static int forceEven(int n)
Returns the specified number with its least significant bit zeroed.

Parameters:
n - an integer to make even
Returns:
the specified number with its least significant bit zeroed

powerOfTen

public static double powerOfTen(int power)
Calculate 10 raised to a specified power.

Parameters:
power - the power to raise 10 to
Returns:
10 raised to the power of power.

modulo10

public static int modulo10(double d,
                           boolean round)
Returns the integer result of (d mod 10). If round is true, then result will be rounded, otherwise, the resulted will be the greatest integer at or below the result.

Parameters:
d - a double to mod against 10
round - if true the double will be rounded to the nearest long, if false floor() will be called.
Returns:
an integer between 0 and 9 of the mod result

forceOdd

public static int forceOdd(int n)
Returns the specified number with its least significant bit set.

Parameters:
n - an integer to make odd
Returns:
the specified number with its least significant bit set

getGCD

public static long getGCD(long a,
                          long b)
Calculate the greatest common denominator for two longs.

Parameters:
a - the first number in long
b - the sencond number in long
Returns:
long value that is the greatest common denominator of the two. 0 if either of the two numbers is 0 or negative.

getLCM

public static long getLCM(long a,
                          long b)
Calculate the least common multiple for two longs.

Parameters:
a - the first number in long
b - the sencond number in long
Returns:
long value that is the least common multiple of the two. 0 if either of the two numbers is 0 or negative.

setScale

public static java.math.BigDecimal setScale(java.math.BigDecimal value,
                                            int scale)
Adjust the scale of the given BigDecimal to the specified scale.

Parameters:
value - the BigDecimal
scale - the scale, which can't be negative.

setScale

public static java.lang.Double setScale(java.lang.Double value,
                                        int scale)

toBase64

public static java.lang.String toBase64(long num)
Converts the given long into a base 64 string.

Parameters:
num - the number to convert into base 64
Returns:
a String representation of num in base 64

toBase64

public static int toBase64(long num,
                           char[] chr,
                           int off)
Converts the given long into a base 64 string. The result is stored in the character array chr, starting with the character at off, counted from the end of the array.

Parameters:
num - the number to convert into base 64
chr - the character array to insert the base 64 representation of the number into
off - the offset into the character array to start writing
Returns:
the index of the character preceding the last character written into the buffer.

fromBase64

public static long fromBase64(java.lang.String s)
Convert a string representation of a base64 number into a long.

Parameters:
s - a String representation of a base 64 number
Returns:
long the number as a long; -1 in case the string s contains a character not present in the base64 character set

fromBase64

public static long fromBase64(java.lang.String s,
                              int offset)
Convert a string representation of a base64 number into a long.

Parameters:
s - a String containing a representation of a base 64 number
offset - the offset of the first character in the string to start begin parsing at.
Returns:
long the number as a long; -1 in case the string s contains a character not present in the base64 character set

fromBase64

public static long fromBase64(java.lang.String s,
                              int beginIndex,
                              int endIndex)
Convert a string representation of a base64 number into a long.

Parameters:
s - a String containing a representation of a base 64 number
beginIndex - the offset of the first character in the string to parse (inclusive).
endIndex - the offset of the last character in the string to parse (inclusive).
Returns:
long the number as a long; -1 in case the string s contains a character not present in the base64 character set

toBase36

public static java.lang.String toBase36(long num)
Converts the given long into a base 36 string.

Parameters:
num - the number to convert into base 36
Returns:
a String representation of num in base 36

toBase36

public static int toBase36(long num,
                           char[] chr,
                           int off)
Converts the given long into a base 36 string. The result is stored in the character array chr, starting with the character at off, counted from the end of the array.

Parameters:
num - the number to convert into base 36
chr - the character array to insert the base 36 representation of the number into
off - the offset into the character array to start writing
Returns:
the index of the character preceding the last character written into the buffer.

fromBase36

public static long fromBase36(java.lang.String s)
                       throws java.text.ParseException
Convert a string representation of a base36 number into a long.

Parameters:
s - a String representation of a base 36 number
Returns:
long the number as a long
Throws:
java.text.ParseException - if the String s could not be parsed

fromBase36

public static long fromBase36(java.lang.String s,
                              int offset)
Convert a string representation of a base36 number into a long. Parsing stops at the first character that can not be part of a base 36 number.

Parameters:
s - a String containing a representation of a base 36 number
offset - the offset of the first character in the string to start begin parsing at.
Returns:
long the number as a long

permutations

public static java.util.List permutations(int n,
                                          int r)
Returns a List of Lists, where each sub-List is a list of Integers representing one permutation of r numbers between 0 and n.

Parameters:
n - upper limit of the range within which the members of the permutation fall
r - count of integers in the permutation
Returns:
List of Lists, where each sub-List is a list of Integers representing one permutation

sgn

public static int sgn(int a,
                      int b)
Returns the value of the sgn mathematical function (the signum function) on the operands a and b.

The signum function returns -1 if a < b, 0 if a == b and +1 if a > b. It serves as a safe way to determine how two integers compare. Simply subtracting the integers and comparing the result with zero can fail for numbers close to Integer.MAX_VALUE and Integer.MIN_VALUE.

Returns:
the value of the sgn function on the supplied operands

sgn

public static int sgn(long a,
                      long b)
Returns the value of the sgn mathematical function (the signum function) on the operands a and b.

The signum function returns -1 if a < b, 0 if a == b and +1 if a > b. It serves as a safe way to determine how two longs compare. Simply subtracting the numbers and comparing the result with zero can fail for numbers close to Long.MAX_VALUE and Long.MIN_VALUE.

Returns:
the value of the sgn function on the supplied operands

bound

public static int bound(int value,
                        int lower,
                        int upper)
Bounds value to be within lower and upper, inclusively.

That is if value < lower then lower is returned and if value > upper then upper is returned, else value itself is returned.

Note that if upper is less than lower, this function returns as if upper is positive infinity.

Parameters:
value - the value to be bounded
lower - the lower bound
upper - the upper bound
Returns:
the bounded value

sunHashCode

public static final int sunHashCode(char[] val)
Private method that calculates the hash code for the char[] val according to the sun's unique implementation which they say will be standard in 1.2 (love those standards)


normalHashCode

public static final int normalHashCode(char[] val)
Private method that calculates the hash code for the char[] val according to the java spec.


unsignedUniqueId

public static final long unsignedUniqueId(java.lang.String s)
Parameters:
s -
Returns:
int
A convenience method to return an unsigned hash for a given String. If a hash code generated is negative, it is offset by adding 2 ^ 32


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.