001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.lang;
007:
008: /**
009: * The <code>TypeConverter</code> class provides efficient and
010: * consistent static type converter utilities for the basic types.
011: *
012: * @author <a href="mailto:jnielsen@sct.com">Jan Nielsen</a>
013: *
014: * @version $Revision: 34761 $
015: **/
016: public final class TypeConverter {
017: /** Canonical string representation of <code>true</code>. */
018: public static final String TRUE = "true";
019:
020: /** Canonical string representation of <code>false</code>. */
021: public static final String FALSE = "false";
022:
023: /**
024: * Returns a canonical <code>String</code> object representing the
025: * specified <code>boolean</code>. If the value is
026: * <code>true</code>, the string <code>"true"</code> will be
027: * returned; if the value is <code>false</code>, the string
028: * <code>"false"</code>.
029: *
030: * @param value <code>boolean</code> value to be converted
031: *
032: * @return canonical string representation of the
033: * <code>boolean</code> value
034: **/
035: public static final String toString(boolean value) {
036: String outValue = null;
037:
038: if (value) {
039: outValue = TRUE;
040: } else {
041: outValue = FALSE;
042: }
043:
044: return outValue;
045: }
046:
047: /**
048: * Returns the <code>boolean</code> value represented by the
049: * specified string, ignoring case. If the value is neither
050: * <code>"true"</code>, nor <code>"false"</code> (ignoring case),
051: * an <code>IllegalArgumentException</code> is thrown.<p/>
052: *
053: * Note: This implementation is different than the
054: * <code>java.lang.Boolean</code> implementation. The
055: * <code>java.lang.Boolean</code> treats <code>null</code> and any
056: * non-<code>"true"</code> value (ignoring case) as
057: * <code>false</code>, i.e., there is no validation of
058: * <code>false</code> values.
059: *
060: * @param value string representation of a boolean value
061: *
062: * @return <code>boolean</code> value represented by the argument
063: *
064: * @throws IllegalArgumentException if value is (ignoring case)
065: * neither <code>"true"</code>, nor <code>"false"</code>
066: **/
067: public static final boolean toBoolean(String value) {
068: try {
069: boolean outValue = false;
070:
071: if (TRUE == value || value.equalsIgnoreCase(TRUE)) {
072: outValue = true;
073: } else if (FALSE == value || value.equalsIgnoreCase(FALSE)) {
074: outValue = false;
075: } else {
076: throw new IllegalArgumentException(ThrowableHelper
077: .getInternationalizedMessage(
078: TypeConverter.class,
079: "error.argument_not_parseable",
080: new String[] { value, "boolean" }));
081: }
082:
083: return outValue;
084: } catch (NullPointerException x) {
085: throw new IllegalArgumentException(ThrowableHelper
086: .getInternationalizedMessage(TypeConverter.class,
087: "error.argument_is_null"));
088: }
089: }
090:
091: /**
092: * Returns a string representation of the argument of radix 10.
093: *
094: * @param value <code>byte</code> value to be converted
095: *
096: * @return string respresentation of the argument
097: **/
098: public static final String toString(byte value) {
099: return toString((int) value);
100: }
101:
102: /**
103: * Returns a signed decimal <code>byte</code> value. The
104: * characters in the string must all be decimal digits, except
105: * that the first character may be an ASCII minus sign '-'
106: * ('\u002D') to indicate a negative value.
107: *
108: * @param value string representation of a <code>byte</code> value
109: *
110: * @return <code>byte</code> value represented by the argument
111: *
112: * @throws IllegalArgumentException if argument is not a parseable
113: * <code>byte</code>
114: **/
115: public static final byte toByte(String value) {
116: try {
117: return Byte.parseByte(value);
118: } catch (NumberFormatException x) {
119: throw new IllegalArgumentException(ThrowableHelper
120: .getInternationalizedMessage(TypeConverter.class,
121: "error.argument_not_parseable",
122: new String[] { value, "byte" }));
123: } catch (NullPointerException x) {
124: throw new IllegalArgumentException(ThrowableHelper
125: .getInternationalizedMessage(TypeConverter.class,
126: "error.argument_is_null"));
127: }
128: }
129:
130: /**
131: * Returns a string representation of the argument. The result
132: * string of length 1 consisting solely of the specified char.
133: *
134: * @param value <code>char</code> value to be converted
135: *
136: * @return string representation of the argument
137: **/
138: public static final String toString(char value) {
139: return String.valueOf(new char[] { value });
140: }
141:
142: /**
143: * Returns the first character of the string.
144: *
145: * @param value string representation of the <code>char</code>
146: *
147: * @return <code>char</code> representation of the argument
148: *
149: * @throws IllegalArgumentException if <code>value</code> is
150: * <code>null</code> or of zero length
151: **/
152: public static final char toChar(String value) {
153: try {
154: return value.charAt(0);
155: } catch (IndexOutOfBoundsException x) {
156: throw new IllegalArgumentException(ThrowableHelper
157: .getInternationalizedMessage(TypeConverter.class,
158: "error.argument_not_parseable",
159: new String[] { value, "char" }));
160: } catch (NullPointerException x) {
161: throw new IllegalArgumentException(ThrowableHelper
162: .getInternationalizedMessage(TypeConverter.class,
163: "error.argument_is_null"));
164: }
165: }
166:
167: /**
168: * Returns a string representation of the specified
169: * <code>short</code> value of radix 10.
170: *
171: * @param value <code>short</code> value to be converted
172: *
173: * @return string representation of the argument
174: **/
175: public static final String toString(short value) {
176: return toString((int) value);
177: }
178:
179: /**
180: * Returns the <code>short</code> representation of the specified
181: * string. The characters in the string must all be decimal
182: * digits, except that the first character may be an ASCII minus
183: * sign '-' ('\u002D') to indicate a negative value.
184: *
185: * @param value string representation of the <code>short</code>
186: *
187: * @return <code>short</code> representation of the argument
188: *
189: * @throws IllegalArgumentException if argument is not a parseable
190: * <code>short</code>
191: **/
192: public static final short toShort(String value) {
193: try {
194: return Short.parseShort(value);
195: } catch (NumberFormatException x) {
196: throw new IllegalArgumentException(ThrowableHelper
197: .getInternationalizedMessage(TypeConverter.class,
198: "error.argument_not_parseable",
199: new String[] { value, "short" }));
200: } catch (NullPointerException x) {
201: throw new IllegalArgumentException(ThrowableHelper
202: .getInternationalizedMessage(TypeConverter.class,
203: "error.argument_is_null"));
204: }
205: }
206:
207: /**
208: * Returns a string representation of the specified
209: * <code>int</code>.
210: *
211: * @param value <code>int</code> >value to be converted
212: *
213: * @return string representation of the argument
214: **/
215: public static final String toString(int value) {
216: return Integer.toString(value);
217: }
218:
219: /**
220: * Parses the string argument as a signed decimal integer. The
221: * characters in the string must all be decimal digits, except
222: * that the first character may be an ASCII minus sign '-'
223: * ('\u002D') to indicate a negative value.
224: *
225: * @param value string representation of the <code>int</code>
226: *
227: * @return <code>int</code> representation of the argument
228: *
229: * @throws IllegalArgumentException if string does not contain a
230: * parseable <code>int</code>
231: **/
232: public static final int toInt(String value) {
233: try {
234: return Integer.parseInt(value);
235: } catch (NumberFormatException x) {
236: throw new IllegalArgumentException(ThrowableHelper
237: .getInternationalizedMessage(TypeConverter.class,
238: "error.argument_not_parseable",
239: new String[] { value, "int" }));
240: } catch (NullPointerException x) {
241: throw new IllegalArgumentException(ThrowableHelper
242: .getInternationalizedMessage(TypeConverter.class,
243: "error.argument_is_null"));
244: }
245: }
246:
247: /**
248: * Returns <code>true</code> if the value can be parsed to an
249: * <code>int</code>.
250: *
251: * @param value string representation of the <code>int</code>
252: *
253: * @return <code>true</code> if argument is an <code>int</code>;
254: * otherwise <code>false</code>
255: **/
256: public static final boolean isInt(String value) {
257: boolean isInt = true;
258:
259: try {
260: TypeConverter.toInt(value);
261: } catch (IllegalArgumentException x) {
262: isInt = false;
263: }
264:
265: return isInt;
266: }
267:
268: /**
269: * Returns a string representation of the specified
270: * <code>long</code>.
271: *
272: * @param value <code>long</code> value to be converted
273: *
274: * @return string representation of the argument
275: **/
276: public static final String toString(long value) {
277: return Long.toString(value);
278: }
279:
280: /**
281: * Parses the string argument as a signed long in the radix
282: * specified by the second argument. The characters in the string
283: * must all be digits of the specified radix (as determined by
284: * whether Character.digit returns a nonnegative value), except
285: * that the first character may be an ASCII minus sign '-'
286: * ('\u002d' to indicate a negative value. The resulting long
287: * value is returned. Note that neither L nor l is permitted to
288: * appear at the end of the string as a type indicator, as would
289: * be permitted in Java programming language source code.
290: *
291: * @param value string representation of the <code>long</code>
292: *
293: * @return <code>long</code> representation of the argument
294: *
295: * @throws IllegalArgumentException if string does not contain a
296: * parseable <code>long</code>
297: **/
298: public static final long toLong(String value) {
299: try {
300: return Long.parseLong(value);
301: } catch (NumberFormatException x) {
302: throw new IllegalArgumentException(ThrowableHelper
303: .getInternationalizedMessage(TypeConverter.class,
304: "error.argument_not_parseable",
305: new String[] { value, "long" }));
306: } catch (NullPointerException x) {
307: throw new IllegalArgumentException(ThrowableHelper
308: .getInternationalizedMessage(TypeConverter.class,
309: "error.argument_is_null"));
310: }
311: }
312:
313: /**
314: * Returns a string representation of the specified
315: * <code>float</code>.
316: *
317: * @param value <code>float</code> value to be converted
318: *
319: * @return string representation of the argument
320: **/
321: public static final String toString(float value) {
322: return Float.toString(value);
323: }
324:
325: /**
326: * Returns a new float initialized to the value represented by the
327: * specified String, as performed by the valueOf method of class
328: * Double.
329: *
330: * @param value string representation of the <code>long</code>
331: *
332: * @return <code>long</code> representation of the argument
333: *
334: * @throws IllegalArgumentException if string does not contain a
335: * parseable <code>long</code>
336: **/
337: public static final float toFloat(String value) {
338: float floatValue = Float.NaN;
339:
340: try {
341: floatValue = Float.parseFloat(value);
342: } catch (NumberFormatException x) {
343: // In JRE 1.3 and JRE 1.2, the values "Infinity" and
344: // "-Infinity" are not parsed to the appropriate typed
345: // "value".
346: if ("Infinity".equals(value)) {
347: floatValue = Float.POSITIVE_INFINITY;
348: } else if ("-Infinity".equals(value)) {
349: floatValue = Float.NEGATIVE_INFINITY;
350: } else {
351: throw new IllegalArgumentException(ThrowableHelper
352: .getInternationalizedMessage(
353: TypeConverter.class,
354: "error.argument_not_parseable",
355: new String[] { value, "float" }));
356: }
357: } catch (NullPointerException x) {
358: throw new IllegalArgumentException(ThrowableHelper
359: .getInternationalizedMessage(TypeConverter.class,
360: "error.argument_is_null"));
361: }
362:
363: return floatValue;
364: }
365:
366: /**
367: * Returns a string representation of the specified
368: * <code>double</code>.
369: *
370: * @param value <code>double</code> value to be converted
371: *
372: * @return string representation of the argument
373: **/
374: public static final String toString(double value) {
375: return Double.toString(value);
376: }
377:
378: /**
379: * Returns a new double initialized to the value represented by
380: * the specified String, as performed by the valueOf method of
381: * class Double.
382: *
383: * @param value string representation of the <code>long</code>
384: *
385: * @return <code>long</code> representation of the argument
386: *
387: * @throws IllegalArgumentException if string does not contain a
388: * parseable <code>long</code>
389: **/
390: public static final double toDouble(String value) {
391: double doubleValue = Double.NaN;
392:
393: try {
394: doubleValue = Double.parseDouble(value);
395: } catch (NumberFormatException x) {
396: // In JRE 1.3 and JRE 1.2, the values "Infinity" and
397: // "-Infinity" are not parsed to the appropriate typed
398: // "value".
399: if ("Infinity".equals(value)) {
400: doubleValue = Double.POSITIVE_INFINITY;
401: } else if ("-Infinity".equals(value)) {
402: doubleValue = Double.NEGATIVE_INFINITY;
403: } else {
404: throw new IllegalArgumentException(ThrowableHelper
405: .getInternationalizedMessage(
406: TypeConverter.class,
407: "error.argument_not_parseable",
408: new String[] { value, "double" }));
409: }
410: } catch (NullPointerException x) {
411: throw new IllegalArgumentException(ThrowableHelper
412: .getInternationalizedMessage(TypeConverter.class,
413: "error.argument_is_null"));
414: }
415:
416: return doubleValue;
417: }
418:
419: /**
420: * Returns a hexidecimal string representation of the specified
421: * byte array.
422: *
423: * @param values <code>byte</code> array to be converted
424: *
425: * @return hexidecimal string representation of argument
426: **/
427: public static final String toHexString(byte[] values) {
428: char[] buffer = new char[2 * values.length];
429:
430: for (int i = 0; i < values.length; i++) {
431: buffer[(i << 1)] = DIGITS[(values[i] & 0xf0) >> 4];
432: buffer[(i << 1) + 1] = DIGITS[(values[i] & 0x0f)];
433: }
434:
435: return new String(buffer);
436: }
437:
438: /**
439: * Converts a long value to a hexidecimal string.
440: *
441: * @param value long to be converted
442: *
443: * @return string representation of the long value
444: **/
445: public static final String toHexString(long value) {
446: return Long.toHexString(value);
447: }
448:
449: /** Hexidecimal digits. */
450: private static final char[] DIGITS = { '0', '1', '2', '3', '4',
451: '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
452:
453: /**
454: * Private constructor.
455: **/
456: private TypeConverter() {
457: }
458: }
|