View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: StringHandling.java,v $
31   * Revision 1.3  2005/04/09 18:04:17  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.2  2005/01/06 22:21:45  colinmacleod
35   * Moved up a version number.
36   * Changed copyright notices to 2005.
37   * Updated the documentation:
38   *   - started working on multiproject:site docu.
39   *   - changed the logo.
40   * Added checkstyle and fixed LOADS of style issues.
41   * Added separate thirdparty subproject.
42   * Added struts (in web), util and webgui (in webtheme) from ivata op.
43   *
44   * Revision 1.3  2004/03/21 21:16:36  colinmacleod
45   * Shortened name to ivata op.
46   *
47   * Revision 1.2  2004/02/01 22:07:32  colinmacleod
48   * Added full names to author tags
49   *
50   * Revision 1.1.1.1  2004/01/27 20:59:47  colinmacleod
51   * Moved ivata op to SourceForge.
52   *
53   * Revision 1.2  2003/10/15 14:13:53  colin
54   * fixing for XDoclet
55   *
56   * Revision 1.9  2003/02/24 19:27:31  colin
57   * restructured file paths
58   *
59   * Revision 1.8  2003/02/04 17:43:52  colin
60   * copyright notice
61   *
62   * Revision 1.7  2002/09/16 14:18:52  colin
63   * added booleanValue
64   *
65   * Revision 1.6  2002/09/09 13:57:35  peter
66   * fixed naming convention
67   *
68   * Revision 1.4  2002/08/15 08:27:04  peter
69   * modified
70   *
71   * Revision 1.1  2002/04/27 17:38:21  colin
72   * first compiling version in EJB/JBuilder project
73   *
74   * Revision 1.1.1.1  2002/04/22 13:51:47  colin
75   * EJB version of the intranet project
76   *
77   * Revision 1.2  2002/02/03 13:09:45  colin
78   * made docbook documentation;
79   * changed name of method valueOf to integerValue
80   *
81   * Revision 1.1  2002/01/20 19:28:25  colin
82   * added tab and tree tags
83   * implemented address book functionality
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         // check we got some length
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             // the first character must be a letter
152             if (i == 0) {
153                 do {
154                     randomByte = (byte) randomGenerator.nextInt('z');
155                 } while (randomByte < 'A'
156                         || (randomByte > 'Z' && randomByte < 'a'));
157                 // the others can also be numbers
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 }