Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EnumSet bit vector values

 
R Das
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am on Java 1.5 and the API http://java.sun.com/javase/7/docs/api/java/util/EnumSet.html mentions that "Enum sets are represented internally as bit vectors.". Is it possible to get that bit value?

The following pseudo code fragment shows what I'm trying to achieve as well as my question.

Thanks!
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R Pervasive wrote: Is it possible to get that bit value?

Yes, it is possible using reflection.
Here is an example for enums up to 64 elements (bits):

This code accesses a private field of Enum object and is highly implementation dependent,
could run on some versions of JVM/operating systems, could break on others or on future versions of JVM,
so don't use this method unless you really must.
 
Rob Spoor
Sheriff
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that won't work if you all of a sudden get more than 64 elements. Instead of the RegularEnumSet class that uses a long field you get the JumboEnumSet class that uses a long[].
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15208
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R Pervasive wrote:I am on Java 1.5 and the API http://java.sun.com/javase/7/docs/api/java/util/EnumSet.html mentions that "Enum sets are represented internally as bit vectors.". Is it possible to get that bit value?

Note that the documentation that your link points to is for the pre-release version of Java 7 - not all of it applies to Java 5.
 
R Das
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the solution and your comments.
 
Federico Minarelli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everybody!
I am facing a similar problem to that exposed in this thread..

I want to use an EnumSet instead of bit fields, but my problem is that I need a constructor which has to accept an integer representing the members of the enum which have to be present in the EnumSet..



Do I have to make an explicit switch (s) in the constructor? Or is there a better solution?

Thanks a lot for your help!
Bye!
 
Aj Neufeld
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reverse of the above works almost as well. Only trick is the EnumSet must be created, such as with the noneOf() call below. Again, this assumes the "RegularEnumSet", with 64 or less bits, but you wanted a bitmask passed in as a 32-bit int, so that seems a safe assumption. Standard disclaimers about subject to changes in implementation details apply.


 
Aj Neufeld
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On reflection (pun intended), this isn't that difficult to do without using reflection:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic