001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    
019    package org.apache.commons.beanutils;
020    
021    /**
022     * <p>Utility methods for converting String scalar values to objects of the
023     * specified Class, String arrays to arrays of the specified Class.</p>
024     *
025     * <p>For more details, see <code>ConvertUtilsBean</code> which provides the
026     * implementations for these methods.</p>
027     *
028     * @author Craig R. McClanahan
029     * @author Ralph Schaer
030     * @author Chris Audley
031     * @version $Revision: 556229 $ $Date: 2007-07-14 02:11:19 -0400 (Sat, 14 Jul 2007) $
032     * @see ConvertUtilsBean
033     */
034    
035    public class ConvertUtils {
036    
037    
038        // ------------------------------------------------------ Static Properties
039    
040        /**
041         * Gets the default value for Boolean conversions.
042         * @return The default Boolean value
043         * @deprecated Register replacement converters for Boolean.TYPE and
044         *  Boolean.class instead
045         */
046        public static boolean getDefaultBoolean() {
047            return (ConvertUtilsBean.getInstance().getDefaultBoolean());
048        }
049    
050        /**
051         * Sets the default value for Boolean conversions.
052         * @param newDefaultBoolean The default Boolean value
053         * @deprecated Register replacement converters for Boolean.TYPE and
054         *  Boolean.class instead
055         */
056        public static void setDefaultBoolean(boolean newDefaultBoolean) {
057            ConvertUtilsBean.getInstance().setDefaultBoolean(newDefaultBoolean);
058        }
059    
060    
061        /**
062         * Gets the default value for Byte conversions.
063         * @return The default Byte value
064         * @deprecated Register replacement converters for Byte.TYPE and
065         *  Byte.class instead
066         */
067        public static byte getDefaultByte() {
068            return ConvertUtilsBean.getInstance().getDefaultByte();
069        }
070    
071        /**
072         * Sets the default value for Byte conversions.
073         * @param newDefaultByte The default Byte value
074         * @deprecated Register replacement converters for Byte.TYPE and
075         *  Byte.class instead
076         */
077        public static void setDefaultByte(byte newDefaultByte) {
078            ConvertUtilsBean.getInstance().setDefaultByte(newDefaultByte);
079        }
080    
081    
082        /**
083         * Gets the default value for Character conversions.
084         * @return The default Character value
085         * @deprecated Register replacement converters for Character.TYPE and
086         *  Character.class instead
087         */
088        public static char getDefaultCharacter() {
089            return ConvertUtilsBean.getInstance().getDefaultCharacter();
090        }
091    
092        /**
093         * Sets the default value for Character conversions.
094         * @param newDefaultCharacter The default Character value
095         * @deprecated Register replacement converters for Character.TYPE and
096         *  Character.class instead
097         */
098        public static void setDefaultCharacter(char newDefaultCharacter) {
099            ConvertUtilsBean.getInstance().setDefaultCharacter(newDefaultCharacter);
100        }
101    
102    
103        /**
104         * Gets the default value for Double conversions.
105         * @return The default Double value
106         * @deprecated Register replacement converters for Double.TYPE and
107         *  Double.class instead
108         */
109        public static double getDefaultDouble() {
110            return ConvertUtilsBean.getInstance().getDefaultDouble();
111        }
112    
113        /**
114         * Sets the default value for Double conversions.
115         * @param newDefaultDouble The default Double value
116         * @deprecated Register replacement converters for Double.TYPE and
117         *  Double.class instead
118         */
119        public static void setDefaultDouble(double newDefaultDouble) {
120            ConvertUtilsBean.getInstance().setDefaultDouble(newDefaultDouble);
121        }
122    
123    
124        /**
125         * Get the default value for Float conversions.
126         * @return The default Float value
127         * @deprecated Register replacement converters for Float.TYPE and
128         *  Float.class instead
129         */
130        public static float getDefaultFloat() {
131            return ConvertUtilsBean.getInstance().getDefaultFloat();
132        }
133    
134        /**
135         * Sets the default value for Float conversions.
136         * @param newDefaultFloat The default Float value
137         * @deprecated Register replacement converters for Float.TYPE and
138         *  Float.class instead
139         */
140        public static void setDefaultFloat(float newDefaultFloat) {
141            ConvertUtilsBean.getInstance().setDefaultFloat(newDefaultFloat);
142        }
143    
144    
145        /**
146         * Gets the default value for Integer conversions.
147         * @return The default Integer value
148         * @deprecated Register replacement converters for Integer.TYPE and
149         *  Integer.class instead
150         */
151        public static int getDefaultInteger() {
152            return ConvertUtilsBean.getInstance().getDefaultInteger();
153        }
154    
155        /**
156         * Sets the default value for Integer conversions.
157         * @param newDefaultInteger The default Integer value
158         * @deprecated Register replacement converters for Integer.TYPE and
159         *  Integer.class instead
160         */
161        public static void setDefaultInteger(int newDefaultInteger) {
162            ConvertUtilsBean.getInstance().setDefaultInteger(newDefaultInteger);
163        }
164    
165    
166        /**
167         * Gets the default value for Long conversions.
168         * @return The default Long value
169         * @deprecated Register replacement converters for Long.TYPE and
170         *  Long.class instead
171         */
172        public static long getDefaultLong() {
173            return (ConvertUtilsBean.getInstance().getDefaultLong());
174        }
175    
176        /**
177         * Sets the default value for Long conversions.
178         * @param newDefaultLong The default Long value
179         * @deprecated Register replacement converters for Long.TYPE and
180         *  Long.class instead
181         */
182        public static void setDefaultLong(long newDefaultLong) {
183            ConvertUtilsBean.getInstance().setDefaultLong(newDefaultLong);
184        }
185    
186    
187        /**
188         * Gets the default value for Short conversions.
189         * @return The default Short value
190         * @deprecated Register replacement converters for Short.TYPE and
191         *  Short.class instead
192         */
193        public static short getDefaultShort() {
194            return ConvertUtilsBean.getInstance().getDefaultShort();
195        }
196    
197        /**
198         * Sets the default value for Short conversions.
199         * @param newDefaultShort The default Short value
200         * @deprecated Register replacement converters for Short.TYPE and
201         *  Short.class instead
202         */
203        public static void setDefaultShort(short newDefaultShort) {
204            ConvertUtilsBean.getInstance().setDefaultShort(newDefaultShort);
205        }
206    
207        // --------------------------------------------------------- Public Classes
208    
209    
210        /**
211         * <p>Convert the specified value into a String.</p>
212         *
213         * <p>For more details see <code>ConvertUtilsBean</code>.</p>
214         *
215         * @param value Value to be converted (may be null)
216         * @return The converted String value
217         *
218         * @see ConvertUtilsBean#convert(Object)
219         */
220        public static String convert(Object value) {
221    
222            return ConvertUtilsBean.getInstance().convert(value);
223    
224        }
225    
226    
227        /**
228         * <p>Convert the specified value to an object of the specified class (if
229         * possible).  Otherwise, return a String representation of the value.</p>
230         *
231         * <p>For more details see <code>ConvertUtilsBean</code>.</p>
232         *
233         * @param value Value to be converted (may be null)
234         * @param clazz Java class to be converted to
235         * @return The converted value
236         *
237         * @see ConvertUtilsBean#convert(String, Class)
238         */
239        public static Object convert(String value, Class clazz) {
240    
241            return ConvertUtilsBean.getInstance().convert(value, clazz);
242    
243        }
244    
245    
246        /**
247         * <p>Convert an array of specified values to an array of objects of the
248         * specified class (if possible).</p>
249         *
250         * <p>For more details see <code>ConvertUtilsBean</code>.</p>
251         *
252         * @param values Array of values to be converted
253         * @param clazz Java array or element class to be converted to
254         * @return The converted value
255         *
256         * @see ConvertUtilsBean#convert(String[], Class)
257         */
258        public static Object convert(String[] values, Class clazz) {
259    
260      return ConvertUtilsBean.getInstance().convert(values, clazz);
261    
262        }
263    
264        /**
265         * <p>Convert the value to an object of the specified class (if
266         * possible).</p>
267         *
268         * @param value Value to be converted (may be null)
269         * @param targetType Class of the value to be converted to
270         * @return The converted value
271         *
272         * @exception ConversionException if thrown by an underlying Converter
273         */
274        public static Object convert(Object value, Class targetType) {
275    
276            return ConvertUtilsBean.getInstance().convert(value, targetType);
277    
278        }
279    
280        /**
281         * <p>Remove all registered {@link Converter}s, and re-establish the
282         * standard Converters.</p>
283         *
284         * <p>For more details see <code>ConvertUtilsBean</code>.</p>
285         *
286         * @see ConvertUtilsBean#deregister()
287         */
288        public static void deregister() {
289    
290            ConvertUtilsBean.getInstance().deregister();
291    
292        }
293    
294    
295        /**
296         * <p>Remove any registered {@link Converter} for the specified destination
297         * <code>Class</code>.</p>
298         *
299         * <p>For more details see <code>ConvertUtilsBean</code>.</p>
300         *
301         * @param clazz Class for which to remove a registered Converter
302         * @see ConvertUtilsBean#deregister(Class)
303         */
304        public static void deregister(Class clazz) {
305    
306            ConvertUtilsBean.getInstance().deregister(clazz);
307    
308        }
309    
310    
311        /**
312         * <p>Look up and return any registered {@link Converter} for the specified
313         * destination class; if there is no registered Converter, return
314         * <code>null</code>.</p>
315         *
316         * <p>For more details see <code>ConvertUtilsBean</code>.</p>
317         *
318         * @param clazz Class for which to return a registered Converter
319         * @return The registered {@link Converter} or <code>null</code> if not found
320         * @see ConvertUtilsBean#lookup(Class)
321         */
322        public static Converter lookup(Class clazz) {
323    
324            return ConvertUtilsBean.getInstance().lookup(clazz);
325    
326        }
327    
328        /**
329         * Look up and return any registered {@link Converter} for the specified
330         * source and destination class; if there is no registered Converter,
331         * return <code>null</code>.
332         *
333         * @param sourceType Class of the value being converted
334         * @param targetType Class of the value to be converted to
335         * @return The registered {@link Converter} or <code>null</code> if not found
336         */
337        public static Converter lookup(Class sourceType, Class targetType) {
338    
339            return ConvertUtilsBean.getInstance().lookup(sourceType, targetType);
340    
341        }
342    
343        /**
344         * <p>Register a custom {@link Converter} for the specified destination
345         * <code>Class</code>, replacing any previously registered Converter.</p>
346         *
347         * <p>For more details see <code>ConvertUtilsBean</code>.</p>
348         *
349         * @param converter Converter to be registered
350         * @param clazz Destination class for conversions performed by this
351         *  Converter
352         * @see ConvertUtilsBean#register(Converter, Class)
353         */
354        public static void register(Converter converter, Class clazz) {
355    
356            ConvertUtilsBean.getInstance().register(converter, clazz);
357    
358        }
359    
360    
361    }