Latest commit

History

309 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Storage-Units Chat

Storage units according to ISO IEC 80000-13:2008 implemented in Java.

Features

  • Immutable, type- and thread-safe object model for storage units
  • Convenience factories to create units
  • Basic arithmetic operators
  • Comparisons and equality checks between units
  • Lossless conversion between all units
  • Human-readable text format, including custom formats
  • Compatible with any java.lang.Number
  • Custom serializers for Jackson, MongoDB, EclipseLink, and others

Available Units

NameSymbolExponentialAbsolute
ByteB20 Byte1 Byte
KibibyteKiB210 Byte1 024 Byte
MebibyteMiB220 Byte1 048 576 Byte
GibibyteGiB230 Byte1 073 741 824 Byte
TebibyteTiB240 Byte1 099 511 627 776 Byte
PebibytePiB250 Byte1 125 899 906 842 624 Byte
ExbibyteEiB260 Byte1 152 921 504 606 846 976 Byte
ZebibyteZiB270 Byte1 180 591 620 717 411 303 424 Byte
YobibyteYiB280 Byte1 208 925 819 614 629 174 706 176 Byte
RobibyteRiB290 Byte1 237 940 039 285 380 274 899 124 224 Byte
QubibyteQiB2100 Byte1 267 650 600 228 229 401 496 703 205 376 Byte
NameSymbolExponentialAbsolute
ByteB100 Byte1 Byte
KilobyteKB103 Byte1 000 Byte
MegabyteMB106 Byte1 000 000 Byte
GigabyteGB109 Byte1 000 000 000 Byte
TerabyteTB1012 Byte1 000 000 000 000 Byte
PetabytePB1015 Byte1 000 000 000 000 000 Byte
ExabyteEB1018 Byte1 000 000 000 000 000 000 Byte
ZettabyteZB1021 Byte1 000 000 000 000 000 000 000 Byte
YottabyteYB1024 Byte1 000 000 000 000 000 000 000 000 Byte
RonnabyteRB1027 Byte1 000 000 000 000 000 000 000 000 000 Byte
QuettabyteQB1030 Byte1 000 000 000 000 000 000 000 000 000 000 Byte

Usage

Factories

Each unit implements a Byte-based static factory method (valueOf(BigInteger) or valueOf(long)) that can be used to represent a given number of bytes in a specific unit. Note that Long.MAX_VALUE == 8 Exabyte, thus use BigInteger if you want to work with anything bigger than a eight Exabyte. When in doubt, always use BigInteger.

// 'long' basedKilobyteunit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"// 'BigInteger' basedKilobyteunit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"

The StorageUnits class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.

Binary Units

// 'long' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)

Decimal Units

// 'long' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(120000) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(1000000) // Megabyte (1.00 MB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)

Additionally high-level factory methods are also available in the StorageUnits class.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Kibibyteunit = kibibyte(1) // 1 024 ByteMebibyteunit = mebibyte(1) // 1 048 576 ByteGibibyteunit = gibibyte(1) // 1 073 741 824 ByteTebibyteunit = tebibyte(1) // 1 099 511 627 776 BytePebibyteunit = pebibyte(1) // 1 125 899 906 842 624 ByteExbibyteunit = exbibyte(1) // 1 152 921 504 606 846 976 ByteZebibyteunit = zebibyte(1) // 1 180 591 620 717 411 303 424 ByteYobibyteunit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 ByteRobibyteunit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 ByteQubibyteunit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 ByteKilobyteunit = kilobyte(1) // 1 000 ByteMegabyteunit = megabyte(1) // 1 000 000 ByteGigabyteunit = gigabyte(1) // 1 000 000 000 ByteTerabyteunit = terabyte(1) // 1 000 000 000 000 BytePetabyteunit = petabyte(1) // 1 000 000 000 000 000 ByteExabyteunit = exabyte(1) // 1 000 000 000 000 000 000 ByteZettabyteunit = zettabyte(1) // 1 000 000 000 000 000 000 000 ByteYottabyteunit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 ByteRonnabyteunit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 ByteQuettabyteunit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte

Add, Subtract, Multiply, Divide

Each unit implements the basic four math operations. All operations retain their original type, e.g. [Kilobyte] + [Megabyte] = [Kilobyte]

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Bytekibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Bytekibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Bytemegabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Bytegigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyteterabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte

Comparison & Equality

Each unit is comparable to each other unit.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kibibyte(1024).compareTo(mebibyte(1)) == 0// truekibibyte(1000).compareTo(mebibyte(1)) == 0// falsepetabyte(3).compareTo(terabyte(3000)) == 0// truemegabyte(1000).equals(gigabyte(1)) // truemegabyte(1024).equals(gigabyte(1)) // falseterabyte(12).equals(gigabyte(12000)) // true

Formatting

Each unit prints a human-readable string, representing the amount of bytes in the given unit using the symbol specified in ISO IEC 80000-13:2008.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
// default pattern '0.00'terabyte(2).toString() // "2.00 TB"gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"// use custom patternkilobyte(212345).toString("0.0") // "212345.0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"// use custom pattern with specific Localekilobyte(212345).toString("0.0", Locale.GERMAN) // "212345,0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000", Locale.GERMAN) // "2.073,689 TiB"// use custom formatFormatcustomFormat = newDecimalFormat("#.00000");
terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"// without creating unit type firstlongnumberOfBytes = 1_000_000_000_000_000L;
formatAsPetabyte(numberOfBytes) // "1.00 PB"formatAsTerabyte(numberOfBytes) // "1000.00 TB"formatAsPebibyte(numberOfBytes) // "0.89 PiB"// use custom patternformatAsTerabyte(numberOfBytes, "#0.#####") // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####") // "0.88818 PiB"// use custom pattern with specific LocaleformatAsTerabyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "0,88818 PiB"// use custom formatformatAsTerabyte(numberOfBytes, customFormat) // "1000.00000 TB"formatAsPebibyte(numberOfBytes, customFormat) // ".88818 PiB"

Conversions

Each unit can be converted to each other unit without loss of information.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Megabyteunit = kilobyte(1000).asMegabyte() // "1.00 MB"Kilobyteunit = gigabyte(12).asKilobyte() // "12000000.00 kB"Gigabyteunit = terabyte(1).asGigabyte() // "1000.00 GB"// convert to best-matchkilobyte(1100).asBestMatchingUnit() // "1.10 MB"kilobyte(1100).asBestMatchingBinaryUnit() // "1.05 MiB"kilobyte(1100).asBestMatchingDecimalUnit() // "1.10 MB"

Each unit can be expressed as a fraction of another unit (precise up to 24 decimal places)

importstaticwtf.metio.storageunits.model.StorageUnits.*;
BigDecimalkilobytes = megabyte(1).inKilobyte() // 1 000BigIntegerbytes = kibibyte(2).inByte() // 2 048BigDecimalterabytes = gigabyte(15).inTerabyte() // 0.015

Serialization/Converters/Mappers

Multiple custom serializers, converters, and mappers are available for all storage units.

Dozer

Use a Dozer converter like this:

importstaticwtf.metio.storageunits.dozer.*;
DozerBeanMapperBuilder.create()
.withCustomConverter(newBigIntegerBinaryStorageUnitConverter())
.withCustomConverter(newBigIntegerDecimalStorageUnitConverter())
.withCustomConverter(newLongBinaryStorageUnitConverter())
.withCustomConverter(newLongDecimalStorageUnitConverter())
.build();

EclipseLink

Use any of the three converters like this:

importstaticwtf.metio.storageunits.eclipselink.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Basic@Converter (
name="binaryConverter",
converterClass=BinaryStorageUnitConverter.class
)
@Convert("binaryConverter")
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Basic@Converter (
name="decimalConverter",
converterClass=DecimalyStorageUnitConverter.class
)
@Convert("decimalConverter")
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

GSON

Use a GSON serializer/deserializer like this:

importstaticwtf.metio.storageunits.gson.*;
newGsonBuilder()
.registerTypeHierarchyAdapter(StorageUnit.class, newStorageUnitSerializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newBinaryStorageUnitDeserializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newDecimalStorageUnitDeserializer())
.create();

Jackson

Use the provided StorageUnitModule like this:

importstaticwtf.metio.storageunits.jackson.*;
ObjectMapperobjectMapper = newObjectMapper();
objectMapper.registerModule(newStorageUnitModule()); // defaults to binary unitsobjectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.BINARY));
objectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.DECIMAL));

Jakarta

Use the provided AttributeConverters like this:

importstaticwtf.metio.storageunits.jakarta.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Convert(converter = BinaryStorageUnitConverter.class)
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Convert(converter = DecimalStorageUnitConverter.class)
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

MapStruct

Use any of the available mappers like this:

importstaticwtf.metio.storageunits.mapstruct.*;
@Mapper( uses = BigIntegerToBinaryUnitMapper.class )
publicinterfaceMovieMapper {
DestinationTypetoDestination(SourceTypesourceValue);
}

ModelMapper

Use any of the available converters like this:

importstaticwtf.metio.storageunits.modelmapper.*;
modelMapper.addConverter(newBigIntegerToBinaryUnitConverter());
modelMapper.addConverter(newBigIntegerToDecimalUnitConverter());
modelMapper.addConverter(newLongToBinaryUnitConverter());
modelMapper.addConverter(newLongToDecimalUnitConverter());
modelMapper.addConverter(newStorageUnitToBigIntegerConverter());

MongoDB

Use any of the three codecs like this:

importstaticwtf.metio.storageunits.mongodb.*;
CodecRegistrybinaryRegistry = CodecRegistries.fromCodecs(newBinaryStorageUnitCodec(), ...);
CodecRegistrydecimalRegistry = CodecRegistries.fromCodecs(newDecimalStorageUnitCodec(), ...);

Orika

Use any of the provided converters like this:

importstaticwtf.metio.storageunits.orika.*;
ConverterFactoryconverterFactory = mapperFactory.getConverterFactory();
converterFactory.registerConverter(newBinaryStorageUnitConverter());
converterFactory.registerConverter(newDecimalStorageUnitConverter());

Integration

To use this project just declare the following dependency inside your POM:

<dependencies>
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-model</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-dozer</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY --><!-- EclipseLink ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-eclipselink</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- EclipseLink ONLY --><!-- GSON ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-gson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- GSON ONLY --><!-- Jackson ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jackson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jackson ONLY --><!-- Jakarta ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jakarta</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jakarta ONLY --><!-- MapStruct ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mapstruct</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MapStruct ONLY --><!-- ModelMapper ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-modelmapper</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- ModelMapper ONLY --><!-- MongoDB ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mongodb</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MongoDB ONLY --><!-- Orika ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-orika</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Orika ONLY -->
</dependencies>

Replace ${version.storage-units} with the latest release.

Reference

Originally inspired by Twitters util package.

Alternatives

About

Java library for storage units

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

21 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Latest commit

History

309 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Storage-Units Chat

Storage units according to ISO IEC 80000-13:2008 implemented in Java.

Features

  • Immutable, type- and thread-safe object model for storage units
  • Convenience factories to create units
  • Basic arithmetic operators
  • Comparisons and equality checks between units
  • Lossless conversion between all units
  • Human-readable text format, including custom formats
  • Compatible with any java.lang.Number
  • Custom serializers for Jackson, MongoDB, EclipseLink, and others

