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.beanutils.locale.converters; 019 020import java.math.BigInteger; 021import java.text.ParseException; 022import java.util.Locale; 023 024import org.apache.commons.beanutils.ConversionException; 025 026/** 027 * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter} 028 * implementation that converts an incoming 029 * locale-sensitive String into a <code>java.math.BigInteger</code> object, 030 * optionally using a default value or throwing a 031 * {@link org.apache.commons.beanutils.ConversionException} 032 * if a conversion error occurs.</p> 033 * 034 */ 035 036public class BigIntegerLocaleConverter extends DecimalLocaleConverter { 037 038 /** 039 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 040 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 041 * if a conversion error occurs. The locale is the default locale for 042 * this instance of the Java Virtual Machine and an unlocalized pattern is used 043 * for the convertion. 044 * 045 */ 046 public BigIntegerLocaleConverter() { 047 048 this(false); 049 } 050 051 /** 052 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 053 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 054 * if a conversion error occurs. The locale is the default locale for 055 * this instance of the Java Virtual Machine. 056 * 057 * @param locPattern Indicate whether the pattern is localized or not 058 */ 059 public BigIntegerLocaleConverter(final boolean locPattern) { 060 061 this(Locale.getDefault(), locPattern); 062 } 063 064 /** 065 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 066 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 067 * if a conversion error occurs. An unlocalized pattern is used for the convertion. 068 * 069 * @param locale The locale 070 */ 071 public BigIntegerLocaleConverter(final Locale locale) { 072 073 this(locale, false); 074 } 075 076 /** 077 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 078 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 079 * if a conversion error occurs. 080 * 081 * @param locale The locale 082 * @param locPattern Indicate whether the pattern is localized or not 083 */ 084 public BigIntegerLocaleConverter(final Locale locale, final boolean locPattern) { 085 086 this(locale, (String) null, locPattern); 087 } 088 089 /** 090 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 091 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 092 * if a conversion error occurs. An unlocalized pattern is used for the convertion. 093 * 094 * @param locale The locale 095 * @param pattern The convertion pattern 096 */ 097 public BigIntegerLocaleConverter(final Locale locale, final String pattern) { 098 099 this(locale, pattern, false); 100 } 101 102 /** 103 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 104 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 105 * if a conversion error occurs. 106 * 107 * @param locale The locale 108 * @param pattern The convertion pattern 109 * @param locPattern Indicate whether the pattern is localized or not 110 */ 111 public BigIntegerLocaleConverter(final Locale locale, final String pattern, final boolean locPattern) { 112 113 super(locale, pattern, locPattern); 114 } 115 116 /** 117 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 118 * that will return the specified default value 119 * if a conversion error occurs. The locale is the default locale for 120 * this instance of the Java Virtual Machine and an unlocalized pattern is used 121 * for the convertion. 122 * 123 * @param defaultValue The default value to be returned 124 */ 125 public BigIntegerLocaleConverter(final Object defaultValue) { 126 127 this(defaultValue, false); 128 } 129 130 /** 131 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 132 * that will return the specified default value 133 * if a conversion error occurs. The locale is the default locale for 134 * this instance of the Java Virtual Machine. 135 * 136 * @param defaultValue The default value to be returned 137 * @param locPattern Indicate whether the pattern is localized or not 138 */ 139 public BigIntegerLocaleConverter(final Object defaultValue, final boolean locPattern) { 140 141 this(defaultValue, Locale.getDefault(), locPattern); 142 } 143 144 /** 145 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 146 * that will return the specified default value 147 * if a conversion error occurs. An unlocalized pattern is used for the convertion. 148 * 149 * @param defaultValue The default value to be returned 150 * @param locale The locale 151 */ 152 public BigIntegerLocaleConverter(final Object defaultValue, final Locale locale) { 153 154 this(defaultValue, locale, false); 155 } 156 157 /** 158 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 159 * that will return the specified default value 160 * if a conversion error occurs. 161 * 162 * @param defaultValue The default value to be returned 163 * @param locale The locale 164 * @param locPattern Indicate whether the pattern is localized or not 165 */ 166 public BigIntegerLocaleConverter(final Object defaultValue, final Locale locale, final boolean locPattern) { 167 168 this(defaultValue, locale, null, locPattern); 169 } 170 171 /** 172 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 173 * that will return the specified default value 174 * if a conversion error occurs. An unlocalized pattern is used for the convertion. 175 * 176 * @param defaultValue The default value to be returned 177 * @param locale The locale 178 * @param pattern The convertion pattern 179 */ 180 public BigIntegerLocaleConverter(final Object defaultValue, final Locale locale, final String pattern) { 181 182 this(defaultValue, locale, pattern, false); 183 } 184 185 /** 186 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 187 * that will return the specified default value 188 * if a conversion error occurs. 189 * 190 * @param defaultValue The default value to be returned 191 * @param locale The locale 192 * @param pattern The convertion pattern 193 * @param locPattern Indicate whether the pattern is localized or not 194 */ 195 public BigIntegerLocaleConverter(final Object defaultValue, final Locale locale, final String pattern, final boolean locPattern) { 196 197 super(defaultValue, locale, pattern, locPattern); 198 } 199 200 /** 201 * Convert the specified locale-sensitive input object into an output object of 202 * BigInteger type. 203 * 204 * @param value The input object to be converted 205 * @param pattern The pattern is used for the convertion 206 * @return The converted value 207 * @throws ConversionException if conversion cannot be performed 208 * successfully 209 * @throws ParseException if an error occurs parsing a String to a Number 210 * @since 1.8.0 211 */ 212 @Override 213 protected Object parse(final Object value, final String pattern) throws ParseException { 214 215 final Object result = super.parse(value, pattern); 216 217 if (result == null || result instanceof BigInteger) { 218 return result; 219 } 220 221 if (result instanceof Number) { 222 return BigInteger.valueOf(((Number)result).longValue()); 223 } 224 225 try { 226 return new BigInteger(result.toString()); 227 } 228 catch (final NumberFormatException ex) { 229 throw new ConversionException("Suplied number is not of type BigInteger: " + result); 230 } 231 232 } 233 234}