• Post Reply Bookmark Topic Watch Topic
  • New Topic

looking for a flag-system with enums  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
in the project i'm currently working on, we use a flag-mechanism to set and retrieve some properties. here's an example of the code:

is there a way to make this mechanism safer. just imagine that somebody calls setFlags(38): this blows the whole thing!
maybe there's a way with the new enums???

Thanks for any insights, renaud
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of allowing them to pass in an integer (thereby blowing away any and all encapsulation of your storage mechanism), just set up some "setter" methods for each of your flags on your flags object. Then, you instantiate the flags object and set any flag that needs to be set. The outside world doesn't know if you're using bits to store the flag values or boolean variables and they shouldn't have to. Hope that helps.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDK 1.5 -- Java 5, or "Tiger" -- actually includes real enums. If you can use Tiger, than that'd be your best bet.

But it's actually possible to do something else in pre-Tiger. First, look at this class. Because the constructor is private, three and only three instances exist. Those three instances are available as public static members of the class:



Now, you write UsingFlags like this:




The idea is that UsingFlags only lets people send in FlagValue object, never an int. That way, only value flag values can be used.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like that suggestion! It makes adding new flag values much easier, since you don't have to add in new setter/getter methods. I guess it's not really that important to hide the implementation, since you're stating it in the name of the class ("flags"). But, if you want/need to access the flag values as JavaBean properties, you might want to add some setters/getters.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The arguably small downside with single-flag instances is that you cannot set multiple flags at once using bitwise or to create the parameter. I would at a minimum change the flags instance field to private access (or the validation code could be bypassed) and add an assertion to setFlags(int,boolean):
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Harkness:
The arguably small downside with single-flag instances is that you cannot set multiple flags at once using bitwise or to create the parameter.


You can use EnumSets instead of single flags: http://java.sun.com/j2se/1.5.0/docs/api/java/util/EnumSet.html
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
You can use EnumSets instead of single flags
Very sweet! Thanks for the link. I can't wait to use Java 1.5 on a project. I was going to sign up for the JDK 1.5 beta exam to force me to learn the new features, but it's not being given close enough to hit before work.
 
Renaud Richardet
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all for the valuable posts! it helped me a lot.
unfortunately, i can't use the real enums from tiger on this project.
Ernest, is there a name behind the pattern you showed us? (or shall we call it the Ernest Friedman-Hill-pattern from now on )

thanks again, Renaud
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Renaud Richardet:
Ernest, is there a name behind the pattern you showed us?


It's a variant of the Typesafe Enum pattern, as far as I can tell.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!