Package org.refcodes.numerical
Class NumericalUtility
- java.lang.Object
-
- org.refcodes.numerical.NumericalUtility
-
public final class NumericalUtility extends java.lang.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 boolean
toBoolean(java.lang.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(java.lang.Double aDouble)
Converts a double value to an array of bytes, left(?)static double
toDouble(java.lang.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(java.lang.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(java.lang.Object... aObjects)
Calculates a hash code from the given object's hash codes.static int
toHashCode(java.lang.String aValue)
Calculates a Java-Version's implementation independent Hash-Code.static int[]
toHashCodes(java.lang.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 long
toLong(byte[] aBytes)
To long.static long
toLong(byte[] aBytes, int aOffset)
To long.static long
toLong(java.lang.String aString)
Creates a long from aString
; useful e.g. when creating a number from a pass-phrase.static java.lang.String
toMd5Hash(java.lang.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 java.lang.IllegalArgumentException
To long.- Parameters:
aBytes
- the bytes- Returns:
- the long
- Throws:
java.lang.IllegalArgumentException
- the illegal argument exception
-
toLong
public static long toLong(byte[] aBytes, int aOffset) throws java.lang.IllegalArgumentException
To long.- Parameters:
aBytes
- the bytesaOffset
- the offset- Returns:
- the long
- Throws:
java.lang.IllegalArgumentException
- the illegal argument exception
-
toInteger
public static int toInteger(byte[] aBytes) throws java.lang.IllegalArgumentException
To integer.- Parameters:
aBytes
- the bytes- Returns:
- the int
- Throws:
java.lang.IllegalArgumentException
- the illegal argument exception
-
toInteger
public static int toInteger(byte[] aBytes, int aOffset) throws java.lang.IllegalArgumentException
To integer.- Parameters:
aBytes
- the bytesaOffset
- the offset- Returns:
- the int
- Throws:
java.lang.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(java.lang.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(java.lang.String aString)
Creates a long from aString
; useful e.g. when creating a number from a pass-phrase.- Parameters:
aString
- TheString
to be converted to a long value.- Returns:
- The long value from the
String
-
toDouble
public static double toDouble(java.lang.String aString)
Creates a double between 0 and 1 from aString
; useful e.g. when creating a number from a pass-phrase.- Parameters:
aString
- TheString
to be converted to a double value.- Returns:
- The double value between 0 and 1 from the
String
.
-
toDoubles
public static double[] toDoubles(java.lang.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.- Parameters:
aString
- TheString
to be converted to a double value.aCount
- The number of doubles to create.- Returns:
- The array with double values between 0 and 1 from the
String
.
-
toHashCode
public static int toHashCode(java.lang.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.
-
toBoolean
public static boolean toBoolean(java.lang.String aValue) throws java.lang.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:
java.lang.IllegalArgumentException
- in case neither true nor false could be determined.
-
toMd5Hash
public static java.lang.String toMd5Hash(java.lang.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(java.lang.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(java.lang.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.
-
-