FloatLocaleConverter.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.ParseException;
  19. import java.util.Locale;

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

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

  27.     /**
  28.      * Builds instances of {@link ByteLocaleConverter}.
  29.      */
  30.     public static class Builder extends DecimalLocaleConverter.Builder<Builder, Float> {

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

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

  41.     }

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

  50.     private FloatLocaleConverter(final Float defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern) {
  51.         super(defaultValue, locale, pattern, useDefault, locPattern);
  52.     }

  53.     /**
  54.      * Parses the specified locale-sensitive input object into an output object of the specified type. This method will return Float value or throw exception if
  55.      * value cannot be stored in the Float.
  56.      *
  57.      * @param value   The input object to be converted
  58.      * @param pattern The pattern is used for the conversion
  59.      * @return The converted value
  60.      * @throws ConversionException if conversion cannot be performed successfully
  61.      * @throws ParseException      if an error occurs parsing a String to a Number
  62.      */
  63.     @Override
  64.     protected Float parse(final Object value, final String pattern) throws ParseException {
  65.         final Number parsed = super.parse(value, pattern);
  66.         final double doubleValue = parsed.doubleValue();
  67.         final double posDouble = doubleValue >= 0 ? doubleValue : doubleValue * -1;
  68.         if (posDouble != 0 && (posDouble < Float.MIN_VALUE || posDouble > Float.MAX_VALUE)) {
  69.             throw new ConversionException("Supplied number is not of type Float: " + parsed);
  70.         }
  71.         return Float.valueOf(parsed.floatValue()); // unlike superclass it returns Float type
  72.     }
  73. }