source: java/main/src/main/java/com/framsticks/util/lang/Strings.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
1package com.framsticks.util.lang;
2
3import java.util.regex.Matcher;
4
5import com.framsticks.util.FramsticksException;
6
7/**
8 * @author Piotr Sniegowski
9 */
10public 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
23        public static void assureNotEmpty(String str) {
24                if (!notEmpty(str)) {
25                        throw new FramsticksException().msg("string is empty");
26                }
27        }
28
29        public static String toStringNullProof(Object object) {
30                return toStringNullProof(object, "");
31        }
32
33        public static String toStringNullProof(Object object, String def) {
34                if (object == null) {
35                        return def;
36                }
37                return object.toString();
38        }
39
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
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) {
59                        return new Pair<String, String>(string, second);
60                } else {
61                        return new Pair<String, String>(string.substring(0, pos), string.substring(pos + 1));
62                }
63        }
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        }
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
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
87}
Note: See TracBrowser for help on using the repository browser.