Class NumberFactory

java.lang.Object
org.apache.commons.rng.core.util.NumberFactory

public final class NumberFactory extends Object
Utility for creating number types from one or two int values or one long value, or a sequence of bytes.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final double
    The multiplier to convert the least significant 53-bits of a long to a double.
    private static final float
    The multiplier to convert the least significant 24-bits of an int to a float.
    private static final int
    Lowest byte mask.
    private static final int
    Number of bytes in a int.
    private static final long
    Lowest byte mask.
    private static final int
    Number of bytes in a long.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Class contains only static methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static void
    checkSize(int expected, int actual)
     
    static int
    extractHi(long v)
    Creates an int from a long, using the high order bits.
    static int
    extractLo(long v)
    Creates an int from a long, using the low order bits.
    private static int
    getInt(byte[] input, int index)
    Gets the int from the buffer starting at the given position.
    private static long
    getLong(byte[] input, int index)
    Gets the long from the buffer starting at the given position.
    static boolean
    makeBoolean(int v)
    Deprecated.
    Since version 1.2.
    static boolean
    makeBoolean(long v)
    Deprecated.
    Since version 1.2.
    static byte[]
    Splits an int into 4 bytes.
    static byte[]
    makeByteArray(int[] input)
    Splits an array of int values into a sequence of bytes.
    static byte[]
    makeByteArray(long v)
    Splits a long into 8 bytes.
    static byte[]
    makeByteArray(long[] input)
    Splits an array of long values into a sequence of bytes.
    static double
    makeDouble(int v, int w)
    Creates a double from two int values.
    static double
    makeDouble(long v)
    Creates a double from a long value.
    static float
    makeFloat(int v)
    Creates a float from an int value.
    static int
    makeInt(byte[] input)
    Creates an int from 4 bytes.
    static int
    makeInt(long v)
    Deprecated.
    Since version 1.2.
    static int[]
    makeIntArray(byte[] input)
    Creates an array of int values from a sequence of bytes.
    static long
    makeLong(byte[] input)
    Creates a long from 8 bytes.
    static long
    makeLong(int v, int w)
    Creates a long from two int values.
    static long[]
    makeLongArray(byte[] input)
    Creates an array of long values from a sequence of bytes.
    private static void
    putInt(int v, byte[] buffer, int index)
    Puts the int into the buffer starting at the given position.
    private static void
    putLong(long v, byte[] buffer, int index)
    Puts the long into the buffer starting at the given position.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • FLOAT_MULTIPLIER

      private static final float FLOAT_MULTIPLIER
      The multiplier to convert the least significant 24-bits of an int to a float. See makeFloat(int).

      This is equivalent to 1.0f / (1 invalid input: '<'invalid input: '<' 24).

      See Also:
    • DOUBLE_MULTIPLIER

      private static final double DOUBLE_MULTIPLIER
      The multiplier to convert the least significant 53-bits of a long to a double. See makeDouble(long) and makeDouble(int, int).

      This is equivalent to 1.0 / (1L invalid input: '<'invalid input: '<' 53).

      See Also:
    • LONG_LOWEST_BYTE_MASK

      private static final long LONG_LOWEST_BYTE_MASK
      Lowest byte mask.
      See Also:
    • LONG_SIZE

      private static final int LONG_SIZE
      Number of bytes in a long.
      See Also:
    • INT_LOWEST_BYTE_MASK

      private static final int INT_LOWEST_BYTE_MASK
      Lowest byte mask.
      See Also:
    • INT_SIZE

      private static final int INT_SIZE
      Number of bytes in a int.
      See Also:
  • Constructor Details

    • NumberFactory

      private NumberFactory()
      Class contains only static methods.
  • Method Details

    • makeBoolean

      @Deprecated public static boolean makeBoolean(int v)
      Deprecated.
      Since version 1.2. Method has become obsolete following RNG-57.
      Creates a boolean from an int value.
      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 a boolean from a long value.
      Parameters:
      v - Number.
      Returns:
      a boolean.
    • makeDouble

      public static double makeDouble(long v)
      Creates a double from a long value.
      Parameters:
      v - Number.
      Returns:
      a double value in the interval [0, 1].
    • makeDouble

      public static double makeDouble(int v, int w)
      Creates a double from two int values.
      Parameters:
      v - Number (high order bits).
      w - Number (low order bits).
      Returns:
      a double value in the interval [0, 1].
    • makeFloat

      public static float makeFloat(int v)
      Creates a float from an int value.
      Parameters:
      v - Number.
      Returns:
      a float value in the interval [0, 1].
    • makeLong

      public static long makeLong(int v, int w)
      Creates a long from two int values.
      Parameters:
      v - Number (high order bits).
      w - Number (low order bits).
      Returns:
      a long value.
    • makeInt

      @Deprecated public static int makeInt(long v)
      Deprecated.
      Since version 1.2. Method has become obsolete following RNG-57.
      Creates an int from a long.
      Parameters:
      v - Number.
      Returns:
      an int value made from the "xor" of the high order bits and low order bits of v.
    • extractHi

      public static int extractHi(long v)
      Creates an int from a long, using the high order bits.

      The returned value is such that if

      
        vL = extractLo(v);
        vH = extractHi(v);
       

      then v is equal to makeLong(vH, vL).

      Parameters:
      v - Number.
      Returns:
      an int value made from the most significant bits of v.
    • extractLo

      public static int extractLo(long v)
      Creates an int from a long, using the low order bits.

      The returned value is such that if

      
        vL = extractLo(v);
        vH = extractHi(v);
       

      then v is equal to makeLong(vH, vL).

      Parameters:
      v - Number.
      Returns:
      an int value made from the least significant bits of v.
    • makeByteArray

      public static byte[] makeByteArray(long v)
      Splits a long into 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 the long into 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 a long from 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:
      IllegalArgumentException - if input.length != 8.
    • getLong

      private static long getLong(byte[] input, int index)
      Gets the long from 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 of long values into a sequence of bytes. This method calls makeByteArray(long) for each element of the input.
      Parameters:
      input - Input.
      Returns:
      an array of bytes.
    • makeLongArray

      public static long[] makeLongArray(byte[] input)
      Creates an array of long values from a sequence of bytes. This method calls makeLong(byte[]) for each subsequence of 8 bytes.
      Parameters:
      input - Input.
      Returns:
      an array of long.
      Throws:
      IllegalArgumentException - if input.length is not a multiple of 8.
    • makeByteArray

      public static byte[] makeByteArray(int v)
      Splits an int into 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 the int into 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 an int from 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:
      IllegalArgumentException - if input.length != 4.
    • getInt

      private static int getInt(byte[] input, int index)
      Gets the int from 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 of int values into a sequence of bytes. This method calls makeByteArray(int) for each element of the input.
      Parameters:
      input - Input.
      Returns:
      an array of bytes.
    • makeIntArray

      public static int[] makeIntArray(byte[] input)
      Creates an array of int values from a sequence of bytes. This method calls makeInt(byte[]) for each subsequence of 4 bytes.
      Parameters:
      input - Input. Length must be a multiple of 4.
      Returns:
      an array of int.
      Throws:
      IllegalArgumentException - if input.length is not a multiple of 4.
    • checkSize

      private static void checkSize(int expected, int actual)
      Parameters:
      expected - Expected value.
      actual - Actual value.
      Throws:
      IllegalArgumentException - if expected != actual.