public final class DataUnitParser extends Object
Parse a textual representation of a data unit and converts it into
a BigDecimal
representing the quantity for a given unit
(e.g., bytes).
If the string is made of digits only, it is assumed to be bytes and the value will remain the same.
The data unit can be written in its prefix form, or in full, whether using binary or decimal notations. (e.g., "kB", "kilobyte", "kilobytes").
Languages supported are English (default) and French (since 2.0.0). The following are acceptable symbols for each data units, for bytes and bits. The symbols are case-insensitive, and accent-insensitive.
French uses the same symbols. Accents aside, the names are also the same, except for replacing "byte" with "octet" (e.g., "gigabyte" becomes "gigaoctet").
French typically write the following prefixes with an "é": méga, mébi, téra, tébi péta, pébi. Both variations are supported (with or without accents).
Refer to DataUnit
for extra information of what they values
represent.
No distinction is made between plural and singular. Numeric values can be integers or decimals numbers (e.g., 2.5kB). A numeric value must be followed by a data unit. Other terms or characters are ignored.
All of the following will be parsed properly:
This class is thread-safe and immutable.
Modifier and Type | Method and Description |
---|---|
static BigDecimal |
parse(String text)
Parses a text representation of a data measurement and returns
the number of bytes it represents.
|
static BigDecimal |
parse(String text,
BigDecimal defaultValue)
Parses a text representation of a data measurement.
|
static BigDecimal |
parse(String text,
DataUnit targetUnit)
Parses a text representation of a data measurement.
|
static BigDecimal |
parse(String text,
DataUnit targetUnit,
BigDecimal defaultValue)
Parses a text representation of a data measurement.
|
public static BigDecimal parse(String text)
DataUnitParserException
is thrown. Default value is zero byte.text
- the data measurement text to parsepublic static BigDecimal parse(String text, BigDecimal defaultValue)
text
- the data measurement text to parsedefaultValue
- default valuepublic static BigDecimal parse(String text, DataUnit targetUnit)
DataUnitParserException
is thrown. Default value is zero byte.text
- the data measurement text to parsetargetUnit
- desired target unit for the returned amountpublic static BigDecimal parse(String text, DataUnit targetUnit, BigDecimal defaultValue)
text
- the data measurement text to parsetargetUnit
- desired target unit for the returned amountdefaultValue
- default valueCopyright © 2008–2023 Norconex Inc.. All rights reserved.