View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18
    19  package org.apache.commons.beanutils;
    20
    21  /***
    22   * <p>Utility methods for converting String scalar values to objects of the
    23   * specified Class, String arrays to arrays of the specified Class.</p>
    24   *
    25   * <p>For more details, see <code>ConvertUtilsBean</code> which provides the
    26   * implementations for these methods.</p>
    27   *
    28   * @author Craig R. McClanahan
    29   * @author Ralph Schaer
    30   * @author Chris Audley
    31   * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
    32   * @see ConvertUtilsBean
    33   */
    34
    35  public class ConvertUtils {
    36
    37
    38      // ------------------------------------------------------ Static Properties
    39
    40      /***
    41       * Gets the default value for Boolean conversions.
    42       * @return The default Boolean value
    43       * @deprecated Register replacement converters for Boolean.TYPE and
    44       *  Boolean.class instead
    45       */
    46      public static boolean getDefaultBoolean() {
    47          return (ConvertUtilsBean.getInstance().getDefaultBoolean());
    48      }
    49
    50      /***
    51       * Sets the default value for Boolean conversions.
    52       * @param newDefaultBoolean The default Boolean value
    53       * @deprecated Register replacement converters for Boolean.TYPE and
    54       *  Boolean.class instead
    55       */
    56      public static void setDefaultBoolean(boolean newDefaultBoolean) {
    57          ConvertUtilsBean.getInstance().setDefaultBoolean(newDefaultBoolean);
    58      }
    59
    60
    61      /***
    62       * Gets the default value for Byte conversions.
    63       * @return The default Byte value
    64       * @deprecated Register replacement converters for Byte.TYPE and
    65       *  Byte.class instead
    66       */
    67      public static byte getDefaultByte() {
    68          return ConvertUtilsBean.getInstance().getDefaultByte();
    69      }
    70
    71      /***
    72       * Sets the default value for Byte conversions.
    73       * @param newDefaultByte The default Byte value
    74       * @deprecated Register replacement converters for Byte.TYPE and
    75       *  Byte.class instead
    76       */
    77      public static void setDefaultByte(byte newDefaultByte) {
    78          ConvertUtilsBean.getInstance().setDefaultByte(newDefaultByte);
    79      }
    80
    81
    82      /***
    83       * Gets the default value for Character conversions.
    84       * @return The default Character value
    85       * @deprecated Register replacement converters for Character.TYPE and
    86       *  Character.class instead
    87       */
    88      public static char getDefaultCharacter() {
    89          return ConvertUtilsBean.getInstance().getDefaultCharacter();
    90      }
    91
    92      /***
    93       * Sets the default value for Character conversions.
    94       * @param newDefaultCharacter The default Character value
    95       * @deprecated Register replacement converters for Character.TYPE and
    96       *  Character.class instead
    97       */
    98      public static void setDefaultCharacter(char newDefaultCharacter) {
    99          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 }