public class StringToEnumConverter extends Object implements Converter<String,Enum>
String to an Enum and back.
Designed to provide nice human readable strings for Enum classes
conforming to one of these patterns:
Converter.ConversionException| Constructor and Description |
|---|
StringToEnumConverter() |
| Modifier and Type | Method and Description |
|---|---|
Enum |
convertToModel(String value,
Class<? extends Enum> targetType,
Locale locale)
Converts the given value from target type to source type.
|
String |
convertToPresentation(Enum value,
Class<? extends String> targetType,
Locale locale)
Converts the given value from source type to target type.
|
static String |
enumToString(Enum<?> value,
Locale locale)
Converts the given enum to a human readable string using the given locale
|
Class<Enum> |
getModelType()
The source type of the converter.
|
Class<String> |
getPresentationType()
The target type of the converter.
|
static <T extends Enum<T>> |
stringToEnum(String value,
Class<T> enumType,
Locale locale)
Converts the given string to the given enum type using the given locale
|
public Enum convertToModel(String value, Class<? extends Enum> targetType, Locale locale) throws Converter.ConversionException
ConverterA converter can optionally use locale to do the conversion.
A converter should in most cases be symmetric so chainingConverter.convertToPresentation(Object, Class, Locale) and
Converter.convertToModel(Object, Class, Locale) should return the original
value.convertToModel in interface Converter<String,Enum>value - The value to convert, compatible with the target type. Can be
nulltargetType - The requested type of the return valuelocale - The locale to use for conversion. Can be null.Converter.ConversionException - If the value could not be convertedpublic static <T extends Enum<T>> T stringToEnum(String value, Class<T> enumType, Locale locale) throws Converter.ConversionException
Compatible with enumToString(Enum, Locale)
value - The string value to convertenumType - The type of enum to createlocale - The locale to use for conversion. If null, the JVM default
locale will be usedConversionException - if the conversion failsConverter.ConversionExceptionpublic static String enumToString(Enum<?> value, Locale locale)
Compatible with stringToEnum(String, Class, Locale)
value - The enum value to convertlocale - The locale to use for conversion. If null, the JVM default
locale will be usedConversionException - if the conversion failspublic String convertToPresentation(Enum value, Class<? extends String> targetType, Locale locale) throws Converter.ConversionException
ConverterA converter can optionally use locale to do the conversion.
A converter should in most cases be symmetric so chainingConverter.convertToPresentation(Object, Class, Locale) and
Converter.convertToModel(Object, Class, Locale) should return the original
value.convertToPresentation in interface Converter<String,Enum>value - The value to convert, compatible with the target type. Can be
nulltargetType - The requested type of the return valuelocale - The locale to use for conversion. Can be null.Converter.ConversionException - If the value could not be convertedpublic Class<Enum> getModelType()
ConverterConverter.convertToPresentation(Object, Class, Locale).getModelType in interface Converter<String,Enum>public Class<String> getPresentationType()
ConverterConverter.convertToModel(Object, Class, Locale).getPresentationType in interface Converter<String,Enum>Copyright © 2017 Vaadin Ltd. All rights reserved.