001 /*
002 * Copyright 2004 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.commons.convert;
017
018 /**
019 * Simple public API for the conversion system consisting of static methods.
020 * <p>
021 * The methods on this class access the default range of converters. If you wish
022 * to add to the default set of converters you cannot use this class. Instead,
023 * you must create an instance of <code>Converter</code> and add the converters
024 * there.
025 *
026 * @author Stephen Colebourne
027 * @version $Id: ConvertUtils.java 155441 2005-02-26 13:19:22Z dirkv $
028 * @since 1.0
029 */
030 public class ConvertUtils {
031
032 /** The default Converter, private to prevent subclasses altering the default */
033 private static final Converter DEFAULT = new Converter(true);
034
035 /**
036 * Restricted constructor.
037 * The main access to this class is via static methods, and it is not intended
038 * to be subclassed.
039 */
040 protected ConvertUtils() {
041 super();
042 }
043
044 //-----------------------------------------------------------------------
045 /**
046 * Convert the specified input object into an output object
047 * of the another type.
048 *
049 * @param value the input value to be converted
050 * @param fromClass the class to convert from, useful if null passed in
051 * @param toClass the class to convert to
052 * @return the converted value
053 * @throws ConversionException (runtime) if conversion fails
054 */
055 public static Object convert(Object value, Class fromClass, Class toClass) {
056 return DEFAULT.convert(value, fromClass, toClass);
057 }
058
059 /**
060 * Convert the specified input object into an output object
061 * of the another type.
062 *
063 * @param value the input value to be converted
064 * @param toClass the class to convert to
065 * @return the converted value
066 * @throws ConversionException (runtime) if conversion fails
067 */
068 public static Object convert(Object value, Class toClass) {
069 return DEFAULT.convert(value, toClass);
070 }
071
072 /**
073 * Convert the specified input object into a <code>String</code>.
074 *
075 * @param value the input value to be converted
076 * @param fromClass the class to convert from, useful if null passed in
077 * @return the converted value
078 * @throws ConversionException (runtime) if conversion fails
079 */
080 public static String convertToString(Object value, Class fromClass) {
081 return DEFAULT.convertToString(value, fromClass);
082 }
083
084 /**
085 * Convert the specified input object into a <code>String</code>.
086 *
087 * @param value the input value to be converted
088 * @return the converted value
089 * @throws ConversionException (runtime) if conversion fails
090 */
091 public static String convertToString(Object value) {
092 return DEFAULT.convertToString(value);
093 }
094
095 }