    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.beanutils2.locale.converters;
    019
    020import java.text.ParseException;
    021import java.util.Locale;
    022
    023import org.apache.commons.beanutils2.ConversionException;
    024
    025/**
    026 * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a
    027 * {@link java.math.BigDecimal} object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion
    028 * error occurs.
    029 */
    030public class FloatLocaleConverter extends DecimalLocaleConverter<Float> {
    031
    032    /**
    033     * Builds instances of {@link ByteLocaleConverter}.
    034     */
    035    public static class Builder extends DecimalLocaleConverter.Builder<Builder, Float> {
    036
    037        /**
    038         * Constructs a new instance.
    039         */
    040        public Builder() {
    041            // empty
    042        }
    043
    044        @Override
    045        public FloatLocaleConverter get() {
    046            return new FloatLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern);
    047        }
    048
    049    }
    050
    051    /**
    052     * Constructs a new builder.
    053     *
    054     * @return a new builder.
    055     */
    056    public static Builder builder() {
    057        return new Builder();
    058    }
    059
    060    private FloatLocaleConverter(final Float defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern) {
    061        super(defaultValue, locale, pattern, useDefault, locPattern);
    062    }
    063
    064    /**
    065     * 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
    066     * value cannot be stored in the Float.
    067     *
    068     * @param value   The input object to be converted
    069     * @param pattern The pattern is used for the conversion
    070     * @return The converted value
    071     * @throws ConversionException if conversion cannot be performed successfully
    072     * @throws ParseException      if an error occurs parsing a String to a Number
    073     */
    074    @Override
    075    protected Float parse(final Object value, final String pattern) throws ParseException {
    076        final Number parsed = super.parse(value, pattern);
    077        final double doubleValue = parsed.doubleValue();
    078        final double posDouble = doubleValue >= 0 ? doubleValue : doubleValue * -1;
    079        if (posDouble != 0 && (posDouble < Float.MIN_VALUE || posDouble > Float.MAX_VALUE)) {
    080            throw new ConversionException("Supplied number is not of type Float: " + parsed);
    081        }
    082        return Float.valueOf(parsed.floatValue()); // unlike superclass it returns Float type
    083    }
    084}