-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.java
More file actions
80 lines (66 loc) · 2.53 KB
/
Strings.java
File metadata and controls
80 lines (66 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package net.theevilreaper.aves.util;
import org.jetbrains.annotations.Contract;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* The class contains some useful methods for string manipulation.
* Each method has a specific context for the game Minecraft and doesn't relate to a general string manipulation utility.
*
* @author theEvilReaper
* @version 1.0.0
* @since 1.2.0
*/
public final class Strings {
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";
private Strings() {
// Utility class nothing to instantiate
}
/**
* Centers a given text with a given length of a line.
*
* @param text The text to center
* @param lineLength The length of a line
* @return The centered text
*/
@Contract(value = "_, _ -> new", pure = true)
public static String centerText(String text, int lineLength) {
text = text.trim();
if (text.isEmpty()) {
throw new IllegalArgumentException("The text can not be empty");
}
if (lineLength < text.length()) {
throw new IllegalArgumentException("The length of the line must be greater than the text length");
}
int totalPadding = lineLength - text.length();
int leftPadding = totalPadding / 2;
int rightPadding = totalPadding / 2; // This ensures equal padding
return SPACE.repeat(leftPadding) + text + SPACE.repeat(rightPadding);
}
/**
* Convert a time value into the given format from the {@link TimeFormat} entry.
*
* @param timeFormat the format, which should be used for the conversion
* @param time the time who should be converted
* @return the converted time
*/
@Contract(pure = true, value = "_, _ -> new")
public static String getTimeString(TimeFormat timeFormat, int time) {
if (time <= 0) {
return timeFormat.getDefaultFormat();
}
int seconds = time % 60;
int totalMinutes = time / 60;
int minutes = totalMinutes % 60;
int hours = totalMinutes / 60;
LocalTime localTime = LocalTime.of(hours, minutes, seconds);
return switch (timeFormat) {
case HH_MM_SS -> localTime.format(HH_MM_SS);
case MM_SS -> localTime.format(MM_SS);
};
}
}