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