| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FloatLocaleConverter |
|
| 1.3846153846153846;1.385 |
| 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.math.BigDecimal</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 FloatLocaleConverter 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 | public FloatLocaleConverter() { | |
| 51 | ||
| 52 | 1 | this(false); |
| 53 | 1 | } |
| 54 | ||
| 55 | /** | |
| 56 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 57 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
| 58 | * if a conversion error occurs. The locale is the default locale for | |
| 59 | * this instance of the Java Virtual Machine. | |
| 60 | * | |
| 61 | * @param locPattern Indicate whether the pattern is localized or not | |
| 62 | */ | |
| 63 | public FloatLocaleConverter(boolean locPattern) { | |
| 64 | ||
| 65 | 2 | this(Locale.getDefault(), locPattern); |
| 66 | 2 | } |
| 67 | ||
| 68 | /** | |
| 69 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 70 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
| 71 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
| 72 | * | |
| 73 | * @param locale The locale | |
| 74 | */ | |
| 75 | public FloatLocaleConverter(Locale locale) { | |
| 76 | ||
| 77 | 1 | this(locale, false); |
| 78 | 1 | } |
| 79 | ||
| 80 | /** | |
| 81 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 82 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
| 83 | * if a conversion error occurs. | |
| 84 | * | |
| 85 | * @param locale The locale | |
| 86 | * @param locPattern Indicate whether the pattern is localized or not | |
| 87 | */ | |
| 88 | public FloatLocaleConverter(Locale locale, boolean locPattern) { | |
| 89 | ||
| 90 | 50 | this(locale, (String) null, locPattern); |
| 91 | 50 | } |
| 92 | ||
| 93 | /** | |
| 94 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 95 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
| 96 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
| 97 | * | |
| 98 | * @param locale The locale | |
| 99 | * @param pattern The convertion pattern | |
| 100 | */ | |
| 101 | public FloatLocaleConverter(Locale locale, String pattern) { | |
| 102 | ||
| 103 | 2 | this(locale, pattern, false); |
| 104 | 2 | } |
| 105 | ||
| 106 | /** | |
| 107 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 108 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
| 109 | * if a conversion error occurs. | |
| 110 | * | |
| 111 | * @param locale The locale | |
| 112 | * @param pattern The convertion pattern | |
| 113 | * @param locPattern Indicate whether the pattern is localized or not | |
| 114 | */ | |
| 115 | public FloatLocaleConverter(Locale locale, String pattern, boolean locPattern) { | |
| 116 | ||
| 117 | 53 | super(locale, pattern, locPattern); |
| 118 | 53 | } |
| 119 | ||
| 120 | /** | |
| 121 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 122 | * that will return the specified default value | |
| 123 | * if a conversion error occurs. The locale is the default locale for | |
| 124 | * this instance of the Java Virtual Machine and an unlocalized pattern is used | |
| 125 | * for the convertion. | |
| 126 | * | |
| 127 | * @param defaultValue The default value to be returned | |
| 128 | */ | |
| 129 | public FloatLocaleConverter(Object defaultValue) { | |
| 130 | ||
| 131 | 1 | this(defaultValue, false); |
| 132 | 1 | } |
| 133 | ||
| 134 | /** | |
| 135 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 136 | * that will return the specified default value | |
| 137 | * if a conversion error occurs. The locale is the default locale for | |
| 138 | * this instance of the Java Virtual Machine. | |
| 139 | * | |
| 140 | * @param defaultValue The default value to be returned | |
| 141 | * @param locPattern Indicate whether the pattern is localized or not | |
| 142 | */ | |
| 143 | public FloatLocaleConverter(Object defaultValue, boolean locPattern) { | |
| 144 | ||
| 145 | 2 | this(defaultValue, Locale.getDefault(), locPattern); |
| 146 | 2 | } |
| 147 | ||
| 148 | /** | |
| 149 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 150 | * that will return the specified default value | |
| 151 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
| 152 | * | |
| 153 | * @param defaultValue The default value to be returned | |
| 154 | * @param locale The locale | |
| 155 | */ | |
| 156 | public FloatLocaleConverter(Object defaultValue, Locale locale) { | |
| 157 | ||
| 158 | 0 | this(defaultValue, locale, false); |
| 159 | 0 | } |
| 160 | ||
| 161 | /** | |
| 162 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 163 | * that will return the specified default value | |
| 164 | * if a conversion error occurs. | |
| 165 | * | |
| 166 | * @param defaultValue The default value to be returned | |
| 167 | * @param locale The locale | |
| 168 | * @param locPattern Indicate whether the pattern is localized or not | |
| 169 | */ | |
| 170 | public FloatLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) { | |
| 171 | ||
| 172 | 2 | this(defaultValue, locale, null, locPattern); |
| 173 | 2 | } |
| 174 | ||
| 175 | /** | |
| 176 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 177 | * that will return the specified default value | |
| 178 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
| 179 | * | |
| 180 | * @param defaultValue The default value to be returned | |
| 181 | * @param locale The locale | |
| 182 | * @param pattern The convertion pattern | |
| 183 | */ | |
| 184 | public FloatLocaleConverter(Object defaultValue, Locale locale, String pattern) { | |
| 185 | ||
| 186 | 0 | this(defaultValue, locale, pattern, false); |
| 187 | 0 | } |
| 188 | ||
| 189 | /** | |
| 190 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
| 191 | * that will return the specified default value | |
| 192 | * if a conversion error occurs. | |
| 193 | * | |
| 194 | * @param defaultValue The default value to be returned | |
| 195 | * @param locale The locale | |
| 196 | * @param pattern The convertion pattern | |
| 197 | * @param locPattern Indicate whether the pattern is localized or not | |
| 198 | */ | |
| 199 | public FloatLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) { | |
| 200 | ||
| 201 | 4 | super(defaultValue, locale, pattern, locPattern); |
| 202 | 4 | } |
| 203 | ||
| 204 | /** | |
| 205 | * Convert the specified locale-sensitive input object into an output object of the | |
| 206 | * specified type. This method will return Float value or throw exception if value | |
| 207 | * can not be stored in the Float. | |
| 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 | 47 | final Number parsed = (Number) super.parse(value, pattern); |
| 219 | 34 | double doubleValue = parsed.doubleValue(); |
| 220 | 34 | double posDouble = (doubleValue >= (double)0) ? doubleValue : (doubleValue * (double)-1); |
| 221 | 34 | if (posDouble != 0 && (posDouble < Float.MIN_VALUE || posDouble > Float.MAX_VALUE)) { |
| 222 | 4 | throw new ConversionException("Supplied number is not of type Float: "+parsed); |
| 223 | } | |
| 224 | 30 | return new Float(parsed.floatValue()); // unlike superclass it returns Float type |
| 225 | } | |
| 226 | } |