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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 hyphen
  29. * 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 the error message to use if conversion fails
  43. */
  44. public StringToUuidConverter(String errorMessage) {
  45. this(ctx -> errorMessage);
  46. }
  47. /**
  48. * Constructs a new converter instance with the given error message provider.
  49. * Empty strings are converted to <code>null</code>.
  50. *
  51. * @param errorMessageProvider the error message provider to use if conversion fails
  52. */
  53. public StringToUuidConverter(ErrorMessageProvider errorMessageProvider) {
  54. this.errorMessageProvider = errorMessageProvider;
  55. }
  56. @Override
  57. public Result <UUID> convertToModel(String value, ValueContext context) {
  58. if (value == null) {
  59. return Result.ok( null );
  60. }
  61. // Remove leading and trailing white space
  62. value = value.trim();
  63. // Parse string as UUID.
  64. UUID uuid = null;
  65. try {
  66. uuid = UUID.fromString(value);
  67. } catch (java.lang.IllegalArgumentException e) {
  68. // Faulty input. Let `uuid` default to null. Report error below.
  69. }
  70. if (null != uuid) {
  71. return Result.ok(uuid); // Return the UUID object, converted from String.
  72. } else {
  73. return Result.error( this.errorMessageProvider.apply(context) );
  74. }
  75. }
  76. @Override
  77. public String convertToPresentation (UUID value, ValueContext context) {
  78. if ( value == null ) {
  79. return null;
  80. }
  81. // `java.util.UUID::toString` generates a textual representation of a
  82. // UUID’s 128-bits as a lowercase hexadecimal `String` in canonical
  83. // 32-character format with four hyphens separating groups of digits.
  84. // https://docs.oracle.com/javase/10/docs/api/java/util/UUID.html#toString()
  85. return value.toString();
  86. }
  87. }