DecimalLocaleConverter.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.text.DecimalFormat;
  19. import java.text.NumberFormat;
  20. import java.text.ParseException;
  21. import java.util.Locale;

  22. import org.apache.commons.beanutils2.ConversionException;
  23. import org.apache.commons.beanutils2.locale.BaseLocaleConverter;
  24. import org.apache.commons.beanutils2.locale.LocaleConverter;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;

  27. /**
  28.  * Standard {@link LocaleConverter} implementation that converts an incoming locale-sensitive String into a {@link Number} object, optionally using a default
  29.  * value or throwing a {@link ConversionException} if a conversion error occurs.
  30.  *
  31.  * @param <T> The converter type.
  32.  * @since 1.7
  33.  */
  34. public class DecimalLocaleConverter<T extends Number> extends BaseLocaleConverter<T> {

  35.     /**
  36.      * Builds instances of {@link DateLocaleConverter}.
  37.      *
  38.      * @param <B> The builder type.
  39.      * @param <T> The Number type.
  40.      */
  41.     public static class Builder<B extends Builder<B, T>, T extends Number> extends BaseLocaleConverter.Builder<B, T> {

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

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

  61.     }

  62.     /** All logging goes through this logger */
  63.     private static final Log LOG = LogFactory.getLog(DecimalLocaleConverter.class);

  64.     /**
  65.      * Constructs a new builder.
  66.      *
  67.      * @param <B> The builder type.
  68.      * @param <T> The Number type.
  69.      * @return a new builder.
  70.      */
  71.     @SuppressWarnings("unchecked")
  72.     public static <B extends Builder<B, T>, T extends Number> B builder() {
  73.         return (B) new Builder<>();
  74.     }

  75.     /**
  76.      * Constructs a new instance.
  77.      *
  78.      * @param defaultValue default value.
  79.      * @param locale       locale.
  80.      * @param pattern      pattern.
  81.      * @param useDefault   use the default.
  82.      * @param locPattern   localized pattern.
  83.      */
  84.     protected DecimalLocaleConverter(final T defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern) {
  85.         super(defaultValue, locale, pattern, useDefault, locPattern);
  86.     }

  87.     /**
  88.      * Converts the specified locale-sensitive input object into an output object of the specified type.
  89.      *
  90.      * @param value   The input object to be converted
  91.      * @param pattern The pattern is used for the conversion
  92.      * @return The converted value
  93.      * @throws ConversionException if conversion cannot be performed successfully
  94.      * @throws ParseException      if an error occurs parsing a String to a Number
  95.      */
  96.     @Override
  97.     protected T parse(final Object value, final String pattern) throws ParseException {
  98.         if (value instanceof Number) {
  99.             return (T) value;
  100.         }

  101.         // Note that despite the ambiguous "getInstance" name, and despite the
  102.         // fact that objects returned from this method have the same toString
  103.         // representation, each call to getInstance actually returns a new
  104.         // object.
  105.         final DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(locale);

  106.         // if some constructors default pattern to null, it makes only sense
  107.         // to handle null pattern gracefully
  108.         if (pattern != null) {
  109.             if (localizedPattern) {
  110.                 formatter.applyLocalizedPattern(pattern);
  111.             } else {
  112.                 formatter.applyPattern(pattern);
  113.             }
  114.         } else {
  115.             LOG.debug("No pattern provided, using default.");
  116.         }

  117.         return (T) formatter.parse((String) value);
  118.     }
  119. }