-
Notifications
You must be signed in to change notification settings - Fork 17
/
StringUtils.java
264 lines (220 loc) · 7.09 KB
/
StringUtils.java
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
/**
* This file is part of LWC (https://github.com/Hidendra/LWC)
*
* This program is 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.
*
* This program 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 <http://www.gnu.org/licenses/>.
*/
package com.griefcraft.util;
import java.security.MessageDigest;
import java.util.Formatter;
public class StringUtils {
/**
* Parse a time string (e.g 2 hours)
*
* @param str
* @return the result in seconds
*/
public static long parseTime(String time) {
long seconds = 0L;
String[] split = time.split(" ");
for (int index = 0; index < split.length; index++) {
if (index == 0) {
continue;
}
String sub = split[index].toLowerCase();
int multiplier = 0; // e.g 2 hours = 2
long unit = 0; // e.g hours = 3600
try {
multiplier = Integer.parseInt(split[index - 1]);
} catch (NumberFormatException e) {
continue;
}
// attempt to match the unit time
if (sub.startsWith("second")) {
unit = 1;
} else if (sub.startsWith("minute")) {
unit = 60;
} else if (sub.startsWith("hour")) {
unit = 3600;
} else if (sub.startsWith("day")) {
unit = 86400;
} else if (sub.startsWith("week")) {
unit = 604800;
} else if (sub.startsWith("month")) {
unit = 2629743;
} else if (sub.startsWith("year")) {
unit = 31556926;
} // i do not think decade, century, millenium, etc is necessary beyond here :3
seconds += multiplier * unit;
}
return seconds;
}
/**
* Convert a given time in seconds to a more readable format
*
* @param time
* @return the time in a more readable format (e.g 2 days 5 hours 1 minute 34 seconds)
*/
public static String timeToString(long time) {
String str = "";
if ((System.currentTimeMillis() / 1000L) - time == 0L) {
return "Not yet known";
}
long days = time / 86400;
time = time - (days * 86400);
long hours = time / 3600;
time = time - (hours * 3600);
long minutes = time / 60;
time = time - (minutes * 60);
long seconds = time;
if (days > 0) {
str += days + " day" + (days == 1 ? "" : "s") + " ";
}
if (hours > 0) {
str += hours + " hour" + (hours == 1 ? "" : "s") + " ";
}
if (minutes > 0) {
str += minutes + " minute" + (minutes == 1 ? "" : "s") + " ";
}
if (seconds > 0) {
str += seconds + " second" + (seconds == 1 ? "" : "s") + " ";
}
return str.trim();
}
/**
* Capitalize the first letter in a word
*
* @param str
* @return
*/
public static String capitalizeFirstLetter(String str) {
if (str.length() == 0) {
return str;
} else if (str.length() == 1) {
return str.toUpperCase();
}
/*
* First cast it all to lower case and get the char array
*/
char[] string = str.toLowerCase().toCharArray();
/*
* First letter -- capitalize it
*/
string[0] = Character.toUpperCase(string[0]);
/*
* Scan for spaces
*/
for (int index = 0; index < string.length; index++) {
if (string[index] == ' ' && index != string.length) {
/*
* convert chars after spaces to upper case
*/
string[index + 1] = Character.toUpperCase(string[index + 1]);
}
}
return new String(string);
}
/**
* Encrypt a string using SHA1
*
* @param plaintext
* @return
*/
public static String encrypt(String plaintext) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA");
md.update(plaintext.getBytes("UTF-8"));
final byte[] raw = md.digest();
return byteArray2Hex(raw);
} catch (final Exception e) {
}
return "";
}
/**
* Check if the command has the correct flag
*
* @param args
* @param checkFlag
* @return
*/
public static boolean hasFlag(String[] args, String checkFlag) {
String flag = args[0].toLowerCase();
return flag.equals(checkFlag) || flag.equals("-" + checkFlag);
}
/**
* Check if the command has the correct flag
*
* @param command
* @param checkFlag
* @return
*/
public static boolean hasFlag(String command, String checkFlag) {
return command.equals(checkFlag) || command.equals("-" + checkFlag);
}
/**
* Join an array command into a String
*
* @param arr
* @param offset
* @return
*/
public static String join(String[] arr, int offset) {
return join(arr, offset, " ");
}
/**
* Join an array command into a String
*
* @param arr
* @param offset
* @param delim
* @return
*/
public static String join(String[] arr, int offset, String delim) {
String str = "";
if (arr == null || arr.length == 0) {
return str;
}
for (int i = offset; i < arr.length; i++) {
str += arr[i] + delim;
}
return str.trim();
}
/**
* Transform a string into one char
*
* @param str The string to transform
* @param chr The char to transform all chars to (ie '*')
* @return the transformed string
*/
public static String transform(String str, char chr) {
final char[] charArray = str.toCharArray();
for (int i = 0; i < charArray.length; i++) {
charArray[i] = chr;
}
return new String(charArray);
}
/**
* Convert a byte array to hex
*
* @param hash the hash to convert
* @return the converted hash
*/
private static String byteArray2Hex(byte[] hash) {
final Formatter formatter = new Formatter();
for (final byte b : hash) {
formatter.format("%02x", b);
}
return formatter.toString();
}
}