-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path133.java
More file actions
502 lines (462 loc) · 12.5 KB
/
Copy path133.java
File metadata and controls
502 lines (462 loc) · 12.5 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
/*
* Copyright (c) 2012-2015, Andrea Funto'. All rights reserved. See LICENSE for details.
*/
package org.dihedron.core.strings;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import org.dihedron.core.License;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility package for string operations.
*
* @author Andrea Funto'
*/
@License
public final class Strings {
/**
* The logger.
*/
private static final Logger logger = LoggerFactory.getLogger(Strings.class);
/**
* Whether the {@link #split(String, String)} operation should trim tokens
* before returning them, by default.
*/
public static final boolean DEFAULT_TRIM = true;
/**
* The default character sequence to use for telling tokens apart in the
* {@link #split(String)} method, and to join them in {@link #join(Object...)}.
*/
public static final String DEFAULT_SEPARATOR = ",";
/**
* The default character used for padding.
*/
public static final char DEFAULT_PADDING = ' ';
/**
* A textual representation of the null string.
*/
public static final String NULL = "<null>";
/**
* Returns the given object as a string, if not null, otherwise
* returns null.
*
* @param object
* the object to be safely converted to a string.
* @return
* the string representation of the object, or null.
*/
public static String asString(Object object) {
return asString(object, null);
}
/**
* Returns the given object as a string, if not null, otherwise
* returns null.
*
* @param object
* the object to be safely converted to a string.
* @param otherwise
* if the object is null, return this value instead.
* @return
* the string representation of the object if not null, the default
* value passed in otherwise.
*/
public static String asString(Object object, String otherwise) {
return object != null ? object.toString() : otherwise;
}
/**
* Checks whether the given string is neither null nor blank.
*
* @param string
* the string to be checked.
* @return
* <code>true</code> if the string is not null and has some content besides
* blank spaces.
*/
public static boolean isValid(String string) {
return (string != null && string.trim().length() > 0);
}
/**
* Checks whether all the given strings are neither null nor blank.
*
* @param strings
* the strings to be checked.
* @return
* <code>true</code> if all the strings are not null and have some content
* besides blank spaces.
*/
public static boolean areValid(String... strings) {
boolean result = false;
if(strings != null) {
result = true;
for(String string : strings) {
result = result && isValid(string);
}
}
return result;
}
/**
* Trims the input string if it is not null.
*
* @param string
* the string to be trimmed if not null.
* @return
* the trimmed string, or null.
*/
public static String trim(String string) {
if(string != null) {
return string.trim();
}
return string;
}
/**
* Trims the given string from blank characters at the beginning.
*
* @param string
* the string to be trimmed if not null.
* @return
* the trimmed string, or null.
*/
public static String trimLeft(String string) {
if(string != null) {
return string.replaceAll("^\\s*", "");
}
return string;
}
/**
* Trims the given string from blank characters at the end.
*
* @param string
* the string to be trimmed if not null.
* @return
* the trimmed string, or null.
*/
public static String trimRight(String string) {
if(string != null) {
return string.replaceAll("\\s*$", "");
}
return string;
}
/**
* Returns a safe concatenation of the input strings, or null if all strings
* are null.
*
* @param strings
* the set of string to concatenate.
* @return
* the concatenation of the input strings, or null if all strings are null.
*/
public static String concatenate(String... strings) {
return join("", strings);
}
/**
* Splits the input string using the default separator (',') to identify its
* tokens; it will also trim the tokens.
*
* @param strings
* the string to split.
* @return
* an array of tokens.
*/
public static String[] split(String strings) {
return split(strings, DEFAULT_SEPARATOR);
}
/**
* Splits the input string using the given separator to identify its tokens;
* it will also trim the tokens.
*
* @param strings
* the string to split.
* @param separator
* the character sequence to use as a token separator.
* @return
* an array of tokens.
*/
public static String[] split(String strings, String separator) {
return split(strings, separator, DEFAULT_TRIM);
}
/**
* Splits the input string using the given separator to identify its tokens;
* it will also trim the tokens, depending on the trim parameter.
*
* @param strings
* the string to split.
* @param separator
* the character sequence to use as a token separator.
* @param trim
* whether the tokens should be trimmed.
* @return
* an array of tokens.
*/
public static String[] split(String strings, String separator, boolean trim) {
StringTokeniser tokeniser = new StringTokeniser(separator);
String [] tokens = tokeniser.tokenise(strings);
if(trim) {
for(int i = 0; i < tokens.length; ++i) {
tokens[i] = tokens[i] != null ? tokens[i].trim() : tokens[i];
}
}
return tokens;
}
public static String join(Object... objects) {
return join(DEFAULT_SEPARATOR, DEFAULT_TRIM, objects);
}
public static String join(String separator, Object... objects) {
return join(separator, DEFAULT_TRIM, objects);
}
public static String join(boolean trim, Object... objects) {
return join(DEFAULT_SEPARATOR, trim, objects);
}
public static String join(String separator, boolean trim, Object... objects) {
StringBuffer buffer = new StringBuffer();
if(objects != null) {
for(Object object : objects) {
if(object != null) {
if(buffer.length() != 0) {
buffer.append(separator);
}
buffer.append(trim ? object.toString().trim() : object);
}
}
}
return buffer.toString();
}
/**
* Joins the given set of strings using the provided separator.
*
* @param separator
* a character sequence used to separate strings.
* @param strings
* the set of strings to be joined.
* @return
* a string containing the list of input strings, or null if no valid input
* was provided.
*/
public static String join(String separator, Collection<String> strings) {
if(strings != null) {
String[] array = new String[strings.size()];
return join(separator, strings.toArray(array));
}
return null;
}
/**
* Joins the given set of strings using the provided separator.
*
* @param separator
* a character sequence used to separate strings.
* @param strings
* the set of strings to be joined.
* @return
* a string containing the list of input strings, or null if no valid input
* was provided.
*/
public static String join(String separator, String... strings) {
StringBuilder builder = new StringBuilder();
if(strings != null) {
boolean first = true;
for(String string : strings) {
if(string != null) {
if(!first) {
builder.append(separator);
}
builder.append(string);
first = false;
}
}
if(builder.length() > 0) {
return builder.toString();
}
}
return null;
}
/**
* Formats an output string by centring the given input string and padding
* it with blanks.
*
* @param string
* the string to be centred.
* @param size
* the final size of the output string.
* @return
* a formatted string, where the input string is centred and the remaining
* space is filled with blanks.
*/
public static String centre(String string, int size) {
return centre(string, size, DEFAULT_PADDING);
}
/**
* Formats an output string by centering the given input string and padding
* it with the given character.
*
* @param string
* the string to be centred.
* @param size
* the final size of the output string.
* @param padding
* the character to be used as padding.
* @return
* a formatted string, where the input string is centred and the remaining
* space is filled with blanks.
*/
public static String centre(String string, int size, char padding) {
if(string == null || size <= string.length()) {
return string;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < (size - string.length()) / 2; i++) {
sb.append(padding);
}
sb.append(string);
while (sb.length() < size) {
sb.append(padding);
}
return sb.toString();
}
/**
* Formats a string by padding it with the default character to the left, until
* the given length is reached.
*
* @param string
* the string to pad.
* @param size
* the final length of the resulting padded string.
* @return
* the original string padded to the left.
*/
public static String padLeft(String string, int size) {
return padLeft(string, size, DEFAULT_PADDING);
}
/**
* Formats a string by padding it with the given character to the left, until
* the given length is reached.
*
* @param string
* the string to pad.
* @param size
* the final length of the resulting padded string.
* @param padding
* the character to use as padding.
* @return
* the original string padded to the left.
*/
public static String padLeft(String string, int size, char padding) {
if(string == null || size <= string.length()) {
return string;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < (size - string.length()); i++) {
sb.append(padding);
}
sb.append(string);
return sb.toString();
}
/**
* Formats a string by padding it with the default character to the right, until
* the given length is reached.
*
* @param string
* the string to pad.
* @param size
* the final length of the resulting padded string.
* @return
* the original string padded to the right.
*/
public static String padRight(String string, int size) {
return padRight(string, size, DEFAULT_PADDING);
}
/**
* Formats a string by padding it with the given character to the right, until
* the given length is reached.
*
* @param string
* the string to pad.
* @param size
* the final length of the resulting padded string.
* @param padding
* the character to use as padding.
* @return
* the original string padded to the right.
*/
public static String padRight(String string, int size, char padding) {
if(string == null || size <= string.length()) {
return string;
}
StringBuilder sb = new StringBuilder();
sb.append(string);
while (sb.length() < size) {
sb.append(padding);
}
return sb.toString();
}
/**
* Returns the first valid string in the given set.
*
* @param strings
* a set of strings.
* @return
* the first valid string in the set, or null if none is valid.
*/
public static String firstValidOf(String... strings) {
if(strings != null) {
for(int i = 0; i < strings.length; ++i) {
if(Strings.isValid(strings[i])) {
return strings[i];
}
}
}
return null;
}
/**
* Reverses the input string.
*
* @param string
* the string to be reversed.
* @return
* the reversed string.
*/
public static String reverse(String string) {
if(string != null) {
return new StringBuilder(string).reverse().toString();
}
return null;
}
/**
* Reads from the given input stream into a String, and returns it.
*
* @param stream
* the input stream; when the method completes the stream will be closed.
* @return
* the data read from the stream as a string, or {@code null}. if the input
* stream is not valid.
*/
public static String fromStream(InputStream stream) throws IOException {
if (stream != null) {
try {
Writer writer = new StringWriter();
Reader reader = new BufferedReader(new InputStreamReader(stream));
int read;
while ((read = reader.read()) != -1) {
writer.write(read);
}
return writer.toString();
} finally {
try {
stream.close();
} catch(IOException e) {
logger.warn("error closing the input stream", e);
}
}
}
return null;
}
/**
* Private constructor to prevent utility class instantiation.
*/
private Strings() {
}
}