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 *      http://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.math.BigInteger;
021import java.text.ParseException;
022import java.util.Locale;
023
024import org.apache.commons.beanutils2.ConversionException;
025
026/**
027 * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a
028 * {@link java.math.BigInteger} object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion
029 * error occurs.
030 */
031public class BigIntegerLocaleConverter extends DecimalLocaleConverter<BigInteger> {
032
033    /**
034     * Builds instances of {@link BigIntegerLocaleConverter}.
035     */
036    public static class Builder extends DecimalLocaleConverter.Builder<Builder, BigInteger> {
037
038        /**
039         * Constructs a new instance.
040         */
041        public Builder() {
042            // empty
043        }
044
045        @Override
046        public BigIntegerLocaleConverter get() {
047            return new BigIntegerLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern);
048        }
049
050    }
051
052    /**
053     * Constructs a new builder.
054     *
055     * @return a new builder.
056     */
057    public static Builder builder() {
058        return new Builder();
059    }
060
061    private BigIntegerLocaleConverter(final BigInteger defaultValue, final Locale locale, final String pattern, final boolean useDefault,
062            final boolean locPattern) {
063        super(defaultValue, locale, pattern, useDefault, locPattern);
064    }
065
066    /**
067     * Parses the specified locale-sensitive input object into an output object of BigInteger type.
068     *
069     * @param value   The input object to be converted
070     * @param pattern The pattern is used for the conversion
071     * @return The converted value
072     * @throws ConversionException if conversion cannot be performed successfully
073     * @throws ParseException      if an error occurs parsing a String to a Number
074     * @since 1.8.0
075     */
076    @Override
077    protected BigInteger parse(final Object value, final String pattern) throws ParseException {
078        final Number result = super.parse(value, pattern);
079
080        if (result == null || result instanceof BigInteger) {
081            return (BigInteger) result;
082        }
083
084        return BigInteger.valueOf(result.longValue());
085    }
086
087}