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.

StringToNumberConverter.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Number} to {@link String} and back.
  10. * Uses the given locale and {@link NumberFormat} for formatting and parsing.
  11. * <p>
  12. * Override and overwrite {@link #getFormat(Locale)} to use a different format.
  13. * </p>
  14. *
  15. * @author Vaadin Ltd
  16. * @since 7.0
  17. */
  18. public class StringToNumberConverter implements Converter<String, Number> {
  19. /**
  20. * Returns the format used by {@link #convertToPresentation(Number, Locale)}
  21. * and {@link #convertToModel(String, Locale)}.
  22. *
  23. * @param locale
  24. * The locale to use
  25. * @return A NumberFormat instance
  26. */
  27. protected NumberFormat getFormat(Locale locale) {
  28. if (locale == null) {
  29. locale = Locale.getDefault();
  30. }
  31. return NumberFormat.getNumberInstance(locale);
  32. }
  33. /*
  34. * (non-Javadoc)
  35. *
  36. * @see
  37. * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
  38. * java.util.Locale)
  39. */
  40. @Override
  41. public Number convertToModel(String value, Locale locale)
  42. throws ConversionException {
  43. if (value == null) {
  44. return null;
  45. }
  46. // Remove leading and trailing white space
  47. value = value.trim();
  48. // Parse and detect errors. If the full string was not used, it is
  49. // an error.
  50. ParsePosition parsePosition = new ParsePosition(0);
  51. Number parsedValue = getFormat(locale).parse(value, parsePosition);
  52. if (parsePosition.getIndex() != value.length()) {
  53. throw new ConversionException("Could not convert '" + value
  54. + "' to " + getModelType().getName());
  55. }
  56. if (parsedValue == null) {
  57. // Convert "" to null
  58. return null;
  59. }
  60. return parsedValue;
  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(Number 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<Number> getModelType() {
  84. return Number.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. }