Question

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)?

Was it helpful?

Solution

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.

OTHER TIPS

See EnumSet which allows you to do this efficiently:

EnumSet<FileAccess> set = EnumSet.of(READ, WRITE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top