From af92a32ba11e8640e19e6f32da94666159e0bc2b Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Tue, 6 Oct 2020 10:45:52 +0300 Subject: [PATCH] Implemented a byte size formatter Formats MB, GB and TB bytesizes appropriately Affects issues: - Close #1511 --- .../formatting/ByteSizeFormatter.java | 42 +++++++++++++++++++ .../plan/delivery/formatting/Formatters.java | 11 ++++- .../json/PerformanceJSONCreator.java | 40 +++++++++--------- .../plan/placeholder/ServerPlaceHolders.java | 6 +-- 4 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/ByteSizeFormatter.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/ByteSizeFormatter.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/ByteSizeFormatter.java new file mode 100644 index 000000000..451dae9e3 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/ByteSizeFormatter.java @@ -0,0 +1,42 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.delivery.formatting; + +/** + * Formatter for amount of Megabytes (MB) + * + * @author Rsl1122 + */ +public class ByteSizeFormatter implements Formatter { + + private final DecimalFormatter decimals; + + public ByteSizeFormatter(DecimalFormatter decimals) { + this.decimals = decimals; + } + + @Override + public String apply(Double value) { + if (value > 1000000) { + return decimals.apply(value / 1000000) + " TB"; + } + if (value > 1000) { + return decimals.apply(value / 1000) + " GB"; + } + return decimals.apply(value) + " MB"; + } +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/Formatters.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/Formatters.java index 2239e3a78..d147fa950 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/Formatters.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/Formatters.java @@ -49,6 +49,7 @@ public class Formatters { private final DecimalFormatter decimalFormatter; private final PercentageFormatter percentageFormatter; + private final ByteSizeFormatter byteSizeFormatter; @Inject public Formatters(PlanConfig config, Locale locale) { @@ -69,7 +70,7 @@ public class Formatters { decimalFormatter = new DecimalFormatter(config); percentageFormatter = new PercentageFormatter(decimalFormatter); - + byteSizeFormatter = new ByteSizeFormatter(decimalFormatter); } public Formatter year() { @@ -127,4 +128,12 @@ public class Formatters { public Formatter decimals() { return decimalFormatter; } + + public Formatter byteSize() { + return byteSizeFormatter; + } + + public Formatter byteSizeLong() { + return value -> byteSizeFormatter.apply((double) value); + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PerformanceJSONCreator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PerformanceJSONCreator.java index 26f57349d..c3747e2e7 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PerformanceJSONCreator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PerformanceJSONCreator.java @@ -50,8 +50,9 @@ public class PerformanceJSONCreator implements ServerTabJSONCreator decimals; - private final Formatter timeAmountFormatter; - private final Formatter percentageFormatter; + private final Formatter timeAmount; + private final Formatter percentage; + private final Formatter byteSize; @Inject public PerformanceJSONCreator( @@ -65,8 +66,9 @@ public class PerformanceJSONCreator implements ServerTabJSONCreator createJSONAsMap(UUID serverUUID) { @@ -97,9 +99,9 @@ public class PerformanceJSONCreator implements ServerTabJSONCreator createInsightsMap(List tpsData) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java index 790e3bb74..4c05d4fa4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java @@ -81,13 +81,13 @@ public class ServerPlaceHolders implements Placeholders { () -> percentage.apply(database.query(TPSQueries.averageCPU(monthAgo(), now(), serverUUID)))); placeholders.registerStatic("server_ram_day", - () -> database.query(TPSQueries.averageRAM(dayAgo(), now(), serverUUID)) + " MB"); + () -> formatters.byteSizeLong().apply(database.query(TPSQueries.averageRAM(dayAgo(), now(), serverUUID)))); placeholders.registerStatic("server_ram_week", - () -> database.query(TPSQueries.averageRAM(weekAgo(), now(), serverUUID)) + " MB"); + () -> formatters.byteSizeLong().apply(database.query(TPSQueries.averageRAM(weekAgo(), now(), serverUUID)))); placeholders.registerStatic("server_ram_month", - () -> database.query(TPSQueries.averageRAM(monthAgo(), now(), serverUUID)) + " MB"); + () -> formatters.byteSizeLong().apply(database.query(TPSQueries.averageRAM(monthAgo(), now(), serverUUID)))); placeholders.registerStatic("server_chunks_day", () -> database.query(TPSQueries.averageChunks(dayAgo(), now(), serverUUID)));