Available Units

NameSymbolExponentialAbsolute
ByteB20 Byte1 Byte
KibibyteKiB210 Byte1 024 Byte
MebibyteMiB220 Byte1 048 576 Byte
GibibyteGiB230 Byte1 073 741 824 Byte
TebibyteTiB240 Byte1 099 511 627 776 Byte
PebibytePiB250 Byte1 125 899 906 842 624 Byte
ExbibyteEiB260 Byte1 152 921 504 606 846 976 Byte
ZebibyteZiB270 Byte1 180 591 620 717 411 303 424 Byte
YobibyteYiB280 Byte1 208 925 819 614 629 174 706 176 Byte
RobibyteRiB290 Byte1 237 940 039 285 380 274 899 124 224 Byte
QubibyteQiB2100 Byte1 267 650 600 228 229 401 496 703 205 376 Byte
NameSymbolExponentialAbsolute
ByteB100 Byte1 Byte
KilobyteKB103 Byte1 000 Byte
MegabyteMB106 Byte1 000 000 Byte
GigabyteGB109 Byte1 000 000 000 Byte
TerabyteTB1012 Byte1 000 000 000 000 Byte
PetabytePB1015 Byte1 000 000 000 000 000 Byte
ExabyteEB1018 Byte1 000 000 000 000 000 000 Byte
ZettabyteZB1021 Byte1 000 000 000 000 000 000 000 Byte
YottabyteYB1024 Byte1 000 000 000 000 000 000 000 000 Byte
RonnabyteRB1027 Byte1 000 000 000 000 000 000 000 000 000 Byte
QuettabyteQB1030 Byte1 000 000 000 000 000 000 000 000 000 000 Byte

Usage

Factories

Each unit implements a Byte-based static factory method (valueOf(BigInteger) or valueOf(long)) that can be used to represent a given number of bytes in a specific unit. Note that Long.MAX_VALUE == 8 Exabyte, thus use BigInteger if you want to work with anything bigger than a eight Exabyte. When in doubt, always use BigInteger.

// 'long' basedKilobyteunit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"// 'BigInteger' basedKilobyteunit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"

The StorageUnits class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.

Binary Units

// 'long' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)

Decimal Units

// 'long' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(120000) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(1000000) // Megabyte (1.00 MB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)

Additionally high-level factory methods are also available in the StorageUnits class.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Kibibyteunit = kibibyte(1) // 1 024 ByteMebibyteunit = mebibyte(1) // 1 048 576 ByteGibibyteunit = gibibyte(1) // 1 073 741 824 ByteTebibyteunit = tebibyte(1) // 1 099 511 627 776 BytePebibyteunit = pebibyte(1) // 1 125 899 906 842 624 ByteExbibyteunit = exbibyte(1) // 1 152 921 504 606 846 976 ByteZebibyteunit = zebibyte(1) // 1 180 591 620 717 411 303 424 ByteYobibyteunit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 ByteRobibyteunit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 ByteQubibyteunit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 ByteKilobyteunit = kilobyte(1) // 1 000 ByteMegabyteunit = megabyte(1) // 1 000 000 ByteGigabyteunit = gigabyte(1) // 1 000 000 000 ByteTerabyteunit = terabyte(1) // 1 000 000 000 000 BytePetabyteunit = petabyte(1) // 1 000 000 000 000 000 ByteExabyteunit = exabyte(1) // 1 000 000 000 000 000 000 ByteZettabyteunit = zettabyte(1) // 1 000 000 000 000 000 000 000 ByteYottabyteunit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 ByteRonnabyteunit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 ByteQuettabyteunit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte

Add, Subtract, Multiply, Divide

Each unit implements the basic four math operations. All operations retain their original type, e.g. [Kilobyte] + [Megabyte] = [Kilobyte]

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Bytekibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Bytekibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Bytemegabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Bytegigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyteterabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte

Comparison & Equality

Each unit is comparable to each other unit.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kibibyte(1024).compareTo(mebibyte(1)) == 0// truekibibyte(1000).compareTo(mebibyte(1)) == 0// falsepetabyte(3).compareTo(terabyte(3000)) == 0// truemegabyte(1000).equals(gigabyte(1)) // truemegabyte(1024).equals(gigabyte(1)) // falseterabyte(12).equals(gigabyte(12000)) // true

Formatting

Each unit prints a human-readable string, representing the amount of bytes in the given unit using the symbol specified in ISO IEC 80000-13:2008.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
// default pattern '0.00'terabyte(2).toString() // "2.00 TB"gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"// use custom patternkilobyte(212345).toString("0.0") // "212345.0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"// use custom pattern with specific Localekilobyte(212345).toString("0.0", Locale.GERMAN) // "212345,0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000", Locale.GERMAN) // "2.073,689 TiB"// use custom formatFormatcustomFormat = newDecimalFormat("#.00000");
terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"// without creating unit type firstlongnumberOfBytes = 1_000_000_000_000_000L;
formatAsPetabyte(numberOfBytes) // "1.00 PB"formatAsTerabyte(numberOfBytes) // "1000.00 TB"formatAsPebibyte(numberOfBytes) // "0.89 PiB"// use custom patternformatAsTerabyte(numberOfBytes, "#0.#####") // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####") // "0.88818 PiB"// use custom pattern with specific LocaleformatAsTerabyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "0,88818 PiB"// use custom formatformatAsTerabyte(numberOfBytes, customFormat) // "1000.00000 TB"formatAsPebibyte(numberOfBytes, customFormat) // ".88818 PiB"

Conversions

Each unit can be converted to each other unit without loss of information.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Megabyteunit = kilobyte(1000).asMegabyte() // "1.00 MB"Kilobyteunit = gigabyte(12).asKilobyte() // "12000000.00 kB"Gigabyteunit = terabyte(1).asGigabyte() // "1000.00 GB"// convert to best-matchkilobyte(1100).asBestMatchingUnit() // "1.10 MB"kilobyte(1100).asBestMatchingBinaryUnit() // "1.05 MiB"kilobyte(1100).asBestMatchingDecimalUnit() // "1.10 MB"

Each unit can be expressed as a fraction of another unit (precise up to 24 decimal places)

importstaticwtf.metio.storageunits.model.StorageUnits.*;
BigDecimalkilobytes = megabyte(1).inKilobyte() // 1 000BigIntegerbytes = kibibyte(2).inByte() // 2 048BigDecimalterabytes = gigabyte(15).inTerabyte() // 0.015

Serialization/Converters/Mappers

Multiple custom serializers, converters, and mappers are available for all storage units.

Dozer

Use a Dozer converter like this:

importstaticwtf.metio.storageunits.dozer.*;
DozerBeanMapperBuilder.create()
.withCustomConverter(newBigIntegerBinaryStorageUnitConverter())
.withCustomConverter(newBigIntegerDecimalStorageUnitConverter())
.withCustomConverter(newLongBinaryStorageUnitConverter())
.withCustomConverter(newLongDecimalStorageUnitConverter())
.build();

EclipseLink

Use any of the three converters like this:

importstaticwtf.metio.storageunits.eclipselink.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Basic@Converter (
name="binaryConverter",
converterClass=BinaryStorageUnitConverter.class
)
@Convert("binaryConverter")
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Basic@Converter (
name="decimalConverter",
converterClass=DecimalyStorageUnitConverter.class
)
@Convert("decimalConverter")
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

GSON

Use a GSON serializer/deserializer like this:

importstaticwtf.metio.storageunits.gson.*;
newGsonBuilder()
.registerTypeHierarchyAdapter(StorageUnit.class, newStorageUnitSerializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newBinaryStorageUnitDeserializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newDecimalStorageUnitDeserializer())
.create();

Jackson

Use the provided StorageUnitModule like this:

importstaticwtf.metio.storageunits.jackson.*;
ObjectMapperobjectMapper = newObjectMapper();
objectMapper.registerModule(newStorageUnitModule()); // defaults to binary unitsobjectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.BINARY));
objectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.DECIMAL));

Jakarta

Use the provided AttributeConverters like this:

importstaticwtf.metio.storageunits.jakarta.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Convert(converter = BinaryStorageUnitConverter.class)
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Convert(converter = DecimalStorageUnitConverter.class)
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

MapStruct

Use any of the available mappers like this:

importstaticwtf.metio.storageunits.mapstruct.*;
@Mapper( uses = BigIntegerToBinaryUnitMapper.class )
publicinterfaceMovieMapper {
DestinationTypetoDestination(SourceTypesourceValue);
}

ModelMapper

Use any of the available converters like this:

importstaticwtf.metio.storageunits.modelmapper.*;
modelMapper.addConverter(newBigIntegerToBinaryUnitConverter());
modelMapper.addConverter(newBigIntegerToDecimalUnitConverter());
modelMapper.addConverter(newLongToBinaryUnitConverter());
modelMapper.addConverter(newLongToDecimalUnitConverter());
modelMapper.addConverter(newStorageUnitToBigIntegerConverter());

MongoDB

Use any of the three codecs like this:

importstaticwtf.metio.storageunits.mongodb.*;
CodecRegistrybinaryRegistry = CodecRegistries.fromCodecs(newBinaryStorageUnitCodec(), ...);
CodecRegistrydecimalRegistry = CodecRegistries.fromCodecs(newDecimalStorageUnitCodec(), ...);

Orika

Use any of the provided converters like this:

importstaticwtf.metio.storageunits.orika.*;
ConverterFactoryconverterFactory = mapperFactory.getConverterFactory();
converterFactory.registerConverter(newBinaryStorageUnitConverter());
converterFactory.registerConverter(newDecimalStorageUnitConverter());

Integration

To use this project just declare the following dependency inside your POM:

<dependencies>
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-model</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-dozer</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY --><!-- EclipseLink ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-eclipselink</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- EclipseLink ONLY --><!-- GSON ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-gson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- GSON ONLY --><!-- Jackson ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jackson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jackson ONLY --><!-- Jakarta ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jakarta</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jakarta ONLY --><!-- MapStruct ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mapstruct</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MapStruct ONLY --><!-- ModelMapper ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-modelmapper</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- ModelMapper ONLY --><!-- MongoDB ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mongodb</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MongoDB ONLY --><!-- Orika ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-orika</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Orika ONLY -->
</dependencies>

Replace ${version.storage-units} with the latest release.

Reference

Originally inspired by Twitters util package.

Alternatives

About

Java library for storage units

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

21 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

309 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Storage-Units Chat

Storage units according to ISO IEC 80000-13:2008 implemented in Java.

Features

  • Immutable, type- and thread-safe object model for storage units
  • Convenience factories to create units
  • Basic arithmetic operators
  • Comparisons and equality checks between units
  • Lossless conversion between all units
  • Human-readable text format, including custom formats
  • Compatible with any java.lang.Number
  • Custom serializers for Jackson, MongoDB, EclipseLink, and others

Available Units

