• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use EnumSet instead of bit fields

 
Federico Minarelli
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody!
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?

Thanks a lot for your help!
Bye!
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm no expert but it looks ok to me
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Federico Minarelli
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your suggestions guys!
bye
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic