Implementing a bitfield using java enums

后端 未结 5 972
南笙
南笙 2020-12-02 11:03

I maintain a large document archive and I often use bit fields to record the status of my documents during processing or when validating them. My legacy code simply uses sta

5条回答
  •  情书的邮戳
    2020-12-02 12:03

    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.

提交回复
热议问题