public enum MemoryUnit extends Enum<MemoryUnit>
MemoryUnit
represents memory amounts at a given unit of
granularity and provides utility methods to convert across units. A
MemoryUnit
does not maintain memory information, but only
helps organize and use memory amounts representations that may be maintained
separately across various contexts.
Note than in this class kilo-, mega- and giga- prefixes means 2^10 = 1024 multiplexing, that is more common in low-level programming, CPU and operation system contexts, not 1000 as defined by International System of Units (SI).
A MemoryUnit
is mainly used to inform memory amount-based methods
how a given memory amount parameter should be interpreted.
API of MemoryUnit
is copied from TimeUnit
enum.
Enum Constant and Description |
---|
BITS
Memory unit representing one bit.
|
BYTES
Memory unit representing one byte, i.
|
CACHE_LINES
Memory unit representing 64 bytes, i.
|
GIGABYTES
Memory unit representing 1024 megabytes.
|
KILOBYTES
Memory unit representing 1024 bytes.
|
LONGS
Memory unit representing 8 bytes, i.
|
MEGABYTES
Memory unit representing 1024 kilobytes.
|
PAGES
Memory unit representing 4096 bytes, i.
|
Modifier and Type | Method and Description |
---|---|
long |
align(long amountToAlign,
MemoryUnit unit)
Aligns the given memory amount in the given unit to this unit.
|
long |
alignAndConvert(long sourceAmount,
MemoryUnit sourceUnit)
Equivalent to
convert(align(sourceAmount, sourceUnit), sourceUnit) . |
long |
convert(long sourceAmount,
MemoryUnit sourceUnit)
Converts the given memory amount in the given unit to this unit.
|
long |
toBits(long amount)
Equivalent to
BITS.convert(amount, this) . |
long |
toBytes(long amount)
Equivalent to
BYTES.convert(amount, this) . |
long |
toCacheLines(long amount)
Equivalent to
CACHE_LINES.convert(amount, this) . |
long |
toGigabytes(long amount)
Equivalent to
GIGABYTES.convert(amount, this) . |
long |
toKilobytes(long amount)
Equivalent to
KILOBYTES.convert(amount, this) . |
long |
toLongs(long amount)
Equivalent to
LONGS.convert(amount, this) . |
long |
toMegabytes(long amount)
Equivalent to
MEGABYTES.convert(amount, this) . |
long |
toPages(long amount)
Equivalent to
PAGES.convert(amount, this) . |
static MemoryUnit |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static MemoryUnit[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final MemoryUnit BITS
public static final MemoryUnit BYTES
public static final MemoryUnit LONGS
long
type.public static final MemoryUnit CACHE_LINES
public static final MemoryUnit KILOBYTES
public static final MemoryUnit PAGES
public static final MemoryUnit MEGABYTES
public static final MemoryUnit GIGABYTES
public static MemoryUnit[] values()
for (MemoryUnit c : MemoryUnit.values()) System.out.println(c);
public static MemoryUnit valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullpublic long convert(long sourceAmount, MemoryUnit sourceUnit)
7
bits
to bytes results in 0
. Conversions from coarser to
finer granularities with arguments that would numerically
overflow saturate to Long.MIN_VALUE
if negative or
Long.MAX_VALUE
if positive.
For example, to convert 4096 bytes to cache lines, use:
MemoryUnit.CACHE_LINES.convert(4096L, MemoryUnit.BYTES)
sourceAmount
- the memory amount in the given sourceUnit
sourceUnit
- the unit of the sourceAmount
argumentLong.MIN_VALUE
if conversion would negatively
overflow, or Long.MAX_VALUE
if it would positively overflow.public long align(long amountToAlign, MemoryUnit unit)
1000
bytes to kilobytes results in 1024
. Negative values are aligned towards
negative infinity: e. g. aligning -5
longs to cache lines results in -8
.amountToAlign
- the memory amount in the given unit
unit
- the unit of the amountToAlign
argumentIllegalArgumentException
- if the given unit
is finer than this unit,
or if the aligned value overflows long
boundspublic long alignAndConvert(long sourceAmount, MemoryUnit sourceUnit)
convert(align(sourceAmount, sourceUnit), sourceUnit)
.sourceAmount
- the memory amount in the given sourceUnit
sourceUnit
- the unit of the sourceAmount
argumentIllegalArgumentException
- if the given sourceUnit
is finer than this unit,
or if the aligned value overflows long
boundspublic long toBits(long amount)
BITS.convert(amount, this)
.amount
- the amountLong.MIN_VALUE
if conversion would negatively
overflow, or Long.MAX_VALUE
if it would positively overflow.public long toBytes(long amount)
BYTES.convert(amount, this)
.amount
- the amountLong.MIN_VALUE
if conversion would negatively
overflow, or Long.MAX_VALUE
if it would positively overflow.public long toLongs(long amount)
LONGS.convert(amount, this)
.amount
- the amountLong.MIN_VALUE
if conversion would negatively
overflow, or Long.MAX_VALUE
if it would positively overflow.public long toCacheLines(long amount)
CACHE_LINES.convert(amount, this)
.amount
- the amountLong.MIN_VALUE
if conversion would negatively
overflow, or Long.MAX_VALUE
if it would positively overflow.public long toKilobytes(long amount)
KILOBYTES.convert(amount, this)
.amount
- the amountLong.MIN_VALUE
if conversion would negatively
overflow, or Long.MAX_VALUE
if it would positively overflow.public long toPages(long amount)
PAGES.convert(amount, this)
.amount
- the amountLong.MIN_VALUE
if conversion would negatively
overflow, or Long.MAX_VALUE
if it would positively overflow.public long toMegabytes(long amount)
MEGABYTES.convert(amount, this)
.amount
- the amountLong.MIN_VALUE
if conversion would negatively
overflow, or Long.MAX_VALUE
if it would positively overflow.public long toGigabytes(long amount)
GIGABYTES.convert(amount, this)
.amount
- the amountCopyright © 2019. All rights reserved.