Package com.norconex.commons.lang.unit
Enum Class DataUnit
- All Implemented Interfaces:
Serializable,Comparable<DataUnit>,Constable
A DataUnit offers representation and conversion of various
quantity of bytes/bits. Release 2.0.0 introduces exa, zetta, and yotta
units.
Decimal vs binary notations
As of 2.0.0, this class aligns with the international standard system of units by treating decimal and binary notations distinctively. Each are represented as:
| Decimal | Binary | ||||
|---|---|---|---|---|---|
| k | kilo | 103 | Ki | kibi | 210 |
| M | mega | 106 | Mi | mebi | 220 |
| G | giga | 109 | Gi | gibi | 230 |
| T | tera | 1012 | Ti | tebi | 240 |
| P | peta | 1015 | Pi | pebi | 250 |
| E | exa | 1018 | Ei | exbi | 260 |
| Z | zetta | 1021 | Zi | zebi | 270 |
| Y | yotta | 1024 | Yi | yobi | 280 |
Bytes vs bits
As of 2.0.0, this class supports bits in addition to bytes. They are represented as:
| B | bytes (8-bit) |
|---|---|
| bit | bits |
Usage examples
// how many kibibyte in a gibiabyte amount (binary notation). long kibibyte = DataUnit.GIB.to(3, KIB).longValue(); // results = 3072 // how many megabyte in a kilobyte amount (decimal notation) float megabyte = DataUnit.KB.to(2500, MB).floatValue(); // results = 2.5
- Since:
- 1.4.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>> -
Enum Constant Summary
Enum Constants -
Field Summary
FieldsModifier and TypeFieldDescription -
Method Summary
Modifier and TypeMethodDescriptionbits()bytes()Converts an amount of a specific unit to this unit.static DataUnitGets the unit value representing the given text.from(BigDecimal sourceAmount, DataUnit sourceUnit) Converts an amount of a specific unit to this unit.fromBits(double amount) fromBits(BigDecimal amount) fromBytes(double amount) fromBytes(BigDecimal amount) intgetName()booleanisBinary()booleanbooleanbooleanto(BigDecimal sourceAmount, DataUnit targetUnit) toBits(double amount) toBits(BigDecimal amount) toBytes(double amount) toBytes(BigDecimal amount) toString()static DataUnitReturns the enum constant of this class with the specified name.static DataUnit[]values()Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
BIT
A single bit. -
B
A single byte (8 bits). -
KBIT
-
MBIT
-
GBIT
-
TBIT
-
PBIT
-
EBIT
-
ZBIT
-
YBIT
-
KB
-
MB
-
GB
-
TB
-
PB
-
EB
-
ZB
-
YB
-
KIBIT
-
MIBIT
-
GIBIT
-
TIBIT
-
PIBIT
-
EIBIT
-
ZIBIT
-
YIBIT
-
KIB
-
MIB
-
GIB
-
TIB
-
PIB
-
EIB
-
ZIB
-
YIB
-
-
Field Details
-
DECIMAL_BIT_UNITS
-
DECIMAL_BYTE_UNITS
-
BINARY_BIT_UNITS
-
BINARY_BYTE_UNITS
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum class has no constant with the specified nameNullPointerException- if the argument is null
-
getSymbol
-
getPrefix
-
getName
-
getGroupIndex
public int getGroupIndex() -
isBitUnit
public boolean isBitUnit() -
isByteUnit
public boolean isByteUnit() -
isBinary
public boolean isBinary() -
isDecimal
public boolean isDecimal() -
bits
-
bytes
-
toBits
-
toBits
-
toBytes
-
toBytes
-
fromBits
-
fromBits
-
fromBytes
-
fromBytes
-
to
-
to
-
from
Converts an amount of a specific unit to this unit.- Parameters:
sourceAmount- source amount to convertsourceUnit- source unit to convert- Returns:
- converted value
-
from
Converts an amount of a specific unit to this unit.- Parameters:
sourceAmount- source amount to convertsourceUnit- source unit to convert- Returns:
- converted value
-
from
Gets the unit value representing the given text. Both symbols or full names are supported (English or French, case-insensitive, ignoring accents).- Parameters:
dataUnit- the textual representation of the unit.- Returns:
- DataUnit instance or
nullif no match.
-
toString
-