From e97cbbc4f8f832a508de2e15fe72fe988f32ced0 Mon Sep 17 00:00:00 2001 From: scienziato1pazzo Date: Fri, 3 Jan 2025 20:04:30 +0100 Subject: [PATCH] Refractor of TimeUtil --- .../me/clip/placeholderapi/util/Format.java | 2 +- .../clip/placeholderapi/util/TimeFormat.java | 69 +++++++++----- .../me/clip/placeholderapi/util/TimeUtil.java | 92 ++++++------------- 3 files changed, 73 insertions(+), 90 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/util/Format.java b/src/main/java/me/clip/placeholderapi/util/Format.java index a149ec5..f3f4083 100644 --- a/src/main/java/me/clip/placeholderapi/util/Format.java +++ b/src/main/java/me/clip/placeholderapi/util/Format.java @@ -40,7 +40,7 @@ public final class Format { @NotNull public static Optional> tablify(@NotNull final Align align, - @NotNull final List> rows) { + @NotNull final List> rows) { return findSpacing(rows) .map(spacing -> buildFormat(align, spacing)) .map(format -> rows.stream() diff --git a/src/main/java/me/clip/placeholderapi/util/TimeFormat.java b/src/main/java/me/clip/placeholderapi/util/TimeFormat.java index f66f9da..84b27cd 100644 --- a/src/main/java/me/clip/placeholderapi/util/TimeFormat.java +++ b/src/main/java/me/clip/placeholderapi/util/TimeFormat.java @@ -1,28 +1,49 @@ -/* - * This file is part of PlaceholderAPI - * - * PlaceholderAPI - * Copyright (c) 2015 - 2024 PlaceholderAPI Team - * - * PlaceholderAPI free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * PlaceholderAPI 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - package me.clip.placeholderapi.util; +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.util.StringJoiner; +import java.util.function.IntUnaryOperator; + public enum TimeFormat { - DAYS, - HOURS, - MINUTES, - SECONDS + + DAYS(seconds -> seconds / 86400), + HOURS(seconds -> (seconds / 3600) % 24), + MINUTES(seconds -> (seconds / 60) % 60), + SECONDS(seconds -> seconds % 60); + + private final IntUnaryOperator convert; + + TimeFormat(IntUnaryOperator convert) { + this.convert = convert; + } + + public int convert(int seconds) { + return convert.applyAsInt(seconds); + } + + public String formatTime(Duration duration) { + return formatTime((int) duration.getSeconds()); + } + + public String formatTime(int seconds) { + final StringJoiner joiner = new StringJoiner(" "); + + int remainingSeconds = seconds; + int days = DAYS.convert(remainingSeconds); + remainingSeconds -= days * 86400; + + int hours = HOURS.convert(remainingSeconds); + remainingSeconds -= hours * 3600; + + int minutes = MINUTES.convert(remainingSeconds); + remainingSeconds -= minutes * 60; + + if (days > 0) joiner.add(days + "d"); + if (hours > 0) joiner.add(hours + "h"); + if (minutes > 0) joiner.add(minutes + "m"); + if (remainingSeconds > 0) joiner.add(remainingSeconds + "s"); + + return joiner.toString(); + } } diff --git a/src/main/java/me/clip/placeholderapi/util/TimeUtil.java b/src/main/java/me/clip/placeholderapi/util/TimeUtil.java index 4e7dbef..68eb0bc 100644 --- a/src/main/java/me/clip/placeholderapi/util/TimeUtil.java +++ b/src/main/java/me/clip/placeholderapi/util/TimeUtil.java @@ -1,85 +1,47 @@ -/* - * This file is part of PlaceholderAPI - * - * PlaceholderAPI - * Copyright (c) 2015 - 2024 PlaceholderAPI Team - * - * PlaceholderAPI free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * PlaceholderAPI 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - package me.clip.placeholderapi.util; import java.time.Duration; -import java.time.temporal.ChronoUnit; -import java.util.StringJoiner; public class TimeUtil { + /** + * Retrieves the remaining time in the specified format. + * + * @param seconds the total number of seconds to convert + * @param type the desired time format (DAYS, HOURS, MINUTES, SECONDS) + * @return a string representing the remaining value in the specified format + */ public static String getRemaining(final int seconds, final TimeFormat type) { - return getRemaining((long) seconds, type); - } - - public static String getRemaining(final long seconds, final TimeFormat type) { - switch (type) { - default: - return String.valueOf(seconds); - - case SECONDS: - return String.valueOf(seconds % 60); - - case MINUTES: - return String.valueOf((seconds / 60) % 60); - - case HOURS: - return String.valueOf((seconds / 3600) % 24); - - case DAYS: - return String.valueOf(seconds / 86400); - } + return String.valueOf(type.convert(seconds)); } /** - * Format the given value with s, m, h and d (seconds, minutes, hours and days) + * Formats the given duration into a human-readable string. * - * @param duration {@link Duration} (eg, Duration.of(20, {@link ChronoUnit#SECONDS}) for 20 - * seconds) - * @return formatted time + * @param duration the {@link Duration} to be formatted (e.g., Duration.of(20, ChronoUnit.SECONDS)) + * @return a formatted string representing the duration (e.g., "1d 3h 20m 15s") */ public static String getTime(final Duration duration) { - return getTime(duration.getSeconds()); + return TimeFormat.DAYS.formatTime(duration); } + /** + * Formats the given number of seconds into a human-readable string. + * + * @param seconds the total number of seconds to format + * @return a formatted string representing the time (e.g., "1d 3h 20m 15s") + */ public static String getTime(final int seconds) { - return getTime((long) seconds); + return TimeFormat.DAYS.formatTime(seconds); } - public static String getTime(long seconds) { - final StringJoiner joiner = new StringJoiner(" "); - - long minutes = seconds / 60; - long hours = minutes / 60; - final long days = hours / 24; - - seconds %= 60; - minutes %= 60; - hours %= 24; - - if (days > 0) joiner.add(days + "d"); - if (hours > 0) joiner.add(hours + "h"); - if (minutes > 0) joiner.add(minutes + "m"); - if (seconds > 0) joiner.add(seconds + "s"); - - return joiner.toString(); + /** + * Formats the given number of seconds into a human-readable string. + * + * @param seconds the total number of seconds to format + * @return a formatted string representing the time (e.g., "1d 3h 20m 15s") + */ + public static String getTime(final long seconds) { + return TimeFormat.DAYS.formatTime((int) seconds); } }