Include Converter for the UUID data type. #5579

Open
vaadin-bot opened this Issue Aug 22, 2014 · 1 comment

Projects

None yet

1 participant

@vaadin-bot
Collaborator

Originally by basil.bourque


Add a com.vaadin.data.util.converter.Converter implementation to handle the UUID data type.

-Motivation*

Java includes a UUID class. UUID is becoming increasingly popular, used in internet protocols, enterprise work, and as primary key values in database such as Postgres. Vaadin’s data model, Table/Grid, and such really should be supporting UUID out-of-the box.

-Example Code*

The following is some source code I found written by "petter" of Vaadin. I modified that code by adding a call to toLowercase as the UUID spec requires that a hex-string representation of the UUID’s 128-bit value be presented in lowercase, though uppercase must be tolerated as input for parsing.

import com.vaadin.data.util.converter.Converter;
import java.util.Locale;
import java.util.UUID;

/**
 - Modified by Basil Bourque to ensure output of hex string is in lowercase as required by the UUID spec.
 - 
 - @author petter@vaadin.com
 -/
public class UUIDToStringConverter implements Converter<String , UUID>
{
    private static final String NULL_STRING = "(none)";

    @Override
    public UUID convertToModel ( String value , Class<? extends UUID> targetType , Locale locale ) throws ConversionException
    {
        try {
            return value == null || value.isEmpty() || value.equals( NULL_STRING ) ? null : UUID.fromString( value );
        } catch ( IllegalArgumentException ex ) {
            throw new ConversionException( ex );
        }
    }

    @Override
    public String convertToPresentation ( UUID value , Class<? extends String> targetType , Locale locale ) throws ConversionException
    {
        return value == null ? NULL_STRING : value.toString().toLowerCase(); // The UUID spec *requires* hex-string output to be lowercase. Must tolerate uppercase for input.
    }

    @Override
    public Class<UUID> getModelType ()
    {
        return UUID.class;
    }

    @Override
    public Class<String> getPresentationType ()
    {
        return String.class;
    }
}


Imported from https://dev.vaadin.com/ issue #14493

@vaadin-bot
Collaborator

Originally by basil.bourque


The reason I added the call to toLowercase is because the JavaDoc for the UUID.class’ implementation of toString incorrectly documents lowercase or uppercase as being allowed for generation of the hex-string representing a UUID. Some implementations of Java may therefore output uppercase hex. But in fact that JavaDoc is incorrect, as the spec for UUID clearly requires the hex-string be lowercase: "The hexadecimal values "a" through "f" are output as lower case characters and are case insensitive on input."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment