format & docs
[storage-units.git] / storage-units-model / src / main / java / wtf / metio / storageunits / model / Megabyte.java
blobaf0bf0f364410ade67e9196a71dbd997e85aed20
1 /*
2 * SPDX-FileCopyrightText: The Storage-Units Authors
3 * SPDX-License-Identifier: 0BSD
4 */
5 package wtf.metio.storageunits.model;
7 import edu.umd.cs.findbugs.annotations.CheckReturnValue;
8 import org.jetbrains.annotations.NotNull;
10 import java.io.Serial;
11 import java.math.BigInteger;
12 import java.util.function.Function;
14 /**
15 * Megabyte as specified in ISO IEC 80000-13:2008 (1 Megabyte = 1 000 000 Byte).
17 public final class Megabyte extends StorageUnit<Megabyte> {
19 @Serial
20 private static final long serialVersionUID = 5901923092058760111L;
22 Megabyte(@NotNull final BigInteger bytes) {
23 super(bytes);
26 /**
27 * @param numberOfBytes The amount of bytes the Megabyte contains.
28 * @return A new Megabyte unit with the given value.
30 @NotNull
31 @CheckReturnValue
32 public static Megabyte valueOf(@NotNull final BigInteger numberOfBytes) {
33 return new Megabyte(numberOfBytes);
36 /**
37 * @param numberOfBytes The amount of bytes the Megabyte contains.
38 * @return A new Megabyte unit with the given value.
40 @NotNull
41 @CheckReturnValue
42 public static Megabyte valueOf(final long numberOfBytes) {
43 return valueOf(BigInteger.valueOf(numberOfBytes));
46 /**
47 * @param numberOfBytes The amount of bytes the Megabyte contains.
48 * @return A new Megabyte unit with the given value.
50 @NotNull
51 @CheckReturnValue
52 public static Megabyte valueOf(@NotNull final Long numberOfBytes) {
53 return valueOf(numberOfBytes.longValue());
56 @Override
57 @NotNull
58 @CheckReturnValue
59 public Megabyte add(final long bytesToAdd) {
60 return add(BigInteger.valueOf(bytesToAdd));
63 @Override
64 @NotNull
65 @CheckReturnValue
66 public Megabyte add(@NotNull final BigInteger bytesToAdd) {
67 return new Megabyte(bytes.add(bytesToAdd));
70 @Override
71 @NotNull
72 @CheckReturnValue
73 public Megabyte add(@NotNull final StorageUnit<?> storageAmount) {
74 return add(storageAmount.bytes);
77 @Override
78 @NotNull
79 @CheckReturnValue
80 public Megabyte divide(final long divisor) {
81 return divide(BigInteger.valueOf(divisor));
84 @Override
85 @NotNull
86 @CheckReturnValue
87 public Megabyte divide(@NotNull final BigInteger divisor) {
88 return new Megabyte(bytes.divide(divisor));
91 @Override
92 @NotNull
93 @CheckReturnValue
94 public Megabyte multiply(final long factor) {
95 return multiply(BigInteger.valueOf(factor));
98 @Override
99 @NotNull
100 @CheckReturnValue
101 public Megabyte multiply(@NotNull final BigInteger factor) {
102 return new Megabyte(bytes.multiply(factor));
105 @Override
106 @NotNull
107 @CheckReturnValue
108 public Megabyte subtract(final long bytesToSubtract) {
109 return subtract(BigInteger.valueOf(bytesToSubtract));
112 @Override
113 @NotNull
114 @CheckReturnValue
115 public Megabyte subtract(@NotNull final BigInteger bytesToSubtract) {
116 return new Megabyte(bytes.subtract(bytesToSubtract));
119 @Override
120 @NotNull
121 @CheckReturnValue
122 public Megabyte subtract(@NotNull final StorageUnit<?> storageAmount) {
123 return subtract(storageAmount.bytes);
126 @Override
127 @NotNull
128 @CheckReturnValue
129 protected BigInteger getNumberOfBytesPerUnit() {
130 return StorageUnit.BYTES_IN_A_MEGABYTE;
133 @Override
134 @NotNull
135 @CheckReturnValue
136 protected String getSymbol() {
137 return "MB";
140 @Override
141 @NotNull
142 @CheckReturnValue
143 protected Function<@NotNull BigInteger, @NotNull StorageUnit<?>> converter() {
144 return StorageUnits::decimalValueOf;