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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * @since 7.0
  21. */
  22. public class StringToDoubleConverter implements Converter<String, Double> {
  23. /**
  24. * Returns the format used by {@link #convertToPresentation(Double, Locale)}
  25. * and {@link #convertToModel(String, Locale)}.
  26. *
  27. * @param locale
  28. * The locale to use
  29. * @return A NumberFormat instance
  30. */
  31. protected NumberFormat getFormat(Locale locale) {
  32. if (locale == null) {
  33. locale = Locale.getDefault();
  34. }
  35. return NumberFormat.getNumberInstance(locale);
  36. }
  37. /*
  38. * (non-Javadoc)
  39. *
  40. * @see
  41. * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
  42. * java.util.Locale)
  43. */
  44. @Override
  45. public Double convertToModel(String value, Locale locale)
  46. throws ConversionException {
  47. if (value == null) {
  48. return null;
  49. }
  50. // Remove leading and trailing white space
  51. value = value.trim();
  52. ParsePosition parsePosition = new ParsePosition(0);
  53. Number parsedValue = getFormat(locale).parse(value, parsePosition);
  54. if (parsePosition.getIndex() != value.length()) {
  55. throw new ConversionException("Could not convert '" + value
  56. + "' to " + getModelType().getName());
  57. }
  58. return parsedValue.doubleValue();
  59. }
  60. /*
  61. * (non-Javadoc)
  62. *
  63. * @see
  64. * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
  65. * .Object, java.util.Locale)
  66. */
  67. @Override
  68. public String convertToPresentation(Double value, Locale locale)
  69. throws ConversionException {
  70. if (value == null) {
  71. return null;
  72. }
  73. return getFormat(locale).format(value);
  74. }
  75. /*
  76. * (non-Javadoc)
  77. *
  78. * @see com.vaadin.data.util.converter.Converter#getModelType()
  79. */
  80. @Override
  81. public Class<Double> getModelType() {
  82. return Double.class;
  83. }
  84. /*
  85. * (non-Javadoc)
  86. *
  87. * @see com.vaadin.data.util.converter.Converter#getPresentationType()
  88. */
  89. @Override
  90. public Class<String> getPresentationType() {
  91. return String.class;
  92. }
  93. }