You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringToDoubleConverter.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.converter;
  5. import java.text.NumberFormat;
  6. import java.text.ParsePosition;
  7. import java.util.Locale;
  8. /**
  9. * A converter that converts from {@link String} to {@link Double} and back.
  10. * Uses the given locale and a {@link NumberFormat} instance for formatting and
  11. * parsing.
  12. * <p>
  13. * Leading and trailing white spaces are ignored when converting from a String.
  14. * </p>
  15. * <p>
  16. * Override and overwrite {@link #getFormat(Locale)} to use a different format.
  17. * </p>
  18. *
  19. * @author Vaadin Ltd
  20. * @version
  21. * @VERSION@
  22. * @since 7.0
  23. */
  24. public class StringToDoubleConverter implements Converter<String, Double> {
  25. /**
  26. * Returns the format used by {@link #convertToPresentation(Double, Locale)}
  27. * and {@link #convertToModel(String, Locale)}.
  28. *
  29. * @param locale
  30. * The locale to use
  31. * @return A NumberFormat instance
  32. */
  33. protected NumberFormat getFormat(Locale locale) {
  34. if (locale == null) {
  35. locale = Locale.getDefault();
  36. }
  37. return NumberFormat.getNumberInstance(locale);
  38. }
  39. /*
  40. * (non-Javadoc)
  41. *
  42. * @see
  43. * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
  44. * java.util.Locale)
  45. */
  46. @Override
  47. public Double convertToModel(String value, Locale locale)
  48. throws ConversionException {
  49. if (value == null) {
  50. return null;
  51. }
  52. // Remove leading and trailing white space
  53. value = value.trim();
  54. ParsePosition parsePosition = new ParsePosition(0);
  55. Number parsedValue = getFormat(locale).parse(value, parsePosition);
  56. if (parsePosition.getIndex() != value.length()) {
  57. throw new ConversionException("Could not convert '" + value
  58. + "' to " + getModelType().getName());
  59. }
  60. return parsedValue.doubleValue();
  61. }
  62. /*
  63. * (non-Javadoc)
  64. *
  65. * @see
  66. * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
  67. * .Object, java.util.Locale)
  68. */
  69. @Override
  70. public String convertToPresentation(Double value, Locale locale)
  71. throws ConversionException {
  72. if (value == null) {
  73. return null;
  74. }
  75. return getFormat(locale).format(value);
  76. }
  77. /*
  78. * (non-Javadoc)
  79. *
  80. * @see com.vaadin.data.util.converter.Converter#getModelType()
  81. */
  82. @Override
  83. public Class<Double> getModelType() {
  84. return Double.class;
  85. }
  86. /*
  87. * (non-Javadoc)
  88. *
  89. * @see com.vaadin.data.util.converter.Converter#getPresentationType()
  90. */
  91. @Override
  92. public Class<String> getPresentationType() {
  93. return String.class;
  94. }
  95. }