ConvertUtils.java

  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. package org.apache.commons.beanutils2;

  18. /**
  19.  * <p>
  20.  * Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class.
  21.  * </p>
  22.  *
  23.  * <p>
  24.  * For more details, see {@code ConvertUtilsBean} which provides the implementations for these methods.
  25.  * </p>
  26.  *
  27.  * @see ConvertUtilsBean
  28.  */
  29. public final class ConvertUtils {

  30.     /**
  31.      * <p>
  32.      * Converts the specified value into a String.
  33.      * </p>
  34.      *
  35.      * <p>
  36.      * For more details see {@code ConvertUtilsBean}.
  37.      * </p>
  38.      *
  39.      * @param value Value to be converted (may be null)
  40.      * @return The converted String value or null if value is null
  41.      * @see ConvertUtilsBean#convert(Object)
  42.      */
  43.     public static String convert(final Object value) {
  44.         return ConvertUtilsBean.getInstance().convert(value);
  45.     }

  46.     /**
  47.      * <p>
  48.      * Converts the value to an object of the specified class (if possible).
  49.      * </p>
  50.      *
  51.      * @param value      Value to be converted (may be null)
  52.      * @param targetType Class of the value to be converted to (must not be null)
  53.      * @return The converted value
  54.      * @throws ConversionException if thrown by an underlying Converter
  55.      */
  56.     public static Object convert(final Object value, final Class<?> targetType) {
  57.         return ConvertUtilsBean.getInstance().convert(value, targetType);
  58.     }

  59.     /**
  60.      * <p>
  61.      * Converts the specified value to an object of the specified class (if possible). Otherwise, return a String representation of the value.
  62.      * </p>
  63.      *
  64.      * <p>
  65.      * For more details see {@code ConvertUtilsBean}.
  66.      * </p>
  67.      *
  68.      * @param value Value to be converted (may be null)
  69.      * @param clazz Java class to be converted to (must not be null)
  70.      * @return The converted value
  71.      * @see ConvertUtilsBean#convert(String, Class)
  72.      */
  73.     public static Object convert(final String value, final Class<?> clazz) {
  74.         return ConvertUtilsBean.getInstance().convert(value, clazz);
  75.     }

  76.     /**
  77.      * <p>
  78.      * Convert an array of specified values to an array of objects of the specified class (if possible).
  79.      * </p>
  80.      *
  81.      * <p>
  82.      * For more details see {@code ConvertUtilsBean}.
  83.      * </p>
  84.      *
  85.      * @param values Array of values to be converted
  86.      * @param clazz  Java array or element class to be converted to (must not be null)
  87.      * @return The converted value
  88.      * @see ConvertUtilsBean#convert(String[], Class)
  89.      */
  90.     public static Object convert(final String[] values, final Class<?> clazz) {
  91.         return ConvertUtilsBean.getInstance().convert(values, clazz);
  92.     }

  93.     /**
  94.      * <p>
  95.      * Remove all registered {@link Converter}s, and re-establish the standard Converters.
  96.      * </p>
  97.      *
  98.      * <p>
  99.      * For more details see {@code ConvertUtilsBean}.
  100.      * </p>
  101.      *
  102.      * @see ConvertUtilsBean#deregister()
  103.      */
  104.     public static void deregister() {
  105.         ConvertUtilsBean.getInstance().deregister();
  106.     }

  107.     /**
  108.      * <p>
  109.      * Remove any registered {@link Converter} for the specified destination {@code Class}.
  110.      * </p>
  111.      *
  112.      * <p>
  113.      * For more details see {@code ConvertUtilsBean}.
  114.      * </p>
  115.      *
  116.      * @param clazz Class for which to remove a registered Converter
  117.      * @see ConvertUtilsBean#deregister(Class)
  118.      */
  119.     public static void deregister(final Class<?> clazz) {
  120.         ConvertUtilsBean.getInstance().deregister(clazz);
  121.     }

  122.     /**
  123.      * Look up and return any registered {@link Converter} for the specified source and destination class; if there is no registered Converter, return
  124.      * {@code null}.
  125.      *
  126.      * @param <T>        The converter type.
  127.      * @param sourceType Class of the value being converted
  128.      * @param targetType Class of the value to be converted to
  129.      * @return The registered {@link Converter} or {@code null} if not found
  130.      */
  131.     public static <T> Converter<T> lookup(final Class<?> sourceType, final Class<T> targetType) {
  132.         return ConvertUtilsBean.getInstance().lookup(sourceType, targetType);
  133.     }

  134.     /**
  135.      * <p>
  136.      * Look up and return any registered {@link Converter} for the specified destination class; if there is no registered Converter, return {@code null}.
  137.      * </p>
  138.      *
  139.      * <p>
  140.      * For more details see {@code ConvertUtilsBean}.
  141.      * </p>
  142.      *
  143.      * @param <T>   The converter type.
  144.      * @param clazz Class for which to return a registered Converter
  145.      * @return The registered {@link Converter} or {@code null} if not found
  146.      * @see ConvertUtilsBean#lookup(Class)
  147.      */
  148.     public static <T> Converter<T> lookup(final Class<T> clazz) {
  149.         return ConvertUtilsBean.getInstance().lookup(clazz);
  150.     }

  151.     /**
  152.      * Change primitive Class types to the associated wrapper class. This is useful for concrete converter implementations which typically treat primitive types
  153.      * like their corresponding wrapper types.
  154.      *
  155.      * @param <T>  The type to be checked.
  156.      * @param type The class type to check.
  157.      * @return The converted type.
  158.      * @since 1.9
  159.      */
  160.     // All type casts are safe because the TYPE members of the wrapper types
  161.     // return their own class.
  162.     @SuppressWarnings("unchecked")
  163.     public static <T> Class<T> primitiveToWrapper(final Class<T> type) {
  164.         if (type == null || !type.isPrimitive()) {
  165.             return type;
  166.         }

  167.         if (type == Integer.TYPE) {
  168.             return (Class<T>) Integer.class;
  169.         }
  170.         if (type == Double.TYPE) {
  171.             return (Class<T>) Double.class;
  172.         }
  173.         if (type == Long.TYPE) {
  174.             return (Class<T>) Long.class;
  175.         }
  176.         if (type == Boolean.TYPE) {
  177.             return (Class<T>) Boolean.class;
  178.         }
  179.         if (type == Float.TYPE) {
  180.             return (Class<T>) Float.class;
  181.         }
  182.         if (type == Short.TYPE) {
  183.             return (Class<T>) Short.class;
  184.         }
  185.         if (type == Byte.TYPE) {
  186.             return (Class<T>) Byte.class;
  187.         }
  188.         if (type == Character.TYPE) {
  189.             return (Class<T>) Character.class;
  190.         }
  191.         return type;
  192.     }

  193.     /**
  194.      * <p>
  195.      * Register a custom {@link Converter} for the specified destination {@code Class}, replacing any previously registered Converter.
  196.      * </p>
  197.      *
  198.      * <p>
  199.      * For more details see {@code ConvertUtilsBean}.
  200.      * </p>
  201.      *
  202.      * @param <T>       The converter type.
  203.      * @param converter Converter to be registered
  204.      * @param clazz     Destination class for conversions performed by this Converter
  205.      * @see ConvertUtilsBean#register(Converter, Class)
  206.      */
  207.     public static <T> void register(final Converter<T> converter, final Class<T> clazz) {
  208.         ConvertUtilsBean.getInstance().register(converter, clazz);
  209.     }

  210.     private ConvertUtils() {
  211.         // empty
  212.     }
  213. }