1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.beanutils.locale.converters;
  17. import java.text.ParseException;
  18. import java.util.Locale;
  19. /**
  20. * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
  21. * implementation that converts an incoming
  22. * locale-sensitive String into a <code>java.lang.Double</code> object,
  23. * optionally using a default value or throwing a
  24. * {@link org.apache.commons.beanutils.ConversionException}
  25. * if a conversion error occurs.</p>
  26. *
  27. * @author Yauheny Mikulski
  28. */
  29. public class DoubleLocaleConverter extends DecimalLocaleConverter {
  30. // ----------------------------------------------------------- Constructors
  31. /**
  32. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  33. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  34. * if a conversion error occurs. The locale is the default locale for
  35. * this instance of the Java Virtual Machine and an unlocalized pattern is used
  36. * for the convertion.
  37. *
  38. */
  39. public DoubleLocaleConverter() {
  40. this(false);
  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.
  47. *
  48. * @param locPattern Indicate whether the pattern is localized or not
  49. */
  50. public DoubleLocaleConverter(boolean locPattern) {
  51. this(Locale.getDefault(), locPattern);
  52. }
  53. /**
  54. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  55. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  56. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  57. *
  58. * @param locale The locale
  59. */
  60. public DoubleLocaleConverter(Locale locale) {
  61. this(locale, false);
  62. }
  63. /**
  64. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  65. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  66. * if a conversion error occurs.
  67. *
  68. * @param locale The locale
  69. * @param locPattern Indicate whether the pattern is localized or not
  70. */
  71. public DoubleLocaleConverter(Locale locale, boolean locPattern) {
  72. this(locale, (String) null, locPattern);
  73. }
  74. /**
  75. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  76. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  77. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  78. *
  79. * @param locale The locale
  80. * @param pattern The convertion pattern
  81. */
  82. public DoubleLocaleConverter(Locale locale, String pattern) {
  83. this(locale, pattern, false);
  84. }
  85. /**
  86. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  87. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  88. * if a conversion error occurs.
  89. *
  90. * @param locale The locale
  91. * @param pattern The convertion pattern
  92. * @param locPattern Indicate whether the pattern is localized or not
  93. */
  94. public DoubleLocaleConverter(Locale locale, String pattern, boolean locPattern) {
  95. super(locale, pattern, locPattern);
  96. }
  97. /**
  98. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  99. * that will return the specified default value
  100. * if a conversion error occurs. The locale is the default locale for
  101. * this instance of the Java Virtual Machine and an unlocalized pattern is used
  102. * for the convertion.
  103. *
  104. * @param defaultValue The default value to be returned
  105. */
  106. public DoubleLocaleConverter(Object defaultValue) {
  107. this(defaultValue, false);
  108. }
  109. /**
  110. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  111. * that will return the specified default value
  112. * if a conversion error occurs. The locale is the default locale for
  113. * this instance of the Java Virtual Machine.
  114. *
  115. * @param defaultValue The default value to be returned
  116. * @param locPattern Indicate whether the pattern is localized or not
  117. */
  118. public DoubleLocaleConverter(Object defaultValue, boolean locPattern) {
  119. this(defaultValue, Locale.getDefault(), locPattern);
  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. An unlocalized pattern is used for the convertion.
  125. *
  126. * @param defaultValue The default value to be returned
  127. * @param locale The locale
  128. */
  129. public DoubleLocaleConverter(Object defaultValue, Locale locale) {
  130. this(defaultValue, locale, false);
  131. }
  132. /**
  133. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  134. * that will return the specified default value
  135. * if a conversion error occurs.
  136. *
  137. * @param defaultValue The default value to be returned
  138. * @param locale The locale
  139. * @param locPattern Indicate whether the pattern is localized or not
  140. */
  141. public DoubleLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) {
  142. this(defaultValue, locale, null, locPattern);
  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. * @param pattern The convertion pattern
  152. */
  153. public DoubleLocaleConverter(Object defaultValue, Locale locale, String pattern) {
  154. this(defaultValue, locale, pattern, false);
  155. }
  156. /**
  157. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  158. * that will return the specified default value
  159. * if a conversion error occurs.
  160. *
  161. * @param defaultValue The default value to be returned
  162. * @param locale The locale
  163. * @param pattern The convertion pattern
  164. * @param locPattern Indicate whether the pattern is localized or not
  165. */
  166. public DoubleLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
  167. super(defaultValue, locale, pattern);
  168. }
  169. protected Object parse(Object value, String pattern) throws ParseException {
  170. Number result = (Number) super.parse(value, pattern);
  171. if (result instanceof Long) {
  172. return new Double(result.doubleValue());
  173. } else {
  174. return (result);
  175. }
  176. }
  177. }