|
@@ -1,36 +1,288 @@ |
1 |
|
- |
package com.temp.shared.utils; |
2 |
|
- |
|
3 |
|
- |
public class StringUtils { |
4 |
|
- |
public static String deNull(String pString) { |
5 |
|
- |
return deNull(pString, ""); |
6 |
|
- |
} |
7 |
|
- |
|
8 |
|
- |
public static String deNull(String pString, String pDefault) { |
9 |
|
- |
return (pString != null) ? pString : pDefault; |
10 |
|
- |
} |
11 |
|
- |
|
12 |
|
- |
public static String noEmpty(String pString) { |
13 |
|
- |
return ((pString = deNull(pString).trim()).length() != 0) ? pString : null; |
14 |
|
- |
} |
15 |
|
- |
|
16 |
|
- |
/** |
17 |
|
- |
* return the index of the first character that is unacceptable for that position to be part of an Identifier. |
18 |
|
- |
* |
19 |
|
- |
* An Identifier is a String that stars with a 7bit Alpha or underscore, and is followed by any number of 7bit AlphaNumerics or underscores. |
20 |
|
- |
* |
21 |
|
- |
* @param toCheck not null or empty |
22 |
|
- |
* |
23 |
|
- |
* @return -1 if OK, otherwise the 'bad' character index. |
24 |
|
- |
*/ |
25 |
|
- |
public static int checkIdentifier(String toCheck) { |
26 |
|
- |
if (!CharacterUtils.is7bitAlphaUnderScore( toCheck.charAt( 0 ) )) { |
27 |
|
- |
return 0; |
28 |
|
- |
} |
29 |
|
- |
for (int i = 1; i <toCheck.length(); i++) { |
30 |
|
- |
if (!CharacterUtils.isAlphaNumericUnderScore7bitAscii( toCheck.charAt( i ) )) { |
31 |
|
- |
return i; |
32 |
|
- |
} |
33 |
|
- |
} |
34 |
|
- |
return -1; |
35 |
|
- |
} |
36 |
|
- |
} |
|
1 |
+ |
package com.temp.shared.utils; |
|
2 |
+ |
|
|
3 |
+ |
import java.util.Iterator; |
|
4 |
+ |
|
|
5 |
+ |
/** |
|
6 |
+ |
* Utility methods around Strings (either as parameters or return values) to |
|
7 |
+ |
* reduce the code in other places (e.g. the Activities). |
|
8 |
+ |
* |
|
9 |
+ |
* @author georgs |
|
10 |
+ |
*/ |
|
11 |
+ |
public class StringUtils { |
|
12 |
+ |
public static final String[] EMPTY_ARRAY = new String[0]; |
|
13 |
+ |
|
|
14 |
+ |
public static final String EMPTY_STRING = ""; |
|
15 |
+ |
public static final String CRNL = "\r\n"; |
|
16 |
+ |
public static final char CRchar = '\r'; |
|
17 |
+ |
public static final char NLchar = '\n'; |
|
18 |
+ |
|
|
19 |
+ |
public static String join(Iterable<?> iterable, String joinString) { |
|
20 |
+ |
Iterator<?> iter = iterable.iterator(); |
|
21 |
+ |
if (iter.hasNext()) { |
|
22 |
+ |
StringBuffer buf = new StringBuffer(translateToString(iter.next())); |
|
23 |
+ |
while (iter.hasNext()) { |
|
24 |
+ |
buf.append(joinString); |
|
25 |
+ |
buf.append(translateToString(iter.next())); |
|
26 |
+ |
} |
|
27 |
+ |
return buf.toString(); |
|
28 |
+ |
} |
|
29 |
+ |
return ""; |
|
30 |
+ |
} |
|
31 |
+ |
|
|
32 |
+ |
private static String translateToString(Object next) { |
|
33 |
+ |
return next == null ? "<NULL>" : next.toString(); |
|
34 |
+ |
} |
|
35 |
+ |
|
|
36 |
+ |
public static boolean isBlank(String ... strings) { |
|
37 |
+ |
if(strings == null) |
|
38 |
+ |
return true; |
|
39 |
+ |
for(String string : strings){ |
|
40 |
+ |
if(string == null || string.trim().isEmpty()){ |
|
41 |
+ |
return true; |
|
42 |
+ |
} |
|
43 |
+ |
} |
|
44 |
+ |
return false; |
|
45 |
+ |
} |
|
46 |
+ |
|
|
47 |
+ |
public static String noEmpty(String s) { |
|
48 |
+ |
if (s != null) { |
|
49 |
+ |
if ((s = s.trim()).isEmpty()) { |
|
50 |
+ |
s = null; |
|
51 |
+ |
} |
|
52 |
+ |
} |
|
53 |
+ |
return s; |
|
54 |
+ |
} |
|
55 |
+ |
|
|
56 |
+ |
public static String noEmpty(String s, String defaultValue) { |
|
57 |
+ |
return deNull(noEmpty(s), defaultValue); |
|
58 |
+ |
} |
|
59 |
+ |
|
|
60 |
+ |
public static String deNull(String s) { |
|
61 |
+ |
return deNull(s, ""); |
|
62 |
+ |
} |
|
63 |
+ |
|
|
64 |
+ |
public static String deNull(String s, String defaultValue) { |
|
65 |
+ |
return (s != null) ? s : defaultValue; |
|
66 |
+ |
} |
|
67 |
+ |
|
|
68 |
+ |
public static String deNullToString(Object value, Object defaultValue) { |
|
69 |
+ |
return deNull(noEmptyToString(value), noEmptyToString(defaultValue)); |
|
70 |
+ |
} |
|
71 |
+ |
|
|
72 |
+ |
public static String noEmptyToString(Object value) { |
|
73 |
+ |
return noEmpty(nullOKtoString(value)); |
|
74 |
+ |
} |
|
75 |
+ |
|
|
76 |
+ |
public static String nullOKtoString(Object value) { |
|
77 |
+ |
return (value == null) ? null : value.toString(); |
|
78 |
+ |
} |
|
79 |
+ |
|
|
80 |
+ |
public static String nullToEmptytoString(Object value) { |
|
81 |
+ |
return (value == null) ? "" : value.toString(); |
|
82 |
+ |
} |
|
83 |
+ |
|
|
84 |
+ |
public static String firstLines(int maxLines, String message) { |
|
85 |
+ |
if (null == (message = normalizeNewLines(message))) { |
|
86 |
+ |
return message; |
|
87 |
+ |
} |
|
88 |
+ |
if (maxLines < 1) { |
|
89 |
+ |
return null; |
|
90 |
+ |
} |
|
91 |
+ |
StringBuilder sb = new StringBuilder(); |
|
92 |
+ |
int from = 0; |
|
93 |
+ |
for (int at; -1 != (at = message.indexOf(NLchar, from)); from = at + 1) { |
|
94 |
+ |
String part = message.substring(from, at); |
|
95 |
+ |
if (sb.length() != 0) { |
|
96 |
+ |
sb.append(NLchar); |
|
97 |
+ |
} |
|
98 |
+ |
if (!isBlank(part)) { |
|
99 |
+ |
sb.append(part); |
|
100 |
+ |
} |
|
101 |
+ |
if (sb.length() != 0) { |
|
102 |
+ |
if (--maxLines == 0) { |
|
103 |
+ |
return sb.toString(); |
|
104 |
+ |
} |
|
105 |
+ |
} |
|
106 |
+ |
} |
|
107 |
+ |
// Not full so add end as Line |
|
108 |
+ |
String end = message.substring(from); |
|
109 |
+ |
if (!isBlank(end)) { |
|
110 |
+ |
if (sb.length() != 0) { |
|
111 |
+ |
sb.append(NLchar); |
|
112 |
+ |
} |
|
113 |
+ |
sb.append(end); |
|
114 |
+ |
} |
|
115 |
+ |
return sb.toString(); |
|
116 |
+ |
} |
|
117 |
+ |
|
|
118 |
+ |
public static String normalizeNewLines(String message) { |
|
119 |
+ |
message = replaceAll(message, CRNL, NLchar); |
|
120 |
+ |
message = replaceAll(message, CRchar, NLchar); |
|
121 |
+ |
return message; |
|
122 |
+ |
} |
|
123 |
+ |
|
|
124 |
+ |
public static String replaceAll(String message, char of, char to) { |
|
125 |
+ |
return (message == null) ? message : message.replace(of, to); |
|
126 |
+ |
} |
|
127 |
+ |
|
|
128 |
+ |
/** |
|
129 |
+ |
* Non-RegEx version, as Java RegEx & Browser RegEx are NOT the same! |
|
130 |
+ |
*/ |
|
131 |
+ |
public static String replaceAll(String message, String of, String to) { |
|
132 |
+ |
if ((message != null) && (of != null) && (to != null)) { |
|
133 |
+ |
int removeFor = of.length(); |
|
134 |
+ |
int adjustBy = to.length(); |
|
135 |
+ |
for (int at, from = 0; -1 != (at = message.indexOf(of, from)); from = at + adjustBy) { |
|
136 |
+ |
message = message.substring(0, at) + to + message.substring(at + removeFor); |
|
137 |
+ |
} |
|
138 |
+ |
} |
|
139 |
+ |
return message; |
|
140 |
+ |
} |
|
141 |
+ |
|
|
142 |
+ |
/** |
|
143 |
+ |
* Simple String to char of the replaceAll |
|
144 |
+ |
*/ |
|
145 |
+ |
public static String replaceAll(String message, String of, char to) { |
|
146 |
+ |
if ((message != null) && (of != null)) { |
|
147 |
+ |
int removeFor = of.length(); |
|
148 |
+ |
for (int at, from = 0; -1 != (at = message.indexOf(of, from)); from = at + 1) { |
|
149 |
+ |
message = message.substring(0, at) + to + message.substring(at + removeFor); |
|
150 |
+ |
} |
|
151 |
+ |
} |
|
152 |
+ |
return message; |
|
153 |
+ |
} |
|
154 |
+ |
|
|
155 |
+ |
/** |
|
156 |
+ |
* Return the simple class name for an Object. |
|
157 |
+ |
* |
|
158 |
+ |
* This behavior is built into Java, but not currently supported in GWT |
|
159 |
+ |
*/ |
|
160 |
+ |
public static String simpleClassNameFor(Object o) { |
|
161 |
+ |
if (o == null) { |
|
162 |
+ |
return "null"; |
|
163 |
+ |
} |
|
164 |
+ |
String name = "." + o.getClass().getName(); |
|
165 |
+ |
return name.substring(name.lastIndexOf('.') + 1); |
|
166 |
+ |
} |
|
167 |
+ |
|
|
168 |
+ |
public static boolean toBoolean(String booleanAsString) { |
|
169 |
+ |
return Boolean.TRUE.toString().equalsIgnoreCase(noEmpty(booleanAsString)); |
|
170 |
+ |
} |
|
171 |
+ |
|
|
172 |
+ |
private static String runningMaxIndentSpaces = ""; |
|
173 |
+ |
|
|
174 |
+ |
public static synchronized String indent(int indent) { // 4 spaces per... |
|
175 |
+ |
if(indent < 1){ |
|
176 |
+ |
return ""; |
|
177 |
+ |
} |
|
178 |
+ |
int spacesNeeded = indent + indent + indent + indent; |
|
179 |
+ |
if (runningMaxIndentSpaces.length() < spacesNeeded){ |
|
180 |
+ |
StringBuilder sb = new StringBuilder(spacesNeeded); |
|
181 |
+ |
for (int i = spacesNeeded; i > 0; i--) { |
|
182 |
+ |
sb.append(' '); |
|
183 |
+ |
} |
|
184 |
+ |
return runningMaxIndentSpaces = sb.toString(); |
|
185 |
+ |
} |
|
186 |
+ |
return runningMaxIndentSpaces.substring(0,spacesNeeded); |
|
187 |
+ |
} |
|
188 |
+ |
|
|
189 |
+ |
/** |
|
190 |
+ |
* Wrap the text (into multiple lines as needed) such that no 'line' exceeds 'maxCharactersPerLine' in length. |
|
191 |
+ |
* |
|
192 |
+ |
* The wrapping algorithm is four fold (applied in the following order): |
|
193 |
+ |
* <li>1st New Line within the maxCharactersPerLine</li> |
|
194 |
+ |
* <li>last space within the maxCharactersPerLine</li> |
|
195 |
+ |
* <li>just beyond the last punctuation (non-AlphaNumeric character) within the 'maxCharactersPerLine'</li> |
|
196 |
+ |
* <li>hard wrapped such that the line length is 'within the maxCharactersPerLine'</li> |
|
197 |
+ |
* |
|
198 |
+ |
* @param text to Wrap. |
|
199 |
+ |
* @param maxCharactersPerLine Don't wrap if null or < 1! |
|
200 |
+ |
* |
|
201 |
+ |
* @return !null result with wrapped text (lines) if maxCharactersPerLine > 0, (otherwise don't wrap) |
|
202 |
+ |
*/ |
|
203 |
+ |
public static String wrap(String text, Integer maxCharactersPerLine) { |
|
204 |
+ |
text = StringUtils.deNull(text).trim(); |
|
205 |
+ |
if ((maxCharactersPerLine == null) || (maxCharactersPerLine < 1)) { |
|
206 |
+ |
return text; |
|
207 |
+ |
} |
|
208 |
+ |
StringBuilder sb = new StringBuilder(); |
|
209 |
+ |
while (maxCharactersPerLine < text.length()) { |
|
210 |
+ |
int breakAt = findBreakAtPosition(text, maxCharactersPerLine); |
|
211 |
+ |
sb.append(text.substring(0, breakAt)).append(NLchar); |
|
212 |
+ |
text = text.substring(breakAt).trim(); |
|
213 |
+ |
} |
|
214 |
+ |
return sb.append(text).toString(); |
|
215 |
+ |
} |
|
216 |
+ |
|
|
217 |
+ |
private static int findBreakAtPosition(String text, int maxCharactersPerLine) { |
|
218 |
+ |
int at = text.indexOf(NLchar); |
|
219 |
+ |
if (at != -1 && (at <= maxCharactersPerLine)) { |
|
220 |
+ |
return at; |
|
221 |
+ |
} |
|
222 |
+ |
text = text.substring(0, maxCharactersPerLine); |
|
223 |
+ |
at = text.lastIndexOf(' '); |
|
224 |
+ |
if (at != -1) { |
|
225 |
+ |
return at; |
|
226 |
+ |
} |
|
227 |
+ |
for (at = maxCharactersPerLine - 1; 0 <= at; at--) { |
|
228 |
+ |
if (!Character.isLetterOrDigit(text.charAt(at))) { |
|
229 |
+ |
return at + 1; |
|
230 |
+ |
} |
|
231 |
+ |
} |
|
232 |
+ |
return maxCharactersPerLine; |
|
233 |
+ |
} |
|
234 |
+ |
|
|
235 |
+ |
public static int indexOfControlCharacter(String text) { |
|
236 |
+ |
if (text != null) { |
|
237 |
+ |
for (int i = 0; i < text.length(); i++) { |
|
238 |
+ |
if (CharacterUtils.isControlChar(text.charAt(i))) { |
|
239 |
+ |
return i; |
|
240 |
+ |
} |
|
241 |
+ |
} |
|
242 |
+ |
} |
|
243 |
+ |
return -1; |
|
244 |
+ |
} |
|
245 |
+ |
|
|
246 |
+ |
public static String makeIdFriendly(String text) { |
|
247 |
+ |
text = noEmpty(text); |
|
248 |
+ |
if (text == null) { |
|
249 |
+ |
return null; |
|
250 |
+ |
} |
|
251 |
+ |
StringBuilder sb = new StringBuilder(); |
|
252 |
+ |
boolean pendingUnderscore = false; |
|
253 |
+ |
for (int i = 0; i < text.length(); i++) { |
|
254 |
+ |
char c = text.charAt(i); |
|
255 |
+ |
if (!CharacterUtils.is7BitAlphaNumeric(c)) { |
|
256 |
+ |
pendingUnderscore = true; |
|
257 |
+ |
} else { |
|
258 |
+ |
if (pendingUnderscore && (sb.length() != 0)) { |
|
259 |
+ |
sb.append('_'); |
|
260 |
+ |
} |
|
261 |
+ |
pendingUnderscore = false; |
|
262 |
+ |
sb.append(c); |
|
263 |
+ |
} |
|
264 |
+ |
} |
|
265 |
+ |
return noEmpty(sb.toString()); |
|
266 |
+ |
} |
|
267 |
+ |
|
|
268 |
+ |
/** |
|
269 |
+ |
* return the index of the first character that is unacceptable for that position to be part of an Identifier. |
|
270 |
+ |
* |
|
271 |
+ |
* An Identifier is a String that stars with a 7Bit Alpha or underscore, and is followed by any number of 7bit AlphaNumerics or underscores. |
|
272 |
+ |
* |
|
273 |
+ |
* @param toCheck not null or empty |
|
274 |
+ |
* |
|
275 |
+ |
* @return -1 if OK, otherwise the 'bad' character index. |
|
276 |
+ |
*/ |
|
277 |
+ |
public static int checkIdentifier(String toCheck) { |
|
278 |
+ |
if (!CharacterUtils.is7BitAlphaUnderScore( toCheck.charAt( 0 ) )) { |
|
279 |
+ |
return 0; |
|
280 |
+ |
} |
|
281 |
+ |
for (int i = 1; i <toCheck.length(); i++) { |
|
282 |
+ |
if (!CharacterUtils.isAlphaNumericUnderScore7BitAscii( toCheck.charAt( i ) )) { |
|
283 |
+ |
return i; |
|
284 |
+ |
} |
|
285 |
+ |
} |
|
286 |
+ |
return -1; |
|
287 |
+ |
} |
|
288 |
+ |
} |