Coverage Report - org.apache.commons.beanutils.locale.converters.IntegerLocaleConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
IntegerLocaleConverter
82%
23/28
50%
1/2
1.154
 
 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  
  *      http://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.beanutils.locale.converters;
 19  
 
 20  
 import org.apache.commons.beanutils.ConversionException;
 21  
 
 22  
 import java.util.Locale;
 23  
 import java.text.ParseException;
 24  
 
 25  
 
 26  
 /**
 27  
  * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
 28  
  * implementation that converts an incoming
 29  
  * locale-sensitive String into a <code>java.lang.Integer</code> object,
 30  
  * optionally using a default value or throwing a
 31  
  * {@link org.apache.commons.beanutils.ConversionException}
 32  
  * if a conversion error occurs.</p>
 33  
  *
 34  
  * @author Yauheny Mikulski
 35  
  */
 36  
 
 37  
 public class IntegerLocaleConverter extends DecimalLocaleConverter {
 38  
 
 39  
 
 40  
     // ----------------------------------------------------------- Constructors
 41  
 
 42  
     /**
 43  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 44  
      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
 45  
      * if a conversion error occurs. The locale is the default locale for
 46  
      * this instance of the Java Virtual Machine and an unlocalized pattern is used
 47  
      * for the convertion.
 48  
      *
 49  
      */
 50  
 
 51  
     public IntegerLocaleConverter() {
 52  
 
 53  1
         this(false);
 54  1
     }
 55  
 
 56  
     /**
 57  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 58  
      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
 59  
      * if a conversion error occurs. The locale is the default locale for
 60  
      * this instance of the Java Virtual Machine.
 61  
      *
 62  
      * @param locPattern    Indicate whether the pattern is localized or not
 63  
      */
 64  
     public IntegerLocaleConverter(boolean locPattern) {
 65  
 
 66  2
         this(Locale.getDefault(), locPattern);
 67  2
     }
 68  
 
 69  
     /**
 70  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 71  
      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
 72  
      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
 73  
      *
 74  
      * @param locale        The locale
 75  
      */
 76  
     public IntegerLocaleConverter(Locale locale) {
 77  
 
 78  2
         this(locale, false);
 79  2
     }
 80  
 
 81  
     /**
 82  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 83  
      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
 84  
      * if a conversion error occurs.
 85  
      *
 86  
      * @param locale        The locale
 87  
      * @param locPattern    Indicate whether the pattern is localized or not
 88  
      */
 89  
     public IntegerLocaleConverter(Locale locale, boolean locPattern) {
 90  
 
 91  52
         this(locale, (String) null, locPattern);
 92  52
     }
 93  
 
 94  
     /**
 95  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 96  
      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
 97  
      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
 98  
      *
 99  
      * @param locale        The locale
 100  
      * @param pattern       The convertion pattern
 101  
      */
 102  
     public IntegerLocaleConverter(Locale locale, String pattern) {
 103  
 
 104  1
         this(locale, pattern, false);
 105  1
     }
 106  
 
 107  
     /**
 108  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 109  
      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
 110  
      * if a conversion error occurs.
 111  
      *
 112  
      * @param locale        The locale
 113  
      * @param pattern       The convertion pattern
 114  
      * @param locPattern    Indicate whether the pattern is localized or not
 115  
      */
 116  
     public IntegerLocaleConverter(Locale locale, String pattern, boolean locPattern) {
 117  
 
 118  54
         super(locale, pattern, locPattern);
 119  54
     }
 120  
 
 121  
     /**
 122  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 123  
      * that will return the specified default value
 124  
      * if a conversion error occurs. The locale is the default locale for
 125  
      * this instance of the Java Virtual Machine and an unlocalized pattern is used
 126  
      * for the convertion.
 127  
      *
 128  
      * @param defaultValue  The default value to be returned
 129  
      */
 130  
     public IntegerLocaleConverter(Object defaultValue) {
 131  
 
 132  1
         this(defaultValue, false);
 133  1
     }
 134  
 
 135  
     /**
 136  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 137  
      * that will return the specified default value
 138  
      * if a conversion error occurs. The locale is the default locale for
 139  
      * this instance of the Java Virtual Machine.
 140  
      *
 141  
      * @param defaultValue  The default value to be returned
 142  
      * @param locPattern    Indicate whether the pattern is localized or not
 143  
      */
 144  
     public IntegerLocaleConverter(Object defaultValue, boolean locPattern) {
 145  
 
 146  2
         this(defaultValue, Locale.getDefault(), locPattern);
 147  2
     }
 148  
 
 149  
     /**
 150  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 151  
      * that will return the specified default value
 152  
      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
 153  
      *
 154  
      * @param defaultValue  The default value to be returned
 155  
      * @param locale        The locale
 156  
      */
 157  
     public IntegerLocaleConverter(Object defaultValue, Locale locale) {
 158  
 
 159  0
         this(defaultValue, locale, false);
 160  0
     }
 161  
 
 162  
     /**
 163  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 164  
      * that will return the specified default value
 165  
      * if a conversion error occurs.
 166  
      *
 167  
      * @param defaultValue  The default value to be returned
 168  
      * @param locale        The locale
 169  
      * @param locPattern    Indicate whether the pattern is localized or not
 170  
      */
 171  
     public IntegerLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) {
 172  
 
 173  2
         this(defaultValue, locale, null, locPattern);
 174  2
     }
 175  
 
 176  
     /**
 177  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 178  
      * that will return the specified default value
 179  
      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
 180  
      *
 181  
      * @param defaultValue  The default value to be returned
 182  
      * @param locale        The locale
 183  
      * @param pattern       The convertion pattern
 184  
      */
 185  
     public IntegerLocaleConverter(Object defaultValue, Locale locale, String pattern) {
 186  
 
 187  0
         this(defaultValue, locale, pattern, false);
 188  0
     }
 189  
 
 190  
     /**
 191  
      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
 192  
      * that will return the specified default value
 193  
      * if a conversion error occurs.
 194  
      *
 195  
      * @param defaultValue  The default value to be returned
 196  
      * @param locale        The locale
 197  
      * @param pattern       The convertion pattern
 198  
      * @param locPattern    Indicate whether the pattern is localized or not
 199  
      */
 200  
     public IntegerLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
 201  
 
 202  4
         super(defaultValue, locale, pattern, locPattern);
 203  4
     }
 204  
 
 205  
     /**
 206  
      * Convert the specified locale-sensitive input object into an output object of the
 207  
      * specified type. This method will return Integer type.
 208  
      *
 209  
      * @param value The input object to be converted
 210  
      * @param pattern The pattern is used for the convertion
 211  
      * @return The converted value
 212  
      *
 213  
      * @exception ConversionException if conversion cannot be performed
 214  
      *  successfully
 215  
      * @throws ParseException if an error occurs parsing a String to a Number
 216  
      */
 217  
     protected Object parse(Object value, String pattern) throws ParseException {
 218  44
         final Number parsed = (Number) super.parse(value, pattern);
 219  32
         if (parsed.longValue() != parsed.intValue()) {
 220  0
             throw new ConversionException("Suplied number is not of type Integer: " + parsed.longValue());
 221  
         }
 222  32
         return new Integer(parsed.intValue()); // unlike superclass it will return proper Integer
 223  
     }
 224  
 }