Class EnumConversion<T extends java.lang.Enum<T>>
- java.lang.Object
-
- com.univocity.parsers.conversions.NullConversion<java.lang.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<java.lang.String,T>
public class EnumConversion<T extends java.lang.Enum<T>> extends ObjectConversion<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 firstEnumSelectorprovided in the constructor of this class.- Author:
- Univocity Software Pty Ltd - parsers@univocity.com
- See Also:
ObjectConversion,EnumSelector
-
-
Constructor Summary
Constructors Constructor Description EnumConversion(java.lang.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(java.lang.Class<T> enumType, EnumSelector... selectors)Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelectors, in the specified order.EnumConversion(java.lang.Class<T> enumType, java.lang.String customEnumElement, EnumSelector... selectors)Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelectors, in the specified order.EnumConversion(java.lang.Class<T> enumType, T valueIfStringIsNull, java.lang.String valueIfEnumIsNull, java.lang.String customEnumElement, EnumSelector... selectors)Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelectors, in the specified order.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected TfromString(java.lang.String input)Creates an instance of T from a String representation.java.lang.Stringrevert(T input)Converts a value of type T back to a String-
Methods 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
-
-
-
-
Constructor Detail
-
EnumConversion
public EnumConversion(java.lang.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()).- Parameters:
enumType- the enumeration type to be converted from/toString
-
EnumConversion
public EnumConversion(java.lang.Class<T> enumType, EnumSelector... selectors)
Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelectors, in the specified order. EachEnumSelectoridentifies which element of each constant of the enumeration class (@linkClass.getEnumConstants()should be used to match equivalentStrings.- Parameters:
enumType- the enumeration type to be converted from/toStringselectors- the selection elements of the enumeration to use for matchingStrings.
-
EnumConversion
public EnumConversion(java.lang.Class<T> enumType, java.lang.String customEnumElement, EnumSelector... selectors)
Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelectors, in the specified order. EachEnumSelectoridentifies which element of each constant of the enumeration class (@linkClass.getEnumConstants()should be used to match equivalentStrings.- Parameters:
enumType- the enumeration type to be converted from/toStringcustomEnumElement- name of custom element of the enumeration (attribute or method) whose values should be used to match equivalentStrings.selectors- the selection elements of the enumeration to use for matchingStrings.
-
EnumConversion
public EnumConversion(java.lang.Class<T> enumType, T valueIfStringIsNull, java.lang.String valueIfEnumIsNull, java.lang.String customEnumElement, EnumSelector... selectors)
Defines a conversion for an enumeration type that will attempt to match Strings the list ofEnumSelectors, in the specified order. EachEnumSelectoridentifies which element of each constant of the enumeration class (@linkClass.getEnumConstants()should be used to match equivalentStrings.- Parameters:
enumType- the enumeration type to be converted from/toStringvalueIfStringIsNull- the default enumeration constant to use if the inputStringisnullvalueIfEnumIsNull- the defaultStringvalue to use if the inputenumconstant isnullcustomEnumElement- name of custom element of the enumeration (attribute or method) whose values should be used to match equivalentStrings.selectors- the selection elements of the enumeration to use for matchingStrings.
-
-
Method Detail
-
revert
public java.lang.String revert(T input)
Description copied from class:ObjectConversionConverts a value of type T back to a StringThis is a general implementation that simply returns the result of input.toString()
- Specified by:
revertin interfaceConversion<java.lang.String,T extends java.lang.Enum<T>>- Overrides:
revertin classObjectConversion<T extends java.lang.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
protected T fromString(java.lang.String input)
Description copied from class:ObjectConversionCreates an instance of T from a String representation.- Specified by:
fromStringin classObjectConversion<T extends java.lang.Enum<T>>- Parameters:
input- The String to be converted to T- Returns:
- an instance of T, converted from the String input.
-
-