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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * @version
  17. * @VERSION@
  18. * @since 7.0
  19. */
  20. public class StringToNumberConverter implements Converter<String, Number> {
  21. /**
  22. * Returns the format used by {@link #convertToPresentation(Number, Locale)}
  23. * and {@link #convertToModel(String, Locale)}.
  24. *
  25. * @param locale
  26. * The locale to use
  27. * @return A NumberFormat instance
  28. */
  29. protected NumberFormat getFormat(Locale locale) {
  30. if (locale == null) {
  31. locale = Locale.getDefault();
  32. }
  33. return NumberFormat.getNumberInstance(locale);
  34. }
  35. /*
  36. * (non-Javadoc)
  37. *
  38. * @see
  39. * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
  40. * java.util.Locale)
  41. */
  42. @Override
  43. public Number convertToModel(String value, Locale locale)
  44. throws ConversionException {
  45. if (value == null) {
  46. return null;
  47. }
  48. // Remove leading and trailing white space
  49. value = value.trim();
  50. // Parse and detect errors. If the full string was not used, it is
  51. // an error.
  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. if (parsedValue == null) {
  59. // Convert "" to null
  60. return null;
  61. }
  62. return parsedValue;
  63. }
  64. /*
  65. * (non-Javadoc)
  66. *
  67. * @see
  68. * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
  69. * .Object, java.util.Locale)
  70. */
  71. @Override
  72. public String convertToPresentation(Number value, Locale locale)
  73. throws ConversionException {
  74. if (value == null) {
  75. return null;
  76. }
  77. return getFormat(locale).format(value);
  78. }
  79. /*
  80. * (non-Javadoc)
  81. *
  82. * @see com.vaadin.data.util.converter.Converter#getModelType()
  83. */
  84. @Override
  85. public Class<Number> getModelType() {
  86. return Number.class;
  87. }
  88. /*
  89. * (non-Javadoc)
  90. *
  91. * @see com.vaadin.data.util.converter.Converter#getPresentationType()
  92. */
  93. @Override
  94. public Class<String> getPresentationType() {
  95. return String.class;
  96. }
  97. }