update to latest parent & fix null issues
[storage-units.git] / src / main / java / de / xn__ho_hia / utils / storage_unit / StorageUnits.java
blob10721bd136504068f59de9a25a7a378eb85c61cb
1 /*
2 * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
7 package de.xn__ho_hia.utils.storage_unit;
9 import static de.xn__ho_hia.utils.storage_unit.NullsafeMath.asBigInteger;
10 import static de.xn__ho_hia.utils.storage_unit.NullsafeMath.multiplyNullsafe;
12 import java.math.BigInteger;
14 /**
15 * Factory for storage units.
17 public final class StorageUnits {
19 private StorageUnits() {
20 // Hidden constructor.
23 /**
24 * @param bytes
25 * The amount to bytes to represent.
26 * @return The appropriate binary-prefixed unit for the given amount of bytes.
28 public static StorageUnit<?> binaryValueOf(final long bytes) {
29 return binaryValueOf(asBigInteger(bytes));
32 /**
33 * @param bytes
34 * The amount to bytes to represent.
35 * @return The appropriate binary-prefixed unit for the given amount of bytes.
37 public static StorageUnit<?> binaryValueOf(final BigInteger bytes) {
38 StorageUnit<?> unit = Kibibyte.valueOf(bytes);
40 if (inbetween(StorageUnit.BYTES_IN_A_MEBIBYTE, bytes, StorageUnit.BYTES_IN_A_GIBIBYTE)) {
41 unit = unit.asMebibyte();
42 } else if (inbetween(StorageUnit.BYTES_IN_A_GIBIBYTE, bytes, StorageUnit.BYTES_IN_A_TEBIBYTE)) {
43 unit = unit.asGibibyte();
44 } else if (inbetween(StorageUnit.BYTES_IN_A_TEBIBYTE, bytes, StorageUnit.BYTES_IN_A_PEBIBYTE)) {
45 unit = unit.asTebibyte();
46 } else if (inbetween(StorageUnit.BYTES_IN_A_PEBIBYTE, bytes, StorageUnit.BYTES_IN_A_EXBIBYTE)) {
47 unit = unit.asPebibyte();
48 } else if (inbetween(StorageUnit.BYTES_IN_A_EXBIBYTE, bytes, StorageUnit.BYTES_IN_A_ZEBIBYTE)) {
49 unit = unit.asExbibyte();
50 } else if (inbetween(StorageUnit.BYTES_IN_A_ZEBIBYTE, bytes, StorageUnit.BYTES_IN_A_YOBIBYTE)) {
51 unit = unit.asZebibyte();
52 } else if (greaterThanEquals(bytes, StorageUnit.BYTES_IN_A_YOBIBYTE)) {
53 unit = unit.asYobibyte();
56 return unit;
59 /**
60 * @param bytes
61 * The amount of bytes to represent.
62 * @return The appropriate metric-prefixed unit for the given amount of bytes.
64 public static StorageUnit<?> metricValueOf(final long bytes) {
65 return metricValueOf(asBigInteger(bytes));
68 /**
69 * @param bytes
70 * The amount of bytes to represent.
71 * @return The appropriate metric-prefixed unit for the given amount of bytes.
73 public static StorageUnit<?> metricValueOf(final BigInteger bytes) {
74 StorageUnit<?> unit = Kilobyte.valueOf(bytes);
76 if (inbetween(StorageUnit.BYTES_IN_A_MEGABYTE, bytes, StorageUnit.BYTES_IN_A_GIGABYTE)) {
77 unit = unit.asMegabyte();
78 } else if (inbetween(StorageUnit.BYTES_IN_A_GIGABYTE, bytes, StorageUnit.BYTES_IN_A_TERABYTE)) {
79 unit = unit.asGigabyte();
80 } else if (inbetween(StorageUnit.BYTES_IN_A_TERABYTE, bytes, StorageUnit.BYTES_IN_A_PETABYTE)) {
81 unit = unit.asTerabyte();
82 } else if (inbetween(StorageUnit.BYTES_IN_A_PETABYTE, bytes, StorageUnit.BYTES_IN_A_EXABYTE)) {
83 unit = unit.asPetabyte();
84 } else if (inbetween(StorageUnit.BYTES_IN_A_EXABYTE, bytes, StorageUnit.BYTES_IN_A_ZETTABYTE)) {
85 unit = unit.asExabyte();
86 } else if (inbetween(StorageUnit.BYTES_IN_A_ZETTABYTE, bytes, StorageUnit.BYTES_IN_A_YOTTABYTE)) {
87 unit = unit.asZettabyte();
88 } else if (greaterThanEquals(bytes, StorageUnit.BYTES_IN_A_YOTTABYTE)) {
89 unit = unit.asYottabyte();
92 return unit;
95 private static boolean inbetween(final BigInteger start, final BigInteger value, final BigInteger endExclusive) {
96 return greaterThanEquals(value, start) && value.compareTo(endExclusive) < 0;
99 private static boolean greaterThanEquals(final BigInteger value, final BigInteger comparison) {
100 return value.compareTo(comparison) >= 0;
104 * @param amount
105 * The amount of kibibytes to create.
106 * @return A new Kibibyte unit with the given value.
108 public static Kibibyte kibibyte(final Long amount) {
109 return kibibyte(amount.longValue());
113 * @param amount
114 * The amount of kibibytes to create.
115 * @return A new Kibibyte unit with the given value.
117 public static Kibibyte kibibyte(final long amount) {
118 return kibibyte(asBigInteger(amount));
122 * @param amount
123 * The amount of kibibytes to create.
124 * @return A new Kibibyte unit with the given value.
126 public static Kibibyte kibibyte(final BigInteger amount) {
127 return new Kibibyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_KIBIBYTE, amount));
131 * @param amount
132 * The amount of mebibytes to create.
133 * @return A new Mebibyte unit with the given value.
135 public static Mebibyte mebibyte(final Long amount) {
136 return mebibyte(amount.longValue());
140 * @param amount
141 * The amount of mebibytes to create.
142 * @return A new Mebibyte unit with the given value.
144 public static Mebibyte mebibyte(final long amount) {
145 return mebibyte(asBigInteger(amount));
149 * @param amount
150 * The amount of mebibytes to create.
151 * @return A new Mebibyte unit with the given value.
153 public static Mebibyte mebibyte(final BigInteger amount) {
154 return new Mebibyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_MEBIBYTE, amount));
158 * @param amount
159 * The amount of gibibytes to create.
160 * @return A new Gibibyte unit with the given value.
162 public static Gibibyte gibibyte(final Long amount) {
163 return gibibyte(amount.longValue());
167 * @param amount
168 * The amount of gibibytes to create.
169 * @return A new Gibibyte unit with the given value.
171 public static Gibibyte gibibyte(final long amount) {
172 return gibibyte(asBigInteger(amount));
176 * @param amount
177 * The amount of gibibytes to create.
178 * @return A new Gibibyte unit with the given value.
180 public static Gibibyte gibibyte(final BigInteger amount) {
181 return new Gibibyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_GIBIBYTE, amount));
185 * @param amount
186 * The amount of tebibytes to create.
187 * @return A new Tebibyte unit with the given value.
189 public static Tebibyte tebibyte(final Long amount) {
190 return tebibyte(amount.longValue());
194 * @param amount
195 * The amount of tebibytes to create.
196 * @return A new Tebibyte unit with the given value.
198 public static Tebibyte tebibyte(final long amount) {
199 return tebibyte(asBigInteger(amount));
203 * @param amount
204 * The amount of tebibytes to create.
205 * @return A new Tebibyte unit with the given value.
207 public static Tebibyte tebibyte(final BigInteger amount) {
208 return new Tebibyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_TEBIBYTE, amount));
212 * @param amount
213 * The amount of pebibytes to create.
214 * @return A new Pebibyte unit with the given value.
216 public static Pebibyte pebibyte(final Long amount) {
217 return pebibyte(amount.longValue());
221 * @param amount
222 * The amount of pebibytes to create.
223 * @return A new Pebibyte unit with the given value.
225 public static Pebibyte pebibyte(final long amount) {
226 return pebibyte(asBigInteger(amount));
230 * @param amount
231 * The amount of pebibytes to create.
232 * @return A new Pebibyte unit with the given value.
234 public static Pebibyte pebibyte(final BigInteger amount) {
235 return new Pebibyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_PEBIBYTE, amount));
239 * @param amount
240 * The amount of exbibytes to create.
241 * @return A new Exbibyte unit with the given value.
243 public static Exbibyte exbibyte(final Long amount) {
244 return exbibyte(amount.longValue());
248 * @param amount
249 * The amount of exbibytes to create.
250 * @return A new Exbibyte unit with the given value.
252 public static Exbibyte exbibyte(final long amount) {
253 return exbibyte(asBigInteger(amount));
257 * @param amount
258 * The amount of exbibytes to create.
259 * @return A new Exbibyte unit with the given value.
261 public static Exbibyte exbibyte(final BigInteger amount) {
262 return new Exbibyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_EXBIBYTE, amount));
266 * @param amount
267 * The amount of zebibytes to create.
268 * @return A new Zebibyte unit with the given value.
270 public static Zebibyte zebibyte(final Long amount) {
271 return zebibyte(amount.longValue());
275 * @param amount
276 * The amount of zebibytes to create.
277 * @return A new Zebibyte unit with the given value.
279 public static Zebibyte zebibyte(final long amount) {
280 return zebibyte(asBigInteger(amount));
284 * @param amount
285 * The amount of zebibytes to create.
286 * @return A new Zebibyte unit with the given value.
288 public static Zebibyte zebibyte(final BigInteger amount) {
289 return new Zebibyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_ZEBIBYTE, amount));
293 * @param amount
294 * The amount of yobibytes to create.
295 * @return A new Yobibyte unit with the given value.
297 public static Yobibyte yobibyte(final Long amount) {
298 return yobibyte(amount.longValue());
302 * @param amount
303 * The amount of yobibytes to create.
304 * @return A new Yobibyte unit with the given value.
306 public static Yobibyte yobibyte(final long amount) {
307 return yobibyte(asBigInteger(amount));
311 * @param amount
312 * The amount of yobibytes to create.
313 * @return A new Yobibyte unit with the given value.
315 public static Yobibyte yobibyte(final BigInteger amount) {
316 return new Yobibyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_YOBIBYTE, amount));
320 * @param amount
321 * The amount of kilobytes to create.
322 * @return A new Kilobyte unit with the given value.
324 public static Kilobyte kilobyte(final long amount) {
325 return new Kilobyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_KILOBYTE, asBigInteger(amount)));
329 * @param amount
330 * The amount of megabytes to create.
331 * @return A new Megabyte unit with the given value.
333 public static Megabyte megabyte(final long amount) {
334 return new Megabyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_MEGABYTE, asBigInteger(amount)));
338 * @param amount
339 * The amount of gigabytes to create.
340 * @return A new Gigabyte unit with the given value.
342 public static Gigabyte gigabyte(final long amount) {
343 return new Gigabyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_GIGABYTE, asBigInteger(amount)));
347 * @param amount
348 * The amount of terabytes to create.
349 * @return A new Terabyte unit with the given value.
351 public static Terabyte terabyte(final long amount) {
352 return new Terabyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_TERABYTE, asBigInteger(amount)));
356 * @param amount
357 * The amount of petabytes to create.
358 * @return A new Petabyte unit with the given value.
360 public static Petabyte petabyte(final long amount) {
361 return new Petabyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_PETABYTE, asBigInteger(amount)));
365 * @param amount
366 * The amount of exabytes to create.
367 * @return A new Exabyte unit with the given value.
369 public static Exabyte exabyte(final long amount) {
370 return new Exabyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_EXABYTE, asBigInteger(amount)));
374 * @param amount
375 * The amount of zettabytes to create.
376 * @return A new Zettabyte unit with the given value.
378 public static Zettabyte zettabyte(final long amount) {
379 return new Zettabyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_ZETTABYTE, asBigInteger(amount)));
383 * @param amount
384 * The amount of yottabytes to create.
385 * @return A new Yottabyte unit with the given value.
387 public static Yottabyte yottabyte(final long amount) {
388 return new Yottabyte(multiplyNullsafe(StorageUnit.BYTES_IN_A_YOTTABYTE, asBigInteger(amount)));