T
- the enumeration type whose constants will be converted from/to String
public class EnumConversion<T extends java.lang.Enum<T>> extends ObjectConversion<T>
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.
ObjectConversion
,
EnumSelector
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,T>[] |
conversions |
private java.lang.reflect.Field |
customEnumField |
private java.lang.reflect.Method |
customEnumMethod |
private java.lang.Class<T> |
enumType |
private EnumSelector[] |
selectors |
Constructor and 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 (@link Class.getEnumConstants() ). |
EnumConversion(java.lang.Class<T> enumType,
EnumSelector... selectors)
Defines a conversion for an enumeration type that will attempt to match Strings the list of
EnumSelector s, 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 of
EnumSelector s, 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 of
EnumSelector s, in the specified order. |
Modifier and Type | Method and Description |
---|---|
protected T |
fromString(java.lang.String input)
Creates an instance of T from a String representation.
|
private java.lang.String |
getKey(T constant,
EnumSelector conversionType) |
private void |
initializeMappings(java.util.Set<EnumSelector> conversionTypes) |
java.lang.String |
revert(T input)
Converts a value of type T back to a String
|
execute, fromInput, getValueIfObjectIsNull, getValueIfStringIsNull, setValueIfObjectIsNull, setValueIfStringIsNull, undo
getValueOnNullInput, getValueOnNullOutput, setValueOnNullInput, setValueOnNullOutput
private final java.lang.reflect.Field customEnumField
private final java.lang.reflect.Method customEnumMethod
private final EnumSelector[] selectors
public EnumConversion(java.lang.Class<T> enumType)
Enum.name()
, (Enum.ordinal()
and (Enum.toString()
of each constant of the given enumeration (@link Class.getEnumConstants()
).enumType
- the enumeration type to be converted from/to String
public EnumConversion(java.lang.Class<T> enumType, EnumSelector... selectors)
EnumSelector
s, 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 String
s.enumType
- the enumeration type to be converted from/to String
selectors
- the selection elements of the enumeration to use for matching String
s.public EnumConversion(java.lang.Class<T> enumType, java.lang.String customEnumElement, EnumSelector... selectors)
EnumSelector
s, 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 String
s.enumType
- the enumeration type to be converted from/to String
customEnumElement
- name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent String
s.selectors
- the selection elements of the enumeration to use for matching String
s.public EnumConversion(java.lang.Class<T> enumType, T valueIfStringIsNull, java.lang.String valueIfEnumIsNull, java.lang.String customEnumElement, EnumSelector... selectors)
EnumSelector
s, 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 String
s.enumType
- the enumeration type to be converted from/to String
valueIfStringIsNull
- the default enumeration constant to use if the input String
is null
valueIfEnumIsNull
- the default String
value to use if the input enum
constant is null
customEnumElement
- name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent String
s.selectors
- the selection elements of the enumeration to use for matching String
s.private void initializeMappings(java.util.Set<EnumSelector> conversionTypes)
private java.lang.String getKey(T constant, EnumSelector conversionType)
public java.lang.String revert(T input)
ObjectConversion
This is a general implementation that simply returns the result of input.toString()
revert
in interface Conversion<java.lang.String,T extends java.lang.Enum<T>>
revert
in class ObjectConversion<T extends java.lang.Enum<T>>
input
- the input of type T to be converted to a StringObjectConversion.getValueIfObjectIsNull()
if the input object is null.protected T fromString(java.lang.String input)
ObjectConversion
fromString
in class ObjectConversion<T extends java.lang.Enum<T>>
input
- The String to be converted to T