public class EnumTranslator<T extends java.lang.Enum<T>> extends java.lang.Object implements TypeTranslator<java.lang.Enum<T>>
TypeTranslator
to use for Enum fields. Writes Enum name (Enum.name()
)
to column as a String. Reads column as a String Enum name
(Enum.name()
) and converts name to Enum
with Enum.valueOf(Class, String)
.
Column should be a sql type that is compatible with java String (like varchar) and wide enough for the longest Enum name.
This class and subclasses may be used as a TypeTranslator
for Enum fields
in one of two ways:
ImplicitType.translator()
or ExplicitType.translator()
.EnumType.translator()
.
Create a subclass for a custom TypeTranslator
for Enum fields. Override
read(ResultSet, int)
and write(PreparedStatement, int, Enum)
if column
is some type other than a String. Override enumToColumn(Enum)
and columnToEnum(Object)
to store something other than Enum name in database.
Constructor and Description |
---|
EnumTranslator() |
Modifier and Type | Method and Description |
---|---|
protected java.lang.Enum<T> |
columnToEnum(java.lang.Object columnValue)
Converts a table column value to an Enum with
Enum.valueOf(Class, String) . |
protected java.lang.Object |
enumToColumn(java.lang.Enum<T> enumValue)
Converts an Enum constant to a value that will be stored in the
table column.
|
T |
getDefaultEnum()
Gets the default Enum associated with
getDefaultEnumName() . |
java.lang.String |
getDefaultEnumName()
Gets the name of the Enum to use in
read(ResultSet, int) when the value read from
database cannot be found in Class.getEnumConstants() of class getEnumClass() . |
java.lang.Class<T> |
getEnumClass()
Gets the class of Enum that is translated.
|
java.lang.Enum<T> |
read(java.sql.ResultSet resultSet,
int columnIndex)
Reads the column as a String and returns
columnToEnum(Object) . |
void |
setDefaultEnumName(java.lang.String defaultEnumName)
Sets the name of the Enum to use in
read(ResultSet, int) when the value read
from the database is not a valid Enum. |
void |
setEnumClass(java.lang.Class<T> enumClass)
Sets the class of Enum to translate.
|
void |
write(java.sql.PreparedStatement preparedStatement,
int parameterIndex,
java.lang.Enum<T> parameter)
Writes the return of
enumToColumn(Enum) for Enum parameter to database as a String. |
public java.lang.Class<T> getEnumClass()
public void setEnumClass(java.lang.Class<T> enumClass)
RowTranslator
to
initialize this translator when Table
object is initialized that
contains an Enum field.enumClass
- Class of Enum to be translatedpublic java.lang.String getDefaultEnumName()
read(ResultSet, int)
when the value read from
database cannot be found in Class.getEnumConstants()
of class getEnumClass()
.public void setDefaultEnumName(java.lang.String defaultEnumName)
read(ResultSet, int)
when the value read
from the database is not a valid Enum. If no default has been set then
read(ResultSet, int)
will return null.
setEnumClass(Class)
must be invoked prior to invoking this method. This method is
invoked by RowTranslator
when Table
object is initialized.
defaultEnumName
- default name of Enum to use or empty string for no defaultpublic T getDefaultEnum()
getDefaultEnumName()
.public void write(java.sql.PreparedStatement preparedStatement, int parameterIndex, java.lang.Enum<T> parameter) throws java.lang.Exception
enumToColumn(Enum)
for Enum parameter to database as a String.
By default enumToColumn(Enum)
returns Enum.name()
. Override this method if
a type other than String is to be written.
See enumToColumn(Enum)
for details about what will be written.
Sets parameter value in the prepared statement.
write
in interface TypeTranslator<java.lang.Enum<T extends java.lang.Enum<T>>>
preparedStatement
- set column value as parameter in this statementparameterIndex
- set parameter at this indexparameter
- value to set in prepared statementjava.lang.Exception
- if errorpublic java.lang.Enum<T> read(java.sql.ResultSet resultSet, int columnIndex) throws java.lang.Exception
columnToEnum(Object)
.
If column is null then null is returned. If no Enum can be found, then the
getDefaultEnum()
is returned.
See columnToEnum(Object)
for details about what will be returned.
Reads value from result set.
read
in interface TypeTranslator<java.lang.Enum<T extends java.lang.Enum<T>>>
resultSet
- read value from this result setcolumnIndex
- read value at this column index from result setjava.lang.Exception
- if errorprotected java.lang.Object enumToColumn(java.lang.Enum<T> enumValue)
Enum.name()
. Override this
method to use a custom value.enumValue
- the Enum constant to convertEnum.name()
; null if enumValue is nullprotected java.lang.Enum<T> columnToEnum(java.lang.Object columnValue)
Enum.valueOf(Class, String)
.
Override this method if you returned a custom value for enumToColumn(Enum)
.columnValue
- value read from table column that stores EnumenumToColumn(Enum)
equals columnValue;
null if columnValue is null; getDefaultEnum()
if columnValue
cannot be found using Enum.valueOf(Class, String)
.