aspose file tools*
The moose likes Java in General and the fly likes EnumSet bit vector values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "EnumSet bit vector values" Watch "EnumSet bit vector values" New topic
Author

EnumSet bit vector values

R Das
Greenhorn

Joined: Sep 11, 2008
Posts: 2
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

Joined: Jun 21, 2008
Posts: 423
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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[].


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

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

Joined: Sep 11, 2008
Posts: 2
Thanks for the solution and your comments.
Federico Minarelli
Greenhorn

Joined: Jan 16, 2010
Posts: 29
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

Joined: Feb 23, 2011
Posts: 3
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

Joined: Feb 23, 2011
Posts: 3
On reflection (pun intended), this isn't that difficult to do without using reflection:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EnumSet bit vector values