fix #22
[storage-units.git] / storage-units / src / main / java / de / xn__ho_hia / storage_unit / Megabyte.java
blob07cb73af97b9a9cf4eec4b8ffd25431f640c0204
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.storage_unit;
9 import static de.xn__ho_hia.quality.null_analysis.Nullsafe.addNullsafe;
10 import static de.xn__ho_hia.quality.null_analysis.Nullsafe.asBigInteger;
11 import static de.xn__ho_hia.quality.null_analysis.Nullsafe.divideNullsafe;
12 import static de.xn__ho_hia.quality.null_analysis.Nullsafe.multiplyNullsafe;
13 import static de.xn__ho_hia.quality.null_analysis.Nullsafe.subtractNullsafe;
15 import java.math.BigInteger;
16 import java.util.function.Function;
18 import org.eclipse.jdt.annotation.NonNull;
20 /**
21 * Megabyte as specified in ISO IEC 80000-13:2008 (1 Megabyte = 1 000 000 Byte).
23 public class Megabyte extends StorageUnit<Megabyte> {
25 private static final long serialVersionUID = 5901923092058760111L;
27 Megabyte(@NonNull final BigInteger bytes) {
28 super(bytes);
31 /**
32 * @param numberOfBytes
33 * The amount of bytes the Megabyte contains.
34 * @return A new Megabyte unit with the given value.
36 @NonNull
37 public static Megabyte valueOf(@NonNull final BigInteger numberOfBytes) {
38 return new Megabyte(numberOfBytes);
41 /**
42 * @param numberOfBytes
43 * The amount of bytes the Megabyte contains.
44 * @return A new Megabyte unit with the given value.
46 @NonNull
47 public static Megabyte valueOf(final long numberOfBytes) {
48 return new Megabyte(asBigInteger(numberOfBytes));
51 /**
52 * @param numberOfBytes
53 * The amount of bytes the Megabyte contains.
54 * @return A new Megabyte unit with the given value.
56 @NonNull
57 public static Megabyte valueOf(@NonNull final Long numberOfBytes) {
58 return valueOf(numberOfBytes.longValue());
61 @Override
62 public Megabyte add(final long bytesToAdd) {
63 return new Megabyte(addNullsafe(bytes, asBigInteger(bytesToAdd)));
66 @Override
67 public Megabyte add(final StorageUnit<?> storageAmount) {
68 return new Megabyte(addNullsafe(bytes, storageAmount.bytes));
71 @Override
72 public Megabyte divide(final long divisor) {
73 return new Megabyte(divideNullsafe(bytes, asBigInteger(divisor)));
76 @Override
77 public Megabyte multiply(final long factor) {
78 return new Megabyte(multiplyNullsafe(bytes, asBigInteger(factor)));
81 @Override
82 public Megabyte subtract(final long bytesToSubtract) {
83 return new Megabyte(subtractNullsafe(bytes, asBigInteger(bytesToSubtract)));
86 @Override
87 public Megabyte subtract(final StorageUnit<?> storageAmount) {
88 return new Megabyte(subtractNullsafe(bytes, storageAmount.bytes));
91 @Override
92 protected BigInteger getNumberOfBytesPerUnit() {
93 return StorageUnit.BYTES_IN_A_MEGABYTE;
96 @Override
97 protected String getSymbol() {
98 return "MB"; //$NON-NLS-1$
101 @Override
102 protected Function<@NonNull BigInteger, @NonNull StorageUnit<?>> converter() {
103 return StorageUnits::metricValueOf;