KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > util > StringUtils


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.util;
17
18 import java.util.regex.Pattern JavaDoc;
19
20 /**
21  * Miscellaneous String/CharSequence utility methods.
22  *
23  * @author Fyodor Kupolov
24  * @version 1.0
25  */

26 public final class StringUtils {
27     private StringUtils() {//singleton
28
}
29
30     /**
31      * Returns true if characters sequence is empty (length=0) or null.
32      *
33      * @param cs characters sequence to test.
34      * @return true if characters sequence is empty (length=0) or null.
35      */

36     public static boolean isEmpty(final CharSequence JavaDoc cs) {
37         return cs == null || cs.length() == 0;
38     }
39
40
41     /**
42      * Returns a trimmed value for specified charsequence
43      *
44      * @param cs charsequence to trim.
45      * @return trimmed value for specified charsequence or empty string if cs=null
46      */

47     public static String JavaDoc nullsafeTrim(final CharSequence JavaDoc cs) {
48         return cs == null ? "" : cs.toString().trim();
49     }
50
51     /**
52      * Returns Null safe string representation of specified object.
53      * @param o object to convert to String.
54      * @return <code>o.toString()</code> or <code>&quot;&quot;</code> if <code>o==null</code>.
55      */

56     public static String JavaDoc nullsafeToString(final Object JavaDoc o) {
57         return o==null?"":o.toString();
58     }
59
60     /**
61      * Checks if specified characters sequence is empty or contains only ascii whitespace characters.
62      *
63      * @param cs characters sequence to check.
64      * @return true if characters sequence is empty or contains only ascii whitespace characters.
65      */

66     public static boolean isAsciiWhitespacesOnly(final CharSequence JavaDoc cs) {
67         if (cs == null) {
68             return true;
69         }
70         int len = cs.length();
71         if (len == 0) {
72             return true;
73         }
74         for (int i = 0; i < len; i++) {
75             if (cs.charAt(i) > ' ') {
76                 return false;
77             }
78         }
79         return true;
80     }
81
82     /**
83      * Checks if specified characters sequence represents a non negative decimal number.
84      *
85      * @param cs characters sequence to check.
86      * @return true if specified characters sequence represents a non negative decimal number.
87      */

88     public static boolean isDecimalInt(final CharSequence JavaDoc cs) {
89         if (cs == null) { //null is not a number.
90
return false;
91         }
92         int len = cs.length();
93         if (len == 0) { //empty string also
94
return false;
95         }
96         for (int i = 0; i < len; i++) {
97             int c = cs.charAt(i);
98             if (c < 0x30 || c > 0x39) {
99                 return false;
100             }
101         }
102         return true;
103     }
104
105     private static Pattern JavaDoc WHITESPACES = Pattern.compile("[\\x00-\\x20&&[^\\r\\n]]+");
106     private static Pattern JavaDoc EOLS = Pattern.compile("[\\r\\n]+");
107
108     /**
109      * Formats specified string for console suitable representation.
110      * <p>All EOL char sequences are replaced with a single system line.separator,
111      * and all other whitespace sequences are replaced with a single space.
112      *
113      * @param string string to format. Nulls are allowed.
114      * @return formatted string.
115      */

116     public static String JavaDoc consoleFormat(String JavaDoc string) {
117         if (string == null) {
118             return "";
119         }
120         String JavaDoc res = string.trim();
121         String JavaDoc sep = System.getProperty("line.separator");
122         if (sep == null) {
123             sep = "\n";
124         }
125         res = EOLS.matcher(res).replaceAll(sep);
126         res = WHITESPACES.matcher(res).replaceAll(" ");
127         return res;
128     }
129
130
131 }
132
Popular Tags