NameSymbolExponentialAbsolute
ByteB20 Byte1 Byte
KibibyteKiB210 Byte1 024 Byte
MebibyteMiB220 Byte1 048 576 Byte
GibibyteGiB230 Byte1 073 741 824 Byte
TebibyteTiB240 Byte1 099 511 627 776 Byte
PebibytePiB250 Byte1 125 899 906 842 624 Byte
ExbibyteEiB260 Byte1 152 921 504 606 846 976 Byte
ZebibyteZiB270 Byte1 180 591 620 717 411 303 424 Byte
YobibyteYiB280 Byte1 208 925 819 614 629 174 706 176 Byte
RobibyteRiB290 Byte1 237 940 039 285 380 274 899 124 224 Byte
QubibyteQiB2100 Byte1 267 650 600 228 229 401 496 703 205 376 Byte
NameSymbolExponentialAbsolute
ByteB100 Byte1 Byte
KilobyteKB103 Byte1 000 Byte
MegabyteMB106 Byte1 000 000 Byte
GigabyteGB109 Byte1 000 000 000 Byte
TerabyteTB1012 Byte1 000 000 000 000 Byte
PetabytePB1015 Byte1 000 000 000 000 000 Byte
ExabyteEB1018 Byte1 000 000 000 000 000 000 Byte
ZettabyteZB1021 Byte1 000 000 000 000 000 000 000 Byte
YottabyteYB1024 Byte1 000 000 000 000 000 000 000 000 Byte
RonnabyteRB1027 Byte1 000 000 000 000 000 000 000 000 000 Byte
QuettabyteQB1030 Byte1 000 000 000 000 000 000 000 000 000 000 Byte

Usage

Factories

Each unit implements a Byte-based static factory method (valueOf(BigInteger) or valueOf(long)) that can be used to represent a given number of bytes in a specific unit. Note that Long.MAX_VALUE == 8 Exabyte, thus use BigInteger if you want to work with anything bigger than a eight Exabyte. When in doubt, always use BigInteger.

// 'long' basedKilobyteunit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"// 'BigInteger' basedKilobyteunit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"

The StorageUnits class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.

Binary Units

// 'long' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)

Decimal Units

// 'long' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(120000) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(1000000) // Megabyte (1.00 MB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)

Additionally high-level factory methods are also available in the StorageUnits class.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Kibibyteunit = kibibyte(1) // 1 024 ByteMebibyteunit = mebibyte(1) // 1 048 576 ByteGibibyteunit = gibibyte(1) // 1 073 741 824 ByteTebibyteunit = tebibyte(1) // 1 099 511 627 776 BytePebibyteunit = pebibyte(1) // 1 125 899 906 842 624 ByteExbibyteunit = exbibyte(1) // 1 152 921 504 606 846 976 ByteZebibyteunit = zebibyte(1) // 1 180 591 620 717 411 303 424 ByteYobibyteunit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 ByteRobibyteunit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 ByteQubibyteunit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 ByteKilobyteunit = kilobyte(1) // 1 000 ByteMegabyteunit = megabyte(1) // 1 000 000 ByteGigabyteunit = gigabyte(1) // 1 000 000 000 ByteTerabyteunit = terabyte(1) // 1 000 000 000 000 BytePetabyteunit = petabyte(1) // 1 000 000 000 000 000 ByteExabyteunit = exabyte(1) // 1 000 000 000 000 000 000 ByteZettabyteunit = zettabyte(1) // 1 000 000 000 000 000 000 000 ByteYottabyteunit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 ByteRonnabyteunit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 ByteQuettabyteunit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte

Add, Subtract, Multiply, Divide

Each unit implements the basic four math operations. All operations retain their original type, e.g. [Kilobyte] + [Megabyte] = [Kilobyte]

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Bytekibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Bytekibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Bytemegabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Bytegigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyteterabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte

Comparison & Equality

Each unit is comparable to each other unit.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kibibyte(1024).compareTo(mebibyte(1)) == 0// truekibibyte(1000).compareTo(mebibyte(1)) == 0// falsepetabyte(3).compareTo(terabyte(3000)) == 0// truemegabyte(1000).equals(gigabyte(1)) // truemegabyte(1024).equals(gigabyte(1)) // falseterabyte(12).equals(gigabyte(12000)) // true

Formatting

Each unit prints a human-readable string, representing the amount of bytes in the given unit using the symbol specified in ISO IEC 80000-13:2008.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
// default pattern '0.00'terabyte(2).toString() // "2.00 TB"gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"// use custom patternkilobyte(212345).toString("0.0") // "212345.0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"// use custom pattern with specific Localekilobyte(212345).toString("0.0", Locale.GERMAN) // "212345,0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000", Locale.GERMAN) // "2.073,689 TiB"// use custom formatFormatcustomFormat = newDecimalFormat("#.00000");
terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"// without creating unit type firstlongnumberOfBytes = 1_000_000_000_000_000L;
formatAsPetabyte(numberOfBytes) // "1.00 PB"formatAsTerabyte(numberOfBytes) // "1000.00 TB"formatAsPebibyte(numberOfBytes) // "0.89 PiB"// use custom patternformatAsTerabyte(numberOfBytes, "#0.#####") // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####") // "0.88818 PiB"// use custom pattern with specific LocaleformatAsTerabyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "0,88818 PiB"// use custom formatformatAsTerabyte(numberOfBytes, customFormat) // "1000.00000 TB"formatAsPebibyte(numberOfBytes, customFormat) // ".88818 PiB"

Conversions

Each unit can be converted to each other unit without loss of information.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Megabyteunit = kilobyte(1000).asMegabyte() // "1.00 MB"Kilobyteunit = gigabyte(12).asKilobyte() // "12000000.00 kB"Gigabyteunit = terabyte(1).asGigabyte() // "1000.00 GB"// convert to best-matchkilobyte(1100).asBestMatchingUnit() // "1.10 MB"kilobyte(1100).asBestMatchingBinaryUnit() // "1.05 MiB"kilobyte(1100).asBestMatchingDecimalUnit() // "1.10 MB"

Each unit can be expressed as a fraction of another unit (precise up to 24 decimal places)

importstaticwtf.metio.storageunits.model.StorageUnits.*;
BigDecimalkilobytes = megabyte(1).inKilobyte() // 1 000BigIntegerbytes = kibibyte(2).inByte() // 2 048BigDecimalterabytes = gigabyte(15).inTerabyte() // 0.015

Serialization/Converters/Mappers

Multiple custom serializers, converters, and mappers are available for all storage units.

Dozer

Use a Dozer converter like this:

importstaticwtf.metio.storageunits.dozer.*;
DozerBeanMapperBuilder.create()
.withCustomConverter(newBigIntegerBinaryStorageUnitConverter())
.withCustomConverter(newBigIntegerDecimalStorageUnitConverter())
.withCustomConverter(newLongBinaryStorageUnitConverter())
.withCustomConverter(newLongDecimalStorageUnitConverter())
.build();

EclipseLink

Use any of the three converters like this:

importstaticwtf.metio.storageunits.eclipselink.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Basic@Converter (
name="binaryConverter",
converterClass=BinaryStorageUnitConverter.class
)
@Convert("binaryConverter")
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Basic@Converter (
name="decimalConverter",
converterClass=DecimalyStorageUnitConverter.class
)
@Convert("decimalConverter")
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

GSON

Use a GSON serializer/deserializer like this:

importstaticwtf.metio.storageunits.gson.*;
newGsonBuilder()
.registerTypeHierarchyAdapter(StorageUnit.class, newStorageUnitSerializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newBinaryStorageUnitDeserializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newDecimalStorageUnitDeserializer())
.create();

Jackson

Use the provided StorageUnitModule like this:

importstaticwtf.metio.storageunits.jackson.*;
ObjectMapperobjectMapper = newObjectMapper();
objectMapper.registerModule(newStorageUnitModule()); // defaults to binary unitsobjectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.BINARY));
objectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.DECIMAL));

Jakarta

Use the provided AttributeConverters like this:

importstaticwtf.metio.storageunits.jakarta.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Convert(converter = BinaryStorageUnitConverter.class)
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Convert(converter = DecimalStorageUnitConverter.class)
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

MapStruct

Use any of the available mappers like this:

importstaticwtf.metio.storageunits.mapstruct.*;
@Mapper( uses = BigIntegerToBinaryUnitMapper.class )
publicinterfaceMovieMapper {
DestinationTypetoDestination(SourceTypesourceValue);
}

ModelMapper

Use any of the available converters like this:

importstaticwtf.metio.storageunits.modelmapper.*;
modelMapper.addConverter(newBigIntegerToBinaryUnitConverter());
modelMapper.addConverter(newBigIntegerToDecimalUnitConverter());
modelMapper.addConverter(newLongToBinaryUnitConverter());
modelMapper.addConverter(newLongToDecimalUnitConverter());
modelMapper.addConverter(newStorageUnitToBigIntegerConverter());

MongoDB

Use any of the three codecs like this:

importstaticwtf.metio.storageunits.mongodb.*;
CodecRegistrybinaryRegistry = CodecRegistries.fromCodecs(newBinaryStorageUnitCodec(), ...);
CodecRegistrydecimalRegistry = CodecRegistries.fromCodecs(newDecimalStorageUnitCodec(), ...);

Orika

Use any of the provided converters like this:

importstaticwtf.metio.storageunits.orika.*;
ConverterFactoryconverterFactory = mapperFactory.getConverterFactory();
converterFactory.registerConverter(newBinaryStorageUnitConverter());
converterFactory.registerConverter(newDecimalStorageUnitConverter());

Integration

To use this project just declare the following dependency inside your POM:

<dependencies>
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-model</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-dozer</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY --><!-- EclipseLink ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-eclipselink</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- EclipseLink ONLY --><!-- GSON ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-gson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- GSON ONLY --><!-- Jackson ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jackson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jackson ONLY --><!-- Jakarta ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jakarta</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jakarta ONLY --><!-- MapStruct ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mapstruct</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MapStruct ONLY --><!-- ModelMapper ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-modelmapper</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- ModelMapper ONLY --><!-- MongoDB ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mongodb</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MongoDB ONLY --><!-- Orika ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-orika</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Orika ONLY -->
</dependencies>

Replace ${version.storage-units} with the latest release.

Reference

Originally inspired by Twitters util package.

Alternatives

About

Java library for storage units

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

21 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

309 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Storage-Units Chat

Storage units according to ISO IEC 80000-13:2008 implemented in Java.

Features

  • Immutable, type- and thread-safe object model for storage units
  • Convenience factories to create units
  • Basic arithmetic operators
  • Comparisons and equality checks between units
  • Lossless conversion between all units
  • Human-readable text format, including custom formats
  • Compatible with any java.lang.Number
  • Custom serializers for Jackson, MongoDB, EclipseLink, and others

Available Units

