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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
package de.cowtipper.cowlection.util;
import com.mojang.realmsclient.util.Pair;
import net.minecraft.util.EnumChatFormatting;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DurationFormatUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public final class Utils {
public static final Pattern VALID_UUID_PATTERN = Pattern.compile("^(\\w{8})-(\\w{4})-(\\w{4})-(\\w{4})-(\\w{12})$");
private static final Pattern VALID_USERNAME = Pattern.compile("^[\\w]{1,16}$");
private static final char[] LARGE_NUMBERS = new char[]{'k', 'm', 'b', 't'};
private Utils() {
}
public static boolean isValidUuid(String uuid) {
return VALID_UUID_PATTERN.matcher(uuid).matches();
}
public static boolean isValidMcName(String username) {
return VALID_USERNAME.matcher(username).matches();
}
public static String fancyCase(String string) {
return WordUtils.capitalizeFully(string.replace('_', ' '));
}
/**
* Turn timestamp into pretty-formatted duration and date details.
*
* @param timestamp last login/logout
* @return 1st: duration between timestamp and now in words; 2nd: formatted date if time differences is >24h, otherwise null
*/
public static Pair<String, String> getDurationAsWords(long timestamp) {
long duration = System.currentTimeMillis() - timestamp;
long daysPast = TimeUnit.MILLISECONDS.toDays(duration);
String dateFormatted = null;
if (daysPast > 1) {
dateFormatted = DateFormatUtils.format(timestamp, "dd-MMM-yyyy");
}
if (daysPast > 31) {
return Pair.of(
DurationFormatUtils.formatPeriod(timestamp, System.currentTimeMillis(), (daysPast > 365 ? "y 'years' " : "") + "M 'months' d 'days'"),
dateFormatted);
} else {
return Pair.of(
DurationFormatUtils.formatDurationWords(duration, true, true),
dateFormatted);
}
}
public static String getDurationAsWord(long timestamp) {
long duration = System.currentTimeMillis() - timestamp;
long secondsPast = TimeUnit.MILLISECONDS.toSeconds(duration);
if (secondsPast < 60) {
return secondsPast + " second" + (secondsPast > 1 ? "s" : "");
}
long minutesPast = TimeUnit.SECONDS.toMinutes(secondsPast);
if (minutesPast < 60) {
return minutesPast + " minute" + (minutesPast > 1 ? "s" : "");
}
long hoursPast = TimeUnit.MINUTES.toHours(minutesPast);
if (hoursPast < 24) {
return hoursPast + " hour" + (hoursPast > 1 ? "s" : "");
}
long daysPast = TimeUnit.HOURS.toDays(hoursPast);
if (daysPast < 31) {
return daysPast + " day" + (daysPast > 1 ? "s" : "");
}
double monthsPast = daysPast / 30.5d;
if (monthsPast < 12) {
return new DecimalFormat("0.#").format(monthsPast) + " month" + (monthsPast >= 2 ? "s" : "");
}
double yearsPast = monthsPast / 12d;
return new DecimalFormat("0.#").format(yearsPast) + " year" + (yearsPast >= 2 ? "s" : "");
}
public static String toRealPath(Path path) {
try {
return path.toRealPath().toString();
} catch (IOException e) {
e.printStackTrace();
return "file not found";
}
}
public static String toRealPath(File path) {
return toRealPath(path.toPath());
}
/**
* Formats a large number with abbreviations for each factor of a thousand (k, m, ...)
*
* @param number the number to format
* @return a String representing the number n formatted in a cool looking way.
* @see <a href="https://stackoverflow.com/a/4753866">Source</a>
*/
public static String formatNumberWithAbbreviations(double number) {
return formatNumberWithAbbreviations(number, 0);
}
private static String formatNumberWithAbbreviations(double number, int iteration) {
@SuppressWarnings("IntegerDivisionInFloatingPointContext") double d = ((long) number / 100) / 10.0;
boolean isRound = (d * 10) % 10 == 0; // true if the decimal part is equal to 0 (then it's trimmed anyway)
// this determines the class, i.e. 'k', 'm' etc
// this decides whether to trim the decimals
// (int) d * 10 / 10 drops the decimal
return d < 1000 ? // this determines the class, i.e. 'k', 'm' etc
(d > 99.9 || isRound || d > 9.99 ? // this decides whether to trim the decimals
(int) d * 10 / 10 : d + "" // (int) d * 10 / 10 drops the decimal
) + "" + LARGE_NUMBERS[iteration]
: formatNumberWithAbbreviations(d, iteration + 1);
}
/**
* Convert Roman numerals to their corresponding Arabic numeral
*
* @param roman Roman numeral
* @return Arabic numeral
* @see <a href="https://www.w3resource.com/javascript-exercises/javascript-math-exercise-22.php">Source</a>
*/
public static int convertRomanToArabic(String roman) {
if (roman == null) return -1;
int number = romanCharToArabic(roman.charAt(0));
for (int i = 1; i < roman.length(); i++) {
int current = romanCharToArabic(roman.charAt(i));
int previous = romanCharToArabic(roman.charAt(i - 1));
if (current <= previous) {
number += current;
} else {
number = number - previous * 2 + current;
}
}
return number;
}
private static int romanCharToArabic(char c) {
switch (c) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return -1;
}
}
/**
* Convert Arabic numerals to their corresponding Roman numerals
*
* @param number Arabic numerals
* @return Roman numerals
* @see <a href="https://stackoverflow.com/a/48357180">Source</a>
*/
public static String convertArabicToRoman(int number) {
if (number == 0) {
return "0";
}
String romanOnes = arabicToRomanChars(number % 10, "I", "V", "X");
number /= 10;
String romanTens = arabicToRomanChars(number % 10, "X", "L", "C");
number /= 10;
String romanHundreds = arabicToRomanChars(number % 10, "C", "D", "M");
number /= 10;
String romanThousands = arabicToRomanChars(number % 10, "M", "", "");
return romanThousands + romanHundreds + romanTens + romanOnes;
}
private static String arabicToRomanChars(int n, String one, String five, String ten) {
switch (n) {
case 1:
return one;
case 2:
return one + one;
case 3:
return one + one + one;
case 4:
return one + five;
case 5:
return five;
case 6:
return five + one;
case 7:
return five + one + one;
case 8:
return five + one + one + one;
case 9:
return one + ten;
}
return "";
}
/**
* Get the minion tier's color for chat formatting
*
* @param tier minion tier
* @return color code corresponding to the tier
*/
public static EnumChatFormatting getMinionTierColor(int tier) {
EnumChatFormatting tierColor;
switch (tier) {
case 1:
tierColor = EnumChatFormatting.WHITE;
break;
case 2:
case 3:
case 4:
tierColor = EnumChatFormatting.GREEN;
break;
case 5:
case 6:
case 7:
tierColor = EnumChatFormatting.DARK_PURPLE;
break;
case 8:
case 9:
case 10:
tierColor = EnumChatFormatting.RED;
break;
case 11:
tierColor = EnumChatFormatting.AQUA;
break;
default:
tierColor = EnumChatFormatting.OBFUSCATED;
}
return tierColor;
}
public static String booleanToSymbol(boolean value) {
return value ? EnumChatFormatting.GREEN + "✔" : EnumChatFormatting.RED + "✘";
}
public static <V> Map<String, V> getLastNMapEntries(Map<String, V> map, int numberOfElements) {
if (map == null || map.isEmpty()) {
return null;
}
if (map.size() <= numberOfElements) {
return map;
}
return map.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
.limit(numberOfElements)
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new));
}
}
|