When working with variables/parameters that can only take a finite number of values, I try to always use Java\'s enum
, as in
public enum BonusT
Just because the accepted answer is not self contained:
Support code:
public interface EnumWithCode & EnumWithCode> {
public Integer getCode();
E fromCode(Integer code);
}
public class EnumWithCodeMap & EnumWithCode> {
private final HashMap _map = new HashMap();
public EnumWithCodeMap(Class valueType) {
for( V v : valueType.getEnumConstants() )
_map.put(v.getCode(), v);
}
public V get(Integer num) {
return _map.get(num);
}
}
Example of use:
public enum State implements EnumWithCode {
NOT_STARTED(0), STARTED(1), ENDED(2);
private static final EnumWithCodeMap map = new EnumWithCodeMap(
State.class);
private final int code;
private State(int code) {
this.code = code;
}
@Override
public Integer getCode() {
return code;
}
@Override
public State fromCode(Integer code) {
return map.get(code);
}
}