Configuration.Type
, CustomField.Type
, IssueActivityType
, IssueRelation.Type
, PermissionType
, Status
public interface IntCodeEnum<E extends Enum<E>>
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 control 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
compatibility 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.
Modifier and Type | Field | Description |
---|---|---|
static int |
DEFAULT_CODE |
Modifier and Type | Method | Description |
---|---|---|
E |
fromCode(Integer code) |
Returns a java.lang.Enum constant matching the given integer value.
|
Integer |
getCode() |
Returns the integer value representing this enum constant.
|
static final int DEFAULT_CODE
Integer getCode()
E fromCode(Integer 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!
You should instead implement a static valueOf(int) wrapped by this method:
static final E valueOf(Integer code) { for (E val: values()) { if (val.code == code) { return val; } } throw new IllegalArgumentException("Unknown code : " + code); }
code
- unique enum constant as defined in iTracker 2IllegalArgumentException
- no matching enum constant for
the given code
Copyright © 2002–2019 itracker. All rights reserved.