View Javadoc
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    *      https://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  
18  package org.apache.commons.beanutils2.locale.converters;
19  
20  import java.math.BigInteger;
21  import java.text.ParseException;
22  import java.util.Locale;
23  
24  import org.apache.commons.beanutils2.ConversionException;
25  
26  /**
27   * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a
28   * {@link java.math.BigInteger} object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion
29   * error occurs.
30   */
31  public class BigIntegerLocaleConverter extends DecimalLocaleConverter<BigInteger> {
32  
33      /**
34       * Builds instances of {@link BigIntegerLocaleConverter}.
35       */
36      public static class Builder extends DecimalLocaleConverter.Builder<Builder, BigInteger> {
37  
38          /**
39           * Constructs a new instance.
40           */
41          public Builder() {
42              // empty
43          }
44  
45          @Override
46          public BigIntegerLocaleConverter get() {
47              return new BigIntegerLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern);
48          }
49  
50      }
51  
52      /**
53       * Constructs a new builder.
54       *
55       * @return a new builder.
56       */
57      public static Builder builder() {
58          return new Builder();
59      }
60  
61      private BigIntegerLocaleConverter(final BigInteger defaultValue, final Locale locale, final String pattern, final boolean useDefault,
62              final boolean locPattern) {
63          super(defaultValue, locale, pattern, useDefault, locPattern);
64      }
65  
66      /**
67       * Parses the specified locale-sensitive input object into an output object of BigInteger type.
68       *
69       * @param value   The input object to be converted
70       * @param pattern The pattern is used for the conversion
71       * @return The converted value
72       * @throws ConversionException if conversion cannot be performed successfully
73       * @throws ParseException      if an error occurs parsing a String to a Number
74       * @since 1.8.0
75       */
76      @Override
77      protected BigInteger parse(final Object value, final String pattern) throws ParseException {
78          final Number result = super.parse(value, pattern);
79  
80          if (result == null || result instanceof BigInteger) {
81              return (BigInteger) result;
82          }
83  
84          return BigInteger.valueOf(result.longValue());
85      }
86  
87  }