• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EnumSet bit vector values

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 423
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22789
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[].
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the solution and your comments.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On reflection (pun intended), this isn't that difficult to do without using reflection:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic