Skip to content

Commit

Permalink
add new SI prefixes (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Hoß <seb@hoß.de>
  • Loading branch information
sebhoss and Sebastian Hoß authored Nov 18, 2022
1 parent 51a18ac commit 8890350
Show file tree
Hide file tree
Showing 21 changed files with 1,398 additions and 137 deletions.
90 changes: 49 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,37 @@ Storage units according to ISO IEC 80000-13:2008 implemented in Java.
* Lossless conversion between all units
* Human-readable text format, including custom formats
* Compatible with any `java.lang.Number`
* Custom serializers for Jackson, MongoDB & EclipseLink
* Custom serializers for Jackson, MongoDB, EclipseLink, and others

### Available Units

| Name | Symbol | Exponential | Absolute |
|----------|--------|---------------------|----------------------------------------|
| Byte | B | 2<sup>0</sup> Byte | 1 Byte |
| Kibibyte | KiB | 2<sup>10</sup> Byte | 1 024 Byte |
| Mebibyte | MiB | 2<sup>20</sup> Byte | 1 048 576 Byte |
| Gibibyte | GiB | 2<sup>30</sup> Byte | 1 073 741 824 Byte |
| Tebibyte | TiB | 2<sup>40</sup> Byte | 1 099 511 627 776 Byte |
| Pebibyte | PiB | 2<sup>50</sup> Byte | 1 125 899 906 842 624 Byte |
| Exbibyte | EiB | 2<sup>60</sup> Byte | 1 152 921 504 606 846 976 Byte |
| Zebibyte | ZiB | 2<sup>70</sup> Byte | 1 180 591 620 717 411 303 424 Byte |
| Yobibyte | YiB | 2<sup>80</sup> Byte | 1 208 925 819 614 629 174 706 176 Byte |

| Name | Symbol | Exponential | Absolute |
|-----------|--------|----------------------|----------------------------------------|
| Byte | B | 10<sup>0</sup> Byte | 1 Byte |
| Kilobyte | KB | 10<sup>3</sup> Byte | 1 000 Byte |
| Megabyte | MB | 10<sup>6</sup> Byte | 1 000 000 Byte |
| Gigabyte | GB | 10<sup>9</sup> Byte | 1 000 000 000 Byte |
| Terabyte | TB | 10<sup>12</sup> Byte | 1 000 000 000 000 Byte |
| Petabyte | PB | 10<sup>15</sup> Byte | 1 000 000 000 000 000 Byte |
| Exabyte | EB | 10<sup>18</sup> Byte | 1 000 000 000 000 000 000 Byte |
| Zettabyte | ZB | 10<sup>21</sup> Byte | 1 000 000 000 000 000 000 000 Byte |
| Yottabyte | YB | 10<sup>24</sup> Byte | 1 000 000 000 000 000 000 000 000 Byte |
| Name | Symbol | Exponential | Absolute |
|----------|--------|----------------------|------------------------------------------------|
| Byte | B | 2<sup>0</sup> Byte | 1 Byte |
| Kibibyte | KiB | 2<sup>10</sup> Byte | 1 024 Byte |
| Mebibyte | MiB | 2<sup>20</sup> Byte | 1 048 576 Byte |
| Gibibyte | GiB | 2<sup>30</sup> Byte | 1 073 741 824 Byte |
| Tebibyte | TiB | 2<sup>40</sup> Byte | 1 099 511 627 776 Byte |
| Pebibyte | PiB | 2<sup>50</sup> Byte | 1 125 899 906 842 624 Byte |
| Exbibyte | EiB | 2<sup>60</sup> Byte | 1 152 921 504 606 846 976 Byte |
| Zebibyte | ZiB | 2<sup>70</sup> Byte | 1 180 591 620 717 411 303 424 Byte |
| Yobibyte | YiB | 2<sup>80</sup> Byte | 1 208 925 819 614 629 174 706 176 Byte |
| Robibyte | RiB | 2<sup>90</sup> Byte | 1 237 940 039 285 380 274 899 124 224 Byte |
| Qubibyte | QiB | 2<sup>100</sup> Byte | 1 267 650 600 228 229 401 496 703 205 376 Byte |

| Name | Symbol | Exponential | Absolute |
|------------|--------|----------------------|------------------------------------------------|
| Byte | B | 10<sup>0</sup> Byte | 1 Byte |
| Kilobyte | KB | 10<sup>3</sup> Byte | 1 000 Byte |
| Megabyte | MB | 10<sup>6</sup> Byte | 1 000 000 Byte |
| Gigabyte | GB | 10<sup>9</sup> Byte | 1 000 000 000 Byte |
| Terabyte | TB | 10<sup>12</sup> Byte | 1 000 000 000 000 Byte |
| Petabyte | PB | 10<sup>15</sup> Byte | 1 000 000 000 000 000 Byte |
| Exabyte | EB | 10<sup>18</sup> Byte | 1 000 000 000 000 000 000 Byte |
| Zettabyte | ZB | 10<sup>21</sup> Byte | 1 000 000 000 000 000 000 000 Byte |
| Yottabyte | YB | 10<sup>24</sup> Byte | 1 000 000 000 000 000 000 000 000 Byte |
| Ronnabyte | RB | 10<sup>27</sup> Byte | 1 000 000 000 000 000 000 000 000 000 Byte |
| Quettabyte | QB | 10<sup>30</sup> Byte | 1 000 000 000 000 000 000 000 000 000 000 Byte |

## Usage

Expand All @@ -52,18 +56,18 @@ Each unit implements a Byte-based static factory method (`valueOf(BigInteger)` o

```java
// 'long' based
Kilobyte unit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"
Kibibyte unit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"
Kilobyte unit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"
Kibibyte unit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"

Megabyte unit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"
Mebibyte unit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"
Megabyte unit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"
Mebibyte unit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"

// 'BigInteger' based
Kilobyte unit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"
Kibibyte unit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"
Kilobyte unit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"
Kibibyte unit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"

Megabyte unit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"
Mebibyte unit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"
Megabyte unit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"
Mebibyte unit = 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.
Expand Down Expand Up @@ -105,15 +109,19 @@ Pebibyte unit = pebibyte(1) // 1 125 899 906 842 624 Byte
Exbibyte unit = exbibyte(1) // 1 152 921 504 606 846 976 Byte
Zebibyte unit = zebibyte(1) // 1 180 591 620 717 411 303 424 Byte
Yobibyte unit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 Byte

Kilobyte unit = kilobyte(1) // 1 000 Byte
Megabyte unit = megabyte(1) // 1 000 000 Byte
Gigabyte unit = gigabyte(1) // 1 000 000 000 Byte
Terabyte unit = terabyte(1) // 1 000 000 000 000 Byte
Petabyte unit = petabyte(1) // 1 000 000 000 000 000 Byte
Exabyte unit = exabyte(1) // 1 000 000 000 000 000 000 Byte
Zettabyte unit = zettabyte(1) // 1 000 000 000 000 000 000 000 Byte
Yottabyte unit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 Byte
Robibyte unit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 Byte
Qubibyte unit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 Byte

Kilobyte unit = kilobyte(1) // 1 000 Byte
Megabyte unit = megabyte(1) // 1 000 000 Byte
Gigabyte unit = gigabyte(1) // 1 000 000 000 Byte
Terabyte unit = terabyte(1) // 1 000 000 000 000 Byte
Petabyte unit = petabyte(1) // 1 000 000 000 000 000 Byte
Exabyte unit = exabyte(1) // 1 000 000 000 000 000 000 Byte
Zettabyte unit = zettabyte(1) // 1 000 000 000 000 000 000 000 Byte
Yottabyte unit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 Byte
Ronnabyte unit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 Byte
Quettabyte unit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte
```

### Add, Subtract, Multiply, Divide
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public final class Kibibyte extends StorageUnit<Kibibyte> {

@Override
@CheckReturnValue
public @NotNull Kibibyte subtract(final StorageUnit<?> storageAmount) {
public @NotNull Kibibyte subtract(final @NotNull StorageUnit<?> storageAmount) {
return subtract(storageAmount.bytes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public final class Kilobyte extends StorageUnit<Kilobyte> {

@Override
@CheckReturnValue
public @NotNull Kilobyte subtract(final StorageUnit<?> storageAmount) {
public @NotNull Kilobyte subtract(final @NotNull StorageUnit<?> storageAmount) {
return subtract(storageAmount.bytes);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* SPDX-FileCopyrightText: The Storage-Units Authors
* SPDX-License-Identifier: 0BSD
*/
package wtf.metio.storageunits.model;

import edu.umd.cs.findbugs.annotations.CheckReturnValue;
import org.jetbrains.annotations.NotNull;

import java.io.Serial;
import java.math.BigInteger;
import java.util.function.Function;

/**
* Qubibyte as specified in ISO IEC 80000-13:2008 (1 Qubibyte = 1 267 650 600 228 229 401 496 703 205 376 Byte).
*/
public final class Qubibyte extends StorageUnit<Qubibyte> {

@Serial
private static final long serialVersionUID = 8611754914470986560L;

Qubibyte(final @NotNull BigInteger bytes) {
super(bytes);
}

/**
* @param numberOfBytes The amount of bytes the Qubibyte contains.
* @return A new Qubibyte unit with the given value.
*/
@CheckReturnValue
public static @NotNull Qubibyte valueOf(final @NotNull BigInteger numberOfBytes) {
return new Qubibyte(numberOfBytes);
}

/**
* @param numberOfBytes The amount of bytes the Qubibyte contains.
* @return A new Qubibyte unit with the given value.
*/
@CheckReturnValue
public static @NotNull Qubibyte valueOf(final long numberOfBytes) {
return valueOf(BigInteger.valueOf(numberOfBytes));
}

/**
* @param numberOfBytes The amount of bytes the Qubibyte contains.
* @return A new Qubibyte unit with the given value.
*/
@CheckReturnValue
public static @NotNull Qubibyte valueOf(final @NotNull Long numberOfBytes) {
return valueOf(numberOfBytes.longValue());
}

@Override
@CheckReturnValue
public @NotNull Qubibyte add(final long bytesToAdd) {
return add(BigInteger.valueOf(bytesToAdd));
}

@Override
@CheckReturnValue
public @NotNull Qubibyte add(final @NotNull BigInteger bytesToAdd) {
return new Qubibyte(bytes.add(bytesToAdd));
}

@Override
@CheckReturnValue
public @NotNull Qubibyte add(final @NotNull StorageUnit<?> storageAmount) {
return add(storageAmount.bytes);
}

@Override
@CheckReturnValue
public @NotNull Qubibyte divide(final long divisor) {
return divide(BigInteger.valueOf(divisor));
}

@Override
@CheckReturnValue
public @NotNull Qubibyte divide(final @NotNull BigInteger divisor) {
return new Qubibyte(bytes.divide(divisor));
}

@Override
@CheckReturnValue
public @NotNull Qubibyte multiply(final long factor) {
return multiply(BigInteger.valueOf(factor));
}

@Override
@CheckReturnValue
public @NotNull Qubibyte multiply(final @NotNull BigInteger factor) {
return new Qubibyte(bytes.multiply(factor));
}

@Override
@CheckReturnValue
public @NotNull Qubibyte subtract(final long bytesToSubtract) {
return subtract(BigInteger.valueOf(bytesToSubtract));
}

@Override
@CheckReturnValue
public @NotNull Qubibyte subtract(final @NotNull BigInteger bytesToSubtract) {
return new Qubibyte(bytes.subtract(bytesToSubtract));
}

@Override
@CheckReturnValue
public @NotNull Qubibyte subtract(final @NotNull StorageUnit<?> storageAmount) {
return subtract(storageAmount.bytes);
}

@Override
@CheckReturnValue
protected @NotNull BigInteger getNumberOfBytesPerUnit() {
return StorageUnit.BYTES_IN_A_QUBIBYTE;
}

@Override
@CheckReturnValue
protected @NotNull String getSymbol() {
return "QiB";
}

@Override
@CheckReturnValue
protected @NotNull Function<@NotNull BigInteger, @NotNull StorageUnit<?>> converter() {
return StorageUnits::binaryValueOf;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* SPDX-FileCopyrightText: The Storage-Units Authors
* SPDX-License-Identifier: 0BSD
*/
package wtf.metio.storageunits.model;

import edu.umd.cs.findbugs.annotations.CheckReturnValue;
import org.jetbrains.annotations.NotNull;

import java.io.Serial;
import java.math.BigInteger;
import java.util.function.Function;

/**
* Quettabyte as specified in ISO IEC 80000-13:2008 (1 Quettabyte = 1 000 000 000 000 000 000 000 000 000 000 Byte).
*/
public final class Quettabyte extends StorageUnit<Quettabyte> {

@Serial
private static final long serialVersionUID = -7866123408102424489L;

Quettabyte(final @NotNull BigInteger bytes) {
super(bytes);
}

/**
* @param numberOfBytes The amount of bytes the Quettabyte contains.
* @return A new Quettabyte unit with the given value.
*/
@CheckReturnValue
public static @NotNull Quettabyte valueOf(final @NotNull BigInteger numberOfBytes) {
return new Quettabyte(numberOfBytes);
}

/**
* @param numberOfBytes The amount of bytes the Quettabyte contains.
* @return A new Quettabyte unit with the given value.
*/
@CheckReturnValue
public static @NotNull Quettabyte valueOf(final long numberOfBytes) {
return valueOf(BigInteger.valueOf(numberOfBytes));
}

/**
* @param numberOfBytes The amount of bytes the Quettabyte contains.
* @return A new Quettabyte unit with the given value.
*/
@CheckReturnValue
public static @NotNull Quettabyte valueOf(final @NotNull Long numberOfBytes) {
return valueOf(numberOfBytes.longValue());
}

@Override
@CheckReturnValue
public @NotNull Quettabyte add(final long bytesToAdd) {
return add(BigInteger.valueOf(bytesToAdd));
}

@Override
@CheckReturnValue
public @NotNull Quettabyte add(final @NotNull BigInteger bytesToAdd) {
return new Quettabyte(bytes.add(bytesToAdd));
}

@Override
@CheckReturnValue
public @NotNull Quettabyte add(final @NotNull StorageUnit<?> storageAmount) {
return add(storageAmount.bytes);
}

@Override
@CheckReturnValue
public @NotNull Quettabyte divide(final long divisor) {
return divide(BigInteger.valueOf(divisor));
}

@Override
@CheckReturnValue
public @NotNull Quettabyte divide(final @NotNull BigInteger divisor) {
return new Quettabyte(bytes.divide(divisor));
}

@Override
@CheckReturnValue
public @NotNull Quettabyte multiply(final long factor) {
return multiply(BigInteger.valueOf(factor));
}

@Override
@CheckReturnValue
public @NotNull Quettabyte multiply(final @NotNull BigInteger factor) {
return new Quettabyte(bytes.multiply(factor));
}

@Override
@CheckReturnValue
public @NotNull Quettabyte subtract(final long bytesToSubtract) {
return subtract(BigInteger.valueOf(bytesToSubtract));
}

@Override
@CheckReturnValue
public @NotNull Quettabyte subtract(final @NotNull BigInteger bytesToSubtract) {
return new Quettabyte(bytes.subtract(bytesToSubtract));
}

@Override
@CheckReturnValue
public @NotNull Quettabyte subtract(final @NotNull StorageUnit<?> storageAmount) {
return subtract(storageAmount.bytes);
}

@Override
@CheckReturnValue
protected @NotNull BigInteger getNumberOfBytesPerUnit() {
return StorageUnit.BYTES_IN_A_QUETTABYTE;
}

@Override
@CheckReturnValue
protected @NotNull String getSymbol() {
return "QB";
}

@Override
@CheckReturnValue
protected @NotNull Function<@NotNull BigInteger, @NotNull StorageUnit<?>> converter() {
return StorageUnits::decimalValueOf;
}

}
Loading

0 comments on commit 8890350

Please sign in to comment.