nl.lxtreme.ols.util
Class NumberUtils

java.lang.Object
  extended by nl.lxtreme.ols.util.NumberUtils

public final class NumberUtils
extends Object

Provides some utility methods for parsing numbers, etc.


Nested Class Summary
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.
 
Method Summary
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>
Comparator<T>
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

convertBitOrder

public static int convertBitOrder(int aValue,
                                  int aBitCount,
                                  NumberUtils.BitOrder aBitOrder)
Converts the given value into a desired bit order.

Parameters:
aValue - the value to convert;
aBitCount - the number of bits that are supposed to be in the given value;
aBitOrder - the desired bit order.
Returns:
the converted value.

convertByteOrder

public static int convertByteOrder(int aValue,
                                   int aByteCount,
                                   ByteOrder aByteOrder)
Converts the given value into a desired byte order.

Parameters:
aValue - the value to convert;
aByteCount - the number of bytes that are supposed to be in the given value;
aByteOrder - the desired byte order.
Returns:
the converted value.

createNumberComparator

public static <T extends Number> Comparator<T> createNumberComparator(boolean aSortAscending)
Creates a number comparator that can be used to sort a list of numbers in ascending or descending order.

Type Parameters:
T - the exact numeric type to sort on;
Parameters:
aSortAscending - true if the numbers should be sorted ascendingly, false if the numbers should be sorted in descending order.
Returns:
a comparator instance, never null.

getBitIndex

public static int getBitIndex(int aMaskValue)
Returns the largest bit-value that is set to '1' of a given mask value.

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.

Parameters:
aMaskValue - the mask value to return the bit-index of.
Returns:
the largest bit-value that is set to '1' of a given mask value, zero-based.

getBitMask

public static int getBitMask(int aBitCount)
Returns the maximum value for the given bit count, e.g., ( 1 << aBitCount ) - 1.

Parameters:
aBitCount - the number of bits to create a bit mask for, > 0.
Returns:
the bit mask.

getPercentage

public static int getPercentage(int aValue,
                                int aRange)
Calculates the percentage for the given value in the given range.

Parameters:
aValue - the value;
aRange - the range (zero-based).
Returns:
the percentage (= value * 100.0 / aRange).

getPercentage

public 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.

Parameters:
aValue - the value;
aLowerBound - the lower bound of the range;
aUpperBound - the upper bound of the range.
Returns:
the percentage (= value * 100.0 / range).

getPercentage

public static int getPercentage(long aValue,
                                long aRange)
Calculates the percentage for the given value in the given range.

Parameters:
aValue - the value;
aRange - the range (zero-based).
Returns:
the percentage (= value * 100.0 / aRange).

getPercentage

public 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.

Parameters:
aValue - the value;
aLowerBound - the lower bound of the range;
aUpperBound - the upper bound of the range.
Returns:
the percentage (= value * 100.0 / range).

isPowerOfTwo

public static boolean isPowerOfTwo(int aValue)
Returns whether the given value is a power of two.

Parameters:
aValue - the value to test as power of two.
Returns:
true if the given value is a power of two, false otherwise.

packBytes

public 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.

For example: given aValue == 0x12003400, the result would be 0x00001234.

Parameters:
aValue - the 4-byte (32-bit) value to pack.
Returns:
the packed representation of the given value.

reverseBits

public static int reverseBits(int aValue,
                              int aBitCount)
Converts the bits of a byte into a desired order.

Parameters:
aValue - the byte value to convert.
Returns:
the converted value, always most significant byte first (assuming you are reading from left to right).

safeParseInt

public static int safeParseInt(String aText)
Parses the given text as an integer, avoiding runtime exceptions.

Parameters:
aText - the text to parse as an integer, can be null or empty.
Returns:
the numeric representation of the given text, or -1 if the text could not be parsed correctly as integer.
See Also:
safeParseInt(String, int)

safeParseInt

public static int safeParseInt(String aText,
                               int aDefault)
Parses the given text as an integer, avoiding runtime exceptions.

Parameters:
aText - the text to parse as an integer, can be null or empty;
aDefault - the default value to return in case parsing failed.
Returns:
the numeric representation of the given text, or the given default if the text could not be parsed correctly as integer.
See Also:
Integer.parseInt(String)

safeParseLong

public static long safeParseLong(String aText)
Parses the given text as an integer, avoiding runtime exceptions.

Parameters:
aText - the text to parse as an integer, can be null or empty.
Returns:
the numeric representation of the given text, or -1 if the text could not be parsed correctly as integer.
See Also:
safeParseLong(String, long)

safeParseLong

public static long safeParseLong(String aText,
                                 long aDefault)
Parses the given text as an integer, avoiding runtime exceptions.

Parameters:
aText - the text to parse as an integer, can be null or empty;
aDefault - the default value to return in case parsing failed.
Returns:
the numeric representation of the given text, or the given default if the text could not be parsed correctly as integer.
See Also:
Long.parseLong(String)

smartParseInt

public 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. Some trailing characters are understood as being part of the number, like "k" to denote a value in thousands, or "m" to denote a value in millions.

Characters recognized are: "k" and "M" to denote units of 1024, 1024*1024.

Parameters:
aText - the text to parse into an integer value, cannot be null.
Returns:
the integer value part of the given text, or 0 if the text couldn't be parsed.

smartParseInt

public 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. Some trailing characters are understood as being part of the number, like "k" to denote a value in thousands, or "m" to denote a value in millions.

Characters recognized are: "k" and "M" to denote units of 1024, 1024*1024.

Parameters:
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.
Returns:
the integer value part of the given text, or the given default value if the text couldn't be parsed.

smartParseInt

public 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. Some trailing characters are understood as being part of the number, like "k" to denote a value in thousands, or "m" to denote a value in millions.

Parameters:
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).
Returns:
the integer value part of the given text, or 0 if the text couldn't be parsed.

smartParseInt

public 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. Some trailing characters are understood as being part of the number, like "k" to denote a value in thousands, or "m" to denote a value in millions.

Parameters:
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.
Returns:
the integer value part of the given text, or the given default value if the text couldn't be parsed.


Copyright © 2012 L'Xtreme IT consultancy. All Rights Reserved.