Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

StringToLongConverter.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2000-2018 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.converter;
  17. import java.text.NumberFormat;
  18. import java.util.Locale;
  19. import com.vaadin.data.ErrorMessageProvider;
  20. import com.vaadin.data.Result;
  21. import com.vaadin.data.ValueContext;
  22. /**
  23. * A converter that converts from {@link String} to {@link Long} and back. Uses
  24. * the given locale and a {@link NumberFormat} instance for formatting and
  25. * parsing.
  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 class StringToLongConverter
  34. extends AbstractStringToNumberConverter<Long> {
  35. /**
  36. * Creates a new converter instance with the given error message. Empty
  37. * strings are converted to <code>null</code>.
  38. *
  39. * @param errorMessage
  40. * the error message to use if conversion fails
  41. */
  42. public StringToLongConverter(String errorMessage) {
  43. this(null, errorMessage);
  44. }
  45. /**
  46. * Creates a new converter instance with the given empty string value and
  47. * error message.
  48. *
  49. * @param emptyValue
  50. * the presentation value to return when converting an empty
  51. * string, may be <code>null</code>
  52. * @param errorMessage
  53. * the error message to use if conversion fails
  54. */
  55. public StringToLongConverter(Long emptyValue, String errorMessage) {
  56. super(emptyValue, errorMessage);
  57. }
  58. /**
  59. * Creates a new converter instance with the given error message provider.
  60. * Empty strings are converted to <code>null</code>.
  61. *
  62. * @param errorMessageProvider
  63. * the error message provider to use if conversion fails
  64. *
  65. * @since 8.4
  66. */
  67. public StringToLongConverter(ErrorMessageProvider errorMessageProvider) {
  68. this(null, errorMessageProvider);
  69. }
  70. /**
  71. * Creates a new converter instance with the given empty string value and
  72. * error message provider.
  73. *
  74. * @param emptyValue
  75. * the presentation value to return when converting an empty
  76. * string, may be <code>null</code>
  77. * @param errorMessageProvider
  78. * the error message provider to use if conversion fails
  79. *
  80. * @since 8.4
  81. */
  82. public StringToLongConverter(Long emptyValue,
  83. ErrorMessageProvider errorMessageProvider) {
  84. super(emptyValue, errorMessageProvider);
  85. }
  86. /**
  87. * Returns the format used by
  88. * {@link #convertToPresentation(Object, ValueContext)} and
  89. * {@link #convertToModel(String, ValueContext)}.
  90. *
  91. * @param locale
  92. * The locale to use
  93. * @return A NumberFormat instance
  94. */
  95. @Override
  96. protected NumberFormat getFormat(Locale locale) {
  97. if (locale == null) {
  98. locale = Locale.getDefault();
  99. }
  100. return NumberFormat.getIntegerInstance(locale);
  101. }
  102. @Override
  103. public Result<Long> convertToModel(String value, ValueContext context) {
  104. Result<Number> n = convertToNumber(value, context);
  105. return n.map(number -> {
  106. if (number == null) {
  107. return null;
  108. }
  109. return number.longValue();
  110. });
  111. }
  112. }