2 * Copyright 2000-2018 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.v7.data.util.converter;
18 import java.math.BigDecimal;
19 import java.math.BigInteger;
20 import java.text.DecimalFormat;
21 import java.text.NumberFormat;
22 import java.util.Locale;
25 * A converter that converts from {@link String} to {@link BigInteger} and back.
26 * Uses the given locale and a {@link NumberFormat} instance for formatting and
29 * Leading and trailing white spaces are ignored when converting from a String.
32 * Override and overwrite {@link #getFormat(Locale)} to use a different format.
39 public class StringToBigIntegerConverter
40 extends AbstractStringToNumberConverter<BigInteger> {
43 protected NumberFormat getFormat(Locale locale) {
44 NumberFormat numberFormat = super.getFormat(locale);
45 if (numberFormat instanceof DecimalFormat) {
46 ((DecimalFormat) numberFormat).setParseBigDecimal(true);
53 public BigInteger convertToModel(String value,
54 Class<? extends BigInteger> targetType, Locale locale)
55 throws ConversionException {
57 BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value,
58 BigDecimal.class, locale);
60 return (bigDecimalValue != null) ? bigDecimalValue.toBigInteger()
65 public Class<BigInteger> getModelType() {
66 return BigInteger.class;