NameSymbolExponentialAbsolute
ByteB20 Byte1 Byte
KibibyteKiB210 Byte1 024 Byte
MebibyteMiB220 Byte1 048 576 Byte
GibibyteGiB230 Byte1 073 741 824 Byte
TebibyteTiB240 Byte1 099 511 627 776 Byte
PebibytePiB250 Byte1 125 899 906 842 624 Byte
ExbibyteEiB260 Byte1 152 921 504 606 846 976 Byte
ZebibyteZiB270 Byte1 180 591 620 717 411 303 424 Byte
YobibyteYiB280 Byte1 208 925 819 614 629 174 706 176 Byte
RobibyteRiB290 Byte1 237 940 039 285 380 274 899 124 224 Byte
QubibyteQiB2100 Byte1 267 650 600 228 229 401 496 703 205 376 Byte
NameSymbolExponentialAbsolute
ByteB100 Byte1 Byte
KilobyteKB103 Byte1 000 Byte
MegabyteMB106 Byte1 000 000 Byte
GigabyteGB109 Byte1 000 000 000 Byte
TerabyteTB1012 Byte1 000 000 000 000 Byte
PetabytePB1015 Byte1 000 000 000 000 000 Byte
ExabyteEB1018 Byte1 000 000 000 000 000 000 Byte
ZettabyteZB1021 Byte1 000 000 000 000 000 000 000 Byte
YottabyteYB1024 Byte1 000 000 000 000 000 000 000 000 Byte
RonnabyteRB1027 Byte1 000 000 000 000 000 000 000 000 000 Byte
QuettabyteQB1030 Byte1 000 000 000 000 000 000 000 000 000 000 Byte

Usage

Factories

Each unit implements a Byte-based static factory method (valueOf(BigInteger) or valueOf(long)) that can be used to represent a given number of bytes in a specific unit. Note that Long.MAX_VALUE == 8 Exabyte, thus use BigInteger if you want to work with anything bigger than a eight Exabyte. When in doubt, always use BigInteger.

// 'long' basedKilobyteunit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"// 'BigInteger' basedKilobyteunit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"

The StorageUnits class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.

Binary Units

// 'long' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)

Decimal Units

// 'long' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(120000) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(1000000) // Megabyte (1.00 MB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)

Additionally high-level factory methods are also available in the StorageUnits class.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Kibibyteunit = kibibyte(1) // 1 024 ByteMebibyteunit = mebibyte(1) // 1 048 576 ByteGibibyteunit = gibibyte(1) // 1 073 741 824 ByteTebibyteunit = tebibyte(1) // 1 099 511 627 776 BytePebibyteunit = pebibyte(1) // 1 125 899 906 842 624 ByteExbibyteunit = exbibyte(1) // 1 152 921 504 606 846 976 ByteZebibyteunit = zebibyte(1) // 1 180 591 620 717 411 303 424 ByteYobibyteunit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 ByteRobibyteunit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 ByteQubibyteunit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 ByteKilobyteunit = kilobyte(1) // 1 000 ByteMegabyteunit = megabyte(1) // 1 000 000 ByteGigabyteunit = gigabyte(1) // 1 000 000 000 ByteTerabyteunit = terabyte(1) // 1 000 000 000 000 BytePetabyteunit = petabyte(1) // 1 000 000 000 000 000 ByteExabyteunit = exabyte(1) // 1 000 000 000 000 000 000 ByteZettabyteunit = zettabyte(1) // 1 000 000 000 000 000 000 000 ByteYottabyteunit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 ByteRonnabyteunit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 ByteQuettabyteunit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte

Add, Subtract, Multiply, Divide

Each unit implements the basic four math operations. All operations retain their original type, e.g. [Kilobyte] + [Megabyte] = [Kilobyte]

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Bytekibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Bytekibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Bytemegabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Bytegigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyteterabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte

Comparison & Equality

Each unit is comparable to each other unit.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kibibyte(1024).compareTo(mebibyte(1)) == 0// truekibibyte(1000).compareTo(mebibyte(1)) == 0// falsepetabyte(3).compareTo(terabyte(3000)) == 0// truemegabyte(1000).equals(gigabyte(1)) // truemegabyte(1024).equals(gigabyte(1)) // falseterabyte(12).equals(gigabyte(12000)) // true

Formatting

Each unit prints a human-readable string, representing the amount of bytes in the given unit using the symbol specified in ISO IEC 80000-13:2008.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
// default pattern '0.00'terabyte(2).toString() // "2.00 TB"gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"// use custom patternkilobyte(212345).toString("0.0") // "212345.0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"// use custom pattern with specific Localekilobyte(212345).toString("0.0", Locale.GERMAN) // "212345,0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000", Locale.GERMAN) // "2.073,689 TiB"// use custom formatFormatcustomFormat = newDecimalFormat("#.00000");
terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"// without creating unit type firstlongnumberOfBytes = 1_000_000_000_000_000L;
formatAsPetabyte(numberOfBytes) // "1.00 PB"formatAsTerabyte(numberOfBytes) // "1000.00 TB"formatAsPebibyte(numberOfBytes) // "0.89 PiB"// use custom patternformatAsTerabyte(numberOfBytes, "#0.#####") // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####") // "0.88818 PiB"// use custom pattern with specific LocaleformatAsTerabyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "0,88818 PiB"// use custom formatformatAsTerabyte(numberOfBytes, customFormat) // "1000.00000 TB"formatAsPebibyte(numberOfBytes, customFormat) // ".88818 PiB"

Conversions

Each unit can be converted to each other unit without loss of information.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Megabyteunit = kilobyte(1000).asMegabyte() // "1.00 MB"Kilobyteunit = gigabyte(12).asKilobyte() // "12000000.00 kB"Gigabyteunit = terabyte(1).asGigabyte() // "1000.00 GB"// convert to best-matchkilobyte(1100).asBestMatchingUnit() // "1.10 MB"kilobyte(1100).asBestMatchingBinaryUnit() // "1.05 MiB"kilobyte(1100).asBestMatchingDecimalUnit() // "1.10 MB"

Each unit can be expressed as a fraction of another unit (precise up to 24 decimal places)

importstaticwtf.metio.storageunits.model.StorageUnits.*;
BigDecimalkilobytes = megabyte(1).inKilobyte() // 1 000BigIntegerbytes = kibibyte(2).inByte() // 2 048BigDecimalterabytes = gigabyte(15).inTerabyte() // 0.015

Serialization/Converters/Mappers

Multiple custom serializers, converters, and mappers are available for all storage units.

Dozer

Use a Dozer converter like this:

importstaticwtf.metio.storageunits.dozer.*;
DozerBeanMapperBuilder.create()
.withCustomConverter(newBigIntegerBinaryStorageUnitConverter())
.withCustomConverter(newBigIntegerDecimalStorageUnitConverter())
.withCustomConverter(newLongBinaryStorageUnitConverter())
.withCustomConverter(newLongDecimalStorageUnitConverter())
.build();

EclipseLink

Use any of the three converters like this:

importstaticwtf.metio.storageunits.eclipselink.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Basic@Converter (
name="binaryConverter",
converterClass=BinaryStorageUnitConverter.class
)
@Convert("binaryConverter")
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Basic@Converter (
name="decimalConverter",
converterClass=DecimalyStorageUnitConverter.class
)
@Convert("decimalConverter")
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

GSON

Use a GSON serializer/deserializer like this:

importstaticwtf.metio.storageunits.gson.*;
newGsonBuilder()
.registerTypeHierarchyAdapter(StorageUnit.class, newStorageUnitSerializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newBinaryStorageUnitDeserializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newDecimalStorageUnitDeserializer())
.create();

Jackson

Use the provided StorageUnitModule like this:

importstaticwtf.metio.storageunits.jackson.*;
ObjectMapperobjectMapper = newObjectMapper();
objectMapper.registerModule(newStorageUnitModule()); // defaults to binary unitsobjectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.BINARY));
objectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.DECIMAL));

Jakarta

Use the provided AttributeConverters like this:

importstaticwtf.metio.storageunits.jakarta.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Convert(converter = BinaryStorageUnitConverter.class)
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Convert(converter = DecimalStorageUnitConverter.class)
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

MapStruct

Use any of the available mappers like this:

importstaticwtf.metio.storageunits.mapstruct.*;
@Mapper( uses = BigIntegerToBinaryUnitMapper.class )
publicinterfaceMovieMapper {
DestinationTypetoDestination(SourceTypesourceValue);
}

ModelMapper

Use any of the available converters like this:

importstaticwtf.metio.storageunits.modelmapper.*;
modelMapper.addConverter(newBigIntegerToBinaryUnitConverter());
modelMapper.addConverter(newBigIntegerToDecimalUnitConverter());
modelMapper.addConverter(newLongToBinaryUnitConverter());
modelMapper.addConverter(newLongToDecimalUnitConverter());
modelMapper.addConverter(newStorageUnitToBigIntegerConverter());

MongoDB

Use any of the three codecs like this:

importstaticwtf.metio.storageunits.mongodb.*;
CodecRegistrybinaryRegistry = CodecRegistries.fromCodecs(newBinaryStorageUnitCodec(), ...);
CodecRegistrydecimalRegistry = CodecRegistries.fromCodecs(newDecimalStorageUnitCodec(), ...);

Orika

Use any of the provided converters like this:

importstaticwtf.metio.storageunits.orika.*;
ConverterFactoryconverterFactory = mapperFactory.getConverterFactory();
converterFactory.registerConverter(newBinaryStorageUnitConverter());
converterFactory.registerConverter(newDecimalStorageUnitConverter());

Integration

To use this project just declare the following dependency inside your POM:

<dependencies>
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-model</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-dozer</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY --><!-- EclipseLink ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-eclipselink</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- EclipseLink ONLY --><!-- GSON ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-gson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- GSON ONLY --><!-- Jackson ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jackson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jackson ONLY --><!-- Jakarta ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jakarta</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jakarta ONLY --><!-- MapStruct ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mapstruct</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MapStruct ONLY --><!-- ModelMapper ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-modelmapper</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- ModelMapper ONLY --><!-- MongoDB ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mongodb</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MongoDB ONLY --><!-- Orika ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-orika</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Orika ONLY -->
</dependencies>

Replace ${version.storage-units} with the latest release.

Reference

Originally inspired by Twitters util package.

Alternatives

About

Java library for storage units

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

21 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Latest commit

History

309 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Storage-Units Chat

Storage units according to ISO IEC 80000-13:2008 implemented in Java.

Features

  • Immutable, type- and thread-safe object model for storage units
  • Convenience factories to create units
  • Basic arithmetic operators
  • Comparisons and equality checks between units
  • Lossless conversion between all units
  • Human-readable text format, including custom formats
  • Compatible with any java.lang.Number
  • Custom serializers for Jackson, MongoDB, EclipseLink, and others

Available Units

