BigDecimalLocaleConverter.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.text.ParseException;
  20. import java.util.Locale;

  21. import org.apache.commons.beanutils2.ConversionException;

  22. /**
  23.  * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a
  24.  * {@link java.math.BigDecimal} object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion
  25.  * error occurs.
  26.  */
  27. public class BigDecimalLocaleConverter extends DecimalLocaleConverter<BigDecimal> {

  28.     /**
  29.      * Builds instances of {@link BigDecimalLocaleConverter}.
  30.      */
  31.     public static class Builder extends DecimalLocaleConverter.Builder<Builder, BigDecimal> {

  32.         /**
  33.          * Constructs a new instance.
  34.          */
  35.         public Builder() {
  36.             // empty
  37.         }

  38.         @Override
  39.         public BigDecimalLocaleConverter get() {
  40.             return new BigDecimalLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern);
  41.         }

  42.     }

  43.     /**
  44.      * Constructs a new builder.
  45.      *
  46.      * @return a new builder.
  47.      */
  48.     public static Builder builder() {
  49.         return new Builder();
  50.     }

  51.     private BigDecimalLocaleConverter(final BigDecimal defaultValue, final Locale locale, final String pattern, final boolean useDefault,
  52.             final boolean locPattern) {
  53.         super(defaultValue, locale, pattern, useDefault, locPattern);
  54.     }

  55.     /**
  56.      * Parses the specified locale-sensitive input object into an output object of BigDecimal type.
  57.      *
  58.      * @param value   The input object to be converted
  59.      * @param pattern The pattern is used for the conversion
  60.      * @return The converted value
  61.      * @throws ConversionException if conversion cannot be performed successfully
  62.      * @throws ParseException      if an error occurs parsing a String to a Number
  63.      * @since 1.8.0
  64.      */
  65.     @Override
  66.     protected BigDecimal parse(final Object value, final String pattern) throws ParseException {
  67.         final Number result = super.parse(value, pattern);

  68.         if (result == null || result instanceof BigDecimal) {
  69.             return (BigDecimal) result;
  70.         }

  71.         try {
  72.             return new BigDecimal(result.toString());
  73.         } catch (final NumberFormatException ex) {
  74.             throw new ConversionException("Supplied number is not of type BigDecimal: " + result);
  75.         }
  76.     }

  77. }