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