Package org.apache.commons.rng.core.util
Class NumberFactory
- java.lang.Object
-
- org.apache.commons.rng.core.util.NumberFactory
-
public final class NumberFactory extends java.lang.ObjectUtility for creating number types from one or twointvalues or onelongvalue, or a sequence of bytes.
-
-
Field Summary
Fields Modifier and Type Field Description private static doubleDOUBLE_MULTIPLIERThe multiplier to convert the least significant 53-bits of alongto adouble.private static floatFLOAT_MULTIPLIERThe multiplier to convert the least significant 24-bits of anintto afloat.private static intINT_LOWEST_BYTE_MASKLowest byte mask.private static intINT_SIZENumber of bytes in aint.private static longLONG_LOWEST_BYTE_MASKLowest byte mask.private static intLONG_SIZENumber of bytes in along.
-
Constructor Summary
Constructors Modifier Constructor Description privateNumberFactory()Class contains only static methods.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static voidcheckSize(int expected, int actual)static intextractHi(long v)Creates anintfrom along, using the high order bits.static intextractLo(long v)Creates anintfrom along, using the low order bits.private static intgetInt(byte[] input, int index)Gets theintfrom the buffer starting at the given position.private static longgetLong(byte[] input, int index)Gets thelongfrom the buffer starting at the given position.static booleanmakeBoolean(int v)Deprecated.Since version 1.2.static booleanmakeBoolean(long v)Deprecated.Since version 1.2.static byte[]makeByteArray(int v)Splits anintinto 4 bytes.static byte[]makeByteArray(int[] input)Splits an array ofintvalues into a sequence of bytes.static byte[]makeByteArray(long v)Splits alonginto 8 bytes.static byte[]makeByteArray(long[] input)Splits an array oflongvalues into a sequence of bytes.static doublemakeDouble(int v, int w)Creates adoublefrom twointvalues.static doublemakeDouble(long v)Creates adoublefrom alongvalue.static floatmakeFloat(int v)Creates afloatfrom anintvalue.static intmakeInt(byte[] input)Creates anintfrom 4 bytes.static intmakeInt(long v)Deprecated.Since version 1.2.static int[]makeIntArray(byte[] input)Creates an array ofintvalues from a sequence of bytes.static longmakeLong(byte[] input)Creates alongfrom 8 bytes.static longmakeLong(int v, int w)Creates alongfrom twointvalues.static long[]makeLongArray(byte[] input)Creates an array oflongvalues from a sequence of bytes.private static voidputInt(int v, byte[] buffer, int index)Puts theintinto the buffer starting at the given position.private static voidputLong(long v, byte[] buffer, int index)Puts thelonginto the buffer starting at the given position.
-
-
-
Field Detail
-
FLOAT_MULTIPLIER
private static final float FLOAT_MULTIPLIER
The multiplier to convert the least significant 24-bits of anintto afloat. SeemakeFloat(int).This is equivalent to
1.0f / (1 << 24).- See Also:
- Constant Field Values
-
DOUBLE_MULTIPLIER
private static final double DOUBLE_MULTIPLIER
The multiplier to convert the least significant 53-bits of alongto adouble. SeemakeDouble(long)andmakeDouble(int, int).This is equivalent to
1.0 / (1L << 53).- See Also:
- Constant Field Values
-
LONG_LOWEST_BYTE_MASK
private static final long LONG_LOWEST_BYTE_MASK
Lowest byte mask.- See Also:
- Constant Field Values
-
LONG_SIZE
private static final int LONG_SIZE
Number of bytes in along.- See Also:
- Constant Field Values
-
INT_LOWEST_BYTE_MASK
private static final int INT_LOWEST_BYTE_MASK
Lowest byte mask.- See Also:
- Constant Field Values
-
INT_SIZE
private static final int INT_SIZE
Number of bytes in aint.- See Also:
- Constant Field Values
-
-
Method Detail
-
makeBoolean
@Deprecated public static boolean makeBoolean(int v)
Deprecated.Since version 1.2. Method has become obsolete following RNG-57.Creates abooleanfrom anintvalue.- Parameters:
v- Number.- Returns:
- a boolean.
-
makeBoolean
@Deprecated public static boolean makeBoolean(long v)
Deprecated.Since version 1.2. Method has become obsolete following RNG-57.Creates abooleanfrom alongvalue.- Parameters:
v- Number.- Returns:
- a boolean.
-
makeDouble
public static double makeDouble(long v)
Creates adoublefrom alongvalue.- Parameters:
v- Number.- Returns:
- a
doublevalue in the interval[0, 1].
-
makeDouble
public static double makeDouble(int v, int w)Creates adoublefrom twointvalues.- Parameters:
v- Number (high order bits).w- Number (low order bits).- Returns:
- a
doublevalue in the interval[0, 1].
-
makeFloat
public static float makeFloat(int v)
Creates afloatfrom anintvalue.- Parameters:
v- Number.- Returns:
- a
floatvalue in the interval[0, 1].
-
makeLong
public static long makeLong(int v, int w)Creates alongfrom twointvalues.- Parameters:
v- Number (high order bits).w- Number (low order bits).- Returns:
- a
longvalue.
-
makeInt
@Deprecated public static int makeInt(long v)
Deprecated.Since version 1.2. Method has become obsolete following RNG-57.Creates anintfrom along.- Parameters:
v- Number.- Returns:
- an
intvalue made from the "xor" of thehigh order bitsandlow order bitsofv.
-
extractHi
public static int extractHi(long v)
Creates anintfrom along, using the high order bits.The returned value is such that if
vL = extractLo(v); vH = extractHi(v);then
vis equal tomakeLong(vH, vL).- Parameters:
v- Number.- Returns:
- an
intvalue made from the most significant bits ofv.
-
extractLo
public static int extractLo(long v)
Creates anintfrom along, using the low order bits.The returned value is such that if
vL = extractLo(v); vH = extractHi(v);then
vis equal tomakeLong(vH, vL).- Parameters:
v- Number.- Returns:
- an
intvalue made from the least significant bits ofv.
-
makeByteArray
public static byte[] makeByteArray(long v)
Splits alonginto 8 bytes.- Parameters:
v- Value.- Returns:
- the bytes that compose the given value (least-significant byte first).
-
putLong
private static void putLong(long v, byte[] buffer, int index)Puts thelonginto the buffer starting at the given position. Adds 8 bytes (least-significant first).- Parameters:
v- Value.buffer- the buffer.index- the index.
-
makeLong
public static long makeLong(byte[] input)
Creates alongfrom 8 bytes.- Parameters:
input- Input.- Returns:
- the value that correspond to the given bytes assuming that the order is in increasing byte significance (i.e. the first byte in the array is the least-significant).
- Throws:
java.lang.IllegalArgumentException- ifinput.length != 8.
-
getLong
private static long getLong(byte[] input, int index)Gets thelongfrom the buffer starting at the given position. Uses 8 bytes (least-significant first).- Parameters:
input- the input bytes.index- the index.- Returns:
- the value that correspond to the given bytes assuming that the order is in increasing byte significance (i.e. the first byte in the array is the least-significant).
-
makeByteArray
public static byte[] makeByteArray(long[] input)
Splits an array oflongvalues into a sequence of bytes. This method callsmakeByteArray(long)for each element of theinput.- Parameters:
input- Input.- Returns:
- an array of bytes.
-
makeLongArray
public static long[] makeLongArray(byte[] input)
Creates an array oflongvalues from a sequence of bytes. This method callsmakeLong(byte[])for each subsequence of 8 bytes.- Parameters:
input- Input.- Returns:
- an array of
long. - Throws:
java.lang.IllegalArgumentException- ifinput.lengthis not a multiple of 8.
-
makeByteArray
public static byte[] makeByteArray(int v)
Splits anintinto 4 bytes.- Parameters:
v- Value.- Returns:
- the bytes that compose the given value (least-significant byte first).
-
putInt
private static void putInt(int v, byte[] buffer, int index)Puts theintinto the buffer starting at the given position. Adds 4 bytes (least-significant first).- Parameters:
v- the value.buffer- the buffer.index- the index.
-
makeInt
public static int makeInt(byte[] input)
Creates anintfrom 4 bytes.- Parameters:
input- Input.- Returns:
- the value that correspond to the given bytes assuming that the order is in increasing byte significance (i.e. the first byte in the array is the least-significant).
- Throws:
java.lang.IllegalArgumentException- ifinput.length != 4.
-
getInt
private static int getInt(byte[] input, int index)Gets theintfrom the buffer starting at the given position. Uses 4 bytes (least-significant first).- Parameters:
input- the input bytes.index- the index.- Returns:
- the value that correspond to the given bytes assuming that the order is in increasing byte significance (i.e. the first byte in the array is the least-significant).
-
makeByteArray
public static byte[] makeByteArray(int[] input)
Splits an array ofintvalues into a sequence of bytes. This method callsmakeByteArray(int)for each element of theinput.- Parameters:
input- Input.- Returns:
- an array of bytes.
-
makeIntArray
public static int[] makeIntArray(byte[] input)
Creates an array ofintvalues from a sequence of bytes. This method callsmakeInt(byte[])for each subsequence of 4 bytes.- Parameters:
input- Input. Length must be a multiple of 4.- Returns:
- an array of
int. - Throws:
java.lang.IllegalArgumentException- ifinput.lengthis not a multiple of 4.
-
checkSize
private static void checkSize(int expected, int actual)- Parameters:
expected- Expected value.actual- Actual value.- Throws:
java.lang.IllegalArgumentException- ifexpected != actual.
-
-