1 /***
2 * BakaTools - a simple utility library for Java
3 * Copyright (C) 2009 Martin Vysny
4 *
5 * This file is part of BakaTools.
6 *
7 * BakaTools is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * BakaTools is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with BakaTools. If not, see <http://www.gnu.org/licenses/>.
19 */
20 package sk.baka.tools;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.StringTokenizer;
26
27 /***
28 * Contains various Strings utilities.
29 * @author Martin Vysny
30 */
31 public final class StrUtils {
32
33 private StrUtils() {
34 throw new AssertionError();
35 }
36
37 /***
38 * Checks if given string is blank (null or whitespace-only).
39 * @param str the string to check
40 * @return true if the string is blank.
41 */
42 public static boolean isBlank(final String str) {
43 return str == null || str.trim().length() == 0;
44 }
45
46 /***
47 * Splits given string and returns a list of splitted trimmed strings.
48 * @param str the strings to split, may be null.
49 * @param delimiters the delimiters
50 * @return a list of trimmed tokens, never null, may be empty.
51 */
52 public static List<String> split(final String str, final String delimiters) {
53 if (isBlank(str)) {
54 return Collections.emptyList();
55 }
56 final StringTokenizer t = new StringTokenizer(str, delimiters);
57 final List<String> result = new ArrayList<String>();
58 for (; t.hasMoreTokens();) {
59 result.add(t.nextToken().trim());
60 }
61 return result;
62 }
63
64 /***
65 * Returns true if given string is null or zero-sized.
66 * @param msg the string to check
67 * @return true if null or zero-sized, false otherwise.
68 */
69 public static boolean isEmpty(String msg) {
70 return msg == null || msg.length() == 0;
71 }
72
73 /***
74 * Truncates given string if its length exceeds maximum length.
75 * @param string the string to truncate
76 * @param maxLength the maximum length
77 * @param start if true then leading characters are truncated, otherwise trailing characters are truncated
78 * @return truncated string
79 */
80 public static String truncate(final String string, final int maxLength, final boolean start) {
81 if (maxLength < 3) {
82 throw new IllegalArgumentException("maxLength must be greater than 2");
83 }
84 if (string == null) {
85 return null;
86 }
87 if (string.length() > maxLength) {
88 if (start) {
89 return "..." + string.substring(string.length() - maxLength + 3);
90 }
91 return string.substring(0, maxLength - 3) + "...";
92 }
93 return string;
94 }
95 }