NameSymbolExponentialAbsolute
ByteB20 Byte1 Byte
KibibyteKiB210 Byte1 024 Byte
MebibyteMiB220 Byte1 048 576 Byte
GibibyteGiB230 Byte1 073 741 824 Byte
TebibyteTiB240 Byte1 099 511 627 776 Byte
PebibytePiB250 Byte1 125 899 906 842 624 Byte
ExbibyteEiB260 Byte1 152 921 504 606 846 976 Byte
ZebibyteZiB270 Byte1 180 591 620 717 411 303 424 Byte
YobibyteYiB280 Byte1 208 925 819 614 629 174 706 176 Byte
RobibyteRiB290 Byte1 237 940 039 285 380 274 899 124 224 Byte
QubibyteQiB2100 Byte1 267 650 600 228 229 401 496 703 205 376 Byte
NameSymbolExponentialAbsolute
ByteB100 Byte1 Byte
KilobyteKB103 Byte1 000 Byte
MegabyteMB106 Byte1 000 000 Byte
GigabyteGB109 Byte1 000 000 000 Byte
TerabyteTB1012 Byte1 000 000 000 000 Byte
PetabytePB1015 Byte1 000 000 000 000 000 Byte
ExabyteEB1018 Byte1 000 000 000 000 000 000 Byte
ZettabyteZB1021 Byte1 000 000 000 000 000 000 000 Byte
YottabyteYB1024 Byte1 000 000 000 000 000 000 000 000 Byte
RonnabyteRB1027 Byte1 000 000 000 000 000 000 000 000 000 Byte
QuettabyteQB1030 Byte1 000 000 000 000 000 000 000 000 000 000 Byte

Usage

Factories

Each unit implements a Byte-based static factory method (valueOf(BigInteger) or valueOf(long)) that can be used to represent a given number of bytes in a specific unit. Note that Long.MAX_VALUE == 8 Exabyte, thus use BigInteger if you want to work with anything bigger than a eight Exabyte. When in doubt, always use BigInteger.

// 'long' basedKilobyteunit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"// 'BigInteger' basedKilobyteunit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"

The StorageUnits class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.

Binary Units

// 'long' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)

Decimal Units

// 'long' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(120000) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(1000000) // Megabyte (1.00 MB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)

Additionally high-level factory methods are also available in the StorageUnits class.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Kibibyteunit = kibibyte(1) // 1 024 ByteMebibyteunit = mebibyte(1) // 1 048 576 ByteGibibyteunit = gibibyte(1) // 1 073 741 824 ByteTebibyteunit = tebibyte(1) // 1 099 511 627 776 BytePebibyteunit = pebibyte(1) // 1 125 899 906 842 624 ByteExbibyteunit = exbibyte(1) // 1 152 921 504 606 846 976 ByteZebibyteunit = zebibyte(1) // 1 180 591 620 717 411 303 424 ByteYobibyteunit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 ByteRobibyteunit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 ByteQubibyteunit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 ByteKilobyteunit = kilobyte(1) // 1 000 ByteMegabyteunit = megabyte(1) // 1 000 000 ByteGigabyteunit = gigabyte(1) // 1 000 000 000 ByteTerabyteunit = terabyte(1) // 1 000 000 000 000 BytePetabyteunit = petabyte(1) // 1 000 000 000 000 000 ByteExabyteunit = exabyte(1) // 1 000 000 000 000 000 000 ByteZettabyteunit = zettabyte(1) // 1 000 000 000 000 000 000 000 ByteYottabyteunit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 ByteRonnabyteunit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 ByteQuettabyteunit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte

Add, Subtract, Multiply, Divide

Each unit implements the basic four math operations. All operations retain their original type, e.g. [Kilobyte] + [Megabyte] = [Kilobyte]

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Bytekibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Bytekibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Bytemegabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Bytegigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyteterabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte

Comparison & Equality

Each unit is comparable to each other unit.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kibibyte(1024).compareTo(mebibyte(1)) == 0// truekibibyte(1000).compareTo(mebibyte(1)) == 0// falsepetabyte(3).compareTo(terabyte(3000)) == 0// truemegabyte(1000).equals(gigabyte(1)) // truemegabyte(1024).equals(gigabyte(1)) // falseterabyte(12).equals(gigabyte(12000)) // true

Formatting

Each unit prints a human-readable string, representing the amount of bytes in the given unit using the symbol specified in ISO IEC 80000-13:2008.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
// default pattern '0.00'terabyte(2).toString() // "2.00 TB"gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"// use custom patternkilobyte(212345).toString("0.0") // "212345.0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"// use custom pattern with specific Localekilobyte(212345).toString("0.0", Locale.GERMAN) // "212345,0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000", Locale.GERMAN) // "2.073,689 TiB"// use custom formatFormatcustomFormat = newDecimalFormat("#.00000");
terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"// without creating unit type firstlongnumberOfBytes = 1_000_000_000_000_000L;
formatAsPetabyte(numberOfBytes) // "1.00 PB"formatAsTerabyte(numberOfBytes) // "1000.00 TB"formatAsPebibyte(numberOfBytes) // "0.89 PiB"// use custom patternformatAsTerabyte(numberOfBytes, "#0.#####") // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####") // "0.88818 PiB"// use custom pattern with specific LocaleformatAsTerabyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "0,88818 PiB"// use custom formatformatAsTerabyte(numberOfBytes, customFormat) // "1000.00000 TB"formatAsPebibyte(numberOfBytes, customFormat) // ".88818 PiB"

Conversions

Each unit can be converted to each other unit without loss of information.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Megabyteunit = kilobyte(1000).asMegabyte() // "1.00 MB"Kilobyteunit = gigabyte(12).asKilobyte() // "12000000.00 kB"Gigabyteunit = terabyte(1).asGigabyte() // "1000.00 GB"// convert to best-matchkilobyte(1100).asBestMatchingUnit() // "1.10 MB"kilobyte(1100).asBestMatchingBinaryUnit() // "1.05 MiB"kilobyte(1100).asBestMatchingDecimalUnit() // "1.10 MB"

Each unit can be expressed as a fraction of another unit (precise up to 24 decimal places)

importstaticwtf.metio.storageunits.model.StorageUnits.*;
BigDecimalkilobytes = megabyte(1).inKilobyte() // 1 000BigIntegerbytes = kibibyte(2).inByte() // 2 048BigDecimalterabytes = gigabyte(15).inTerabyte() // 0.015

Serialization/Converters/Mappers

Multiple custom serializers, converters, and mappers are available for all storage units.

Dozer

Use a Dozer converter like this:

importstaticwtf.metio.storageunits.dozer.*;
DozerBeanMapperBuilder.create()
.withCustomConverter(newBigIntegerBinaryStorageUnitConverter())
.withCustomConverter(newBigIntegerDecimalStorageUnitConverter())
.withCustomConverter(newLongBinaryStorageUnitConverter())
.withCustomConverter(newLongDecimalStorageUnitConverter())
.build();

EclipseLink

Use any of the three converters like this:

importstaticwtf.metio.storageunits.eclipselink.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Basic@Converter (
name="binaryConverter",
converterClass=BinaryStorageUnitConverter.class
)
@Convert("binaryConverter")
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Basic@Converter (
name="decimalConverter",
converterClass=DecimalyStorageUnitConverter.class
)
@Convert("decimalConverter")
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

GSON

Use a GSON serializer/deserializer like this:

importstaticwtf.metio.storageunits.gson.*;
newGsonBuilder()
.registerTypeHierarchyAdapter(StorageUnit.class, newStorageUnitSerializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newBinaryStorageUnitDeserializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newDecimalStorageUnitDeserializer())
.create();

Jackson

Use the provided StorageUnitModule like this:

importstaticwtf.metio.storageunits.jackson.*;
ObjectMapperobjectMapper = newObjectMapper();
objectMapper.registerModule(newStorageUnitModule()); // defaults to binary unitsobjectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.BINARY));
objectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.DECIMAL));

Jakarta

Use the provided AttributeConverters like this:

importstaticwtf.metio.storageunits.jakarta.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Convert(converter = BinaryStorageUnitConverter.class)
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Convert(converter = DecimalStorageUnitConverter.class)
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

MapStruct

Use any of the available mappers like this:

importstaticwtf.metio.storageunits.mapstruct.*;
@Mapper( uses = BigIntegerToBinaryUnitMapper.class )
publicinterfaceMovieMapper {
DestinationTypetoDestination(SourceTypesourceValue);
}

ModelMapper

Use any of the available converters like this:

importstaticwtf.metio.storageunits.modelmapper.*;
modelMapper.addConverter(newBigIntegerToBinaryUnitConverter());
modelMapper.addConverter(newBigIntegerToDecimalUnitConverter());
modelMapper.addConverter(newLongToBinaryUnitConverter());
modelMapper.addConverter(newLongToDecimalUnitConverter());
modelMapper.addConverter(newStorageUnitToBigIntegerConverter());

MongoDB

Use any of the three codecs like this:

importstaticwtf.metio.storageunits.mongodb.*;
CodecRegistrybinaryRegistry = CodecRegistries.fromCodecs(newBinaryStorageUnitCodec(), ...);
CodecRegistrydecimalRegistry = CodecRegistries.fromCodecs(newDecimalStorageUnitCodec(), ...);

Orika

Use any of the provided converters like this:

importstaticwtf.metio.storageunits.orika.*;
ConverterFactoryconverterFactory = mapperFactory.getConverterFactory();
converterFactory.registerConverter(newBinaryStorageUnitConverter());
converterFactory.registerConverter(newDecimalStorageUnitConverter());

Integration

To use this project just declare the following dependency inside your POM:

<dependencies>
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-model</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-dozer</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY --><!-- EclipseLink ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-eclipselink</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- EclipseLink ONLY --><!-- GSON ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-gson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- GSON ONLY --><!-- Jackson ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jackson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jackson ONLY --><!-- Jakarta ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jakarta</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jakarta ONLY --><!-- MapStruct ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mapstruct</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MapStruct ONLY --><!-- ModelMapper ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-modelmapper</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- ModelMapper ONLY --><!-- MongoDB ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mongodb</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MongoDB ONLY --><!-- Orika ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-orika</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Orika ONLY -->
</dependencies>

Replace ${version.storage-units} with the latest release.

Reference

Originally inspired by Twitters util package.

Alternatives

About

Java library for storage units

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

21 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

309 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Storage-Units Chat

Storage units according to ISO IEC 80000-13:2008 implemented in Java.

Features

  • Immutable, type- and thread-safe object model for storage units
  • Convenience factories to create units
  • Basic arithmetic operators
  • Comparisons and equality checks between units
  • Lossless conversion between all units
  • Human-readable text format, including custom formats
  • Compatible with any java.lang.Number
  • Custom serializers for Jackson, MongoDB, EclipseLink, and others

Available Units

NameSymbolExponentialAbsolute
ByteB20 Byte1 Byte
KibibyteKiB210 Byte1 024 Byte
MebibyteMiB220 Byte1 048 576 Byte
GibibyteGiB230 Byte1 073 741 824 Byte
TebibyteTiB240 Byte1 099 511 627 776 Byte
PebibytePiB250 Byte1 125 899 906 842 624 Byte
ExbibyteEiB260 Byte1 152 921 504 606 846 976 Byte
ZebibyteZiB270 Byte1 180 591 620 717 411 303 424 Byte
YobibyteYiB280 Byte1 208 925 819 614 629 174 706 176 Byte
RobibyteRiB290 Byte1 237 940 039 285 380 274 899 124 224 Byte
QubibyteQiB2100 Byte1 267 650 600 228 229 401 496 703 205 376 Byte
NameSymbolExponentialAbsolute
ByteB100 Byte1 Byte
KilobyteKB103 Byte1 000 Byte
MegabyteMB106 Byte1 000 000 Byte
GigabyteGB109 Byte1 000 000 000 Byte
TerabyteTB1012 Byte1 000 000 000 000 Byte
PetabytePB1015 Byte1 000 000 000 000 000 Byte
ExabyteEB1018 Byte1 000 000 000 000 000 000 Byte
ZettabyteZB1021 Byte1 000 000 000 000 000 000 000 Byte
YottabyteYB1024 Byte1 000 000 000 000 000 000 000 000 Byte
RonnabyteRB1027 Byte1 000 000 000 000 000 000 000 000 000 Byte
QuettabyteQB1030 Byte1 000 000 000 000 000 000 000 000 000 000 Byte

