diff options
author | Basil Bourque <basil-bourque@users.noreply.github.com> | 2019-04-10 11:28:46 -0700 |
---|---|---|
committer | Sun Zhe <31067185+ZheSun88@users.noreply.github.com> | 2019-04-10 21:28:46 +0300 |
commit | fb5dc06d578a7268d9f9b826fc7e3b39ea5e1451 (patch) | |
tree | b7ac1d87785a797dfef1aea102c2a4f479ca73cb /server | |
parent | 258450a82b61b1582905978433b8ed23575924b8 (diff) | |
download | vaadin-framework-fb5dc06d578a7268d9f9b826fc7e3b39ea5e1451.tar.gz vaadin-framework-fb5dc06d578a7268d9f9b826fc7e3b39ea5e1451.zip |
Create StringToUuidConverter.java (#11387)
* Create StringToUuidConverter.java
Implements the `Converter` interface, to support converting back-and-forth between `String` and `UUID` (a type built into Java 5 and later).
See Issue # 11,051.
https://github.com/vaadin/framework/issues/11051
* Formatting cleanup, remove sinces
Diffstat (limited to 'server')
-rw-r--r-- | server/src/main/java/com/vaadin/data/converter/StringToUuidConverter.java | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/data/converter/StringToUuidConverter.java b/server/src/main/java/com/vaadin/data/converter/StringToUuidConverter.java new file mode 100644 index 0000000000..be4a5fce19 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/converter/StringToUuidConverter.java @@ -0,0 +1,99 @@ +/* + * Copyright 2000-2018 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.data.converter; + +import java.util.UUID; + +import com.vaadin.data.Converter; +import com.vaadin.data.ErrorMessageProvider; +import com.vaadin.data.Result; +import com.vaadin.data.ValueContext; + +/** + * A converter that converts from {@link String} to {@link UUID} and back. + * <p> + * Leading and trailing white spaces are ignored when converting from a String. + * </p> + * <p> + * The String representation uses the canonical format of 32-characters with a hyphen + * to separate each of five groups of hexadecimal digits as defined in: + * RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace + * http://www.ietf.org/rfc/rfc4122.txt + * </p> + * + * @author Vaadin Ltd + * @since + */ +public class StringToUuidConverter implements Converter <String, UUID> { + + private ErrorMessageProvider errorMessageProvider; + + /** + * Constructs a converter for String to UUID and back. + * + * @param errorMessage the error message to use if conversion fails + */ + public StringToUuidConverter(String errorMessage) { + this(ctx -> errorMessage); + } + + /** + * Constructs a new converter instance with the given error message provider. + * Empty strings are converted to <code>null</code>. + * + * @param errorMessageProvider the error message provider to use if conversion fails + */ + public StringToUuidConverter(ErrorMessageProvider errorMessageProvider) { + this.errorMessageProvider = errorMessageProvider; + } + + @Override + public Result <UUID> convertToModel(String value, ValueContext context) { + if (value == null) { + return Result.ok( null ); + } + + // Remove leading and trailing white space + value = value.trim(); + + // Parse string as UUID. + UUID uuid = null; + try { + uuid = UUID.fromString(value); + } catch (java.lang.IllegalArgumentException e) { + // Faulty input. Let `uuid` default to null. Report error below. + } + + if (null != uuid) { + return Result.ok(uuid); // Return the UUID object, converted from String. + } else { + return Result.error( this.errorMessageProvider.apply(context) ); + } + } + + @Override + public String convertToPresentation (UUID value, ValueContext context) { + if ( value == null ) { + return null; + } + // `java.util.UUID::toString` generates a textual representation of a + // UUID’s 128-bits as a lowercase hexadecimal `String` in canonical + // 32-character format with four hyphens separating groups of digits. + // https://docs.oracle.com/javase/10/docs/api/java/util/UUID.html#toString() + return value.toString(); + } +} |