Implementing a bitfield using java enums

后端 未结 5 971
南笙
南笙 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 11:51

    I have made a complete library for this problem: http://claude-martin.ch/enumbitset/

    The main goal was to store sets of enum types in bitfields. But it also supports other types.

    With this you would not need any extra methods like your "getStatusFlags()". It can be used on any existing enum type simply by adding the interface EnumBitSetHelper (it is used like a "trait"). Each enum constant can then create an "EnumBitSet" which has all methods of Java's EnumSet and BitSet. Then you can work with these sets of enum constants and convert them to bitfield values.

    It supports many formats such as BigInteger and long to easily store the value into a bit field. But note that this only works with Java version 8 and newer.

提交回复
热议问题