[84] | 1 | package com.framsticks.util.lang; |
---|
| 2 | |
---|
[101] | 3 | import java.util.regex.Matcher; |
---|
| 4 | |
---|
[96] | 5 | import com.framsticks.util.FramsticksException; |
---|
| 6 | |
---|
[84] | 7 | /** |
---|
| 8 | * @author Piotr Sniegowski |
---|
| 9 | */ |
---|
| 10 | public abstract class Strings { |
---|
| 11 | |
---|
| 12 | public static String valueOf(Object o) { |
---|
| 13 | if (o == null) { |
---|
| 14 | return null; |
---|
| 15 | } |
---|
| 16 | return o.toString(); |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | public static boolean notEmpty(String str) { |
---|
| 20 | return str != null && !str.equals(""); |
---|
| 21 | } |
---|
| 22 | |
---|
[96] | 23 | public static void assureNotEmpty(String str) { |
---|
| 24 | if (!notEmpty(str)) { |
---|
| 25 | throw new FramsticksException().msg("string is empty"); |
---|
| 26 | } |
---|
| 27 | } |
---|
| 28 | |
---|
[84] | 29 | public static String toStringNullProof(Object object) { |
---|
[97] | 30 | return toStringNullProof(object, ""); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | public static String toStringNullProof(Object object, String def) { |
---|
[84] | 34 | if (object == null) { |
---|
[97] | 35 | return def; |
---|
[84] | 36 | } |
---|
| 37 | return object.toString(); |
---|
| 38 | } |
---|
| 39 | |
---|
[99] | 40 | public static String toStringEmptyProof(Object object, Object def) { |
---|
| 41 | if (object == null) { |
---|
| 42 | return def.toString(); |
---|
| 43 | } |
---|
| 44 | String v = object.toString(); |
---|
| 45 | return notEmpty(v) ? v : def.toString(); |
---|
| 46 | } |
---|
| 47 | |
---|
[84] | 48 | public static String collapse(String s) { |
---|
| 49 | if (s == null) { |
---|
| 50 | return null; |
---|
| 51 | } |
---|
| 52 | s = s.trim(); |
---|
| 53 | return (s.equals("")) ? null : s; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | public static Pair<String, String> splitIntoPair(String string, char separator, String second) { |
---|
| 57 | int pos = string.indexOf(separator); |
---|
| 58 | if (pos == -1) { |
---|
[96] | 59 | return new Pair<String, String>(string, second); |
---|
[84] | 60 | } else { |
---|
[96] | 61 | return new Pair<String, String>(string.substring(0, pos), string.substring(pos + 1)); |
---|
[84] | 62 | } |
---|
| 63 | } |
---|
[86] | 64 | |
---|
| 65 | public static String capitalize(String input) { |
---|
| 66 | return input.substring(0, 1).toUpperCase() + input.substring(1); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public static String uncapitalize(String input) { |
---|
| 70 | return input.substring(0, 1).toLowerCase() + input.substring(1); |
---|
| 71 | } |
---|
[97] | 72 | |
---|
| 73 | public static String commonPrefix(String a, String b) { |
---|
| 74 | int length = Math.min(a.length(), b.length()); |
---|
| 75 | int i = 0; |
---|
| 76 | while ((i < length) && (a.charAt(i) == b.charAt(i))) { |
---|
| 77 | ++i; |
---|
| 78 | } |
---|
| 79 | return a.substring(0, i); |
---|
| 80 | } |
---|
| 81 | |
---|
[101] | 82 | public static CharSequence takeGroup(CharSequence input, Matcher matcher, int group) { |
---|
| 83 | // return (matcher.start(group) == matcher.end(group)) ? null : input.subSequence(matcher.start(group), matcher.end(group)); |
---|
| 84 | return input.subSequence(matcher.start(group), matcher.end(group)); |
---|
| 85 | } |
---|
| 86 | |
---|
[84] | 87 | } |
---|