Skip to content

Commit 8890350

Browse files
sebhossSebastian Hoß
andauthored
add new SI prefixes (#28)
Co-authored-by: Sebastian Hoß <seb@hoß.de>
1 parent 51a18ac commit 8890350

21 files changed

+1398
-137
lines changed

README.md

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,37 @@ Storage units according to ISO IEC 80000-13:2008 implemented in Java.
1616
* Lossless conversion between all units
1717
* Human-readable text format, including custom formats
1818
* Compatible with any `java.lang.Number`
19-
* Custom serializers for Jackson, MongoDB & EclipseLink
19+
* Custom serializers for Jackson, MongoDB, EclipseLink, and others
2020

2121
### Available Units
2222

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

4751
## Usage
4852

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

5357
```java
5458
// 'long' based
55-
Kilobyte unit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"
56-
Kibibyte unit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"
59+
Kilobyte unit = Kilobyte.valueOf(500) // 500 Byte or "0.50 kB"
60+
Kibibyte unit = Kibibyte.valueOf(512) // 512 Byte or "0.50 KiB"
5761

58-
Megabyte unit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"
59-
Mebibyte unit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"
62+
Megabyte unit = Megabyte.valueOf(1_000_000) // 1 000 000 Byte or "1.00 MB"
63+
Mebibyte unit = Mebibyte.valueOf(1_048_576) // 1 048 576 Byte or "1.00 MiB"
6064

6165
// 'BigInteger' based
62-
Kilobyte unit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"
63-
Kibibyte unit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"
66+
Kilobyte unit = Kilobyte.valueOf(BigInteger.valueOf(500)) // 500 Byte or "0.50 kB"
67+
Kibibyte unit = Kibibyte.valueOf(BigInteger.valueOf(512)) // 512 Byte or "0.50 KiB"
6468

65-
Megabyte unit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"
66-
Mebibyte unit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"
69+
Megabyte unit = Megabyte.valueOf(BigInteger.valueOf(1000000)) // 1 000 000 Byte or "1.00 MB"
70+
Mebibyte unit = Mebibyte.valueOf(BigInteger.valueOf(1_048_576)) // 1 048 576 Byte or "1.00 MB"
6771
```
6872

6973
The `StorageUnits` class offers three factory methods that automatically pick the best-matching unit for a given number of bytes.
@@ -105,15 +109,19 @@ Pebibyte unit = pebibyte(1) // 1 125 899 906 842 624 Byte
105109
Exbibyte unit = exbibyte(1) // 1 152 921 504 606 846 976 Byte
106110
Zebibyte unit = zebibyte(1) // 1 180 591 620 717 411 303 424 Byte
107111
Yobibyte unit = yobibyte(1) // 1 208 925 819 614 629 174 706 176 Byte
108-
109-
Kilobyte unit = kilobyte(1) // 1 000 Byte
110-
Megabyte unit = megabyte(1) // 1 000 000 Byte
111-
Gigabyte unit = gigabyte(1) // 1 000 000 000 Byte
112-
Terabyte unit = terabyte(1) // 1 000 000 000 000 Byte
113-
Petabyte unit = petabyte(1) // 1 000 000 000 000 000 Byte
114-
Exabyte unit = exabyte(1) // 1 000 000 000 000 000 000 Byte
115-
Zettabyte unit = zettabyte(1) // 1 000 000 000 000 000 000 000 Byte
116-
Yottabyte unit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 Byte
112+
Robibyte unit = robibyte(1) // 1 237 940 039 285 380 274 899 124 224 Byte
113+
Qubibyte unit = qubibyte(1) // 1 267 650 600 228 229 401 496 703 205 376 Byte
114+
115+
Kilobyte unit = kilobyte(1) // 1 000 Byte
116+
Megabyte unit = megabyte(1) // 1 000 000 Byte
117+
Gigabyte unit = gigabyte(1) // 1 000 000 000 Byte
118+
Terabyte unit = terabyte(1) // 1 000 000 000 000 Byte
119+
Petabyte unit = petabyte(1) // 1 000 000 000 000 000 Byte
120+
Exabyte unit = exabyte(1) // 1 000 000 000 000 000 000 Byte
121+
Zettabyte unit = zettabyte(1) // 1 000 000 000 000 000 000 000 Byte
122+
Yottabyte unit = yottabyte(1) // 1 000 000 000 000 000 000 000 000 Byte
123+
Ronnabyte unit = ronnabyte(1) // 1 000 000 000 000 000 000 000 000 000 Byte
124+
Quettabyte unit = quettabyte(1) // 1 000 000 000 000 000 000 000 000 000 000 Byte
117125
```
118126

119127
### Add, Subtract, Multiply, Divide

storage-units-model/src/main/java/wtf/metio/storageunits/model/Kibibyte.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public final class Kibibyte extends StorageUnit<Kibibyte> {
106106

107107
@Override
108108
@CheckReturnValue
109-
public @NotNull Kibibyte subtract(final StorageUnit<?> storageAmount) {
109+
public @NotNull Kibibyte subtract(final @NotNull StorageUnit<?> storageAmount) {
110110
return subtract(storageAmount.bytes);
111111
}
112112

storage-units-model/src/main/java/wtf/metio/storageunits/model/Kilobyte.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public final class Kilobyte extends StorageUnit<Kilobyte> {
106106

107107
@Override
108108
@CheckReturnValue
109-
public @NotNull Kilobyte subtract(final StorageUnit<?> storageAmount) {
109+
public @NotNull Kilobyte subtract(final @NotNull StorageUnit<?> storageAmount) {
110110
return subtract(storageAmount.bytes);
111111
}
112112

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* SPDX-FileCopyrightText: The Storage-Units Authors
3+
* SPDX-License-Identifier: 0BSD
4+
*/
5+
package wtf.metio.storageunits.model;
6+
7+
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
import java.io.Serial;
11+
import java.math.BigInteger;
12+
import java.util.function.Function;
13+
14+
/**
15+
* Qubibyte as specified in ISO IEC 80000-13:2008 (1 Qubibyte = 1 267 650 600 228 229 401 496 703 205 376 Byte).
16+
*/
17+
public final class Qubibyte extends StorageUnit<Qubibyte> {
18+
19+
@Serial
20+
private static final long serialVersionUID = 8611754914470986560L;
21+
22+
Qubibyte(final @NotNull BigInteger bytes) {
23+
super(bytes);
24+
}
25+
26+
/**
27+
* @param numberOfBytes The amount of bytes the Qubibyte contains.
28+
* @return A new Qubibyte unit with the given value.
29+
*/
30+
@CheckReturnValue
31+
public static @NotNull Qubibyte valueOf(final @NotNull BigInteger numberOfBytes) {
32+
return new Qubibyte(numberOfBytes);
33+
}
34+
35+
/**
36+
* @param numberOfBytes The amount of bytes the Qubibyte contains.
37+
* @return A new Qubibyte unit with the given value.
38+
*/
39+
@CheckReturnValue
40+
public static @NotNull Qubibyte valueOf(final long numberOfBytes) {
41+
return valueOf(BigInteger.valueOf(numberOfBytes));
42+
}
43+
44+
/**
45+
* @param numberOfBytes The amount of bytes the Qubibyte contains.
46+
* @return A new Qubibyte unit with the given value.
47+
*/
48+
@CheckReturnValue
49+
public static @NotNull Qubibyte valueOf(final @NotNull Long numberOfBytes) {
50+
return valueOf(numberOfBytes.longValue());
51+
}
52+
53+
@Override
54+
@CheckReturnValue
55+
public @NotNull Qubibyte add(final long bytesToAdd) {
56+
return add(BigInteger.valueOf(bytesToAdd));
57+
}
58+
59+
@Override
60+
@CheckReturnValue
61+
public @NotNull Qubibyte add(final @NotNull BigInteger bytesToAdd) {
62+
return new Qubibyte(bytes.add(bytesToAdd));
63+
}
64+
65+
@Override
66+
@CheckReturnValue
67+
public @NotNull Qubibyte add(final @NotNull StorageUnit<?> storageAmount) {
68+
return add(storageAmount.bytes);
69+
}
70+
71+
@Override
72+
@CheckReturnValue
73+
public @NotNull Qubibyte divide(final long divisor) {
74+
return divide(BigInteger.valueOf(divisor));
75+
}
76+
77+
@Override
78+
@CheckReturnValue
79+
public @NotNull Qubibyte divide(final @NotNull BigInteger divisor) {
80+
return new Qubibyte(bytes.divide(divisor));
81+
}
82+
83+
@Override
84+
@CheckReturnValue
85+
public @NotNull Qubibyte multiply(final long factor) {
86+
return multiply(BigInteger.valueOf(factor));
87+
}
88+
89+
@Override
90+
@CheckReturnValue
91+
public @NotNull Qubibyte multiply(final @NotNull BigInteger factor) {
92+
return new Qubibyte(bytes.multiply(factor));
93+
}
94+
95+
@Override
96+
@CheckReturnValue
97+
public @NotNull Qubibyte subtract(final long bytesToSubtract) {
98+
return subtract(BigInteger.valueOf(bytesToSubtract));
99+
}
100+
101+
@Override
102+
@CheckReturnValue
103+
public @NotNull Qubibyte subtract(final @NotNull BigInteger bytesToSubtract) {
104+
return new Qubibyte(bytes.subtract(bytesToSubtract));
105+
}
106+
107+
@Override
108+
@CheckReturnValue
109+
public @NotNull Qubibyte subtract(final @NotNull StorageUnit<?> storageAmount) {
110+
return subtract(storageAmount.bytes);
111+
}
112+
113+
@Override
114+
@CheckReturnValue
115+
protected @NotNull BigInteger getNumberOfBytesPerUnit() {
116+
return StorageUnit.BYTES_IN_A_QUBIBYTE;
117+
}
118+
119+
@Override
120+
@CheckReturnValue
121+
protected @NotNull String getSymbol() {
122+
return "QiB";
123+
}
124+
125+
@Override
126+
@CheckReturnValue
127+
protected @NotNull Function<@NotNull BigInteger, @NotNull StorageUnit<?>> converter() {
128+
return StorageUnits::binaryValueOf;
129+
}
130+
131+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* SPDX-FileCopyrightText: The Storage-Units Authors
3+
* SPDX-License-Identifier: 0BSD
4+
*/
5+
package wtf.metio.storageunits.model;
6+
7+
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
import java.io.Serial;
11+
import java.math.BigInteger;
12+
import java.util.function.Function;
13+
14+
/**
15+
* Quettabyte as specified in ISO IEC 80000-13:2008 (1 Quettabyte = 1 000 000 000 000 000 000 000 000 000 000 Byte).
16+
*/
17+
public final class Quettabyte extends StorageUnit<Quettabyte> {
18+
19+
@Serial
20+
private static final long serialVersionUID = -7866123408102424489L;
21+
22+
Quettabyte(final @NotNull BigInteger bytes) {
23+
super(bytes);
24+
}
25+
26+
/**
27+
* @param numberOfBytes The amount of bytes the Quettabyte contains.
28+
* @return A new Quettabyte unit with the given value.
29+
*/
30+
@CheckReturnValue
31+
public static @NotNull Quettabyte valueOf(final @NotNull BigInteger numberOfBytes) {
32+
return new Quettabyte(numberOfBytes);
33+
}
34+
35+
/**
36+
* @param numberOfBytes The amount of bytes the Quettabyte contains.
37+
* @return A new Quettabyte unit with the given value.
38+
*/
39+
@CheckReturnValue
40+
public static @NotNull Quettabyte valueOf(final long numberOfBytes) {
41+
return valueOf(BigInteger.valueOf(numberOfBytes));
42+
}
43+
44+
/**
45+
* @param numberOfBytes The amount of bytes the Quettabyte contains.
46+
* @return A new Quettabyte unit with the given value.
47+
*/
48+
@CheckReturnValue
49+
public static @NotNull Quettabyte valueOf(final @NotNull Long numberOfBytes) {
50+
return valueOf(numberOfBytes.longValue());
51+
}
52+
53+
@Override
54+
@CheckReturnValue
55+
public @NotNull Quettabyte add(final long bytesToAdd) {
56+
return add(BigInteger.valueOf(bytesToAdd));
57+
}
58+
59+
@Override
60+
@CheckReturnValue
61+
public @NotNull Quettabyte add(final @NotNull BigInteger bytesToAdd) {
62+
return new Quettabyte(bytes.add(bytesToAdd));
63+
}
64+
65+
@Override
66+
@CheckReturnValue
67+
public @NotNull Quettabyte add(final @NotNull StorageUnit<?> storageAmount) {
68+
return add(storageAmount.bytes);
69+
}
70+
71+
@Override
72+
@CheckReturnValue
73+
public @NotNull Quettabyte divide(final long divisor) {
74+
return divide(BigInteger.valueOf(divisor));
75+
}
76+
77+
@Override
78+
@CheckReturnValue
79+
public @NotNull Quettabyte divide(final @NotNull BigInteger divisor) {
80+
return new Quettabyte(bytes.divide(divisor));
81+
}
82+
83+
@Override
84+
@CheckReturnValue
85+
public @NotNull Quettabyte multiply(final long factor) {
86+
return multiply(BigInteger.valueOf(factor));
87+
}
88+
89+
@Override
90+
@CheckReturnValue
91+
public @NotNull Quettabyte multiply(final @NotNull BigInteger factor) {
92+
return new Quettabyte(bytes.multiply(factor));
93+
}
94+
95+
@Override
96+
@CheckReturnValue
97+
public @NotNull Quettabyte subtract(final long bytesToSubtract) {
98+
return subtract(BigInteger.valueOf(bytesToSubtract));
99+
}
100+
101+
@Override
102+
@CheckReturnValue
103+
public @NotNull Quettabyte subtract(final @NotNull BigInteger bytesToSubtract) {
104+
return new Quettabyte(bytes.subtract(bytesToSubtract));
105+
}
106+
107+
@Override
108+
@CheckReturnValue
109+
public @NotNull Quettabyte subtract(final @NotNull StorageUnit<?> storageAmount) {
110+
return subtract(storageAmount.bytes);
111+
}
112+
113+
@Override
114+
@CheckReturnValue
115+
protected @NotNull BigInteger getNumberOfBytesPerUnit() {
116+
return StorageUnit.BYTES_IN_A_QUETTABYTE;
117+
}
118+
119+
@Override
120+
@CheckReturnValue
121+
protected @NotNull String getSymbol() {
122+
return "QB";
123+
}
124+
125+
@Override
126+
@CheckReturnValue
127+
protected @NotNull Function<@NotNull BigInteger, @NotNull StorageUnit<?>> converter() {
128+
return StorageUnits::decimalValueOf;
129+
}
130+
131+
}

0 commit comments

Comments
 (0)