org.itracker.model
Interface IntCodeEnum<E extends Enum<E>>

All Known Implementing Classes:
CustomField.Type, IssueActivityType, Status

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.

Author:
johnny

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

DEFAULT_CODE

static final int DEFAULT_CODE
See Also:
Constant Field Values
Method Detail

getCode

int getCode()
Returns the integer value representing this enum constant.

Returns:
unique constant as defined in iTracker 2

fromCode

E fromCode(int code)
Returns a java.lang.Enum constant matching the given integer value.

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!

Parameters:
code - unique enum constant as defined in iTracker 2
Returns:
java.lang.Enum constant instance for the given code
Throws:
IllegalArgumentException - no matching enum constant for the given code


Copyright © 2002-2012 itracker. All Rights Reserved.