public class EnumConverter<T> extends DefaultObjectConverter<T> implements LazyInitializeConverter
EnumConverter is a converter for Enums or any other data type that can be enumerated. If it is an Enum,
you can use EnumConverter(Class) to create a converter. For other data types, you can use other
constructors.
Before JDK1.5, there is no Enum type, so this is only one way to define an enumeration. For example, in SwingConstants, the following values are defined.
public static final int CENTER = 0;
public static final int TOP = 1;
public static final int LEFT = 2;
public static final int BOTTOM = 3;
public static final int RIGHT = 4;
The problem comes when you want to display it in UI. You don't want to use 0, 1, 2, 3, 4 as the value doesn't mean
anything from user point of view. You want to use a more meaningful name such as "Center", "Top", "Left", "Bottom",
"Right". Obviously you need a converter here to convert from the integer to string, such as converting from 0 to
"Center" and vice verse. That's what EnumConverter for.| Constructor and Description |
|---|
EnumConverter()
Creates an empty EnumConverter.
|
EnumConverter(java.lang.Class<? extends java.lang.Enum> enumType)
The constructor to create an EnumConverter for an Enum type.
|
EnumConverter(java.lang.String name,
java.lang.Class<?> type,
T[] values,
java.lang.String[] strings)
Creates an EnumConverter.
|
EnumConverter(java.lang.String name,
java.lang.Class<?> type,
T[] values,
java.lang.String[] strings,
T defaultValue)
Creates an EnumConverter.
|
EnumConverter(java.lang.String name,
T[] values,
java.lang.String[] strings)
Creates an EnumConverter.
|
| Modifier and Type | Method and Description |
|---|---|
T |
fromString(java.lang.String string,
ConverterContext context)
Converts the string to the object.
|
ConverterContext |
getConverterContext()
Gets the converter context of this converter.
|
T |
getDefault()
Gets the default value of the converter if it failed to find the matching object for a particular string.
|
java.lang.String |
getName()
Gets the name of the converter.
|
java.lang.Object[] |
getObjects()
Gets the
objects array. |
java.lang.String[] |
getStrings()
Gets the
strings array. |
java.lang.Class<?> |
getType()
Gets the data type of the converter.
|
void |
initialize(java.lang.Class<?> clazz,
ConverterContext converterContext)
Initialize the converter.
|
java.lang.String |
toString(T value,
ConverterContext context)
Converts the object to string.
|
static java.lang.String[] |
toStrings(java.lang.Object[] values)
Converts an object array to a String array using ObjectConverterManager.
|
static java.lang.String[] |
toStrings(java.lang.Object[] values,
ConverterContext converterContext)
Converts an object array to a String array using ObjectConverterManager.
|
fromString, getObjectConverterManager, toString, toStringConverterpublic EnumConverter()
initialize(Class, ConverterContext)
method.public EnumConverter(java.lang.Class<? extends java.lang.Enum> enumType)
enumType - the enum typepublic EnumConverter(java.lang.String name,
T[] values,
java.lang.String[] strings)
name - the name of the converter. The name is used to create ConverterContext and later on the
EditorContext.values - the value array. All elements in the value array should have the same type and it must have at
last one element in the array.strings - the names array. It contains the meaningful names for the elements in the value array. They should
one to one match with each other. The length of name array should be the same as that of value
array. Otherwise IllegalArgumentException will be thrown.public EnumConverter(java.lang.String name,
java.lang.Class<?> type,
T[] values,
java.lang.String[] strings)
name - the name of the converter. The name is used to create ConverterContext and later on the
EditorContext.type - the type of the element in value array.values - the value array. All elements in the value array should have the same type and it must have at
last one element in the array.strings - the names array. It contains the meaningful names for the elements in the value array. They should
one to one match with each other. The length of name array should be the same as that of value
array. Otherwise IllegalArgumentException will be thrown.public EnumConverter(java.lang.String name,
java.lang.Class<?> type,
T[] values,
java.lang.String[] strings,
T defaultValue)
name - the name of the converter. The name is used to create ConverterContext and later on the
EditorContext.type - the type of the element in value array.values - the value array. All elements in the value array should have the same type and it must have
at last one element in the array.strings - the names array. It contains the meaningful names for the elements in the value array. They
should one to one match with each other. The length of name array should be the same as that
of value array. Otherwise IllegalArgumentException will be thrown.defaultValue - the default valuepublic void initialize(java.lang.Class<?> clazz,
ConverterContext converterContext)
LazyInitializeConverterObjectConverterManager in the getConverter
method.initialize in interface LazyInitializeConverterclazz - the actual data type.converterContext - the actual converter context.public ConverterContext getConverterContext()
public java.lang.String toString(T value, ConverterContext context)
strings array. An empty string will be returned if nothing matches. Otherwise, it will return the
corresponding string.toString in interface ObjectConverter<T>toString in class DefaultObjectConverter<T>value - the object to be converted.context - the converter context.public T fromString(java.lang.String string, ConverterContext context)
strings array and find the
matching object from the value array. The default value will be returned if nothing matches. Otherwise, it will
return the string itself that is passed in.fromString in interface ObjectConverter<T>fromString in class DefaultObjectConverter<T>string - the string to be convertedcontext - the converter context.public java.lang.String getName()
public java.lang.Class<?> getType()
public T getDefault()
public java.lang.Object[] getObjects()
objects array.objects array.public java.lang.String[] getStrings()
strings array.strings array.public static java.lang.String[] toStrings(java.lang.Object[] values)
This method can be used, for example, for Enum type, to provide a default string representation of the enum values.
ObjectConverter converter = new EnumConverter("Rank", Rank.values(),
EnumConverter.toStrings(Rank.values()));
Of course, you can still define your own string array for the enum values if the default one doesn't work well.values - the object array.public static java.lang.String[] toStrings(java.lang.Object[] values,
ConverterContext converterContext)
values - the object array.converterContext - the converter context used when calling ObjectConverterManager.toString.