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.text.ParseException; 021import java.util.Locale; 022 023import org.apache.commons.beanutils.ConversionException; 024 025/** 026 * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter} 027 * implementation that converts an incoming 028 * locale-sensitive String into a <code>java.lang.Integer</code> object, 029 * optionally using a default value or throwing a 030 * {@link org.apache.commons.beanutils.ConversionException} 031 * if a conversion error occurs.</p> 032 * 033 */ 034 035public class IntegerLocaleConverter extends DecimalLocaleConverter { 036 037 /** 038 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 039 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 040 * if a conversion error occurs. The locale is the default locale for 041 * this instance of the Java Virtual Machine and an unlocalized pattern is used 042 * for the convertion. 043 * 044 */ 045 046 public IntegerLocaleConverter() { 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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 IntegerLocaleConverter(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 the 202 * specified type. This method will return Integer 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 */ 211 @Override 212 protected Object parse(final Object value, final String pattern) throws ParseException { 213 final Number parsed = (Number) super.parse(value, pattern); 214 if (parsed.longValue() != parsed.intValue()) { 215 throw new ConversionException("Suplied number is not of type Integer: " + parsed.longValue()); 216 } 217 return Integer.valueOf(parsed.intValue()); // unlike superclass it will return proper Integer 218 } 219}