This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I would like to apply the item 32 of the book "Effective Java" (i.e. "Use EnumSet instead of bit fields") to a particular case: I have to deal with an application which gives me an integer number representing the bit field from which I should build my Set..
In the old version of my class, I have something like this:
Hence, i want to now if the integer field represents a BOLD text and so on.. The improved version should use EnumSet instead of these ugly constants, but I have to face the problem that for backward compatibility I still need a reference to the integer bit fields. Here is what i wrote:
This seems to work fine.. The question is: is there a better way to do this?
I'd reduce the visibility of the getValue() method as much as possible, to prevent other classes from using it instead of the enum (unless you need it elsewhere of course). You can go as far as private because private means private within the top-level class (Text). You can then even remove it and use f.value inside the constructor.
You may also want to add another constructor that takes a Collection<Flag>; you copy it to the enum set. And finally, I'd make this constructor deprecated to discourage people from using it.