Implementing a bitfield using java enums
Instead of defining constructor parameters, you could simply use the internal ordinal()
value to calculate this.
public enum StatusFlag {
DOCUMENT_STATUS_NOT_DEFINED,
DOCUMENT_STATUS_OK,
DOCUMENT_STATUS_MISSING_TID_DIR,
DOCUMENT_STATUS_MISSING_TIF_FILE,
DOCUMENT_STATUS_MISSING_PDF_FILE,
DOCUMENT_STATUS_MISSING_OCR_FILE,
DOCUMENT_STATUS_PAGE_COUNT_TIF,
DOCUMENT_STATUS_PAGE_COUNT_PDF,
DOCUMENT_STATUS_UNAVAILABLE;
public long getStatusFlagValue(){
return 1 << this.ordinal();
}
}
Please note that now you should abstain from reordering, inserting (other than at the end) or deleting entries, otherwise the flag values will change, and the meaning of your database contents will change.
your approach is exactly the way to do it.
A slightly better way would be to store the result of 1 << this.ordinal()
in a field when
the enum values are constructed. This way, you don't have to provide each value manually, and the flag is only computed once.
public enum StatusFlag {DOCUMENT_STATUS_NOT_DEFIND, DOCUMENT_STATUS_OK, DOCUMENT_STATUS_MISSING_TID_DIR, DOCUMENT_STATUS_MISSING_TIF_FILE, DOCUMENT_STATUS_MISSING_PDF_FILE, DOCUMENT_STATUS_MISSING_OCR_FILE, DOCUMENT_STATUS_PAGE_COUNT_TIF, DOCUMENT_STATUS_PAGE_COUNT_PDF, DOCUMENT_STATUS_UNAVAILABLE; public final int flag; StatusFlag() { this.flag = 1 << this.ordinal(); } }
These days, I would use the approach used in the question (manually provide the value of the flag via a constructor parameter) as it is more maintainable:
public enum StatusFlag {
DOCUMENT_STATUS_NOT_DEFINED(0),
DOCUMENT_STATUS_OK(1),
DOCUMENT_STATUS_MISSING_TID_DIR(2),
DOCUMENT_STATUS_MISSING_TIF_FILE(3),
DOCUMENT_STATUS_MISSING_PDF_FILE(4),
DOCUMENT_STATUS_MISSING_OCR_FILE(5),
DOCUMENT_STATUS_PAGE_COUNT_TIF(6),
DOCUMENT_STATUS_PAGE_COUNT_PDF(7),
DOCUMENT_STATUS_UNAVAILABLE(8);
public final int flag;
StatusFlag(int id) {
this.flag = 1 << id;
}
}