KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > StringUtil


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import java.util.Arrays JavaDoc;
7
8 public class StringUtil {
9
10   public static final char SPACE = ' ';
11   public static final String JavaDoc SPACE_STRING = " ";
12
13   public static final String JavaDoc EMPTY = "";
14
15   public static final String JavaDoc NULL_STRING = "<null>";
16
17   public static final boolean isNullOrBlank(String JavaDoc s) {
18     return s == null || EMPTY.equals(s.trim());
19   }
20
21   public static final String JavaDoc safeToString(Object JavaDoc object) {
22     return object != null ? object.toString() : NULL_STRING;
23   }
24
25   /**
26    * @param list a list of String objects
27    * @param value a string to search for, may be null -- will return true if one of the array values in
28    * <code>list</code> is null.
29    * @return true if <code>value</code> is contained in <code>list</code>, will search for <code>null</code> as
30    * well.
31    */

32   public static final boolean exists(String JavaDoc[] list, String JavaDoc value) {
33     if (list != null) {
34       for (int pos = 0; pos < list.length; ++pos) {
35         if (list[pos] == value || (list[pos] != null && value != null && list[pos].equals(value))) { return true; }
36       }
37     }
38     return false;
39   }
40
41   /**
42    * @return <code>position</code>th, <code>position</code>st, <code>position</code>nd, etc. - whatever is
43    * appropriate.
44    */

45   public static String JavaDoc ordinal(long position) {
46     long mod10 = Math.abs(position) % 10;
47     long mod100 = Math.abs(position) % 100;
48     StringBuffer JavaDoc rv = new StringBuffer JavaDoc(EMPTY + position);
49     if (mod10 == 1) {
50       rv.append(mod100 == 11 ? "th" : "st");
51     } else if (mod10 == 2) {
52       rv.append(mod100 == 12 ? "th" : "nd");
53     } else if (mod10 == 3) {
54       rv.append(mod100 == 13 ? "th" : "rd");
55     } else {
56       rv.append("th");
57     }
58     return rv.toString();
59   }
60
61   public static String JavaDoc indentLines(String JavaDoc source) {
62     return indentLines(source, 1);
63   }
64
65   public static String JavaDoc indentLines(String JavaDoc source, int indentLevel) {
66     return indentLines(new StringBuffer JavaDoc(source), indentLevel).toString();
67   }
68
69   public static StringBuffer JavaDoc indentLines(StringBuffer JavaDoc source, int indentLevel) {
70     return indentLines(source, indentLevel, '\t');
71   }
72
73   public static StringBuffer JavaDoc indentLines(StringBuffer JavaDoc source, int indentLevel, char indentChar) {
74     if ((source == null) || (indentLevel == 0)) { return source; }
75
76     final String JavaDoc indentStr;
77
78     if (indentLevel <= 0) {
79       // I suppose one could write something to remove indentation, but I don't feel like it now
80
throw new IllegalArgumentException JavaDoc("Negative indentation not supported");
81     } else if (indentLevel > 1) {
82       char[] chars = new char[indentLevel];
83       Arrays.fill(chars, indentChar);
84       indentStr = new String JavaDoc(chars);
85     } else {
86       // errr....the call below is jdk1.4 specific
87
// indentStr = Character.toString(indentChar);
88

89       indentStr = new String JavaDoc(new char[] { indentChar });
90     }
91
92     source.insert(0, indentStr);
93
94     int index = 0;
95     while ((index = indexOfStringBuffer(source, "\n", index)) != -1) {
96       index++;
97       if (index == source.length()) {
98         break;
99       }
100       source.insert(index, indentStr);
101     }
102
103     return source;
104   }
105
106   public static int indexOfStringBuffer(StringBuffer JavaDoc source, String JavaDoc search, int start) {
107     return source.toString().indexOf(search, start);
108   }
109
110   /**
111    * Creates a String representation of an array of objects by calling <code>toString</code> on each one. Formatting
112    * is controlled by the parameters.
113    *
114    * @param objs (required) the array of objects to display
115    * @param separator (optional) a string to place between each object
116    * @param prefix (optional) a string to prefix each object with
117    * @param postfix (optional) a string to append to each object
118    * @return a String representation of the array, never returns null
119    */

120   public static final String JavaDoc toString(Object JavaDoc[] objs, String JavaDoc separator, String JavaDoc prefix, String JavaDoc postfix) {
121     StringBuffer JavaDoc rv = new StringBuffer JavaDoc();
122     if (objs != null) {
123       for (int pos = 0; pos < objs.length; ++pos) {
124         if (rv.length() > 0 && separator != null) {
125           rv.append(separator);
126         }
127         if (prefix != null) {
128           rv.append(prefix);
129         }
130         rv.append(objs[pos] != null ? objs[pos].toString() : "null");
131         if (postfix != null) {
132           rv.append(postfix);
133         }
134       }
135     } else {
136       rv.append(NULL_STRING);
137     }
138     return rv.toString();
139   }
140
141   public static final String JavaDoc toString(int[] objs, String JavaDoc separator, String JavaDoc prefix, String JavaDoc postfix) {
142     StringBuffer JavaDoc rv = new StringBuffer JavaDoc();
143     if (objs != null) {
144       for (int pos = 0; pos < objs.length; ++pos) {
145         if (rv.length() > 0 && separator != null) {
146           rv.append(separator);
147         }
148         if (prefix != null) {
149           rv.append(prefix);
150         }
151         rv.append(objs[pos]);
152         if (postfix != null) {
153           rv.append(postfix);
154         }
155       }
156     } else {
157       rv.append(NULL_STRING);
158     }
159     return rv.toString();
160   }
161
162   public static final String JavaDoc toPaddedString(long value, int radix, int paddedWidth) {
163     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
164     String JavaDoc strValue = Long.toString(value, radix);
165     for (int pos = 0; pos < paddedWidth - strValue.length(); ++pos) {
166       result.append("0");
167     }
168     result.append(strValue);
169     return result.toString();
170   }
171
172   public static final String JavaDoc rightJustify(String JavaDoc s, int fieldSize) {
173     if (s.length() == fieldSize) return s;
174     if (s.length() > fieldSize) {
175       final int i = Math.max(s.length() - (fieldSize - 3), 1);
176       return leftPad(s.substring(i), fieldSize, '.');
177     }
178     return leftPad(s, fieldSize, ' ');
179   }
180
181   public static final String JavaDoc rightPad(String JavaDoc s, int size, char padChar) {
182     if (s.length() >= size) return s;
183     final int padCount = size - s.length();
184     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
185     sb.append(s);
186     for (int i = 0; i < padCount; i++) {
187       sb.append(padChar);
188     }
189     String JavaDoc rv = sb.toString();
190     Assert.eval(rv.length() == size);
191     return rv;
192   }
193
194   public static final String JavaDoc leftPad(String JavaDoc s, int size, char padChar) {
195     if (s.length() >= size) return s;
196     final int padCount = size - s.length();
197     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
198     for (int i = 0; i < padCount; i++) {
199       sb.append(padChar);
200     }
201     sb.append(s);
202     String JavaDoc rv = sb.toString();
203     Assert.eval(rv.length() == size);
204     return rv;
205   }
206
207   /**
208    * Simple search/replace for non-pattern strings. optionally skipping over quoted strings.
209    *
210    * @param source the original string to perform the search/replace on, a modified version of this is returned, if null
211    * then null will be returned immediately
212    * @param search the string to search for in <code>source</code>, if null then null is returned immediately
213    * @param replace the string to replace <code>search</code> occurrences in <code>source</code>, if null then the
214    * search string is simply removed from source and not replaced with anything
215    * @param skipQuotedStrings if true then quoted strings will be skipped over
216    * @return a modified version of <code>source</code>, or null if <code>source</code> or <code>search</code> are
217    * null
218    */

219   public static final String JavaDoc replaceAll(String JavaDoc source, String JavaDoc search, String JavaDoc replace, boolean skipQuotedStrings) {
220     if (source == null || search == null) { return null; }
221     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
222     int beginQuoteIdx = 0;
223     for (int pos = 0; pos < source.length(); ++pos) {
224       if (skipQuotedStrings && source.startsWith("'", pos)) {
225         // Skip the single-quoted string
226
beginQuoteIdx = pos;
227         for (++pos; pos < source.length() && !source.startsWith("'", pos); ++pos) {
228           // skip
229
}
230
231         result.append(source.substring(beginQuoteIdx, pos + 1));
232       } else if (skipQuotedStrings && source.startsWith("\"", pos)) {
233         // Skip the double-quoted string
234
beginQuoteIdx = pos;
235         for (++pos; pos < source.length() && !source.startsWith("\"", pos); ++pos) {
236           // skip
237
}
238         result.append(source.substring(beginQuoteIdx, pos + 1));
239       } else if (source.startsWith(search, pos)) {
240         if (replace != null) {
241           result.append(replace);
242         }
243         pos += search.length() - 1;
244       } else {
245         result.append(source.charAt(pos));
246       }
247     }
248     return result.toString();
249   }
250 }
Popular Tags