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.

StringToUuidConverter.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.util.UUID;
  18. import com.vaadin.data.Converter;
  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 UUID} and back.
  24. * <p>
  25. * Leading and trailing white spaces are ignored when converting from a String.
  26. * </p>
  27. * <p>
  28. * The String representation uses the canonical format of 32-characters with a
  29. * hyphen to separate each of five groups of hexadecimal digits as defined in:
  30. * RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace
  31. * http://www.ietf.org/rfc/rfc4122.txt
  32. * </p>
  33. *
  34. * @author Vaadin Ltd
  35. * @since 8.8
  36. */
  37. public class StringToUuidConverter implements Converter<String, UUID> {
  38. private ErrorMessageProvider errorMessageProvider;
  39. /**
  40. * Constructs a converter for String to UUID and back.
  41. *
  42. * @param errorMessage
  43. * the error message to use if conversion fails
  44. */
  45. public StringToUuidConverter(String errorMessage) {
  46. this(ctx -> errorMessage);
  47. }
  48. /**
  49. * Constructs a new converter instance with the given error message
  50. * provider. Empty strings are converted to <code>null</code>.
  51. *
  52. * @param errorMessageProvider
  53. * the error message provider to use if conversion fails
  54. */
  55. public StringToUuidConverter(ErrorMessageProvider errorMessageProvider) {
  56. this.errorMessageProvider = errorMessageProvider;
  57. }
  58. @Override
  59. public Result<UUID> convertToModel(String value, ValueContext context) {
  60. if (value == null) {
  61. return Result.ok(null);
  62. }
  63. // Remove leading and trailing white space
  64. value = value.trim();
  65. // Parse string as UUID.
  66. UUID uuid = null;
  67. try {
  68. uuid = UUID.fromString(value);
  69. } catch (java.lang.IllegalArgumentException e) {
  70. // Faulty input. Let `uuid` default to null. Report error below.
  71. }
  72. if (null != uuid) {
  73. return Result.ok(uuid); // Return the UUID object, converted from
  74. // String.
  75. } else {
  76. return Result.error(this.errorMessageProvider.apply(context));
  77. }
  78. }
  79. @Override
  80. public String convertToPresentation(UUID value, ValueContext context) {
  81. if (value == null) {
  82. return null;
  83. }
  84. // `java.util.UUID::toString` generates a textual representation of a
  85. // UUID’s 128-bits as a lowercase hexadecimal `String` in canonical
  86. // 32-character format with four hyphens separating groups of digits.
  87. // https://docs.oracle.com/javase/10/docs/api/java/util/UUID.html#toString()
  88. return value.toString();
  89. }
  90. }