Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BRules |
|
| 3.3636363636363638;3.364 |
1 | /* | |
2 | * Copyright (C) 2011-2014 Bekwam, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package routines; | |
17 | ||
18 | import java.io.ByteArrayInputStream; | |
19 | import java.io.InputStream; | |
20 | import java.nio.ByteBuffer; | |
21 | import java.nio.CharBuffer; | |
22 | import java.nio.charset.Charset; | |
23 | import java.nio.charset.CharsetEncoder; | |
24 | import java.nio.charset.CodingErrorAction; | |
25 | import java.util.Date; | |
26 | import java.util.List; | |
27 | ||
28 | import javax.xml.parsers.SAXParser; | |
29 | import javax.xml.parsers.SAXParserFactory; | |
30 | ||
31 | import org.apache.commons.lang3.StringUtils; | |
32 | import org.joda.time.LocalDate; | |
33 | import org.joda.time.Years; | |
34 | import org.xml.sax.SAXParseException; | |
35 | import org.xml.sax.helpers.DefaultHandler; | |
36 | ||
37 | import com.google.i18n.phonenumbers.NumberParseException; | |
38 | import com.google.i18n.phonenumbers.PhoneNumberUtil; | |
39 | import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; | |
40 | ||
41 | /** | |
42 | * BRules is a collection of Java methods used for validation, formatting, and | |
43 | * transformation. | |
44 | * | |
45 | * @author Carl2 | |
46 | * @since 1.0.0 | |
47 | */ | |
48 | 0 | public class BRules { |
49 | ||
50 | private final static String ERROR_MESSAGE_LISTTYPE_ARG = "listType must be 'ul' or 'ol'"; | |
51 | private final static String UTF8_CHARSET = "UTF-8"; | |
52 | private final static int DEFAULT_PAD_SIZE = 10; | |
53 | private final static String DEFAULT_LIST_TO_STRING_DELIMITER = ","; | |
54 | ||
55 | /** | |
56 | * isPhoneNum: true if valid in accordance with country specifier; uses | |
57 | * strict check | |
58 | * | |
59 | * {talendTypes} String | |
60 | * | |
61 | * {Category} BRules | |
62 | * | |
63 | * {param} string("regionCode") input: The country or region code to use | |
64 | * {param} string("phoneNumber") input: The phone number to check | |
65 | * | |
66 | * {example} isPhoneNum("US", "(301) 555-5555") # true | |
67 | */ | |
68 | public static boolean isPhoneNum(String _countryCode, String _toValidate) { | |
69 | 8 | return isPhoneNum(_countryCode, _toValidate, false); |
70 | } | |
71 | ||
72 | /** | |
73 | * isPhoneNum: true if valid in accordance with country specifier and | |
74 | * loose flag (false for strict) | |
75 | * | |
76 | * {talendTypes} String | |
77 | * | |
78 | * {Category} BRules | |
79 | * | |
80 | * {param} string("regionCode") input: The country or region code to use | |
81 | * {param} string("phoneNumber") input: The phone number to check | |
82 | * {param} boolean: use loose validation (true) or strict (false) | |
83 | * | |
84 | * {example} isPhoneNum("US", "(301) 555-5555") # true | |
85 | */ | |
86 | public static boolean isPhoneNum(String _countryCode, String _toValidate, boolean _loose) { | |
87 | ||
88 | 10 | boolean valid = false; |
89 | ||
90 | 10 | PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); |
91 | ||
92 | try { | |
93 | 10 | PhoneNumber pn = phoneUtil.parse(_toValidate, _countryCode); |
94 | 4 | if( _loose ) { |
95 | 2 | valid = phoneUtil.isPossibleNumber(pn); |
96 | 1 | } |
97 | else { | |
98 | 2 | valid = phoneUtil.isValidNumber(pn); |
99 | } | |
100 | 1 | } |
101 | 5 | catch(NumberParseException ignore) {} |
102 | ||
103 | 10 | return valid; |
104 | } | |
105 | ||
106 | /** | |
107 | * all: true if all arguments are not empty (not null for Objects, | |
108 | * not null, empty string, or whitespace for java.lang.String) | |
109 | * | |
110 | * {talendTypes} String | |
111 | * | |
112 | * {Category} BRules | |
113 | * | |
114 | * {param} object() input: a variable number of params to check | |
115 | * | |
116 | * {example} all("one", "two", "") # false | |
117 | * {example} all("one") # true | |
118 | */ | |
119 | public static boolean all(Object..._objects) { | |
120 | 16 | boolean allSet = true; |
121 | 16 | if( _objects == null ) return false; |
122 | 30 | for( Object obj : _objects) { |
123 | 24 | if( obj instanceof String ) { |
124 | 18 | if( StringUtils.isEmpty((String)obj) ) { |
125 | 6 | allSet = false; |
126 | 6 | break; |
127 | } | |
128 | } | |
129 | else { | |
130 | 6 | if( obj == null ) { |
131 | 2 | allSet = false; |
132 | 2 | break; |
133 | } | |
134 | } | |
135 | } | |
136 | 14 | return allSet; |
137 | } | |
138 | ||
139 | /** | |
140 | * xor: true if one and only one argument is not empty (not null for Objects, | |
141 | * not null, empty string, or whitespace for java.lang.String) | |
142 | * | |
143 | * {talendTypes} String | |
144 | * | |
145 | * {Category} BRules | |
146 | * | |
147 | * {param} object() input: a variable number of params to check | |
148 | * | |
149 | * {example} xor("one", "") # true | |
150 | * {example} xor("", "") # false | |
151 | */ | |
152 | public static boolean xor(Object..._objects) { | |
153 | 20 | boolean onlyOneSet = false; |
154 | 20 | if( _objects == null ) return false; |
155 | 50 | for( Object obj : _objects) { |
156 | 36 | if( obj instanceof String ) { |
157 | 26 | if( StringUtils.isNotEmpty((String)obj) ) { |
158 | 12 | if( !onlyOneSet ) { |
159 | 8 | onlyOneSet = true; |
160 | 4 | } |
161 | else { | |
162 | 4 | onlyOneSet = false; |
163 | 4 | break; |
164 | } | |
165 | } | |
166 | } | |
167 | else { | |
168 | 10 | if( obj != null ) { |
169 | 2 | if( !onlyOneSet ) { |
170 | 2 | onlyOneSet = true; |
171 | 1 | } |
172 | else { | |
173 | 0 | onlyOneSet = false; |
174 | 0 | break; |
175 | } | |
176 | } | |
177 | } | |
178 | } | |
179 | 18 | return onlyOneSet; |
180 | } | |
181 | ||
182 | /** | |
183 | * isBlank: true if the string is null, the empty string, or whitespace | |
184 | * | |
185 | * {talendTypes} String | |
186 | * | |
187 | * {Category} BRules | |
188 | * | |
189 | * {param} string("hello") input : string to be tested | |
190 | * | |
191 | * {example} isBlank("hello") # false | |
192 | * {example} isBlank(null) # true | |
193 | * {example} isBlank("") # true | |
194 | * {example} isBlank(" ") # true | |
195 | * | |
196 | */ | |
197 | public static boolean isBlank(String _s) { | |
198 | 8 | return StringUtils.isBlank(_s); |
199 | } | |
200 | ||
201 | /** | |
202 | * isXML: true if the passed-in string adheres to XML well-formedness | |
203 | * and the specified charset | |
204 | * | |
205 | * {talendTypes} String | |
206 | * | |
207 | * {Category} BRules | |
208 | * | |
209 | * {param} string("<message>hello</message>") input: xml to be tested | |
210 | * {param} string("ISO8859_1") input: the charset of the xml | |
211 | * | |
212 | * {example} isXML("<message>hello</message>", "ISO8859_1") # true | |
213 | * {example} isXML("<message>hello", "ISO8859_1") # false | |
214 | */ | |
215 | public static boolean isXML(String _xml, String _charset) throws Exception { | |
216 | ||
217 | 24 | boolean result = false; |
218 | ||
219 | 24 | if( StringUtils.isEmpty(_xml) ) return false; |
220 | ||
221 | try { | |
222 | 22 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
223 | 22 | factory.setValidating(true); |
224 | ||
225 | 22 | SAXParser parser = factory.newSAXParser(); |
226 | ||
227 | 22 | InputStream is = new ByteArrayInputStream(_xml.getBytes(_charset)); |
228 | ||
229 | 20 | parser.parse(is, new DefaultHandler()); |
230 | ||
231 | 16 | result = true; |
232 | 8 | } |
233 | 10 | catch(SAXParseException ignore) {} |
234 | ||
235 | 20 | return result; |
236 | } | |
237 | ||
238 | /** | |
239 | * isXML: true if the passed-in string adheres to XML well-formedness | |
240 | * using a UTF-8 string | |
241 | * | |
242 | * {talendTypes} String | |
243 | * | |
244 | * {Category} BRules | |
245 | * | |
246 | * {param} string("<message>hello</message>") input: xml to be tested | |
247 | * | |
248 | * {example} isXML("<message>hello</message>") # true | |
249 | * {example} isXML("<message>hello") # false | |
250 | */ | |
251 | public static boolean isXML(String _xml) throws Exception { | |
252 | 8 | return isXML(_xml, UTF8_CHARSET); |
253 | } | |
254 | ||
255 | /** | |
256 | * okChars: true if the passed-in string is valid for the specified | |
257 | * character set | |
258 | * | |
259 | * The supported character set values are those supported by Java. The | |
260 | * particular constants include "ASCII", "Cp1252", "ISO8859_1", "UTF-8". | |
261 | * | |
262 | * If the string parameter is empty or null, 'true' is returned. | |
263 | * | |
264 | * {talendTypes} String | |
265 | * | |
266 | * {Category} BRules | |
267 | * | |
268 | * {param} string("hello") input: string to be tested | |
269 | * {param} string("charset") input: charset of string | |
270 | * | |
271 | * {example} okChar("hello", "ISO8859_1") # true | |
272 | */ | |
273 | public static boolean okChars(String _s, String _charset) { | |
274 | 16 | if( StringUtils.isEmpty(_s) ) { return true; } |
275 | 12 | CharsetEncoder encoder = Charset.forName(_charset).newEncoder(); |
276 | 12 | return encoder.canEncode(_s); |
277 | } | |
278 | ||
279 | /** | |
280 | * toCharset: convert a string to the character set used by Windows Latin-1. | |
281 | * Will convert unmappable characters to a space (" ") rather than throwing | |
282 | * an error | |
283 | * | |
284 | * For example, this will conveniently map a \u2122 (TM) symbol to a | |
285 | * Windows-recognized hex 99 | |
286 | * | |
287 | * {talendTypes} String | |
288 | * | |
289 | * {Category} BRules | |
290 | * | |
291 | * {param} string(_s) input string to convert | |
292 | * {example} toCharset("My Product\u2122") # returns "My Product " | |
293 | */ | |
294 | public static String toCharset(String _s) { | |
295 | 0 | return toCharset(_s, "Cp1252"); |
296 | } | |
297 | ||
298 | /** | |
299 | * toCharset: convert a string to the specified character set | |
300 | * Will convert unmappable characters to a space (" ") rather than throwing | |
301 | * an error | |
302 | * | |
303 | * For example, this will conveniently map a \u2122 (TM) symbol to a | |
304 | * Windows-recognized hex 99 | |
305 | * | |
306 | * Uses Java names for charsets: Cp1252, US-ASCII | |
307 | * {talendTypes} String | |
308 | * | |
309 | * {Category} BRules | |
310 | * | |
311 | * {param} string(_s) input string to convert | |
312 | * {param} string(_charset) character set to use for conversion | |
313 | * {example} toCharset("My Product\u2122", "Cp1252") # returns "My Product " | |
314 | */ | |
315 | public static String toCharset(String _s, String _charset) { | |
316 | 0 | String cs = (StringUtils.isEmpty(_charset))?"Cp1252":_charset; |
317 | 0 | return toCharset(_s,cs," "); |
318 | } | |
319 | ||
320 | /** | |
321 | * toCharset: convert a string to the specified character set | |
322 | * | |
323 | * Will convert unmappable characters to a specified character | |
324 | * | |
325 | * For example, this will conveniently map a \u2122 (TM) symbol to a | |
326 | * Windows-recognized hex 99 | |
327 | * | |
328 | * Uses Java names for charsets: Cp1252, US-ASCII, ISO-8859-1, UTF-8, UTF-16 | |
329 | * | |
330 | * {talendTypes} String | |
331 | * | |
332 | * {Category} BRules | |
333 | * | |
334 | * {param} string(_s) input string to convert | |
335 | * {param} string(_charset) character set to use for conversion | |
336 | * {param} string(_replaceCh) character to use for unmappables | |
337 | * {example} toCharset("My Product\u2122", "Cp1252", "?") # returns "My Product?" | |
338 | */ | |
339 | public static String toCharset(String _s, String _charset, String _replaceCh) { | |
340 | 0 | String s = ""; |
341 | 0 | String cs = (StringUtils.isEmpty(_charset))?"Cp1252":_charset; |
342 | 0 | String rc = (StringUtils.isEmpty(_replaceCh))?" ":_replaceCh; |
343 | ||
344 | try { | |
345 | 0 | CharsetEncoder enc = Charset.forName(cs).newEncoder(); |
346 | 0 | enc.onUnmappableCharacter(CodingErrorAction.REPLACE); |
347 | 0 | enc.replaceWith( rc.getBytes() ); |
348 | 0 | ByteBuffer buf = enc.encode(CharBuffer.wrap(_s)); |
349 | 0 | s = new String(buf.array(),cs); |
350 | 0 | } |
351 | 0 | catch (Exception ignore) {} |
352 | ||
353 | 0 | return s; |
354 | } | |
355 | ||
356 | /** | |
357 | * isJSON: true if the passed-in string adheres to JSON format | |
358 | * | |
359 | * Not supported for JDKs < 6; will throw exception | |
360 | * | |
361 | * Empty expressions - {} and [] - will return true | |
362 | * | |
363 | * {talendTypes} String | |
364 | * | |
365 | * {Category} BRules | |
366 | * | |
367 | * {param} string("{name: 'Carl', program: 'BRules'}") input: json to be tested | |
368 | * | |
369 | * {example} isJSON("<message>hello</message>") # false | |
370 | * {example} isJSON("{name: 'Carl', program: 'BRules'}") # true | |
371 | * {example} isJSON("{}") #true | |
372 | * {example} isJSON(null) #false | |
373 | */ | |
374 | public static boolean isJSON(String _json) { | |
375 | 0 | return BRulesJSON.isJSON(_json); |
376 | } | |
377 | ||
378 | /** | |
379 | * hasJSONPath: true if the passed-in json has elements referenced in path | |
380 | * | |
381 | * Not supported for JDKs < 6; will throw exception | |
382 | * | |
383 | * {talendTypes} String | |
384 | * | |
385 | * {Category} BRules | |
386 | * | |
387 | * {param} string("{name: 'Carl', program: 'BRules'}") input: json to be tested | |
388 | * | |
389 | * {example} hasJSONPath("<message>hello</message>") # false | |
390 | * {example} hasJSONPath("{name: 'Carl', program: 'BRules'}", "$.name") # true | |
391 | * {example} isJSON("{}") #true | |
392 | * {example} hasJSONPath(null) #false | |
393 | */ | |
394 | public static boolean hasJSONPath(String _json, String _path) throws Exception { | |
395 | 0 | return BRulesJSON.hasJSONPath(_json, _path); |
396 | } | |
397 | ||
398 | /** | |
399 | * comma: join the string representation of objects together with a | |
400 | * comma | |
401 | * | |
402 | * Appends an empty string where an element is null | |
403 | * | |
404 | * {talendTypes} String | |
405 | * | |
406 | * {Category} BRules | |
407 | * | |
408 | * {param} object() input: a variable number of strings to join | |
409 | * | |
410 | * {example} comma("one", "two", "three") # "one,two,three" | |
411 | */ | |
412 | public static String comma(Object..._objects) { | |
413 | 8 | return join(",", _objects); |
414 | } | |
415 | ||
416 | /** | |
417 | * join: join the string representation of objects together with a | |
418 | * character | |
419 | * | |
420 | * Appends an empty string where an element is null | |
421 | * | |
422 | * {talendTypes} String | |
423 | * | |
424 | * {Category} BRules | |
425 | * | |
426 | * {param} object() input: a variable number of strings to join | |
427 | * | |
428 | * {example} join("one", "two", "three") # "one,two,three" | |
429 | */ | |
430 | public static String join(String _delim, Object..._objects) { | |
431 | 16 | StringBuffer sb = new StringBuffer(); |
432 | 16 | boolean firstPass = true; |
433 | 16 | if( _objects != null ) { |
434 | 32 | for( Object obj : _objects) { |
435 | ||
436 | 20 | if( !firstPass ) { |
437 | 8 | sb.append(_delim); |
438 | 4 | } else { |
439 | 12 | firstPass = false; |
440 | } | |
441 | ||
442 | 20 | if( obj == null ) { |
443 | 0 | sb.append(""); |
444 | 0 | } |
445 | else { | |
446 | 20 | sb.append( obj.toString() ); |
447 | } | |
448 | } | |
449 | } | |
450 | 16 | return sb.toString(); |
451 | } | |
452 | ||
453 | /** | |
454 | * ul: form an html list of the specified css style from | |
455 | * the list of objects | |
456 | * | |
457 | * null objects returns an empty list (ex, "<ul></ul>") | |
458 | * | |
459 | * {talendTypes} String | |
460 | * | |
461 | * {Category} BRules | |
462 | * | |
463 | * {param} styleClass : a style to apply for the toplevel list element | |
464 | * {param} object() input: a variable number of strings to join | |
465 | * | |
466 | * {example} ul("infolist", "a", "b") # "<ul><li>a</li><li>b</li></ul>" | |
467 | */ | |
468 | public static String ul(String _styleClass, Object..._objects) { | |
469 | 10 | return htmlList(_styleClass, "ul", _objects); |
470 | } | |
471 | ||
472 | /** | |
473 | * ol: form an html list of the specified css style from | |
474 | * the list of objects | |
475 | * | |
476 | * null objects returns an empty list (ex, "<ol></ol>") | |
477 | * | |
478 | * {talendTypes} String | |
479 | * | |
480 | * {Category} BRules | |
481 | * | |
482 | * {param} styleClass : a style to apply for the toplevel list element | |
483 | * {param} object() input: a variable number of strings to join | |
484 | * | |
485 | * {example} ol("infolist", "a", "b") # "<ol><li>a</li><li>b</li></ol>" | |
486 | */ | |
487 | public static String ol(String _styleClass, Object..._objects) { | |
488 | 6 | return htmlList(_styleClass, "ol", _objects); |
489 | } | |
490 | ||
491 | protected static String htmlList(String _styleClass, String _listType, Object..._objects) { | |
492 | ||
493 | 18 | if( _listType == null || |
494 | 9 | !(_listType.equals("ul") || _listType.equals("ol")) ) { |
495 | 2 | throw new IllegalArgumentException(ERROR_MESSAGE_LISTTYPE_ARG); |
496 | } | |
497 | ||
498 | 16 | StringBuffer sb = new StringBuffer(); |
499 | ||
500 | 16 | if( _styleClass != null && _styleClass.length() > 0 ) { |
501 | 4 | sb.append("<" + _listType + " class=\"" + _styleClass + "\">" ); |
502 | 2 | } |
503 | else { | |
504 | 12 | sb.append("<" + _listType + ">"); |
505 | } | |
506 | ||
507 | 16 | if( _objects != null ) { |
508 | 48 | for( Object obj : _objects) { |
509 | 36 | sb.append("<li>" + ((obj==null)?"":obj.toString()) + "</li>"); |
510 | } | |
511 | } | |
512 | ||
513 | 16 | sb.append("</" + _listType + ">"); |
514 | 16 | return sb.toString(); |
515 | } | |
516 | ||
517 | /** | |
518 | * p: form an html paragraph of the specified css style from | |
519 | * the list of objects | |
520 | * | |
521 | * null objects returns an empty list (ex, "<p></p>") | |
522 | * | |
523 | * {talendTypes} String | |
524 | * | |
525 | * {Category} BRules | |
526 | * | |
527 | * {param} styleClass : a style to apply for the toplevel list element | |
528 | * {param} object() input: a variable number of strings to join | |
529 | * | |
530 | * {example} p("note", "a") # "<p class=\"note\">a</p>" | |
531 | */ | |
532 | public static String p(String _styleClass, String _text) { | |
533 | 8 | StringBuffer sb = new StringBuffer(); |
534 | 8 | if( _styleClass != null && _styleClass.length() > 0 ) { |
535 | 2 | sb.append("<p class=\"" + _styleClass + "\">" ); |
536 | 1 | } |
537 | else { | |
538 | 6 | sb.append("<p>"); |
539 | } | |
540 | 8 | sb.append((_text==null)?"":_text); |
541 | 8 | sb.append("</p>"); |
542 | 8 | return sb.toString(); |
543 | } | |
544 | ||
545 | /** | |
546 | * Pad a 10 character string with spaces | |
547 | * | |
548 | * If the string exceeds 10 chars, the input string will be returned | |
549 | * | |
550 | * The method is deprecated because of the limited utility in a predefined | |
551 | * size. | |
552 | * | |
553 | * {talendTypes} String | |
554 | * | |
555 | * {Category} BRules | |
556 | * | |
557 | * {param} pad(stringToPad) input: The string to be divided | |
558 | * | |
559 | * {example} pad("100") # " 100" | |
560 | */ | |
561 | @Deprecated | |
562 | public static String pad(String s) { | |
563 | 10 | return pad(s, DEFAULT_PAD_SIZE); |
564 | } | |
565 | ||
566 | /** | |
567 | * Left pads the input string with spaces | |
568 | * | |
569 | * {talendTypes} String | |
570 | * | |
571 | * {Category} BRules | |
572 | * | |
573 | * {param} string(stringToPad) stringToPad: string to pad | |
574 | * {param} int(numPadChars) numPadChars: number of padded chars | |
575 | * | |
576 | * {example} pad("100",6) # "000100" | |
577 | * | |
578 | * @param s - input string to pad | |
579 | * @param size - number of chars to pad | |
580 | * @return padded string or input string | |
581 | * @since 1.0.0 | |
582 | */ | |
583 | public static String pad(String s, int size) { | |
584 | 20 | if( size < 0 ) throw new IllegalArgumentException("size must be > 0"); |
585 | 20 | if( StringUtils.isEmpty(s) ) return s; |
586 | 12 | return StringUtils.leftPad(s, size); |
587 | } | |
588 | ||
589 | /** | |
590 | * Left pads the input string with the specified character | |
591 | * | |
592 | * {talendTypes} String | |
593 | * | |
594 | * {Category} BRules | |
595 | * | |
596 | * {param} string(stringToPad) stringToPad: string to pad | |
597 | * {param} int(numPadChars) numPadChars: number of padded chars | |
598 | * {param} char(charToUse) charToUse: char to use as padding | |
599 | * | |
600 | * {example} pad("100", 6, '0') # "000100" | |
601 | * | |
602 | * @param s - input string to pad | |
603 | * @param size - number of chars to pad | |
604 | * @param ch - character to pad with | |
605 | * @return padded string or input string | |
606 | * @since 1.4.0 | |
607 | */ | |
608 | public static String pad(String s, int size, char ch) { | |
609 | 10 | if( size < 0 ) throw new IllegalArgumentException("size must be > 0"); |
610 | 10 | if( StringUtils.isEmpty(s) ) return s; |
611 | 6 | return StringUtils.leftPad(s, size, ch); |
612 | } | |
613 | ||
614 | /** | |
615 | * Left pads the input integer with the specified character | |
616 | * | |
617 | * {talendTypes} String | |
618 | * | |
619 | * {Category} BRules | |
620 | * | |
621 | * {param} int(integerToPad) integerToPad: int to pad | |
622 | * {param} int(numPadChars) numPadChars: number of padded chars | |
623 | * {param} char(charToUse) charToUse: char to use as padding | |
624 | * | |
625 | * {example} pad(100, 6, '0') # "000100" | |
626 | * | |
627 | * @param s - input string to pad | |
628 | * @param size - number of chars to pad | |
629 | * @param ch - character to pad with | |
630 | * @return padded string or input string | |
631 | * @since 1.4.0 | |
632 | */ | |
633 | public static String pad(Integer i, int size, char ch) { | |
634 | 6 | if( i == null ) return null; |
635 | 4 | return StringUtils.leftPad(String.valueOf(i), size, ch); |
636 | } | |
637 | ||
638 | /** | |
639 | * Left pads the input long with the specified character | |
640 | * | |
641 | * {talendTypes} String | |
642 | * | |
643 | * {Category} BRules | |
644 | * | |
645 | * {param} long(integerToPad) integerToPad: int to pad | |
646 | * {param} long(numPadChars) numPadChars: number of padded chars | |
647 | * {param} char(charToUse) charToUse: char to use as padding | |
648 | * | |
649 | * {example} pad(100L, 6, '0') # "000100" | |
650 | * | |
651 | * @param s - input string to pad | |
652 | * @param size - number of chars to pad | |
653 | * @param ch - character to pad with | |
654 | * @return padded string or input string | |
655 | * @since 1.4.0 | |
656 | */ | |
657 | public static String pad(Long lng, int size, char ch) { | |
658 | 6 | if( lng == null ) return null; |
659 | 4 | return StringUtils.leftPad(String.valueOf(lng), size, ch); |
660 | } | |
661 | ||
662 | /** | |
663 | * Right pads the input string with spaces | |
664 | * | |
665 | * {talendTypes} String | |
666 | * | |
667 | * {Category} BRules | |
668 | * | |
669 | * {param} string(stringToPad) stringToPad: string to pad | |
670 | * {param} int(numPadChars) numPadChars: number of padded chars | |
671 | * | |
672 | * {example} pad("100", 6) # "100 " | |
673 | * | |
674 | * @param s - input string to pad | |
675 | * @param size - number of chars to pad | |
676 | * @return padded string or input string | |
677 | * @since 1.4.0 | |
678 | */ | |
679 | public static String padRight(String s, int size) { | |
680 | 10 | if( size < 0 ) throw new IllegalArgumentException("size must be > 0"); |
681 | 10 | if( StringUtils.isEmpty(s) ) return s; |
682 | 6 | return StringUtils.rightPad(s, size); |
683 | } | |
684 | ||
685 | /** | |
686 | * Right pads the input string with the specified character | |
687 | * | |
688 | * {talendTypes} String | |
689 | * | |
690 | * {Category} BRules | |
691 | * | |
692 | * {param} string(stringToPad) stringToPad: string to pad | |
693 | * {param} int(numPadChars) numPadChars: number of padded chars | |
694 | * {param} char(charToUse) charToUse: char to use as padding | |
695 | * | |
696 | * {example} pad("100", 6, '0') # "100000" | |
697 | * | |
698 | * @param s - input string to pad | |
699 | * @param size - number of chars to pad | |
700 | * @param ch - character to pad with | |
701 | * @return padded string or input string | |
702 | * @since 1.4.0 | |
703 | */ | |
704 | public static String padRight(String s, int size, char ch) { | |
705 | 10 | if( size < 0 ) throw new IllegalArgumentException("size must be > 0"); |
706 | 10 | if( StringUtils.isEmpty(s) ) return s; |
707 | 6 | return StringUtils.rightPad(s, size, ch); |
708 | } | |
709 | ||
710 | /** | |
711 | * Take off leading zeros; assumes a number | |
712 | * | |
713 | * {talendTypes} String | |
714 | * | |
715 | * {Category} BRules | |
716 | * | |
717 | * {param} trimLeadingZeros(string) input: The string to be divided | |
718 | * | |
719 | */ | |
720 | public static String trimLeadingZeros(String num_s) { | |
721 | ||
722 | 12 | if( num_s == null || num_s.length() == 0 ) return ""; |
723 | ||
724 | try { | |
725 | 8 | int i = Integer.parseInt(num_s); |
726 | 6 | return String.valueOf(i); |
727 | } | |
728 | 2 | catch(NumberFormatException exc) { |
729 | 2 | return num_s; |
730 | } | |
731 | } | |
732 | ||
733 | /** | |
734 | * Calculates age in whole years based on today's date | |
735 | * | |
736 | * {talendTypes} Integer | |
737 | * | |
738 | * {Category} BRules | |
739 | * | |
740 | * {param} date(birthDate) input: the date of birth | |
741 | * | |
742 | * @param birthDate date of birth | |
743 | * @return whole years of age | |
744 | * | |
745 | */ | |
746 | public static Integer ageInYears(Date birthDate) { | |
747 | 2 | return ageInYears(birthDate, new Date()); |
748 | } | |
749 | ||
750 | /** | |
751 | * Calculates age in whole years using the specified | |
752 | * as of date | |
753 | * | |
754 | * Returns null if birthDate or asOfDate is null | |
755 | * | |
756 | * {talendTypes} Integer | |
757 | * | |
758 | * {Category} BRules | |
759 | * | |
760 | * {param} date(birthDate) input: the date of birth | |
761 | * {param} date(asOfDate) input: date used for comparison; alternative to | |
762 | * today | |
763 | * | |
764 | * @param birthDate date of birth | |
765 | * @param asOfDate date of comparison (instead of today) | |
766 | * @return whole years of age | |
767 | */ | |
768 | public static Integer ageInYears(Date birthDate, Date asOfDate) { | |
769 | ||
770 | 8 | if( birthDate == null || asOfDate == null ) { |
771 | 4 | return null; |
772 | } | |
773 | ||
774 | 4 | return ageInYears(new LocalDate(birthDate), new LocalDate(asOfDate)); |
775 | } | |
776 | ||
777 | /** | |
778 | * For internal use only | |
779 | * | |
780 | * Joda Time classes aren't exposed to the calling Talend jobs because of | |
781 | * library dependency management | |
782 | * | |
783 | * @param birthDate date of birth | |
784 | * @param asOfDate date of comparison (instead of today) | |
785 | * @return whole years of age | |
786 | */ | |
787 | static Integer ageInYears(LocalDate birthDate, LocalDate asOfDate) { | |
788 | ||
789 | 10 | if( birthDate == null || asOfDate == null ) { |
790 | 4 | return null; |
791 | } | |
792 | ||
793 | 6 | Years age = Years.yearsBetween(birthDate, asOfDate); |
794 | ||
795 | 6 | return age.getYears(); |
796 | } | |
797 | ||
798 | /** | |
799 | * Forms a comma-separated list given the input java.util.List | |
800 | * | |
801 | * Handles different types | |
802 | * | |
803 | * Nulls are skipped, for example [A, null, B] -> A,,B | |
804 | * | |
805 | * {talendTypes} String | |
806 | * | |
807 | * {Category} BRules | |
808 | * | |
809 | * {param} list(inputList) input: the list to convert | |
810 | * | |
811 | * @param inputList list of objects of any time | |
812 | * @return empty string or list of values separated by comma | |
813 | */ | |
814 | public static String listToString(List<?> inputList) { | |
815 | 10 | return listToString(inputList, DEFAULT_LIST_TO_STRING_DELIMITER); |
816 | } | |
817 | ||
818 | /** | |
819 | * Forms a comma-separated list given the input java.util.List using the | |
820 | * specified delimeter | |
821 | * | |
822 | * Handles different types | |
823 | * | |
824 | * Nulls are skipped, for example [A, null, B] -> A,,B | |
825 | * | |
826 | * {talendTypes} String | |
827 | * | |
828 | * {Category} BRules | |
829 | * | |
830 | * {param} list(inputList) input: the list to convert | |
831 | * {param} string(delimiter) input: delimiter to used in string separating | |
832 | * items | |
833 | * | |
834 | * @param inputList list of objects of any time | |
835 | * @return empty string or list of values separated by delimiter | |
836 | * | |
837 | */ | |
838 | public static String listToString(List<?> inputList, String delimiter) { | |
839 | 12 | return listToString(inputList, delimiter, null); |
840 | } | |
841 | ||
842 | /** | |
843 | * Forms a comma-separated list given the input java.util.List | |
844 | * | |
845 | * Handles different types | |
846 | * | |
847 | * Nulls are skipped, for example [A, null, B] -> A,,B | |
848 | * | |
849 | * {talendTypes} String | |
850 | * | |
851 | * {Category} BRules | |
852 | * | |
853 | * {param} list(inputList) input: the list to convert | |
854 | * {param} string(delimiter) input: delimiter to used in string separating | |
855 | * items | |
856 | * {param} string(escapeString) input: string to wrap each item | |
857 | * | |
858 | * @param inputList list of objects of any time | |
859 | * @param escapeString String added to start and end of each element | |
860 | * @return empty string or list of values separated by comma | |
861 | */ | |
862 | public static String listToString(List<?> inputList, String delimiter, String escapeString) { | |
863 | ||
864 | 14 | StringBuffer sb = new StringBuffer(""); |
865 | ||
866 | 14 | if( inputList == null ) { |
867 | 2 | return sb.toString(); |
868 | } | |
869 | ||
870 | 12 | boolean initialized = false; |
871 | 33 | for( Object obj : inputList ) { |
872 | ||
873 | 30 | if( initialized ) { |
874 | 20 | sb.append(delimiter); |
875 | 10 | } else { |
876 | 10 | initialized = true; |
877 | } | |
878 | ||
879 | 30 | if( obj != null ) { |
880 | 28 | if( escapeString == null ) { |
881 | 22 | sb.append( String.valueOf(obj) ); |
882 | 11 | } else { |
883 | 6 | sb.append( escapeString + String.valueOf(obj) + escapeString ); |
884 | } | |
885 | } | |
886 | } | |
887 | ||
888 | 12 | return sb.toString(); |
889 | } | |
890 | } |