StringLocaleConverter.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.converters;

  18. import java.math.BigDecimal;
  19. import java.math.BigInteger;
  20. import java.text.DecimalFormat;
  21. import java.text.NumberFormat;
  22. import java.text.ParseException;
  23. import java.text.SimpleDateFormat;
  24. import java.util.Date;
  25. import java.util.Locale;

  26. import org.apache.commons.beanutils2.ConversionException;
  27. import org.apache.commons.beanutils2.locale.BaseLocaleConverter;
  28. import org.apache.commons.beanutils2.locale.LocaleConverter;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;

  31. /**
  32.  * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive object into a {@link String}
  33.  * object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion error occurs.
  34.  */
  35. public class StringLocaleConverter extends BaseLocaleConverter<String> {

  36.     /**
  37.      * Builds instances of {@link StringLocaleConverter}.
  38.      */
  39.     public static class Builder extends BaseLocaleConverter.Builder<Builder, String> {

  40.         /**
  41.          * Constructs a new instance.
  42.          */
  43.         public Builder() {
  44.             // empty
  45.         }

  46.         /**
  47.          * Gets a new instance.
  48.          * <p>
  49.          * Defaults construct a {@link LocaleConverter} that will throw a {@link ConversionException} if a conversion error occurs. The locale is the default
  50.          * locale for this instance of the Java Virtual Machine and an unlocalized pattern is used for the conversion.
  51.          * </p>
  52.          *
  53.          * @return a new instance.
  54.          */
  55.         @Override
  56.         public StringLocaleConverter get() {
  57.             return new StringLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern);
  58.         }

  59.     }

  60.     /** All logging goes through this logger */
  61.     private static final Log LOG = LogFactory.getLog(StringLocaleConverter.class);

  62.     /**
  63.      * Constructs a new builder.
  64.      *
  65.      * @return a new builder.
  66.      */
  67.     public static Builder builder() {
  68.         return new Builder();
  69.     }

  70.     private StringLocaleConverter(final String defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern) {
  71.         super(defaultValue, locale, pattern, useDefault, locPattern);
  72.     }

  73.     /**
  74.      * Gets an instance of DecimalFormat.
  75.      *
  76.      * @param locale  The locale
  77.      * @param pattern The pattern is used for the conversion
  78.      * @return The format for the locale and pattern
  79.      * @throws ConversionException      if conversion cannot be performed successfully
  80.      * @throws IllegalArgumentException if an error occurs parsing a String to a Number
  81.      */
  82.     private DecimalFormat getDecimalFormat(final Locale locale, final String pattern) {
  83.         final DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale);

  84.         // if some constructors default pattern to null, it makes only sense to handle null pattern gracefully
  85.         if (pattern != null) {
  86.             if (localizedPattern) {
  87.                 numberFormat.applyLocalizedPattern(pattern);
  88.             } else {
  89.                 numberFormat.applyPattern(pattern);
  90.             }
  91.         } else {
  92.             LOG.debug("No pattern provided, using default.");
  93.         }

  94.         return numberFormat;
  95.     }

  96.     /**
  97.      * Parses the specified locale-sensitive input object into an output object of the specified type.
  98.      *
  99.      * @param value   The input object to be converted
  100.      * @param pattern The pattern is used for the conversion
  101.      * @return The converted value
  102.      * @throws ConversionException if conversion cannot be performed successfully
  103.      * @throws ParseException      if an error occurs
  104.      */
  105.     @Override
  106.     protected String parse(final Object value, final String pattern) throws ParseException {
  107.         String result = null;

  108.         if (value instanceof Integer || value instanceof Long || value instanceof BigInteger || value instanceof Byte || value instanceof Short) {
  109.             result = getDecimalFormat(locale, pattern).format(((Number) value).longValue());
  110.         } else if (value instanceof Double || value instanceof BigDecimal || value instanceof Float) {
  111.             result = getDecimalFormat(locale, pattern).format(((Number) value).doubleValue());
  112.         } else if (value instanceof Date) { // java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp
  113.             result = new SimpleDateFormat(pattern, locale).format(value);
  114.         } else {
  115.             result = value.toString();
  116.         }

  117.         return result;
  118.     }
  119. }