Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

EnumSet, what the ?

Thread view: 
Roedy Green - 08 Jul 2005 05:48 GMT
I discovered that only the static members of EnumSet are public.

In other words, EnumSets appear to be immutable at least  to anyone
but Sun.

This is highly puzzling. Why bother with a bitset representation for a
set without any mathematical set operations? There is complement, but
where are union (or), intersection (and), set difference (xor),
bitwise operations?  Equals at first looks to be but they defined it
as object identity not bit map identity.

The general purpose "of" method works mostly with enum constant
operands, not with entire EnumSets.

Perhaps I should simply rewrite EnumSet.

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

George Cherry - 08 Jul 2005 06:19 GMT
>I discovered that only the static members of EnumSet are public.
>
[quoted text clipped - 6 lines]
> bitwise operations?  Equals at first looks to be but they defined it
> as object identity not bit map identity.

Have you looked at EnumSets' super classes, AbstractSet and
AbstractCollection? Maybe their bulk operations will give you
what you want. For example, AbstractSet overrides the equals
in Object so that it gives you true set equality, not object equality.

George W. Cherry

> The general purpose "of" method works mostly with enum constant
> operands, not with entire EnumSets.
>
> Perhaps I should simply rewrite EnumSet.
Roedy Green - 08 Jul 2005 07:15 GMT
>Perhaps I should simply rewrite EnumSet.

I discovered another peculiar feature of  RegularEnumSet

look at this code:

  void addRange(E from, E to) {
       elements = (-1L >>>  (from.ordinal() - to.ordinal() - 1)) <<
from.ordinal();
   }

He is constructing a bit mask of ones for the range, 1=included,
0=excluded, 0=unused, counting bits from the lsb. e.g. binary
000111111100 for ordinals 2 through 8. Then instead of doing elements
|= for addRange he does elements = which really should have been
called setRange.

Growl!

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Jesper Nordenberg - 08 Jul 2005 11:59 GMT
> I discovered that only the static members of EnumSet are public.
>
> In other words, EnumSets appear to be immutable at least  to anyone
> but Sun.

EnumSet is not immutable, it implements the mutating methods in the
Set interface.

> This is highly puzzling. Why bother with a bitset representation for a
> set without any mathematical set operations? There is complement, but
> where are union (or), intersection (and), set difference (xor),
> bitwise operations?  Equals at first looks to be but they defined it
> as object identity not bit map identity.

An EnumSet is not a bitset, so it shouldn't contain any bit operations
and correctly doesn't. Sun has chosen to internally represent the set
as a bitset, but that's another issue. The EnumSet implements all
methods in the Set interface, so union == addAll(), intersection ==
retainAll(), difference == a combination of retainAll(), addAll() and
removeAll().

/JN
George Cherry - 08 Jul 2005 19:16 GMT
>> I discovered that only the static members of EnumSet are public.
>>
[quoted text clipped - 16 lines]
> retainAll(), difference == a combination of retainAll(), addAll() and
> removeAll().

Yes, I said something similar (but not so well)
in an earlier post to Roedy. Hey Roedy, Java's
a high-level language; get your mind out of the
bit gutter.  : o )

George
Roedy Green - 09 Jul 2005 09:49 GMT
>Yes, I said something similar (but not so well)
>in an earlier post to Roedy. Hey Roedy, Java's
>a high-level language; get your mind out of the
>bit gutter.  : o )

my background is math.  I expect operations like union, intersection,
on something that calls itself a set, which map very nicely to what an
assembler programmer like me does with bitmaps.  It turns out the
interesting methods of an EnumSet are way down in the AbstractSet
class.

There seems to be a heck of a lot of dithering compared with the way I
implemented such a feature in Abundance.

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

George Cherry - 09 Jul 2005 20:15 GMT
>>Yes, I said something similar (but not so well)
>>in an earlier post to Roedy. Hey Roedy, Java's
[quoted text clipped - 6 lines]
> interesting methods of an EnumSet are way down in the AbstractSet
> class.

Okay, but Joshua Bloch's elegant Collections Framework
gives you all the set operations--and they work between different
Set implementations (including EnumSet) very conveniently.
EnumSet implements the Collections Framework's root
interface, Collection, and, of course, its subinterface, Set.
It turns out that EnumSet's relevant bulk operations are
way up in the Collection interface, and they are defined
explicitly as set operations in the API for the Set interface.
(Are interfaces neat or what?)

Bloch's excellent tutorial on the Java Collections Framework
is at

http://java.sun.com/docs/books/tutorial/collections/

The following is from the tutorial

The bulk operations are particularly well suited to Sets; when applied to
sets, they perform standard set-algebraic operations. Suppose s1 and s2 are
Sets. Here's what the bulk operations do:
 a.. s1.containsAll(s2): Returns true if s2 is a subset of s1. (s2 is a
subset of s1 if set s1 contains all the elements in s2.)
 b.. s1.addAll(s2): Transforms s1 into the union of s1 and s2. (The union
of two sets is the set containing all the elements contained in either set.)
 c.. s1.retainAll(s2): Transforms s1 into the intersection of s1 and s2.
(The intersection of two sets is the set containing only the elements that
are common to both sets.)
 d.. s1.removeAll(s2): Transforms s1 into the (asymmetric) set difference
of s1 and s2. (For example, the set difference of s1 - s2 is the set
containing all the elements found in s1 but not in s2.)
> There seems to be a heck of a lot of dithering compared with the way I
> implemented such a feature in Abundance.

Fooling around directly with bits is a heck of a lot of dithering compared
with the way Bloch specified and implemented set operations in the
Java Collections Framework--and you still get the efficiency of bit
vector dithering, because the implementation of EnumSet uses bit
vectors behind the high level scene.

George W. Cherry


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.