1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 package com.ivata.mask.util;
87 /***
88 * <p>
89 * StringHandling is a helper class for handling Strings. It contains methods
90 * for converting to and from different data types, and for handling
91 * <code>null</code> conditions.
92 * </p>
93 *
94 * <p>
95 * Don't create an instance of this class; use the static final methods.
96 * </p>
97 *
98 * @since ivata masks 0.4 (2001-12-27)
99 * @author Colin MacLeod
100 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
101 * @version $Revision: 1.3 $
102 */
103 public final class StringHandling {
104 /***
105 * <p>
106 * This method works like <code>Boolean.valueOf</code>, but allows
107 * <code>null</code> values without an exception, and also translates the
108 * string <code>"on"</code> as <code>true</code> which is useful for
109 * converting request values.
110 * </p>
111 *
112 * @param convertToBoolean
113 * a String to be converted to a boolean value. <code>null</code>
114 * Strings return in a <code>null
115 * Boolean</code> returned.
116 * @return <code>null</code> if <code>convertToBoolean</code> is
117 * <code>null</code> or if <code>convertToBoolean</code> equals
118 * the string <code>"null"</code>. It returns a
119 * <code>Boolean true</code> value if the string is equal to
120 * <code>"true"</code> or <code>"on"</code> (ignoring case),
121 * otherwise a <code>Boolean
122 * false</code> value is returned.
123 */
124 public static Boolean booleanValue(final String convertToBoolean) {
125 if ((convertToBoolean == null) || (convertToBoolean.equals("null"))) {
126 return null;
127 }
128 return new Boolean(convertToBoolean.equalsIgnoreCase("true")
129 || convertToBoolean.equalsIgnoreCase("on"));
130 }
131 /***
132 * <p>
133 * Generates a random string of specified length, when the length 0 or less,
134 * defaults to 8 The string only consists of letters and numbers and the
135 * first character is always a letter.
136 * </p>
137 *
138 * @param length
139 * the length of the string to generate.
140 * @return a random string of the specified length.
141 */
142 public static String generateRandomString(final int length) {
143
144 assert (length > 0);
145 String returnString = "";
146 Character ch;
147 java.util.Random randomGenerator = new java.util.Random();
148 int i;
149 byte randomByte;
150 for (i = 0; i < length; i++) {
151
152 if (i == 0) {
153 do {
154 randomByte = (byte) randomGenerator.nextInt('z');
155 } while (randomByte < 'A'
156 || (randomByte > 'Z' && randomByte < 'a'));
157
158 } else {
159 do {
160 randomByte = (byte) randomGenerator.nextInt('z');
161 } while (randomByte < '0'
162 || (randomByte > '9' && randomByte < 'A')
163 || (randomByte > 'Z' && randomByte < 'a'));
164 }
165 ch = new Character((char) randomByte);
166 returnString += ch.toString();
167 }
168 return returnString;
169 }
170 /***
171 * <p>
172 * If the string supplied is not <code>null</code>, return the string,
173 * otherwise return the empty string.
174 * </p>
175 *
176 * @param check
177 * an object to compare against null
178 * @return an empty string, if <code>checkString</code> is
179 * <code>null</code>, otherwise the value of
180 * <code>checkString</code> unaltered.
181 * @see #getNotNull(Object o, String nullString)
182 */
183 public static String getNotNull(final Object check) {
184 if (check == null) {
185 return "";
186 } else {
187 return check.toString();
188 }
189 }
190 /***
191 * <p>
192 * Handle null objects in a standard way. If the object you pass is null,
193 * then the string <code>nullString</code> will be returned.
194 * </p>
195 *
196 * @param check
197 * an object to compare against null
198 * @param nullString
199 * the string to return if o is null
200 * @return the parameter nullString if o is null, otherwise o.toString()
201 * @see #getNotNull(Object check)
202 */
203 public static String getNotNull(final Object check,
204 final String nullString) {
205 if (check != null) {
206 return check.toString();
207 } else {
208 return nullString;
209 }
210 }
211 /***
212 * <p>
213 * This method works like <code>Integer.valueOf</code>, but allows
214 * <code>null</code> values without an exception.
215 * </p>
216 *
217 * @param convertToInteger
218 * a String to be converted to an integer number.
219 * <code>null</code> Strings return in a <code>null
220 * Integer</code>
221 * returned.
222 * @return <code>null</code> if <code>convertToInteger</code> is
223 * <code>null</code> or if <code>convertToInteger</code> equals
224 * the string value "null", otherwise an integer representing the
225 * base 10 value of <code>convertToInteger</code>.
226 * @see java.lang.Integer#valueOf(String convertToInteger)
227 */
228 public static Integer integerValue(final String convertToInteger) {
229 if (isNullOrEmpty(convertToInteger)
230 || (convertToInteger.equals("null"))) {
231 return null;
232 }
233 return Integer.valueOf(convertToInteger);
234 }
235 /***
236 * <p>
237 * This method does just what it says: evaluates the String you give it and
238 * returns <code>true</code> if it is <code>null</code> or an empty
239 * string.
240 * </p>
241 *
242 * @param checkString
243 * the string to check for being <code>null</code> or empty.
244 * @return <code>true</code> if <code>checkString</code> is
245 * <code>null</code> or an empty string
246 */
247 public static boolean isNullOrEmpty(final String checkString) {
248 return ((checkString == null) || (checkString.trim().equals("")));
249 }
250 /***
251 * <p>
252 * Works like the standard <code>Integer.toString()</code> except that it
253 * allows <code>null</code> values, returning a <code>null</code> string
254 * in this case.
255 * </p>
256 *
257 * @param convert
258 * the <code>Integer</code> you want to convert to a string.
259 * @return <code>null</code> if the integer <code>convert</code>. is
260 * <code>null</code>, otherwise the string equivalent of
261 * <code>convert</code>.
262 */
263 public static String toString(final Integer convert) {
264 if (convert == null) {
265 return null;
266 } else {
267 return convert.toString();
268 }
269 }
270 /***
271 * <p>
272 * Private default constructor ensures utility class functionality.
273 * </p>
274 */
275 private StringHandling() {
276 }
277 }