-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path128.java
More file actions
523 lines (454 loc) · 15.1 KB
/
Copy path128.java
File metadata and controls
523 lines (454 loc) · 15.1 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package org.codelogger.utils;
import static com.google.common.collect.Lists.newArrayList;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.codelogger.utils.component.StringProcessChain;
/**
* A useful tools to handle string, likes judge blank or not blank, first
* character to upper or lower case, index with, judge contains, encoding,
* replace, delete and so on...
*
* @author DengDefei
*/
public class StringUtils {
public static final int INDEX_OF_NOT_FOUND = -1;
private final static String WORD_PATTERN = "[\\p{N}\\p{L}]+[\\p{Pd}]|[\\p{N}\\p{L}]+";
private final static Vector<Pattern> patternCache = new Vector<Pattern>(1);
private StringUtils() {
}
/**
* Returns true if the source string is null or length = 0; false otherwise.
*
* @param source string to be tested.
* @return true if the source string is null or length = 0; false otherwise.
*/
public static boolean isEmpty(final String source) {
return source == null || source.length() == 0;
}
/**
* Returns true if the source string is null or all characters in this string
* is space; false otherwise.
*
* @param source string to be tested.
* @return true if the source string is null or all characters in this string
* is space; false otherwise.
*/
public static boolean isBlank(final String source) {
if (isEmpty(source)) {
return true;
}
int strLen = source.length();
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(source.charAt(i))) {
return false;
}
}
return true;
}
/**
* Returns true if the source string is not null and at least one character in
* this string is not space; false otherwise.
*
* @param source string to be tested.
* @return true if the source string is not null and at least one character in
* this string is not space; false otherwise.
*/
public static boolean isNotBlank(final String source) {
return !isBlank(source);
}
/**
* Convert first character in given string to upper case.<br>
* If given source string is blank or first character is upper case, return
* it's self.
*
* @param source string to be tested.
* @return a new string has been converted or it's self.
*/
public static String firstCharToUpperCase(final String source) {
if (isBlank(source) || Character.isUpperCase(source.charAt(0))) {
return source;
}
StringBuilder destination = getStringBuild();
destination.append(source.substring(0, 1).toUpperCase());
destination.append(source.substring(1));
return destination.toString();
}
/**
* Convert first character in given string to lower case.<br>
* If given source string is blank or first character is lower case, return
* it's self.
*
* @param source string want to be handle.
* @return a new string has been converted or it's self.
*/
public static String firstCharToLowerCase(final String source) {
if (isBlank(source) || Character.isLowerCase(source.charAt(0))) {
return source;
}
StringBuilder destination = getStringBuild();
destination.append(source.substring(0, 1).toLowerCase());
destination.append(source.substring(1));
return destination.toString();
}
/**
* Returns the index within given source string of the first occurrence of the
* specified target string with ignore case sensitive.
*
* @param source source string to be tested.
* @param target target string to be tested.
* @return index number if found, -1 otherwise.
*/
public static int indexOfIgnoreCase(final String source, final String target) {
int targetIndex = source.indexOf(target);
if (targetIndex == INDEX_OF_NOT_FOUND) {
String sourceLowerCase = source.toLowerCase();
String targetLowerCase = target.toLowerCase();
targetIndex = sourceLowerCase.indexOf(targetLowerCase);
return targetIndex;
} else {
return targetIndex;
}
}
/**
* Returns the index within given source string of the first occurrence of the
* specified target string with ignore case sensitive, starting at the specify
* index.
*
* @param source source string to be tested.
* @param target target string to be tested.
* @return index number if found, -1 otherwise.
*/
public static int indexOfIgnoreCase(final String source, final String target, final int fromIndex) {
int targetIndex = source.indexOf(target, fromIndex);
if (targetIndex == INDEX_OF_NOT_FOUND) {
String sourceLowerCase = source.toLowerCase();
String targetLowerCase = target.toLowerCase();
targetIndex = sourceLowerCase.indexOf(targetLowerCase, fromIndex);
return targetIndex;
} else {
return targetIndex;
}
}
/**
* Returns true if given string have white span, false otherwise.<br>
*
* @param source string to be tested.
* @return true if given string have white span, false otherwise.
*/
public static boolean containsWhitespace(final String source) {
int strLen = source.length();
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(source.charAt(i))) {
return true;
}
}
return false;
}
/**
* Returns true if given string contains given target string ignore case,
* false otherwise.<br>
*
* @param source string to be tested.
* @param target string to be tested.
* @return true if given string contains given target string ignore case,
* false otherwise.
*/
public static boolean containsIgnoreCase(final String source, final String target) {
if (source != null && target != null) {
return indexOfIgnoreCase(source, target) != INDEX_OF_NOT_FOUND;
} else {
return false;
}
}
/**
* Returns words in given text by given word minimum length.<br>
* <p>
* e.g: <br>
* text: "I have a dream, but now it is only a dream."<br>
* wordMinimumLength: 2 <br>
* result:["have","dream","but","now","it","is","only","dream"].<br>
* <p>
* NB. "1word","word1" also were words.
*
* @param text source string to be handled.
* @param wordMinimumLength word minimum length to partition.
* @return words in given text by given word minimum length.
*/
public static List<String> getWords(final String text, final int wordMinimumLength) {
List<String> allWords = newArrayList();
if (text == null) {
return allWords;
}
Matcher matcher = matchText(text);
for (; matcher.find();) {
String word = matcher.group(0);
if (wordMinimumLength < 2) {
allWords.add(word);
} else {
if (word.length() >= wordMinimumLength) {
allWords.add(word);
}
}
}
return allWords;
}
/**
* 根据正则表达式提取字符串
*
* @param text 源字符串
* @param regex 正则表达式
* @return 所以符合正则表达式的字符串的集合
*/
public static List<String> getStringsByRegex(final String text, final String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
List<String> matchedStrings = new ArrayList<String>();
while (matcher.find()) {
matchedStrings.add(matcher.group(0));
}
return matchedStrings;
}
/**
* Returns true if given text contains given word; false otherwise.
*
* @param text string text to be tested.
* @param word string word to be tested.
* @return true if given text contains given word; false otherwise.
*/
public static boolean containsWord(final String text, final String word) {
if (text == null || word == null) {
return false;
}
if (text.contains(word)) {
Matcher matcher = matchText(text);
for (; matcher.find();) {
String matchedWord = matcher.group(0);
if (matchedWord.equals(word)) {
return true;
}
}
}
return false;
}
/**
* Returns true if given text contains given word ignore case; false
* otherwise.
*
* @param text string text to be tested.
* @param word string word to be tested.
* @return true if given text contains given word ignore case; false
* otherwise.
*/
public static boolean containsWordIgnoreCase(final String text, final String word) {
if (text == null || word == null) {
return false;
}
return containsWord(text.toLowerCase(), word.toLowerCase());
}
/**
* Trim all white space in the given string.<br>
* If given source string self if it's null or length == 0 or not have white
* space.
*
* @param source
* @return a new string has been removed all white space if given source
* string have white space; given source string self otherwise.
*/
public static String trimAllWhitespace(final String source) {
return StringProcessChain.getInstance(source).trimAllWhitespace().getResult();
}
/**
* Returns true if given source string start with target string ignore case
* sensitive; false otherwise.
*
* @param source string to be tested.
* @param target string to be tested.
* @return true if given source string start with target string ignore case
* sensitive; false otherwise.
*/
public static boolean startsWithIgnoreCase(final String source, final String target) {
if (source.startsWith(target)) {
return true;
}
if (source.length() < target.length()) {
return false;
}
return source.substring(0, target.length()).equalsIgnoreCase(target);
}
/**
* Returns true if given source string end with target string ignore case
* sensitive; false otherwise.
*
* @param source string to be tested.
* @param target string to be tested.
* @return true if given source string end with target string ignore case
* sensitive; false otherwise.
*/
public static boolean endsWithIgnoreCase(final String source, final String target) {
if (source.endsWith(target)) {
return true;
}
if (source.length() < target.length()) {
return false;
}
return source.substring(source.length() - target.length()).equalsIgnoreCase(target);
}
/**
* Start a string process chain for given source.
*
* @param source source String.
* @return return a StringProcessChain for given source.
*/
public static StringProcessChain startStringProcess(final String source) {
return StringProcessChain.getInstance(source);
}
/**
* Returns a random alphabetic: A-Z.
*
* @return a random alphabetic: A-Z
*/
public static char getRandomUpperCaseAlphabetic() {
return (char) MathUtils.randomInt(65, 90);
}
/**
* Returns a random alphabetic: a-z.
*
* @return a random alphabetic: a-z
*/
public static char getRandomLowerCaseAlphabetic() {
return (char) MathUtils.randomInt(97, 122);
}
/**
* Returns a random alphabetic: a-zA-Z.
*
* @return a random alphabetic: a-zA-Z
*/
public static char getRandomAlphabetic() {
int randomInt = MathUtils.randomInt(1);
if (randomInt == 1) {
return getRandomUpperCaseAlphabetic();
} else {
return getRandomLowerCaseAlphabetic();
}
}
/**
* Returns a random given length size alphabetic string.
*
* @param stringLength the length of the random string you want.
* @return a random given length size alphabetic string.
*/
public static String getRandomString(final int stringLength) {
StringBuilder stringBuilder = getStringBuild();
for (int i = 0; i < stringLength; i++) {
stringBuilder.append(getRandomAlphabetic());
}
return stringBuilder.toString();
}
/**
* Returns a random passowrd char: a-zA-Z0-9.
*
* @return a random passowrd char: a-zA-Z0-9
*/
public static char getRandomPasswordChar() {
int randomInt = MathUtils.randomInt(2);
if (randomInt == 0) {
return (char) MathUtils.randomInt(48, 57);
} else {
return getRandomAlphabetic();
}
}
/**
* Returns a random given length size passowrd string([a-zA-Z0-9]+).
*
* @param stringLength the length of the random passowrd you want.
* @return a random given length size passowrd string([a-zA-Z0-9]+).
*/
public static String getRandomPasswordString(final int stringLength) {
StringBuilder stringBuilder = getStringBuild();
for (int i = 0; i < stringLength; i++) {
stringBuilder.append(getRandomPasswordChar());
}
return stringBuilder.toString();
}
/**
* Return encoded string by given charset.
*
* @param source source string to be handle.
* @param sourceCharset source string charset name.
* @param encodingCharset want encoding to which charset.
* @return a new string has been encoded.
* @throws IllegalArgumentExceptionWORD_PATTERN If the named charset is not
* supported
*/
public static String encoding(final String source, final String sourceCharset,
final String encodingCharset) throws IllegalArgumentException {
byte[] sourceBytes;
String encodeString = null;
try {
sourceBytes = source.getBytes(sourceCharset);
encodeString = new String(sourceBytes, encodingCharset);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(String.format("Unsupported encoding:%s or %s",
sourceCharset, encodingCharset));
}
return encodeString;
}
/**
* Sort the array in reverse order.</br>反转字符串。
*
* @param string string to be handled.
*/
public static String reverse(final String string) {
return isEmpty(string) ? string : new StringBuilder(string).reverse().toString();
}
/**
* Count how much target in souce string.</br>统计target在source中出现的次数。
*
* @param source source string
* @param target target string
* @return the count of target in source string.
*/
public static int count(final String source, final String target) {
if (isEmpty(source) || isEmpty(target)) {
return 0;
}
int targetLength = target.length();
int replacedLength = source.length() - source.replace(target, "").length();
return replacedLength / targetLength;
}
/**
* Count how much target in souce string.</br>统计target在source中出现的次数。
*
* @param source source string
* @param target target string
* @return the count of target in source string.
*/
public static int countIgnoreCase(final String source, final String target) {
if (isEmpty(source) || isEmpty(target)) {
return 0;
}
return count(source.toLowerCase(), target.toLowerCase());
}
private static StringBuilder getStringBuild() {
return new StringBuilder();
}
private static synchronized Pattern getWordPatternFromCache() {
if (patternCache.isEmpty()) {
return Pattern.compile(WORD_PATTERN);
} else {
Pattern pattern = patternCache.get(0);
patternCache.remove(0);
return pattern;
}
}
private static Matcher matchText(final String text) {
Pattern pattern = getWordPatternFromCache();
Matcher matcher = pattern.matcher(text);
patternCache.add(pattern);
return matcher;
}
}