Package org.refcodes.numerical
Class NumericalUtility
- java.lang.Object
-
- org.refcodes.numerical.NumericalUtility
-
public final class NumericalUtility extends Object
This class contains some useful static methods for working with bitwise operations objects.CATION: This class may not have an optimized runtime behavior!
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
sum(int... aSummands)
Calculates the sum from the provided summands.static byte[]
toBigEndianBytes(long aValue, int aLength)
Converts the given value to a byte array in big endian notation.static boolean
toBoolean(String aValue)
Converts the given String to a boolean value.static byte[]
toBytes(int aInteger)
Converts a integer value to an array of bytes, left byte being the most significant one (highest order).static byte[]
toBytes(long aLong)
Converts a long value to an array of bytes, left byte being the most significant one (highest order).static byte[]
toBytes(Double aDouble)
Converts a double value to an array of bytes, left(?)static double
toDouble(String aString)
Creates a double between 0 and 1 from aString
; useful e.g. when creating a number from a pass-phrase.static double[]
toDoubles(String aString, int aCount)
Creates an array with double values between 0 and 1 from aString
; useful e.g. when creating a bunch of numbers from a pass-phrase.static int
toHashCode(Object... aObjects)
Calculates a hash code from the given object's hash codes.static int
toHashCode(String aValue)
Calculates a Java-Version's implementation independent Hash-Code.static int[]
toHashCodes(String aSource, int aCount)
Creates a given number of IDs from the provided source text.static int
toInteger(byte[] aBytes)
To integer.static int
toInteger(byte[] aBytes, int aOffset)
To integer.static byte[]
toLittleEndianBytes(long aValue, int aLength)
Converts the given value to a byte array in little endian notation.static long
toLong(byte[] aBytes)
To long.static long
toLong(byte[] aBytes, int aOffset)
To long.static long
toLong(String aString)
Creates a long from aString
; useful e.g. when creating a number from a pass-phrase.static String
toMd5Hash(String aText)
Generates a md5 hash of the given text.static double
toScaled(double aValue, double aMin, double aMax)
Scales the given value (which must be between 0 .. 1 to be between aMin .. aMax.static short[]
toShorts(byte[] aBytes)
Converts a byte array to a short array.static long
toUnsignedLong(int aSignedIntValue)
Converts a signed int to an unsigned long value.
-
-
-
Method Detail
-
toBytes
public static byte[] toBytes(long aLong)
Converts a long value to an array of bytes, left byte being the most significant one (highest order).- Parameters:
aLong
- The long value to be converted- Returns:
- The array of bytes representing the long value.
-
toBytes
public static byte[] toBytes(int aInteger)
Converts a integer value to an array of bytes, left byte being the most significant one (highest order).- Parameters:
aInteger
- The integer value to be converted- Returns:
- The array of bytes representing the long value.
-
toLong
public static long toLong(byte[] aBytes) throws IllegalArgumentException
To long.- Parameters:
aBytes
- the bytes- Returns:
- the long
- Throws:
IllegalArgumentException
- the illegal argument exception
-
toLong
public static long toLong(byte[] aBytes, int aOffset) throws IllegalArgumentException
To long.- Parameters:
aBytes
- the bytesaOffset
- the offset- Returns:
- the long
- Throws:
IllegalArgumentException
- the illegal argument exception
-
toInteger
public static int toInteger(byte[] aBytes) throws IllegalArgumentException
To integer.- Parameters:
aBytes
- the bytes- Returns:
- the int
- Throws:
IllegalArgumentException
- the illegal argument exception
-
toInteger
public static int toInteger(byte[] aBytes, int aOffset) throws IllegalArgumentException
To integer.- Parameters:
aBytes
- the bytesaOffset
- the offset- Returns:
- the int
- Throws:
IllegalArgumentException
- the illegal argument exception
-
toShorts
public static short[] toShorts(byte[] aBytes)
Converts a byte array to a short array.- Parameters:
aBytes
- The bytes to be converted to short.- Returns:
- The according short array.
-
toBytes
public static byte[] toBytes(Double aDouble)
Converts a double value to an array of bytes, left(?) byte being the most significant one (highest order).- Parameters:
aDouble
- The double value to be converted- Returns:
- The array of bytes representing the double value.
-
toLong
public static long toLong(String aString)
Creates a long from aString
; useful e.g. when creating a number from a pass-phrase.
-
toDouble
public static double toDouble(String aString)
Creates a double between 0 and 1 from aString
; useful e.g. when creating a number from a pass-phrase.
-
toDoubles
public static double[] toDoubles(String aString, int aCount)
Creates an array with double values between 0 and 1 from aString
; useful e.g. when creating a bunch of numbers from a pass-phrase.
-
toHashCode
public static int toHashCode(Object... aObjects)
Calculates a hash code from the given object's hash codes.- Parameters:
aObjects
- The objects for which to calculate the has codes.- Returns:
- The hash codes for the given objects.
-
toUnsignedLong
public static long toUnsignedLong(int aSignedIntValue)
Converts a signed int to an unsigned long value. This is not do a simple ABS function, here we take a look at the bits of the value and calculate which the unsigned (positive) value would be. The positive value is returned in a (signed) long value.- Parameters:
aSignedIntValue
- The signed integer value.- Returns:
- The unsigned long value.
-
toLittleEndianBytes
public static byte[] toLittleEndianBytes(long aValue, int aLength)
Converts the given value to a byte array in little endian notation.- Parameters:
aValue
- The value for which to get the byte array.aLength
- The number of bytes to use.- Returns:
- The according array
-
toBigEndianBytes
public static byte[] toBigEndianBytes(long aValue, int aLength)
Converts the given value to a byte array in big endian notation.- Parameters:
aValue
- The value for which to get the byte array.aLength
- The number of bytes to use.- Returns:
- The according array
-
toBoolean
public static boolean toBoolean(String aValue) throws IllegalArgumentException
Converts the given String to a boolean value. Accepted values for a "true" boolean are "1", "on", "yes", "true". Accepted values for a "false" boolean are "0", "off", "no", "false".- Parameters:
aValue
- The text value to be tested whether it represents true or false.- Returns:
- True or false depending on the value passed.n
- Throws:
IllegalArgumentException
- in case neither true nor false could be determined.
-
toMd5Hash
public static String toMd5Hash(String aText)
Generates a md5 hash of the given text.- Parameters:
aText
- The text to be md5 hashed.- Returns:
- The md5 hashed text.
-
toScaled
public static double toScaled(double aValue, double aMin, double aMax)
Scales the given value (which must be between 0 .. 1 to be between aMin .. aMax.- Parameters:
aValue
- The value between 0 .. 1 to be scaled.aMin
- The min value.aMax
- The max value.- Returns:
- A value between aMin ... aMax.
-
sum
public static int sum(int... aSummands)
Calculates the sum from the provided summands.- Parameters:
aSummands
- The summands to be added up.- Returns:
- The sum of the provided summands.
-
toHashCode
public static int toHashCode(String aValue)
Calculates a Java-Version's implementation independent Hash-Code.- Parameters:
aValue
- TheString
from which to get the Hash-Code.- Returns:
- The according Hash-Code.
-
toHashCodes
public static int[] toHashCodes(String aSource, int aCount)
Creates a given number of IDs from the provided source text. Useful when creating numbers from a pass-phrase.- Parameters:
aSource
- The source text from which to generate the IDs.aCount
- The number of IDs to generate.- Returns:
- The IDs being generated.
-
-