|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface IntCodeEnum<E extends Enum<E>>
An interface to be implemented by java.lang.Enum classes that need to associate a unique and constant integer code to their enum constants.
The main use case is to allow to persist an enum constant to
an integer rather than a string value, which is supported directly
by the java.lang.Enum class through the enum.name() and enum.valueOf(String)
methods.
The enum.ordinal() position could be used, but isn't the best approach for
this use case because we don't have any constrol on it :
it is zero-based and changes if the position of the enum constant changes.
Using the enum name isn't satisfactory either because we would have
to update the database if we ever need to rename an enum constant.
This class allows to migrate to Java 5 enums retaining full backwards
compatiblity with iTracker 2, in which all enumerations were simply defined
as static final int fields.
This interface allows to handle all such enums consistently and to use a single Hibernate custom type to persist them all.
| Field Summary | |
|---|---|
static int |
DEFAULT_CODE
|
| Method Summary | |
|---|---|
E |
fromCode(int code)
Returns a java.lang.Enum constant matching the given integer value. |
int |
getCode()
Returns the integer value representing this enum constant. |
| Field Detail |
|---|
static final int DEFAULT_CODE
| Method Detail |
|---|
int getCode()
E fromCode(int code)
This method should actually be static, so that we don't need
an enum constant instance to lookup another instance by code.
However Java interfaces don't allow static methods and Java 5 enums
must inherit java.lang.Enum directly. So there's no way to create
a common base class with a static fromCode(int) method
for all enums in our application for EnumCodeUserType to use
in a type-safe way!
code - unique enum constant as defined in iTracker 2
IllegalArgumentException - no matching enum constant for
the given code
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||