KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > value > Helper


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.value;
10
11
12 /**
13  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
14  * @version $Revision: 1.1.1.1 $
15  */

16 public class Helper
17 {
18
19    public static final int[] EMPTY_INT_ARRAY = new int[0];
20    public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
21    public static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
22    public static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
23
24    public static final Converter INTEGER_CONVERTER = new Converter()
25    {
26       public boolean accept(Class JavaDoc clazz)
27       {
28          return Integer JavaDoc.class.equals(clazz);
29       }
30       public Object JavaDoc toObject(String JavaDoc value) throws NullConversionException, FormatConversionException
31       {
32          if (value == null)
33          {
34             return null;
35          }
36          try
37          {
38             return Integer.decode(value);
39          }
40          catch (NumberFormatException JavaDoc e)
41          {
42             throw new FormatConversionException();
43          }
44       }
45       public String JavaDoc toString(Object JavaDoc value) throws NullConversionException, FormatConversionException
46       {
47          if (value == null)
48          {
49             return null;
50          }
51          if (!(value instanceof Integer JavaDoc))
52          {
53             throw new FormatConversionException();
54          }
55          return value.toString();
56       }
57    };
58
59    public static final Converter BOOLEAN_CONVERTER = new Converter()
60    {
61       public boolean accept(Class JavaDoc clazz)
62       {
63          return Boolean JavaDoc.class.equals(clazz);
64       }
65       public Object JavaDoc toObject(String JavaDoc value) throws NullConversionException, FormatConversionException
66       {
67          if (value == null)
68          {
69             return null;
70          }
71          return Boolean.valueOf(Helper.toBoolean(value));
72       }
73       public String JavaDoc toString(Object JavaDoc value) throws NullConversionException, FormatConversionException
74       {
75          if (value == null)
76          {
77             return null;
78          }
79          if (!(value instanceof Boolean JavaDoc))
80          {
81             throw new FormatConversionException();
82          }
83          return value.toString();
84       }
85    };
86
87    public static int toInt(String JavaDoc value) throws NullConversionException, FormatConversionException
88    {
89       try
90       {
91          if (value == null)
92          {
93             throw new NullConversionException();
94          }
95          return Integer.parseInt(value);
96       }
97       catch (NumberFormatException JavaDoc e)
98       {
99          throw new FormatConversionException();
100       }
101    }
102
103    public static boolean toBoolean(String JavaDoc value) throws NullConversionException, FormatConversionException
104    {
105       if ("true".equals(value))
106       {
107          return true;
108       }
109       else if ("false".equals(value))
110       {
111          return false;
112       }
113       else if (value == null)
114       {
115          throw new NullConversionException();
116       }
117       else
118       {
119          throw new FormatConversionException();
120       }
121    }
122
123    public static String JavaDoc toString(int value)
124    {
125       return Integer.toString(value);
126    }
127
128    public static String JavaDoc toString(boolean value)
129    {
130       return Boolean.toString(value);
131    }
132
133    public static String JavaDoc toString(Object JavaDoc value, Converter converter) throws NullConversionException, FormatConversionException, IllegalArgumentException JavaDoc
134    {
135       if (converter == null)
136       {
137          throw new IllegalArgumentException JavaDoc();
138       }
139       return converter.toString(value);
140    }
141
142    public static String JavaDoc[] toStringArray(int[] values) throws IllegalArgumentException JavaDoc
143    {
144       if (values == null)
145       {
146          throw new IllegalArgumentException JavaDoc();
147       }
148       String JavaDoc[] strings = new String JavaDoc[values.length];
149       for (int i = 0;i < strings.length;i++)
150       {
151          strings[i] = Integer.toString(values[i]);
152       }
153       return strings;
154    }
155
156    public static String JavaDoc[] toStringArray(boolean[] values) throws IllegalArgumentException JavaDoc
157    {
158       if (values == null)
159       {
160          throw new IllegalArgumentException JavaDoc();
161       }
162       String JavaDoc[] strings = new String JavaDoc[values.length];
163       for (int i = 0;i < strings.length;i++)
164       {
165          strings[i] = Boolean.toString(values[i]);
166       }
167       return strings;
168    }
169
170    public static String JavaDoc[] toStringArray(Object JavaDoc[] values, Converter converter) throws NullConversionException, FormatConversionException, IllegalArgumentException JavaDoc
171    {
172       if (values == null)
173       {
174          throw new IllegalArgumentException JavaDoc();
175       }
176       if (converter == null)
177       {
178          throw new IllegalArgumentException JavaDoc();
179       }
180       String JavaDoc[] strings = new String JavaDoc[values.length];
181       for (int i = 0;i < strings.length;i++)
182       {
183          Object JavaDoc value = values[i];
184          if (value != null)
185          {
186             strings[i] = converter.toString(values[i]);
187          }
188       }
189       return strings;
190    }
191 }
192
Popular Tags