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.

AbstractStringToNumberConverter.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.util.converter;
  17. import java.text.NumberFormat;
  18. import java.text.ParsePosition;
  19. import java.util.Locale;
  20. import com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException;
  21. /**
  22. * A converter that converts from the number type T to {@link String} and back.
  23. * Uses the given locale and {@link NumberFormat} for formatting and parsing.
  24. * Automatically trims the input string, removing any leading and trailing white
  25. * space.
  26. * <p>
  27. * Override and overwrite {@link #getFormat(Locale)} to use a different format.
  28. * </p>
  29. *
  30. * @author Vaadin Ltd
  31. * @since 8.0
  32. */
  33. public abstract class AbstractStringToNumberConverter<T>
  34. implements Converter<String, T> {
  35. /**
  36. * Returns the format used by {@link #convertToPresentation(Object, Locale)}
  37. * and {@link #convertToModel(Object, Locale)}.
  38. *
  39. * @param locale
  40. * The locale to use
  41. * @return A NumberFormat instance
  42. */
  43. protected NumberFormat getFormat(Locale locale) {
  44. if (locale == null) {
  45. locale = Locale.getDefault();
  46. }
  47. return NumberFormat.getNumberInstance(locale);
  48. }
  49. /**
  50. * Convert the value to a Number using the given locale and
  51. * {@link #getFormat(Locale)}.
  52. *
  53. * @param value
  54. * The value to convert
  55. * @param locale
  56. * The locale to use for conversion
  57. * @return The converted value
  58. * @throws ConversionException
  59. * If there was a problem converting the value
  60. */
  61. protected Number convertToNumber(String value, Locale locale)
  62. throws ConversionException {
  63. if (value == null) {
  64. return null;
  65. }
  66. // Remove leading and trailing white space
  67. value = value.trim();
  68. // Parse and detect errors. If the full string was not used, it is
  69. // an error.
  70. ParsePosition parsePosition = new ParsePosition(0);
  71. Number parsedValue = getFormat(locale).parse(value, parsePosition);
  72. if (parsePosition.getIndex() != value.length()) {
  73. throw new ConversionException("Could not convert '" + value + "'");
  74. }
  75. if (parsedValue == null) {
  76. // Convert "" to null
  77. return null;
  78. }
  79. return parsedValue;
  80. }
  81. @Override
  82. public String convertToPresentation(T value, Locale locale) {
  83. if (value == null) {
  84. return null;
  85. }
  86. return getFormat(locale).format(value);
  87. }
  88. }