Is there a way in Java to declare an enumeration whose values can be used together? For example:
enum FileAccess { Read, Write, ReadWrite }
Is it possible to define ReadWrite as Read | Write
(or anything that would yield the same result)?
Is there a way in Java to declare an enumeration whose values can be used together? For example:
enum FileAccess { Read, Write, ReadWrite }
Is it possible to define ReadWrite as Read | Write
(or anything that would yield the same result)?
You use EnumSet:
EnumSet<FileAccess> readWrite = EnumSet.of(FileAccess.Read, FileAccess.Write);
This is actually somewhat more elegant than the C#/.NET way, IMO - aside from anything else, you can easily distinguish between a set and a single value.
See EnumSet which allows you to do this efficiently:
EnumSet set = EnumSet.of(READ, WRITE);