Top Description Fields Constructors Methods
com.univocity.parsers.conversions

public Class EnumConversion<T extends Enum<T>>

extends ObjectConversion<T extends Enum<T>>
Class Inheritance
Type Parameters
<T>
the enumeration type whose constants will be converted from/to String
Imports
com.univocity.parsers.common.*, java.lang.reflect.*, java.util.*

Converts Strings to enumeration constants and vice versa.

This class supports multiple types of identification of enumeration constants. For example, you can match the literal (Enum#name() the ordinal (Enum#ordinal() or the result of a method defined in your enumeration.

The reverse conversion from an enumeration to String (in EnumConversion#revert(Enum) will return a String using the first EnumSelector provided in the constructor of this class.

Author
Univocity Software Pty Ltd - [email protected]
See Also
ObjectConversion, EnumSelector

Field Summary

Modifier and TypeField and Description
private final Map<String, T>[]
private final Field
private final Method
private final Class<T>
private final EnumSelector[]

Constructor Summary

AccessConstructor and Description
public
EnumConversion(Class<T>
the enumeration type to be converted from/to String
enumType
)

Defines a conversion for an enumeration type that will attempt to match Strings against the results of the output produced by (Enum#name(), (Enum#ordinal() and (Enum#toString() of each constant of the given enumeration (@link Class#getEnumConstants()).

public
EnumConversion(Class<T>
the enumeration type to be converted from/to String
enumType
,
EnumSelector...
the selection elements of the enumeration to use for matching Strings.
selectors
)

Defines a conversion for an enumeration type that will attempt to match Strings the list of EnumSelectors, in the specified order.

public
EnumConversion(Class<T>
the enumeration type to be converted from/to String
enumType
,
String
name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent Strings.
customEnumElement
,
EnumSelector...
the selection elements of the enumeration to use for matching Strings.
selectors
)

Defines a conversion for an enumeration type that will attempt to match Strings the list of EnumSelectors, in the specified order.

public
EnumConversion(Class<T>
the enumeration type to be converted from/to String
enumType
,
T
the default enumeration constant to use if the input String is null
valueIfStringIsNull
,
String
the default String value to use if the input enum constant is null
valueIfEnumIsNull
,
String
name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent Strings.
customEnumElement
,
EnumSelector...
the selection elements of the enumeration to use for matching Strings.
selectors
)

Defines a conversion for an enumeration type that will attempt to match Strings the list of EnumSelectors, in the specified order.

Method Summary

Modifier and TypeMethod and Description
protected T
fromString(String
The String to be converted to T
input
)

Implements abstract com.univocity.parsers.conversions.ObjectConversion.fromString.

Creates an instance of T from a String representation.
private String
getKey(T constant, EnumSelector conversionType)

private void
public String
revert(T
the input of type T to be converted to a String
input
)

Overrides com.univocity.parsers.conversions.ObjectConversion.revert.

Implements com.univocity.parsers.conversions.Conversion.revert.

Converts a value of type T back to a String

This is a general implementation that simply returns the result of input.toString()

Inherited from com.univocity.parsers.conversions.ObjectConversion:
executefromInputgetValueIfObjectIsNullgetValueIfStringIsNullsetValueIfObjectIsNullsetValueIfStringIsNullundo

Field Detail

conversionsback to summary
private final Map<String, T>[] conversions
customEnumFieldback to summary
private final Field customEnumField
customEnumMethodback to summary
private final Method customEnumMethod
enumTypeback to summary
private final Class<T> enumType
selectorsback to summary
private final EnumSelector[] selectors

Constructor Detail

EnumConversionback to summary
public EnumConversion(Class<T> enumType)

Defines a conversion for an enumeration type that will attempt to match Strings against the results of the output produced by (Enum#name(), (Enum#ordinal() and (Enum#toString() of each constant of the given enumeration (@link Class#getEnumConstants()).

Parameters
enumType:Class<T>

the enumeration type to be converted from/to String

EnumConversionback to summary
public EnumConversion(Class<T> enumType, EnumSelector... selectors)

Defines a conversion for an enumeration type that will attempt to match Strings the list of EnumSelectors, in the specified order. Each EnumSelector identifies which element of each constant of the enumeration class (@link Class#getEnumConstants() should be used to match equivalent Strings.

Parameters
enumType:Class<T>

the enumeration type to be converted from/to String

selectors:EnumSelector[]

the selection elements of the enumeration to use for matching Strings.

EnumConversionback to summary
public EnumConversion(Class<T> enumType, String customEnumElement, EnumSelector... selectors)

Defines a conversion for an enumeration type that will attempt to match Strings the list of EnumSelectors, in the specified order. Each EnumSelector identifies which element of each constant of the enumeration class (@link Class#getEnumConstants() should be used to match equivalent Strings.

Parameters
enumType:Class<T>

the enumeration type to be converted from/to String

customEnumElement:String

name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent Strings.

selectors:EnumSelector[]

the selection elements of the enumeration to use for matching Strings.

EnumConversionback to summary
public EnumConversion(Class<T> enumType, T valueIfStringIsNull, String valueIfEnumIsNull, String customEnumElement, EnumSelector... selectors)

Defines a conversion for an enumeration type that will attempt to match Strings the list of EnumSelectors, in the specified order. Each EnumSelector identifies which element of each constant of the enumeration class (@link Class#getEnumConstants() should be used to match equivalent Strings.

Parameters
enumType:Class<T>

the enumeration type to be converted from/to String

valueIfStringIsNull:T

the default enumeration constant to use if the input String is null

valueIfEnumIsNull:String

the default String value to use if the input enum constant is null

customEnumElement:String

name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent Strings.

selectors:EnumSelector[]

the selection elements of the enumeration to use for matching Strings.

Annotations
@SuppressWarnings:unchecked

Method Detail

fromStringback to summary
protected T fromString(String input)

Implements abstract com.univocity.parsers.conversions.ObjectConversion.fromString.

Doc from com.univocity.parsers.conversions.ObjectConversion.fromString.

Creates an instance of T from a String representation.

Parameters
input:String

The String to be converted to T

Returns:T

an instance of T, converted from the String input.

Annotations
@Override
getKeyback to summary
private String getKey(T constant, EnumSelector conversionType)
initializeMappingsback to summary
private void initializeMappings(Set<EnumSelector> conversionTypes)
revertback to summary
public String revert(T input)

Overrides com.univocity.parsers.conversions.ObjectConversion.revert.

Implements com.univocity.parsers.conversions.Conversion.revert.

Doc from com.univocity.parsers.conversions.ObjectConversion.revert.

Converts a value of type T back to a String

This is a general implementation that simply returns the result of input.toString()

Parameters
input:T

the input of type T to be converted to a String

Returns:String

the conversion result, or the value of ObjectConversion#getValueIfObjectIsNull() if the input object is null.

Annotations
@Override