Usage

Factories

Each unit implements a Byte-based static factory method (valueOf(BigInteger) or valueOf(long)) that can be used to represent a given number of bytes in a specific unit. Note that Long.MAX_VALUE == 8 Exabyte, thus use BigInteger if you want to work with anything bigger than a eight Exabyte. When in doubt, always use BigInteger.

// 'long' basedKilobyteunit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"// 'BigInteger' basedKilobyteunit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"

The StorageUnits class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.

Binary Units

// 'long' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)

Decimal Units

// 'long' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(120000) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(1000000) // Megabyte (1.00 MB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)

Additionally high-level factory methods are also available in the StorageUnits class.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Kibibyteunit = kibibyte(1) // 1 024 ByteMebibyteunit = mebibyte(1) // 1 048 576 ByteGibibyteunit = gibibyte(1) // 1 073 741 824 ByteTebibyteunit = tebibyte(1) // 1 099 511 627 776 BytePebibyteunit = pebibyte(1) // 1 125 899 906 842 624 ByteExbibyteunit = exbibyte(1) // 1 152 921 504 606 846 976 ByteZebibyteunit = zebibyte(1) // 1 180 591 620 717 411 303 424 ByteYobibyteunit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 ByteRobibyteunit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 ByteQubibyteunit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 ByteKilobyteunit = kilobyte(1) // 1 000 ByteMegabyteunit = megabyte(1) // 1 000 000 ByteGigabyteunit = gigabyte(1) // 1 000 000 000 ByteTerabyteunit = terabyte(1) // 1 000 000 000 000 BytePetabyteunit = petabyte(1) // 1 000 000 000 000 000 ByteExabyteunit = exabyte(1) // 1 000 000 000 000 000 000 ByteZettabyteunit = zettabyte(1) // 1 000 000 000 000 000 000 000 ByteYottabyteunit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 ByteRonnabyteunit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 ByteQuettabyteunit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte

Add, Subtract, Multiply, Divide

Each unit implements the basic four math operations. All operations retain their original type, e.g. [Kilobyte] + [Megabyte] = [Kilobyte]

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Bytekibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Bytekibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Bytemegabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Bytegigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyteterabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte

Comparison & Equality

Each unit is comparable to each other unit.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kibibyte(1024).compareTo(mebibyte(1)) == 0// truekibibyte(1000).compareTo(mebibyte(1)) == 0// falsepetabyte(3).compareTo(terabyte(3000)) == 0// truemegabyte(1000).equals(gigabyte(1)) // truemegabyte(1024).equals(gigabyte(1)) // falseterabyte(12).equals(gigabyte(12000)) // true

Formatting

Each unit prints a human-readable string, representing the amount of bytes in the given unit using the symbol specified in ISO IEC 80000-13:2008.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
// default pattern '0.00'terabyte(2).toString() // "2.00 TB"gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"// use custom patternkilobyte(212345).toString("0.0") // "212345.0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"// use custom pattern with specific Localekilobyte(212345).toString("0.0", Locale.GERMAN) // "212345,0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000", Locale.GERMAN) // "2.073,689 TiB"// use custom formatFormatcustomFormat = newDecimalFormat("#.00000");
terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"// without creating unit type firstlongnumberOfBytes = 1_000_000_000_000_000L;
formatAsPetabyte(numberOfBytes) // "1.00 PB"formatAsTerabyte(numberOfBytes) // "1000.00 TB"formatAsPebibyte(numberOfBytes) // "0.89 PiB"// use custom patternformatAsTerabyte(numberOfBytes, "#0.#####") // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####") // "0.88818 PiB"// use custom pattern with specific LocaleformatAsTerabyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "0,88818 PiB"// use custom formatformatAsTerabyte(numberOfBytes, customFormat) // "1000.00000 TB"formatAsPebibyte(numberOfBytes, customFormat) // ".88818 PiB"

Conversions

Each unit can be converted to each other unit without loss of information.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Megabyteunit = kilobyte(1000).asMegabyte() // "1.00 MB"Kilobyteunit = gigabyte(12).asKilobyte() // "12000000.00 kB"Gigabyteunit = terabyte(1).asGigabyte() // "1000.00 GB"// convert to best-matchkilobyte(1100).asBestMatchingUnit() // "1.10 MB"kilobyte(1100).asBestMatchingBinaryUnit() // "1.05 MiB"kilobyte(1100).asBestMatchingDecimalUnit() // "1.10 MB"

Each unit can be expressed as a fraction of another unit (precise up to 24 decimal places)

importstaticwtf.metio.storageunits.model.StorageUnits.*;
BigDecimalkilobytes = megabyte(1).inKilobyte() // 1 000BigIntegerbytes = kibibyte(2).inByte() // 2 048BigDecimalterabytes = gigabyte(15).inTerabyte() // 0.015

Serialization/Converters/Mappers

Multiple custom serializers, converters, and mappers are available for all storage units.

Dozer

Use a Dozer converter like this:

importstaticwtf.metio.storageunits.dozer.*;
DozerBeanMapperBuilder.create()
.withCustomConverter(newBigIntegerBinaryStorageUnitConverter())
.withCustomConverter(newBigIntegerDecimalStorageUnitConverter())
.withCustomConverter(newLongBinaryStorageUnitConverter())
.withCustomConverter(newLongDecimalStorageUnitConverter())
.build();

EclipseLink

Use any of the three converters like this:

importstaticwtf.metio.storageunits.eclipselink.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Basic@Converter (
name="binaryConverter",
converterClass=BinaryStorageUnitConverter.class
)
@Convert("binaryConverter")
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Basic@Converter (
name="decimalConverter",
converterClass=DecimalyStorageUnitConverter.class
)
@Convert("decimalConverter")
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

GSON

Use a GSON serializer/deserializer like this:

importstaticwtf.metio.storageunits.gson.*;
newGsonBuilder()
.registerTypeHierarchyAdapter(StorageUnit.class, newStorageUnitSerializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newBinaryStorageUnitDeserializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newDecimalStorageUnitDeserializer())
.create();

Jackson

Use the provided StorageUnitModule like this:

importstaticwtf.metio.storageunits.jackson.*;
ObjectMapperobjectMapper = newObjectMapper();
objectMapper.registerModule(newStorageUnitModule()); // defaults to binary unitsobjectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.BINARY));
objectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.DECIMAL));

Jakarta

Use the provided AttributeConverters like this:

importstaticwtf.metio.storageunits.jakarta.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Convert(converter = BinaryStorageUnitConverter.class)
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Convert(converter = DecimalStorageUnitConverter.class)
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

MapStruct

Use any of the available mappers like this:

importstaticwtf.metio.storageunits.mapstruct.*;
@Mapper( uses = BigIntegerToBinaryUnitMapper.class )
publicinterfaceMovieMapper {
DestinationTypetoDestination(SourceTypesourceValue);
}

ModelMapper

Use any of the available converters like this:

importstaticwtf.metio.storageunits.modelmapper.*;
modelMapper.addConverter(newBigIntegerToBinaryUnitConverter());
modelMapper.addConverter(newBigIntegerToDecimalUnitConverter());
modelMapper.addConverter(newLongToBinaryUnitConverter());
modelMapper.addConverter(newLongToDecimalUnitConverter());
modelMapper.addConverter(newStorageUnitToBigIntegerConverter());

MongoDB

Use any of the three codecs like this:

importstaticwtf.metio.storageunits.mongodb.*;
CodecRegistrybinaryRegistry = CodecRegistries.fromCodecs(newBinaryStorageUnitCodec(), ...);
CodecRegistrydecimalRegistry = CodecRegistries.fromCodecs(newDecimalStorageUnitCodec(), ...);

Orika

Use any of the provided converters like this:

importstaticwtf.metio.storageunits.orika.*;
ConverterFactoryconverterFactory = mapperFactory.getConverterFactory();
converterFactory.registerConverter(newBinaryStorageUnitConverter());
converterFactory.registerConverter(newDecimalStorageUnitConverter());

Integration

To use this project just declare the following dependency inside your POM:

<dependencies>
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-model</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-dozer</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY --><!-- EclipseLink ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-eclipselink</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- EclipseLink ONLY --><!-- GSON ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-gson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- GSON ONLY --><!-- Jackson ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jackson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jackson ONLY --><!-- Jakarta ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jakarta</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jakarta ONLY --><!-- MapStruct ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mapstruct</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MapStruct ONLY --><!-- ModelMapper ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-modelmapper</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- ModelMapper ONLY --><!-- MongoDB ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mongodb</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MongoDB ONLY --><!-- Orika ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-orika</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Orika ONLY -->
</dependencies>

Replace ${version.storage-units} with the latest release.

Reference

Originally inspired by Twitters util package.

Alternatives

About

Java library for storage units

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

21 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

309 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Storage-Units Chat

Storage units according to ISO IEC 80000-13:2008 implemented in Java.

Features

  • Immutable, type- and thread-safe object model for storage units
  • Convenience factories to create units
  • Basic arithmetic operators
  • Comparisons and equality checks between units
  • Lossless conversion between all units
  • Human-readable text format, including custom formats
  • Compatible with any java.lang.Number
  • Custom serializers for Jackson, MongoDB, EclipseLink, and others

Available Units

NameSymbolExponentialAbsolute
ByteB20 Byte1 Byte
KibibyteKiB210 Byte1 024 Byte
MebibyteMiB220 Byte1 048 576 Byte
GibibyteGiB230 Byte1 073 741 824 Byte
TebibyteTiB240 Byte1 099 511 627 776 Byte
PebibytePiB250 Byte1 125 899 906 842 624 Byte
ExbibyteEiB260 Byte1 152 921 504 606 846 976 Byte
ZebibyteZiB270 Byte1 180 591 620 717 411 303 424 Byte
YobibyteYiB280 Byte1 208 925 819 614 629 174 706 176 Byte
RobibyteRiB290 Byte1 237 940 039 285 380 274 899 124 224 Byte
QubibyteQiB2100 Byte1 267 650 600 228 229 401 496 703 205 376 Byte
NameSymbolExponentialAbsolute
ByteB100 Byte1 Byte
KilobyteKB103 Byte1 000 Byte
MegabyteMB106 Byte1 000 000 Byte
GigabyteGB109 Byte1 000 000 000 Byte
TerabyteTB1012 Byte1 000 000 000 000 Byte
PetabytePB1015 Byte1 000 000 000 000 000 Byte
ExabyteEB1018 Byte1 000 000 000 000 000 000 Byte
ZettabyteZB1021 Byte1 000 000 000 000 000 000 000 Byte
YottabyteYB1024 Byte1 000 000 000 000 000 000 000 000 Byte
RonnabyteRB1027 Byte1 000 000 000 000 000 000 000 000 000 Byte
QuettabyteQB1030 Byte1 000 000 000 000 000 000 000 000 000 000 Byte

