-
Notifications
You must be signed in to change notification settings - Fork 17
/
StringUtils.java
280 lines (234 loc) · 8.18 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright 2011 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package com.griefcraft.util;
import java.security.MessageDigest;
import java.util.Formatter;
public class StringUtils {
/**
* Parse a time string (e.g 2 hours)
*
* @param time
* @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 <= 0) {
return "Not yet known";
}
long days = time / 86400;
time -= days * 86400;
long hours = time / 3600;
time -= hours * 3600;
long minutes = time / 60;
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") + " ";
}
if (str.equals("")) {
return "less than a second";
}
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();
}
char[] string = str.toLowerCase().toCharArray();
// First letter - capatalize 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 found spaces to uppercase
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 into a String, where the array values are delimited by spaces.
*
* @param arr
* @return
*/
public static String join(String[] arr) {
return join(arr, 0);
}
/**
* Join an array into a String, where the array values are delimited by spaces, starting at the given offset.
*
* @param arr
* @param offset
* @return
*/
public static String join(String[] arr, int offset) {
return join(arr, offset, " ");
}
/**
* Join an array into a String, where the array values are delimited by the given string, starting at the given offset.
*
* @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();
}
}