Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/main/java/net/theevilreaper/aves/util/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import org.jetbrains.annotations.Contract;

import java.util.StringJoiner;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
* The class contains some useful methods for string manipulation.
Expand All @@ -14,8 +15,11 @@
*/
public final class Strings {

private static final String INT_FORMAT = "%02d";
private static final int TIME_DIVIDER = 60;
private static final DateTimeFormatter HH_MM_SS =
DateTimeFormatter.ofPattern("HH:mm:ss");

private static final DateTimeFormatter MM_SS =
DateTimeFormatter.ofPattern("mm:ss");

public static final String SPACE = " ";
public static final String UTF_8_HEART = "\u2665";
Expand Down Expand Up @@ -55,24 +59,22 @@ public static String centerText(String text, int lineLength) {
* @param time the time who should be converted
* @return the converted time
*/
@Contract(pure = true)
@Contract(pure = true, value = "_, _ -> new")
public static String getTimeString(TimeFormat timeFormat, int time) {
if (time <= 0) {
return timeFormat.getDefaultFormat();
}

int minutes = time / TIME_DIVIDER;
int seconds = time % TIME_DIVIDER;
int seconds = time % 60;
int totalMinutes = time / 60;
int minutes = totalMinutes % 60;
int hours = totalMinutes / 60;

StringJoiner stringJoiner = new StringJoiner(":");
LocalTime localTime = LocalTime.of(hours, minutes, seconds);

if (timeFormat == TimeFormat.HH_MM_SS) {
int hours = minutes / TIME_DIVIDER;
minutes = minutes % TIME_DIVIDER;
stringJoiner.add(String.format(INT_FORMAT, hours));
}
stringJoiner.add(String.format(INT_FORMAT, minutes));
stringJoiner.add(String.format(INT_FORMAT, seconds));
return stringJoiner.toString();
return switch (timeFormat) {
case HH_MM_SS -> localTime.format(HH_MM_SS);
case MM_SS -> localTime.format(MM_SS);
};
}
}
21 changes: 5 additions & 16 deletions src/main/java/net/theevilreaper/aves/util/TimeFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,18 @@ public enum TimeFormat {
/**
* Indicates a format which contains minutes and seconds.
*/
MM_SS("00:00"),
MM_SS,
/**
* Indicates a format which contains hours, minutes and seconds.
*/
HH_MM_SS("00:00:00");

private final String defaultFormat;
HH_MM_SS;

/**
* Creates a new entry from the {@link TimeFormat} with the given format.
* Returns the default format for the given enum entry.
*
* @param defaultFormat the format to set
*/
TimeFormat(String defaultFormat) {
this.defaultFormat = defaultFormat;
}

/**
* Returns the default format from the specific format.
*
* @return the given format
* @return the default format
*/
public String getDefaultFormat() {
return defaultFormat;
return this == HH_MM_SS ? "00:00:00" : "00:00";
}
}
2 changes: 0 additions & 2 deletions src/test/java/net/theevilreaper/aves/util/StringsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class StringsTest {
private static final Stream<Arguments> TIME_ARGUMENTS = Stream.of(
Arguments.of("00:00", Strings.getTimeString(TimeFormat.MM_SS, 0)),
Arguments.of("01:00", Strings.getTimeString(TimeFormat.MM_SS, 60)),
Arguments.of("70:00", Strings.getTimeString(TimeFormat.MM_SS, 4200)),
Arguments.of("70:55", Strings.getTimeString(TimeFormat.MM_SS, 4255)),
Arguments.of("01:00:00", Strings.getTimeString(TimeFormat.HH_MM_SS, 3600)),
Arguments.of("00:59:59", Strings.getTimeString(TimeFormat.HH_MM_SS, 3599)),
Arguments.of("01:01:39", Strings.getTimeString(TimeFormat.HH_MM_SS, 3699)),
Expand Down