LocaleConvertUtils.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.locale;

  18. import java.util.Locale;

  19. /**
  20.  * <p>
  21.  * Utility methods for converting locale-sensitive String scalar values to objects of the specified Class, String arrays to arrays of the specified Class and
  22.  * object to locale-sensitive String scalar value.
  23.  * </p>
  24.  *
  25.  * <p>
  26.  * The implementations for these method are provided by {@link LocaleConvertUtilsBean}. These static utility method use the default instance. More sophisticated
  27.  * can be provided by using a {@code LocaleConvertUtilsBean} instance.
  28.  * </p>
  29.  */
  30. public final class LocaleConvertUtils {

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

  47.     /**
  48.      * <p>
  49.      * Converts the specified locale-sensitive value into a String using the particular conversion pattern.
  50.      * </p>
  51.      *
  52.      * <p>
  53.      * For more details see {@code LocaleConvertUtilsBean}
  54.      * </p>
  55.      *
  56.      * @param value   The Value to be converted
  57.      * @param locale  The locale
  58.      * @param pattern The conversion pattern
  59.      * @return the converted value
  60.      * @see LocaleConvertUtilsBean#convert(Object, Locale, String)
  61.      */
  62.     public static String convert(final Object value, final Locale locale, final String pattern) {
  63.         return LocaleConvertUtilsBean.getInstance().convert(value, locale, pattern);
  64.     }

  65.     /**
  66.      * <p>
  67.      * Converts the specified locale-sensitive value into a String using the conversion pattern.
  68.      * </p>
  69.      *
  70.      * <p>
  71.      * For more details see {@code LocaleConvertUtilsBean}
  72.      * </p>
  73.      *
  74.      * @param value   The Value to be converted
  75.      * @param pattern The conversion pattern
  76.      * @return the converted value
  77.      * @see LocaleConvertUtilsBean#convert(Object, String)
  78.      */
  79.     public static String convert(final Object value, final String pattern) {
  80.         return LocaleConvertUtilsBean.getInstance().convert(value, pattern);
  81.     }

  82.     /**
  83.      * <p>
  84.      * Converts the specified value to an object of the specified class (if possible). Otherwise, return a String representation of the value.
  85.      * </p>
  86.      *
  87.      * <p>
  88.      * For more details see {@code LocaleConvertUtilsBean}
  89.      * </p>
  90.      *
  91.      * @param value The String scalar value to be converted
  92.      * @param clazz The Data type to which this value should be converted.
  93.      * @return the converted value
  94.      * @see LocaleConvertUtilsBean#convert(String, Class)
  95.      */
  96.     public static Object convert(final String value, final Class<?> clazz) {
  97.         return LocaleConvertUtilsBean.getInstance().convert(value, clazz);
  98.     }

  99.     /**
  100.      * <p>
  101.      * Converts the specified value to an object of the specified class (if possible) using the conversion pattern. Otherwise, return a String representation of
  102.      * the value.
  103.      * </p>
  104.      *
  105.      * <p>
  106.      * For more details see {@code LocaleConvertUtilsBean}
  107.      * </p>
  108.      *
  109.      * @param value   The String scalar value to be converted
  110.      * @param clazz   The Data type to which this value should be converted.
  111.      * @param locale  The locale
  112.      * @param pattern The conversion pattern
  113.      * @return the converted value
  114.      * @see LocaleConvertUtilsBean#convert(String, Class, Locale, String)
  115.      */
  116.     public static Object convert(final String value, final Class<?> clazz, final Locale locale, final String pattern) {
  117.         return LocaleConvertUtilsBean.getInstance().convert(value, clazz, locale, pattern);
  118.     }

  119.     /**
  120.      * <p>
  121.      * Converts the specified value to an object of the specified class (if possible) using the conversion pattern. Otherwise, return a String representation of
  122.      * the value.
  123.      * </p>
  124.      *
  125.      * <p>
  126.      * For more details see {@code LocaleConvertUtilsBean}
  127.      * </p>
  128.      *
  129.      * @param value   The String scalar value to be converted
  130.      * @param clazz   The Data type to which this value should be converted.
  131.      * @param pattern The conversion pattern
  132.      * @return the converted value
  133.      * @see LocaleConvertUtilsBean#convert(String, Class, String)
  134.      */
  135.     public static Object convert(final String value, final Class<?> clazz, final String pattern) {
  136.         return LocaleConvertUtilsBean.getInstance().convert(value, clazz, pattern);
  137.     }

  138.     /**
  139.      * <p>
  140.      * Convert an array of specified values to an array of objects of the specified class (if possible).
  141.      * </p>
  142.      *
  143.      * <p>
  144.      * For more details see {@code LocaleConvertUtilsBean}
  145.      * </p>
  146.      *
  147.      * @param values Value to be converted (may be null)
  148.      * @param clazz  Java array or element class to be converted to
  149.      * @return the converted value
  150.      * @see LocaleConvertUtilsBean#convert(String[], Class)
  151.      */
  152.     public static Object convert(final String[] values, final Class<?> clazz) {
  153.         return LocaleConvertUtilsBean.getInstance().convert(values, clazz);
  154.     }

  155.     /**
  156.      * <p>
  157.      * Convert an array of specified values to an array of objects of the specified class (if possible) using the conversion pattern.
  158.      * </p>
  159.      *
  160.      * <p>
  161.      * For more details see {@code LocaleConvertUtilsBean}
  162.      * </p>
  163.      *
  164.      * @param values  Value to be converted (may be null)
  165.      * @param clazz   Java array or element class to be converted to
  166.      * @param locale  The locale
  167.      * @param pattern The conversion pattern
  168.      * @return the converted value
  169.      * @see LocaleConvertUtilsBean#convert(String[], Class, Locale, String)
  170.      */
  171.     public static Object convert(final String[] values, final Class<?> clazz, final Locale locale, final String pattern) {
  172.         return LocaleConvertUtilsBean.getInstance().convert(values, clazz, locale, pattern);
  173.     }

  174.     /**
  175.      * <p>
  176.      * Convert an array of specified values to an array of objects of the specified class (if possible) using the conversion pattern.
  177.      * </p>
  178.      *
  179.      * <p>
  180.      * For more details see {@code LocaleConvertUtilsBean}
  181.      * </p>
  182.      *
  183.      * @param values  Value to be converted (may be null)
  184.      * @param clazz   Java array or element class to be converted to
  185.      * @param pattern The conversion pattern
  186.      * @return the converted value
  187.      * @see LocaleConvertUtilsBean#convert(String[], Class, String)
  188.      */
  189.     public static Object convert(final String[] values, final Class<?> clazz, final String pattern) {
  190.         return LocaleConvertUtilsBean.getInstance().convert(values, clazz, pattern);
  191.     }

  192.     /**
  193.      * <p>
  194.      * Remove any registered {@link LocaleConverter}.
  195.      * </p>
  196.      *
  197.      * <p>
  198.      * For more details see {@code LocaleConvertUtilsBean}
  199.      * </p>
  200.      *
  201.      * @see LocaleConvertUtilsBean#deregister()
  202.      */
  203.     public static void deregister() {
  204.         LocaleConvertUtilsBean.getInstance().deregister();
  205.     }

  206.     /**
  207.      * <p>
  208.      * Remove any registered {@link LocaleConverter} for the specified locale and Class.
  209.      * </p>
  210.      *
  211.      * <p>
  212.      * For more details see {@code LocaleConvertUtilsBean}
  213.      * </p>
  214.      *
  215.      * @param clazz  Class for which to remove a registered Converter
  216.      * @param locale The locale
  217.      * @see LocaleConvertUtilsBean#deregister(Class, Locale)
  218.      */
  219.     public static void deregister(final Class<?> clazz, final Locale locale) {
  220.         LocaleConvertUtilsBean.getInstance().deregister(clazz, locale);
  221.     }

  222.     /**
  223.      * <p>
  224.      * Remove any registered {@link LocaleConverter} for the specified locale.
  225.      * </p>
  226.      *
  227.      * <p>
  228.      * For more details see {@code LocaleConvertUtilsBean}
  229.      * </p>
  230.      *
  231.      * @param locale The locale
  232.      * @see LocaleConvertUtilsBean#deregister(Locale)
  233.      */
  234.     public static void deregister(final Locale locale) {
  235.         LocaleConvertUtilsBean.getInstance().deregister(locale);
  236.     }

  237.     /**
  238.      * <p>
  239.      * Gets applyLocalized.
  240.      * </p>
  241.      *
  242.      * <p>
  243.      * For more details see {@code LocaleConvertUtilsBean}
  244.      * </p>
  245.      *
  246.      * @return {@code true} if pattern is localized, otherwise {@code false}
  247.      * @see LocaleConvertUtilsBean#getApplyLocalized()
  248.      */
  249.     public static boolean getApplyLocalized() {
  250.         return LocaleConvertUtilsBean.getInstance().getApplyLocalized();
  251.     }

  252.     /**
  253.      * <p>
  254.      * Gets the {@link Locale} which will be used when no {@link Locale} is passed to a method.
  255.      * </p>
  256.      *
  257.      * <p>
  258.      * For more details see {@code LocaleConvertUtilsBean}
  259.      * </p>
  260.      *
  261.      * @return the default locale
  262.      * @see LocaleConvertUtilsBean#getDefaultLocale()
  263.      */
  264.     public static Locale getDefaultLocale() {
  265.         return LocaleConvertUtilsBean.getInstance().getDefaultLocale();
  266.     }

  267.     /**
  268.      * <p>
  269.      * Look up and return any registered {@link LocaleConverter} for the specified destination class and locale; if there is no registered Converter, return
  270.      * {@code null}.
  271.      * </p>
  272.      *
  273.      * <p>
  274.      * For more details see {@code LocaleConvertUtilsBean}
  275.      * </p>
  276.      *
  277.      * @param <T>    The converter type.
  278.      * @param clazz  Class for which to return a registered Converter
  279.      * @param locale The Locale
  280.      * @return The registered locale Converter, if any
  281.      * @see LocaleConvertUtilsBean#lookup(Class, Locale)
  282.      */
  283.     public static <T> LocaleConverter<T> lookup(final Class<T> clazz, final Locale locale) {
  284.         return LocaleConvertUtilsBean.getInstance().lookup(clazz, locale);
  285.     }

  286.     /**
  287.      * <p>
  288.      * Register a custom {@link LocaleConverter} for the specified destination {@code Class}, replacing any previously registered converter.
  289.      * </p>
  290.      *
  291.      * <p>
  292.      * For more details see {@code LocaleConvertUtilsBean}
  293.      * </p>
  294.      *
  295.      * @param <T>       The converter type.
  296.      * @param converter The LocaleConverter to be registered
  297.      * @param clazz     The Destination class for conversions performed by this Converter
  298.      * @param locale    The locale
  299.      * @see LocaleConvertUtilsBean#register(LocaleConverter, Class, Locale)
  300.      */
  301.     public static <T> void register(final LocaleConverter<T> converter, final Class<T> clazz, final Locale locale) {
  302.         LocaleConvertUtilsBean.getInstance().register(converter, clazz, locale);
  303.     }

  304.     /**
  305.      * <p>
  306.      * Sets applyLocalized.
  307.      * </p>
  308.      *
  309.      * <p>
  310.      * For more details see {@code LocaleConvertUtilsBean}
  311.      * </p>
  312.      *
  313.      * @param newApplyLocalized {@code true} if pattern is localized, otherwise {@code false}
  314.      * @see LocaleConvertUtilsBean#setApplyLocalized(boolean)
  315.      */
  316.     public static void setApplyLocalized(final boolean newApplyLocalized) {
  317.         LocaleConvertUtilsBean.getInstance().setApplyLocalized(newApplyLocalized);
  318.     }

  319.     /**
  320.      * <p>
  321.      * Sets the {@link Locale} which will be used when no {@link Locale} is passed to a method.
  322.      * </p>
  323.      *
  324.      * <p>
  325.      * For more details see {@code LocaleConvertUtilsBean}
  326.      * </p>
  327.      *
  328.      * @param locale the default locale
  329.      * @see LocaleConvertUtilsBean#setDefaultLocale(Locale)
  330.      */
  331.     public static void setDefaultLocale(final Locale locale) {
  332.         LocaleConvertUtilsBean.getInstance().setDefaultLocale(locale);
  333.     }

  334.     private LocaleConvertUtils() {
  335.         // empty
  336.     }
  337. }