From 45925fc7399abf52a68bc5e45cd135f9e1aab2b4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Ho=C3=9F?= Date: Sat, 29 Oct 2022 14:09:31 +0200 Subject: [PATCH] fix #24 parse string inputs --- .../wtf/metio/storageunits/model/StorageUnits.java | 52 ++++++++++++++++++++++ .../storageunits/model/StorageUnitsParseTest.java | 32 +++++++++++++ 2 files changed, 84 insertions(+) create mode 100755 storage-units-model/src/test/java/wtf/metio/storageunits/model/StorageUnitsParseTest.java diff --git a/storage-units-model/src/main/java/wtf/metio/storageunits/model/StorageUnits.java b/storage-units-model/src/main/java/wtf/metio/storageunits/model/StorageUnits.java index 22d257e..7ca5747 100755 --- a/storage-units-model/src/main/java/wtf/metio/storageunits/model/StorageUnits.java +++ b/storage-units-model/src/main/java/wtf/metio/storageunits/model/StorageUnits.java @@ -7,6 +7,7 @@ package wtf.metio.storageunits.model; import edu.umd.cs.findbugs.annotations.CheckReturnValue; import org.jetbrains.annotations.NotNull; +import java.math.BigDecimal; import java.math.BigInteger; import java.text.DecimalFormat; import java.text.Format; @@ -24,6 +25,57 @@ public final class StorageUnits { } /** + * @param value The storage unit as string + * @return The best matching binary- or decimal-prefixed unit for the given input. + */ + @CheckReturnValue + public static @NotNull StorageUnit parse(final @NotNull String value) { + final var trimmed = value.trim(); + + if (trimmed.endsWith("kB")) { + return kilobyte(parseValue(value, "kB")); + } else if (trimmed.endsWith("MB")) { + return megabyte(parseValue(value, "MB")); + } else if (trimmed.endsWith("GB")) { + return gigabyte(parseValue(value, "GB")); + } else if (trimmed.endsWith("TB")) { + return terabyte(parseValue(value, "TB")); + } else if (trimmed.endsWith("PB")) { + return petabyte(parseValue(value, "PB")); + } else if (trimmed.endsWith("EB")) { + return exabyte(parseValue(value, "EB")); + } else if (trimmed.endsWith("ZB")) { + return zettabyte(parseValue(value, "ZB")); + } else if (trimmed.endsWith("YB")) { + return yottabyte(parseValue(value, "YB")); + } + + if (trimmed.endsWith("KiB")) { + return kibibyte(parseValue(value, "KiB")); + } else if (trimmed.endsWith("MiB")) { + return mebibyte(parseValue(value, "MiB")); + } else if (trimmed.endsWith("GiB")) { + return gibibyte(parseValue(value, "GiB")); + } else if (trimmed.endsWith("TiB")) { + return tebibyte(parseValue(value, "TiB")); + } else if (trimmed.endsWith("PiB")) { + return pebibyte(parseValue(value, "PiB")); + } else if (trimmed.endsWith("EiB")) { + return exbibyte(parseValue(value, "EiB")); + } else if (trimmed.endsWith("ZiB")) { + return zebibyte(parseValue(value, "ZiB")); + } else if (trimmed.endsWith("YiB")) { + return yobibyte(parseValue(value, "YiB")); + } + + return bytes(parseValue(value, "b")); + } + + private static @NotNull BigInteger parseValue(final @NotNull String value, final @NotNull String unit) { + return new BigDecimal(value.replace(unit, "").trim()).toBigInteger(); + } + + /** * @param bytes The amount to bytes to represent. * @return The appropriate binary-prefixed unit for the given amount of bytes. */ diff --git a/storage-units-model/src/test/java/wtf/metio/storageunits/model/StorageUnitsParseTest.java b/storage-units-model/src/test/java/wtf/metio/storageunits/model/StorageUnitsParseTest.java new file mode 100755 index 0000000..5dce10a --- /dev/null +++ b/storage-units-model/src/test/java/wtf/metio/storageunits/model/StorageUnitsParseTest.java @@ -0,0 +1,32 @@ +/* + * SPDX-FileCopyrightText: The Storage-Units Authors + * SPDX-License-Identifier: 0BSD + */ +package wtf.metio.storageunits.model; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; + +import java.math.BigInteger; +import java.util.Map; +import java.util.stream.Stream; + +class StorageUnitsParseTest { + + @TestFactory + Stream shouldParseStrings() { + return Map.ofEntries( + Map.entry("100", BigInteger.valueOf(100L)), + Map.entry("100 b", BigInteger.valueOf(100L)), + Map.entry("10 kB", BigInteger.valueOf(10L * 1000L)), + Map.entry("10 KiB", BigInteger.valueOf(10L * 1024L)), + Map.entry("5 MB", BigInteger.valueOf(5L * 1000L * 1000L)), + Map.entry("5 MiB", BigInteger.valueOf(5L * 1024L * 1024L)) + ) + .entrySet().stream() + .map(entry -> DynamicTest.dynamicTest(entry.getKey(), + () -> Assertions.assertEquals(entry.getValue(), StorageUnits.parse(entry.getKey()).inByte()))); + } + +} -- 2.11.4.GIT