All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databasesandlife.util.wicket.UuidConverter Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
package com.databasesandlife.util.wicket;

import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.Locale;
import java.util.UUID;

/**
 * Wicket converter to allow user to enter UUIDs and display errors if they are not valid.
 *
 * @author This source is copyright Adrian Smith and licensed under the LGPL 3.
 * @see Project on GitHub
 */
public class UuidConverter implements IConverter {

    @Override public @CheckForNull UUID convertToObject(@CheckForNull String value, @Nonnull Locale locale)
    throws ConversionException {
        if (value == null) return null;

        try { return UUID.fromString(value); }
        catch (IllegalArgumentException e) {
            throw new ConversionException("Cannot understand "+UuidConverter.class+" '" + value + "'")
                .setSourceValue(value)
                .setResourceKey("UuidConverter.IllegalArgumentException")
                .setTargetType(UuidConverter.class)
                .setConverter(this)
                .setLocale(locale);
        }
    }

    @Override public @CheckForNull String convertToString(@CheckForNull UUID value, @Nonnull Locale locale) {
        if (value == null) return null;
        return value.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy