Package com.peterphi.std.types
Enum Class StorageUnit
- All Implemented Interfaces:
Serializable
,Comparable<StorageUnit>
,Constable
Represents Storage unit conversions in a similar fashion to how TimeUnit represents time unit conversions
This enumeration reflects the confused landscape of storage units: it contains computer science units (binary), storage industry units (decimal) and network industry units (decimal, bit-based)
KILOBYTE as used here is, in fact, according to the ISO a Kibibyte. If your code needs to present ISO units to users, call the
This enumeration reflects the confused landscape of storage units: it contains computer science units (binary), storage industry units (decimal) and network industry units (decimal, bit-based)
KILOBYTE as used here is, in fact, according to the ISO a Kibibyte. If your code needs to present ISO units to users, call the
getISOUnit()
method (which returns the short-form ISO unit - eg. "KiB" for KILOBYTES, "KB" for DECIMAL_KILOBYTES
and "Kb" for KILOBIT)-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantDescriptionOne bit (4 bits)One byte (8 bits)One decimal Exabyte (ISO: EB).One decimal gigabyte (ISO: GB).One decimal kilobyte (ISO: KB).One decimal megabyte (ISO: MB).One decimal Petabyte (ISO: PB).One decimal terabyte (ISO: TB).One decimal yottabyte (ISO YB).One decimal zettabyte (ISO ZB).One Exabit (ISO: Eb).One Exabyte (ISO: EiB)
The ISO long form name for this unit is Exbibibyte (which we avoid using as the enum name to avoid confusion)One Gigabit (ISO: Gb).One Gigabyte (ISO: GiB)
The ISO long form name for this unit is Gibibyte (which we avoid using as the enum name to avoid confusion)One Kilobit (ISO: Kb).One Kilobyte (ISO: KiB).One Megabit (ISO: Mb).One Megabyte (ISO: MiB)
The ISO long form name for this unit is Mebibyte (which we avoid using as the enum name to avoid confusion)One nibble (4 bits)One Petabit (ISO: Pb).One Petabyte (ISO: PiB)
The ISO long form name for this unit is Pebibyte (which we avoid using as the enum name to avoid confusion)One Terabit (ISO: Tb).One Terabyte (ISO: TiB)
The ISO long form name for this unit is Tebibyte (which we avoid using as the enum name to avoid confusion)One yottabit (ISO Yb).One yottabyte (ISO YiB)
The ISO long form name for this unit is Yobibyte (which we avoid using as the enum name to avoid confusion)One zettabit (ISO Zb).One zettabyte (ISO ZiB)
The ISO long form name for this unit is Zebibyte (which we avoid using as the enum name to avoid confusion) -
Field Summary
Modifier and TypeFieldDescriptionstatic final StorageUnit[]
The range of quantifiers for binary units (bytes to terabytes (ISO: B to TiB))static final StorageUnit[]
static final StorageUnit[]
The range of quantifiers for ISO decimal units (bytes to terabytes (ISO: B to TB))static final StorageUnit[]
static final int
The rounding mode this class uses; this is, by default,BigDecimal.ROUND_HALF_UP
static final StorageUnit[]
-
Method Summary
Modifier and TypeMethodDescriptionlong
convert
(long amount, StorageUnit unit) convert
(BigDecimal amount, StorageUnit unit) convert
(BigInteger amount, StorageUnit unit) fromBits
(BigDecimal bits) fromBits
(BigInteger bits) static StorageUnit
getAppropriateBinaryUnit
(BigInteger amount, StorageUnit sourceUnit) Attempts to locate the most appropriate binary unit (binary units are what everyone but the ISO and the storage industry mean by "KB", "MB", etc.static StorageUnit
getAppropriateDecimalUnit
(BigInteger amount, StorageUnit sourceUnit) Attempts to locate the most appropriate decimal unit (decimal units are what the ISO and the storage industry (but nobody else) mean by "KB", "MB", etc.static StorageUnit
getAppropriateUnit
(BigInteger amount, StorageUnit sourceUnit, StorageUnit[] options) Attempts to locate the most appropriate of the provided units to express the provided amount in for human use (balancing precision and sensible expression)Return the ISO unit for this storage unit.
for MEGABYTE this will return MiB (to avoid further confusion)Get the plural version of this unit (e.g.Get the singular version of this unit (e.g.static StorageUnit
largest
(StorageUnit unit, StorageUnit unit2) Returns the the larger unit ofunit
and unit2static StorageUnit
Parse the non-ISO abbreviation: MiB and MB both -> MEGABYTE (which is technically a "Mebibyte")static StorageUnit
Parse the ISO abbreviation: (e.g.static StorageUnit
smallest
(StorageUnit unit, StorageUnit unit2) Returns the the most diminutive unit ofunit
and unit2toBits
(BigDecimal amount) toBits
(BigInteger amount) long
toBytes
(long amount) toBytes
(BigDecimal amount) toBytes
(BigInteger amount) toString
(long amount) toString
(long amount, boolean shortType) toString
(BigDecimal amount) toString
(BigDecimal amount, boolean shortType) toString
(BigInteger amount) toString
(BigInteger amount, boolean shortType) static StorageUnit
Returns the enum constant of this class with the specified name.static StorageUnit[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
BITS
One bit (4 bits) -
NIBBLES
One nibble (4 bits) -
BYTES
One byte (8 bits) -
KILOBYTES
One Kilobyte (ISO: KiB). 1 Kilobyte = 1024 bytes (8192 bits).
The ISO long form name for this unit is Kibibyte (which we avoid using as the enum name to avoid confusion) -
MEGABYTES
One Megabyte (ISO: MiB)
The ISO long form name for this unit is Mebibyte (which we avoid using as the enum name to avoid confusion) -
GIGABYTES
One Gigabyte (ISO: GiB)
The ISO long form name for this unit is Gibibyte (which we avoid using as the enum name to avoid confusion) -
TERABYTES
One Terabyte (ISO: TiB)
The ISO long form name for this unit is Tebibyte (which we avoid using as the enum name to avoid confusion) -
PETABYTES
One Petabyte (ISO: PiB)
The ISO long form name for this unit is Pebibyte (which we avoid using as the enum name to avoid confusion) -
EXABYTES
One Exabyte (ISO: EiB)
The ISO long form name for this unit is Exbibibyte (which we avoid using as the enum name to avoid confusion) -
ZETTABYTES
One zettabyte (ISO ZiB)
The ISO long form name for this unit is Zebibyte (which we avoid using as the enum name to avoid confusion) -
YOTTABYTES
One yottabyte (ISO YiB)
The ISO long form name for this unit is Yobibyte (which we avoid using as the enum name to avoid confusion) -
DECIMAL_KILOBYTES
One decimal kilobyte (ISO: KB). As used by the storage industry. 1 decimal kilobyte = 1000 bytes (8000 bits) -
DECIMAL_MEGABYTES
One decimal megabyte (ISO: MB). As used by the storage industry -
DECIMAL_GIGABYTES
One decimal gigabyte (ISO: GB). As used by the storage industry -
DECIMAL_TERABYTES
One decimal terabyte (ISO: TB). As used by the storage industry -
DECIMAL_PETABYTES
One decimal Petabyte (ISO: PB). As used by the storage industry -
DECIMAL_EXABYTES
One decimal Exabyte (ISO: EB). As used by the storage industry -
DECIMAL_ZETTABYTES
One decimal zettabyte (ISO ZB). As used by the storage industry -
DECIMAL_YOTTABYTES
One decimal yottabyte (ISO YB). As used by the storage industry -
KILOBITS
One Kilobit (ISO: Kb). As used by the network industry. 1 kilobit = 125 bytes (1000 bits).
N.B. this is a different value to what Google means by kilobit in its calculator (where it interprets 1 kilobit as 128 bytes -- which is a kibibit) -
MEGABITS
One Megabit (ISO: Mb). As used by the network industry. 1 megabit = 125000 bytes (1,000,000 bits).
N.B. this is a different value to what Google means by kilobit in its calculator (where it interprets 1 kilobit as 128 bytes -- which is a kibibit) -
GIGABITS
One Gigabit (ISO: Gb). As used by the network industry.
N.B. this is a different value to what Google means by kilobit in its calculator (where it interprets 1 kilobit as 128 bytes -- which is a kibibit) -
TERABITS
One Terabit (ISO: Tb). As used by the network industry.
N.B. this is a different value to what Google means by kilobit in its calculator (where it interprets 1 kilobit as 128 bytes -- which is a kibibit) -
PETABITS
One Petabit (ISO: Pb). As used by the network industry.
N.B. this is a different value to what Google means by kilobit in its calculator (where it interprets 1 kilobit as 128 bytes -- which is a kibibit) -
EXABITS
One Exabit (ISO: Eb). As used by the network industry.
N.B. this is a different value to what Google means by kilobit in its calculator (where it interprets 1 kilobit as 128 bytes -- which is a kibibit) -
ZETTABITS
One zettabit (ISO Zb). As used by the network industry.
N.B. this is a different value to what Google means by kilobit in its calculator (where it interprets 1 kilobit as 128 bytes -- which is a kibibit) -
YOTTABITS
One yottabit (ISO Yb). As used by the network industry.
N.B. this is a different value to what Google means by kilobit in its calculator (where it interprets 1 kilobit as 128 bytes -- which is a kibibit)
-
-
Field Details
-
NETWORK_DECIMAL
-
STORAGE_DECIMAL
-
COMPSCI_BINARY
-
DECIMAL_QUANTIFIERS
The range of quantifiers for ISO decimal units (bytes to terabytes (ISO: B to TB)) -
BINARY_QUANTIFIERS
The range of quantifiers for binary units (bytes to terabytes (ISO: B to TiB)) -
ROUNDING_MODE
public static final int ROUNDING_MODEThe rounding mode this class uses; this is, by default,BigDecimal.ROUND_HALF_UP
- See Also:
-
-
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
-
getISOUnit
Return the ISO unit for this storage unit.
for MEGABYTE this will return MiB (to avoid further confusion)- Returns:
-
getPlural
Get the plural version of this unit (e.g. "Bytes")- Returns:
-
getSingular
Get the singular version of this unit (e.g. "Byte")- Returns:
-
convert
-
convert
-
convert
-
toBits
-
toBits
-
fromBits
-
fromBits
-
toBytes
public long toBytes(long amount) -
toBytes
-
toBytes
-
toString
-
toString
-
toString
-
toString
-
toString
-
toString
-
getAppropriateBinaryUnit
Attempts to locate the most appropriate binary unit (binary units are what everyone but the ISO and the storage industry mean by "KB", "MB", etc. - and what the ISO refer to as "KiB", "MiB", etc.) to express the provided amount in for human use (balancing precision and sensible expression)- Parameters:
amount
- the amountsourceUnit
- the unit the amount is expressed in- Returns:
- the unit which is considered best for expressing the provided amount to a human
-
getAppropriateDecimalUnit
Attempts to locate the most appropriate decimal unit (decimal units are what the ISO and the storage industry (but nobody else) mean by "KB", "MB", etc. (i.e. KB = 1000 bytes) to express the provided amount in for human use (balancing precision and sensible expression)- Parameters:
amount
- the amountsourceUnit
- the unit the amount is expressed in- Returns:
- the unit which is considered best for expressing the provided amount to a human
-
getAppropriateUnit
public static StorageUnit getAppropriateUnit(BigInteger amount, StorageUnit sourceUnit, StorageUnit[] options) Attempts to locate the most appropriate of the provided units to express the provided amount in for human use (balancing precision and sensible expression)- Parameters:
amount
- the amountsourceUnit
- the unit the amount is expressed inoptions
- the storage units which may be used- Returns:
- the unit which is considered best for expressing the provided amount to a human
-
smallest
Returns the the most diminutive unit ofunit
and unit2- Parameters:
unit
- a unit (should not be null)unit2
- a unit (should not be null)- Returns:
- the most diminutive unit of
unit
and unit2 (or null if both units were null) - Throws:
NullPointerException
- if one unit is null
-
largest
Returns the the larger unit ofunit
and unit2- Parameters:
unit
- a unit (should not be null)unit2
- a unit (should not be null)- Returns:
- the larger unit of
unit
and unit2 (or null if both units were null) - Throws:
NullPointerException
- if one unit is null
-
parseISO
Parse the ISO abbreviation: (e.g. MiB -> MEGABYTE (1024*1024 bytes))- Parameters:
unit
-- Returns:
-
parse
Parse the non-ISO abbreviation: MiB and MB both -> MEGABYTE (which is technically a "Mebibyte")- Parameters:
toParse
-- Returns:
- A StorageUnit representing the unit, assuming that both MiB and MB mean "binary megabyte"
(
StorageUnit.MEGABYTES
)
-