public final class NumberUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
NumberUtils.BitOrder
Denotes in which order bits are to be interpreted in a byte.
|
static class |
NumberUtils.UnitDefinition
Denotes how "k", "M" units should be interpreted.
|
Modifier and Type | Method and Description |
---|---|
static int |
convertBitOrder(int aValue,
int aBitCount,
NumberUtils.BitOrder aBitOrder)
Converts the given value into a desired bit order.
|
static int |
convertByteOrder(int aValue,
int aByteCount,
ByteOrder aByteOrder)
Converts the given value into a desired byte order.
|
static <T extends Number> |
createNumberComparator(boolean aSortAscending)
Creates a number comparator that can be used to sort a list of numbers in
ascending or descending order.
|
static int |
getBitIndex(int aMaskValue)
Returns the largest bit-value that is set to '1' of a given mask value.
|
static int |
getBitMask(int aBitCount)
Returns the maximum value for the given bit count, e.g.,
( 1 << aBitCount ) - 1.
|
static int |
getPercentage(int aValue,
int aRange)
Calculates the percentage for the given value in the given range.
|
static int |
getPercentage(int aValue,
int aLowerBound,
int aUpperBound)
Calculates the percentage for the given value in the range denoted by the
given lower and upper bounds.
|
static int |
getPercentage(long aValue,
long aRange)
Calculates the percentage for the given value in the given range.
|
static int |
getPercentage(long aValue,
long aLowerBound,
long aUpperBound)
Calculates the percentage for the given value in the range denoted by the
given lower and upper bounds.
|
static boolean |
isPowerOfTwo(int aValue)
Returns whether the given value is a power of two.
|
static int |
packBytes(int aValue)
"Packs" a given value according to the given byte mask by removing all zero
bytes from the given integer value and shifting all non-zero bytes to
consecutive places.
|
static int |
reverseBits(int aValue,
int aBitCount)
Converts the bits of a byte into a desired order.
|
static int |
safeParseInt(String aText)
Parses the given text as an integer, avoiding runtime exceptions.
|
static int |
safeParseInt(String aText,
int aDefault)
Parses the given text as an integer, avoiding runtime exceptions.
|
static long |
safeParseLong(String aText)
Parses the given text as an integer, avoiding runtime exceptions.
|
static long |
safeParseLong(String aText,
long aDefault)
Parses the given text as an integer, avoiding runtime exceptions.
|
static int |
smartParseInt(String aText)
Provides a "smart" integer parsing routine that allows (decimal) numbers in
string form with all kind of trailing characters to be parsed into an
integer.
|
static int |
smartParseInt(String aText,
int aDefault)
Provides a "smart" integer parsing routine that allows (decimal) numbers in
string form with all kind of trailing characters to be parsed into an
integer.
|
static int |
smartParseInt(String aText,
NumberUtils.UnitDefinition aUnitDefinition)
Provides a "smart" integer parsing routine that allows (decimal) numbers in
string form with all kind of trailing characters to be parsed into an
integer.
|
static int |
smartParseInt(String aText,
NumberUtils.UnitDefinition aUnitDefinition,
int aDefault)
Provides a "smart" integer parsing routine that allows (decimal) numbers in
string form with all kind of trailing characters to be parsed into an
integer.
|
public static int convertBitOrder(int aValue, int aBitCount, NumberUtils.BitOrder aBitOrder)
aValue
- the value to convert;aBitCount
- the number of bits that are supposed to be in the given value;aBitOrder
- the desired bit order.public static int convertByteOrder(int aValue, int aByteCount, ByteOrder aByteOrder)
aValue
- the value to convert;aByteCount
- the number of bytes that are supposed to be in the given value;aByteOrder
- the desired byte order.public static <T extends Number> Comparator<T> createNumberComparator(boolean aSortAscending)
T
- the exact numeric type to sort on;aSortAscending
- true
if the numbers should be sorted ascendingly,
false
if the numbers should be sorted in descending
order.null
.public static int getBitIndex(int aMaskValue)
E.g., for a mask value of 128, the result will be 7, while for a mask value of 3, the value will be 1.
aMaskValue
- the mask value to return the bit-index of.public static int getBitMask(int aBitCount)
aBitCount
- the number of bits to create a bit mask for, > 0.public static int getPercentage(int aValue, int aRange)
aValue
- the value;aRange
- the range (zero-based).public static int getPercentage(int aValue, int aLowerBound, int aUpperBound)
aValue
- the value;aLowerBound
- the lower bound of the range;aUpperBound
- the upper bound of the range.public static int getPercentage(long aValue, long aRange)
aValue
- the value;aRange
- the range (zero-based).public static int getPercentage(long aValue, long aLowerBound, long aUpperBound)
aValue
- the value;aLowerBound
- the lower bound of the range;aUpperBound
- the upper bound of the range.public static boolean isPowerOfTwo(int aValue)
aValue
- the value to test as power of two.true
if the given value is a power of two,
false
otherwise.public static int packBytes(int aValue)
For example: given aValue == 0x12003400, the result would be 0x00001234.
aValue
- the 4-byte (32-bit) value to pack.public static int reverseBits(int aValue, int aBitCount)
aValue
- the byte value to convert.public static int safeParseInt(String aText)
aText
- the text to parse as an integer, can be null
or
empty.safeParseInt(String, int)
public static int safeParseInt(String aText, int aDefault)
aText
- the text to parse as an integer, can be null
or
empty;aDefault
- the default value to return in case parsing failed.Integer.parseInt(String)
public static long safeParseLong(String aText)
aText
- the text to parse as an integer, can be null
or
empty.safeParseLong(String, long)
public static long safeParseLong(String aText, long aDefault)
aText
- the text to parse as an integer, can be null
or
empty;aDefault
- the default value to return in case parsing failed.Long.parseLong(String)
public static int smartParseInt(String aText)
Characters recognized are: "k" and "M" to denote units of 1024, 1024*1024.
aText
- the text to parse into an integer value, cannot be
null
.public static int smartParseInt(String aText, int aDefault)
Characters recognized are: "k" and "M" to denote units of 1024, 1024*1024.
aText
- the text to parse into an integer value, cannot be
null
;aDefault
- the default value to return in case the given text couldn't be
parsed into a valid number.public static int smartParseInt(String aText, NumberUtils.UnitDefinition aUnitDefinition)
aText
- the text to parse into an integer value, cannot be
null
;aUnitDefinition
- the unit definition for "k" and "M" characters, should be either
SI (units of 1000) or BINARY (units of 1024).public static int smartParseInt(String aText, NumberUtils.UnitDefinition aUnitDefinition, int aDefault)
aText
- the text to parse into an integer value, cannot be
null
;aUnitDefinition
- the unit definition for "k" and "M" characters, should be either
SI (units of 1000) or BINARY (units of 1024);aDefault
- the default value to return in case the given text couldn't be
parsed into a valid number.Copyright © 2015 L'Xtreme IT consultancy. All rights reserved.