Usage

Factories

Each unit implements a Byte-based static factory method (valueOf(BigInteger) or valueOf(long)) that can be used to represent a given number of bytes in a specific unit. Note that Long.MAX_VALUE == 8 Exabyte, thus use BigInteger if you want to work with anything bigger than a eight Exabyte. When in doubt, always use BigInteger.

// 'long' basedKilobyteunit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"// 'BigInteger' basedKilobyteunit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"

The StorageUnits class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.

Binary Units

// 'long' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)

Decimal Units

// 'long' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(120000) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(1000000) // Megabyte (1.00 MB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)

Additionally high-level factory methods are also available in the StorageUnits class.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Kibibyteunit = kibibyte(1) // 1 024 ByteMebibyteunit = mebibyte(1) // 1 048 576 ByteGibibyteunit = gibibyte(1) // 1 073 741 824 ByteTebibyteunit = tebibyte(1) // 1 099 511 627 776 BytePebibyteunit = pebibyte(1) // 1 125 899 906 842 624 ByteExbibyteunit = exbibyte(1) // 1 152 921 504 606 846 976 ByteZebibyteunit = zebibyte(1) // 1 180 591 620 717 411 303 424 ByteYobibyteunit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 ByteRobibyteunit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 ByteQubibyteunit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 ByteKilobyteunit = kilobyte(1) // 1 000 ByteMegabyteunit = megabyte(1) // 1 000 000 ByteGigabyteunit = gigabyte(1) // 1 000 000 000 ByteTerabyteunit = terabyte(1) // 1 000 000 000 000 BytePetabyteunit = petabyte(1) // 1 000 000 000 000 000 ByteExabyteunit = exabyte(1) // 1 000 000 000 000 000 000 ByteZettabyteunit = zettabyte(1) // 1 000 000 000 000 000 000 000 ByteYottabyteunit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 ByteRonnabyteunit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 ByteQuettabyteunit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte

Add, Subtract, Multiply, Divide

Each unit implements the basic four math operations. All operations retain their original type, e.g. [Kilobyte] + [Megabyte] = [Kilobyte]

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Bytekibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Bytekibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Bytemegabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Bytegigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyteterabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte

Comparison & Equality

Each unit is comparable to each other unit.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kibibyte(1024).compareTo(mebibyte(1)) == 0// truekibibyte(1000).compareTo(mebibyte(1)) == 0// falsepetabyte(3).compareTo(terabyte(3000)) == 0// truemegabyte(1000).equals(gigabyte(1)) // truemegabyte(1024).equals(gigabyte(1)) // falseterabyte(12).equals(gigabyte(12000)) // true

Formatting

Each unit prints a human-readable string, representing the amount of bytes in the given unit using the symbol specified in ISO IEC 80000-13:2008.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
// default pattern '0.00'terabyte(2).toString() // "2.00 TB"gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"// use custom patternkilobyte(212345).toString("0.0") // "212345.0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"// use custom pattern with specific Localekilobyte(212345).toString("0.0", Locale.GERMAN) // "212345,0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000", Locale.GERMAN) // "2.073,689 TiB"// use custom formatFormatcustomFormat = newDecimalFormat("#.00000");
terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"// without creating unit type firstlongnumberOfBytes = 1_000_000_000_000_000L;
formatAsPetabyte(numberOfBytes) // "1.00 PB"formatAsTerabyte(numberOfBytes) // "1000.00 TB"formatAsPebibyte(numberOfBytes) // "0.89 PiB"// use custom patternformatAsTerabyte(numberOfBytes, "#0.#####") // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####") // "0.88818 PiB"// use custom pattern with specific LocaleformatAsTerabyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "0,88818 PiB"// use custom formatformatAsTerabyte(numberOfBytes, customFormat) // "1000.00000 TB"formatAsPebibyte(numberOfBytes, customFormat) // ".88818 PiB"

Conversions

Each unit can be converted to each other unit without loss of information.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Megabyteunit = kilobyte(1000).asMegabyte() // "1.00 MB"Kilobyteunit = gigabyte(12).asKilobyte() // "12000000.00 kB"Gigabyteunit = terabyte(1).asGigabyte() // "1000.00 GB"// convert to best-matchkilobyte(1100).asBestMatchingUnit() // "1.10 MB"kilobyte(1100).asBestMatchingBinaryUnit() // "1.05 MiB"kilobyte(1100).asBestMatchingDecimalUnit() // "1.10 MB"

Each unit can be expressed as a fraction of another unit (precise up to 24 decimal places)

importstaticwtf.metio.storageunits.model.StorageUnits.*;
BigDecimalkilobytes = megabyte(1).inKilobyte() // 1 000BigIntegerbytes = kibibyte(2).inByte() // 2 048BigDecimalterabytes = gigabyte(15).inTerabyte() // 0.015

Serialization/Converters/Mappers

Multiple custom serializers, converters, and mappers are available for all storage units.

Dozer

Use a Dozer converter like this:

importstaticwtf.metio.storageunits.dozer.*;
DozerBeanMapperBuilder.create()
.withCustomConverter(newBigIntegerBinaryStorageUnitConverter())
.withCustomConverter(newBigIntegerDecimalStorageUnitConverter())
.withCustomConverter(newLongBinaryStorageUnitConverter())
.withCustomConverter(newLongDecimalStorageUnitConverter())
.build();

EclipseLink

Use any of the three converters like this:

importstaticwtf.metio.storageunits.eclipselink.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Basic@Converter (
name="binaryConverter",
converterClass=BinaryStorageUnitConverter.class
)
@Convert("binaryConverter")
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Basic@Converter (
name="decimalConverter",
converterClass=DecimalyStorageUnitConverter.class
)
@Convert("decimalConverter")
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

GSON

Use a GSON serializer/deserializer like this:

importstaticwtf.metio.storageunits.gson.*;
newGsonBuilder()
.registerTypeHierarchyAdapter(StorageUnit.class, newStorageUnitSerializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newBinaryStorageUnitDeserializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newDecimalStorageUnitDeserializer())
.create();

Jackson

Use the provided StorageUnitModule like this:

importstaticwtf.metio.storageunits.jackson.*;
ObjectMapperobjectMapper = newObjectMapper();
objectMapper.registerModule(newStorageUnitModule()); // defaults to binary unitsobjectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.BINARY));
objectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.DECIMAL));

Jakarta

Use the provided AttributeConverters like this:

importstaticwtf.metio.storageunits.jakarta.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Convert(converter = BinaryStorageUnitConverter.class)
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Convert(converter = DecimalStorageUnitConverter.class)
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

MapStruct

Use any of the available mappers like this:

importstaticwtf.metio.storageunits.mapstruct.*;
@Mapper( uses = BigIntegerToBinaryUnitMapper.class )
publicinterfaceMovieMapper {
DestinationTypetoDestination(SourceTypesourceValue);
}

ModelMapper

Use any of the available converters like this:

importstaticwtf.metio.storageunits.modelmapper.*;
modelMapper.addConverter(newBigIntegerToBinaryUnitConverter());
modelMapper.addConverter(newBigIntegerToDecimalUnitConverter());
modelMapper.addConverter(newLongToBinaryUnitConverter());
modelMapper.addConverter(newLongToDecimalUnitConverter());
modelMapper.addConverter(newStorageUnitToBigIntegerConverter());

MongoDB

Use any of the three codecs like this:

importstaticwtf.metio.storageunits.mongodb.*;
CodecRegistrybinaryRegistry = CodecRegistries.fromCodecs(newBinaryStorageUnitCodec(), ...);
CodecRegistrydecimalRegistry = CodecRegistries.fromCodecs(newDecimalStorageUnitCodec(), ...);

Orika

Use any of the provided converters like this:

importstaticwtf.metio.storageunits.orika.*;
ConverterFactoryconverterFactory = mapperFactory.getConverterFactory();
converterFactory.registerConverter(newBinaryStorageUnitConverter());
converterFactory.registerConverter(newDecimalStorageUnitConverter());

Integration

To use this project just declare the following dependency inside your POM:

<dependencies>
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-model</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-dozer</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY --><!-- EclipseLink ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-eclipselink</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- EclipseLink ONLY --><!-- GSON ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-gson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- GSON ONLY --><!-- Jackson ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jackson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jackson ONLY --><!-- Jakarta ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jakarta</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jakarta ONLY --><!-- MapStruct ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mapstruct</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MapStruct ONLY --><!-- ModelMapper ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-modelmapper</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- ModelMapper ONLY --><!-- MongoDB ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mongodb</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MongoDB ONLY --><!-- Orika ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-orika</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Orika ONLY -->
</dependencies>

Replace ${version.storage-units} with the latest release.

Reference

Originally inspired by Twitters util package.

Alternatives

About

Java library for storage units

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

21 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Latest commit

History

309 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Storage-Units Chat

Storage units according to ISO IEC 80000-13:2008 implemented in Java.

Features

  • Immutable, type- and thread-safe object model for storage units
  • Convenience factories to create units
  • Basic arithmetic operators
  • Comparisons and equality checks between units
  • Lossless conversion between all units
  • Human-readable text format, including custom formats
  • Compatible with any java.lang.Number
  • Custom serializers for Jackson, MongoDB, EclipseLink, and others

Available Units

NameSymbolExponentialAbsolute
ByteB20 Byte1 Byte
KibibyteKiB210 Byte1 024 Byte
MebibyteMiB220 Byte1 048 576 Byte
GibibyteGiB230 Byte1 073 741 824 Byte
TebibyteTiB240 Byte1 099 511 627 776 Byte
PebibytePiB250 Byte1 125 899 906 842 624 Byte
ExbibyteEiB260 Byte1 152 921 504 606 846 976 Byte
ZebibyteZiB270 Byte1 180 591 620 717 411 303 424 Byte
YobibyteYiB280 Byte1 208 925 819 614 629 174 706 176 Byte
RobibyteRiB290 Byte1 237 940 039 285 380 274 899 124 224 Byte
QubibyteQiB2100 Byte1 267 650 600 228 229 401 496 703 205 376 Byte
NameSymbolExponentialAbsolute
ByteB100 Byte1 Byte
KilobyteKB103 Byte1 000 Byte
MegabyteMB106 Byte1 000 000 Byte
GigabyteGB109 Byte1 000 000 000 Byte
TerabyteTB1012 Byte1 000 000 000 000 Byte
PetabytePB1015 Byte1 000 000 000 000 000 Byte
ExabyteEB1018 Byte1 000 000 000 000 000 000 Byte
ZettabyteZB1021 Byte1 000 000 000 000 000 000 000 Byte
YottabyteYB1024 Byte1 000 000 000 000 000 000 000 000 Byte
RonnabyteRB1027 Byte1 000 000 000 000 000 000 000 000 000 Byte
QuettabyteQB1030 Byte1 000 000 000 000 000 000 000 000 000 000 Byte

Usage

Factories

