Class EnumConversion<T extends Enum<T>>
java.lang.Object
com.univocity.parsers.conversions.NullConversion<String,T>
com.univocity.parsers.conversions.ObjectConversion<T>
com.univocity.parsers.conversions.EnumConversion<T>
- Type Parameters:
T
- the enumeration type whose constants will be converted from/toString
- All Implemented Interfaces:
Conversion<String,
T>
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 revert(Enum)
will return a String using the first EnumSelector
provided in the constructor of this class.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Field
private final Method
private final EnumSelector[]
-
Constructor Summary
ConstructorsConstructorDescriptionEnumConversion
(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 (@linkClass.getEnumConstants()
).EnumConversion
(Class<T> enumType, EnumSelector... selectors) Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelector
s, in the specified order.EnumConversion
(Class<T> enumType, String customEnumElement, EnumSelector... selectors) Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelector
s, in the specified order.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 ofEnumSelector
s, in the specified order. -
Method Summary
Modifier and TypeMethodDescriptionprotected T
fromString
(String input) Creates an instance of T from a String representation.private String
getKey
(T constant, EnumSelector conversionType) private void
initializeMappings
(Set<EnumSelector> conversionTypes) Converts a value of type T back to a StringMethods inherited from class com.univocity.parsers.conversions.ObjectConversion
execute, fromInput, getValueIfObjectIsNull, getValueIfStringIsNull, setValueIfObjectIsNull, setValueIfStringIsNull, undo
Methods inherited from class com.univocity.parsers.conversions.NullConversion
getValueOnNullInput, getValueOnNullOutput, setValueOnNullInput, setValueOnNullOutput
-
Field Details
-
enumType
-
customEnumField
-
customEnumMethod
-
selectors
-
conversions
-
-
Constructor Details
-
EnumConversion
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 (@linkClass.getEnumConstants()
).- Parameters:
enumType
- the enumeration type to be converted from/toString
-
EnumConversion
Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelector
s, in the specified order. EachEnumSelector
identifies which element of each constant of the enumeration class (@linkClass.getEnumConstants()
should be used to match equivalentString
s.- Parameters:
enumType
- the enumeration type to be converted from/toString
selectors
- the selection elements of the enumeration to use for matchingString
s.
-
EnumConversion
Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelector
s, in the specified order. EachEnumSelector
identifies which element of each constant of the enumeration class (@linkClass.getEnumConstants()
should be used to match equivalentString
s.- Parameters:
enumType
- the enumeration type to be converted from/toString
customEnumElement
- name of custom element of the enumeration (attribute or method) whose values should be used to match equivalentString
s.selectors
- the selection elements of the enumeration to use for matchingString
s.
-
EnumConversion
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 ofEnumSelector
s, in the specified order. EachEnumSelector
identifies which element of each constant of the enumeration class (@linkClass.getEnumConstants()
should be used to match equivalentString
s.- Parameters:
enumType
- the enumeration type to be converted from/toString
valueIfStringIsNull
- the default enumeration constant to use if the inputString
isnull
valueIfEnumIsNull
- the defaultString
value to use if the inputenum
constant isnull
customEnumElement
- name of custom element of the enumeration (attribute or method) whose values should be used to match equivalentString
s.selectors
- the selection elements of the enumeration to use for matchingString
s.
-
-
Method Details
-
initializeMappings
-
getKey
-
revert
Description copied from class:ObjectConversion
Converts a value of type T back to a StringThis is a general implementation that simply returns the result of input.toString()
- Specified by:
revert
in interfaceConversion<String,
T extends Enum<T>> - Overrides:
revert
in classObjectConversion<T extends Enum<T>>
- Parameters:
input
- the input of type T to be converted to a String- Returns:
- the conversion result, or the value of
ObjectConversion.getValueIfObjectIsNull()
if the input object is null.
-
fromString
Description copied from class:ObjectConversion
Creates an instance of T from a String representation.- Specified by:
fromString
in classObjectConversion<T extends Enum<T>>
- Parameters:
input
- The String to be converted to T- Returns:
- an instance of T, converted from the String input.
-