Each unit implements a Byte-based static factory method (valueOf(BigInteger) or valueOf(long)) that can be used to represent a given number of bytes in a specific unit. Note that Long.MAX_VALUE == 8 Exabyte, thus use BigInteger if you want to work with anything bigger than a eight Exabyte. When in doubt, always use BigInteger.

// 'long' basedKilobyteunit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"// 'BigInteger' basedKilobyteunit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"Kibibyteunit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"Megabyteunit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"Mebibyteunit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"

The StorageUnits class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.

Binary Units

// 'long' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(256) // Kibibyte (0.25 KiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(1048576) // Mebibyte (1.00 MiB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(256)) // Kibibyte (0.25 MiB)StorageUnit<?> unit = StorageUnits.binaryValueOf(BigInteger.valueOf(1048576)) // Mebibyte (1.00 MiB)

Decimal Units

// 'long' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(120000) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(1000000) // Megabyte (1.00 MB)// 'BigInteger' basedStorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(120000)) // Kilobyte (120.00 kB)StorageUnit<?> unit = StorageUnits.decimalValueOf(BigInteger.valueOf(1000000)) // Megabyte (1.00 MB)

Additionally high-level factory methods are also available in the StorageUnits class.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Kibibyteunit = kibibyte(1) // 1 024 ByteMebibyteunit = mebibyte(1) // 1 048 576 ByteGibibyteunit = gibibyte(1) // 1 073 741 824 ByteTebibyteunit = tebibyte(1) // 1 099 511 627 776 BytePebibyteunit = pebibyte(1) // 1 125 899 906 842 624 ByteExbibyteunit = exbibyte(1) // 1 152 921 504 606 846 976 ByteZebibyteunit = zebibyte(1) // 1 180 591 620 717 411 303 424 ByteYobibyteunit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 ByteRobibyteunit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 ByteQubibyteunit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 ByteKilobyteunit = kilobyte(1) // 1 000 ByteMegabyteunit = megabyte(1) // 1 000 000 ByteGigabyteunit = gigabyte(1) // 1 000 000 000 ByteTerabyteunit = terabyte(1) // 1 000 000 000 000 BytePetabyteunit = petabyte(1) // 1 000 000 000 000 000 ByteExabyteunit = exabyte(1) // 1 000 000 000 000 000 000 ByteZettabyteunit = zettabyte(1) // 1 000 000 000 000 000 000 000 ByteYottabyteunit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 ByteRonnabyteunit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 ByteQuettabyteunit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte

Add, Subtract, Multiply, Divide

Each unit implements the basic four math operations. All operations retain their original type, e.g. [Kilobyte] + [Megabyte] = [Kilobyte]

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kilobyte(4).add(kilobyte(8)) // 4 Kilobyte + 8 Kilobyte = 12 Kilobyte = 12 000 Bytekibibyte(1).add(1024) // 1 Kibibyte + 1 024 Byte = 2 Kibibyte = 2 048 Bytekibibyte(1).subtract(24) // 1 024 Byte - 24 Byte = 1 000 Bytemegabyte(5).subtract(kilobyte(500)) // 5 Megabyte - 500 Kilobyte = 4.5 Megabyte = 4 500 Kilobyte = 4 500 000 Bytegigabyte(1).multiply(5) // 1 Gigabyte times 5 = 5 Gigabyteterabyte(1).divide(5) // 1 Terabyte divided by 5 = 0.2 Terabyte = 200 Gigabyte

Comparison & Equality

Each unit is comparable to each other unit.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
kibibyte(1024).compareTo(mebibyte(1)) == 0// truekibibyte(1000).compareTo(mebibyte(1)) == 0// falsepetabyte(3).compareTo(terabyte(3000)) == 0// truemegabyte(1000).equals(gigabyte(1)) // truemegabyte(1024).equals(gigabyte(1)) // falseterabyte(12).equals(gigabyte(12000)) // true

Formatting

Each unit prints a human-readable string, representing the amount of bytes in the given unit using the symbol specified in ISO IEC 80000-13:2008.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
// default pattern '0.00'terabyte(2).toString() // "2.00 TB"gigabyte(1).add(megabyte(200)).toString() // "1.20 GB"petabyte(1).subtract(terabyte(250)).toString() // "0.75 PB"// use custom patternkilobyte(212345).toString("0.0") // "212345.0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000") // "2,073.689 TiB"kilobyte(120).asMegabyte().add(gigabyte(1)).toString("#,##0.00000") // "1,000.12000 MB"// use custom pattern with specific Localekilobyte(212345).toString("0.0", Locale.GERMAN) // "212345,0 kB"gibibyte(2123458).asTebibyte().toString("#,###.000", Locale.GERMAN) // "2.073,689 TiB"// use custom formatFormatcustomFormat = newDecimalFormat("#.00000");
terabyte(4).asTebibyte().toString(customFormat) // "3.63798 TiB"// without creating unit type firstlongnumberOfBytes = 1_000_000_000_000_000L;
formatAsPetabyte(numberOfBytes) // "1.00 PB"formatAsTerabyte(numberOfBytes) // "1000.00 TB"formatAsPebibyte(numberOfBytes) // "0.89 PiB"// use custom patternformatAsTerabyte(numberOfBytes, "#0.#####") // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####") // "0.88818 PiB"// use custom pattern with specific LocaleformatAsTerabyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "1000 TB"formatAsPebibyte(numberOfBytes, "#0.#####", Locale.GERMAN) // "0,88818 PiB"// use custom formatformatAsTerabyte(numberOfBytes, customFormat) // "1000.00000 TB"formatAsPebibyte(numberOfBytes, customFormat) // ".88818 PiB"

Conversions

Each unit can be converted to each other unit without loss of information.

importstaticwtf.metio.storageunits.model.StorageUnits.*;
Megabyteunit = kilobyte(1000).asMegabyte() // "1.00 MB"Kilobyteunit = gigabyte(12).asKilobyte() // "12000000.00 kB"Gigabyteunit = terabyte(1).asGigabyte() // "1000.00 GB"// convert to best-matchkilobyte(1100).asBestMatchingUnit() // "1.10 MB"kilobyte(1100).asBestMatchingBinaryUnit() // "1.05 MiB"kilobyte(1100).asBestMatchingDecimalUnit() // "1.10 MB"

Each unit can be expressed as a fraction of another unit (precise up to 24 decimal places)

importstaticwtf.metio.storageunits.model.StorageUnits.*;
BigDecimalkilobytes = megabyte(1).inKilobyte() // 1 000BigIntegerbytes = kibibyte(2).inByte() // 2 048BigDecimalterabytes = gigabyte(15).inTerabyte() // 0.015

Serialization/Converters/Mappers

Multiple custom serializers, converters, and mappers are available for all storage units.

Dozer

Use a Dozer converter like this:

importstaticwtf.metio.storageunits.dozer.*;
DozerBeanMapperBuilder.create()
.withCustomConverter(newBigIntegerBinaryStorageUnitConverter())
.withCustomConverter(newBigIntegerDecimalStorageUnitConverter())
.withCustomConverter(newLongBinaryStorageUnitConverter())
.withCustomConverter(newLongDecimalStorageUnitConverter())
.build();

EclipseLink

Use any of the three converters like this:

importstaticwtf.metio.storageunits.eclipselink.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Basic@Converter (
name="binaryConverter",
converterClass=BinaryStorageUnitConverter.class
)
@Convert("binaryConverter")
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Basic@Converter (
name="decimalConverter",
converterClass=DecimalyStorageUnitConverter.class
)
@Convert("decimalConverter")
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

GSON

Use a GSON serializer/deserializer like this:

importstaticwtf.metio.storageunits.gson.*;
newGsonBuilder()
.registerTypeHierarchyAdapter(StorageUnit.class, newStorageUnitSerializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newBinaryStorageUnitDeserializer())
.registerTypeHierarchyAdapter(StorageUnit.class, newDecimalStorageUnitDeserializer())
.create();

Jackson

Use the provided StorageUnitModule like this:

importstaticwtf.metio.storageunits.jackson.*;
ObjectMapperobjectMapper = newObjectMapper();
objectMapper.registerModule(newStorageUnitModule()); // defaults to binary unitsobjectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.BINARY));
objectMapper.registerModule(newStorageUnitModule(StorageUnitModule.PreferredUnitType.DECIMAL));

Jakarta

Use the provided AttributeConverters like this:

importstaticwtf.metio.storageunits.jakarta.*;
@EntitypublicclassHardDiskimplementsSerializable {
@Convert(converter = BinaryStorageUnitConverter.class)
publicStorageUnit<?> getFreeSize() {
returnfreeSize;
}
@Convert(converter = DecimalStorageUnitConverter.class)
publicStorageUnit<?> getTotalSize() {
returntotalSize;
}
}

MapStruct

Use any of the available mappers like this:

importstaticwtf.metio.storageunits.mapstruct.*;
@Mapper( uses = BigIntegerToBinaryUnitMapper.class )
publicinterfaceMovieMapper {
DestinationTypetoDestination(SourceTypesourceValue);
}

ModelMapper

Use any of the available converters like this:

importstaticwtf.metio.storageunits.modelmapper.*;
modelMapper.addConverter(newBigIntegerToBinaryUnitConverter());
modelMapper.addConverter(newBigIntegerToDecimalUnitConverter());
modelMapper.addConverter(newLongToBinaryUnitConverter());
modelMapper.addConverter(newLongToDecimalUnitConverter());
modelMapper.addConverter(newStorageUnitToBigIntegerConverter());

MongoDB

Use any of the three codecs like this:

importstaticwtf.metio.storageunits.mongodb.*;
CodecRegistrybinaryRegistry = CodecRegistries.fromCodecs(newBinaryStorageUnitCodec(), ...);
CodecRegistrydecimalRegistry = CodecRegistries.fromCodecs(newDecimalStorageUnitCodec(), ...);

Orika

Use any of the provided converters like this:

importstaticwtf.metio.storageunits.orika.*;
ConverterFactoryconverterFactory = mapperFactory.getConverterFactory();
converterFactory.registerConverter(newBinaryStorageUnitConverter());
converterFactory.registerConverter(newDecimalStorageUnitConverter());

Integration

To use this project just declare the following dependency inside your POM:

<dependencies>
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-model</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-dozer</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Dozer ONLY --><!-- EclipseLink ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-eclipselink</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- EclipseLink ONLY --><!-- GSON ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-gson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- GSON ONLY --><!-- Jackson ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jackson</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jackson ONLY --><!-- Jakarta ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-jakarta</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Jakarta ONLY --><!-- MapStruct ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mapstruct</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MapStruct ONLY --><!-- ModelMapper ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-modelmapper</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- ModelMapper ONLY --><!-- MongoDB ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-mongodb</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- MongoDB ONLY --><!-- Orika ONLY -->
<dependency>
<groupId>wtf.metio.storage-units</groupId>
<artifactId>storage-units-orika</artifactId>
<version>${version.storage-units}</version>
</dependency>
<!-- Orika ONLY -->
</dependencies>

Replace ${version.storage-units} with the latest release.

Reference

Originally inspired by Twitters util package.

Alternatives

About

Java library for storage units

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

21 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages