views:

118

answers:

2

I want to have all the efficiencies of EnumSet and pass it around without worrying that somebody would modify it.

+4  A: 

What's wrong with Collections.unmodifiableSet() wrapping an EnumSet?

True, the original EnumSet is still mutable, but as long as you discard the original reference, it's as good as immutable inside the wrapper.

edit: OK, since EnumSet doesn't offer any instance methods over and above the Set interface, the only reason for not using this solution is that the EnumSet type is useful for documentation purposes, and you lose that when wrapping it in a Set. Other than that, EnumSet behaviour will be preserved.

skaffman
It doesn't have all methods of an `EnumSet`
Colin Hebert
@Colin what methods would that be? You could still use the static methods to create the enumset before wrapping it, and I don't see any new instance methods in enumset that other sets don't have either.
seanizer
@skaffman. I think I want it mostly for documentation purposes. The thing about EnumSet is that it specifies a certain iteration order, and if you use `Set` in place of `EnumSet` it may produce different results than you expect: somebody sees the `Set` in method signature and passes in `HashSet` for example. Without this requirement your suggestion would be fine. BTW, the `EnumSet` does not have additional methods ( only constructors ), though it would be really nice to have `first`, `last` and `reverseIterator` in some cases.
Alexander Pogrebnyak
@seanizer, Hum, I didn't noticed that the new methods where static.
Colin Hebert
@alexander, @colin there wouldn't be much point in letting a set have additional methods that are not backed by the set interface, it would break the conventions of the collections framework. So additional methods would require an additional (sub) interface and that just wouldn't be justified by the needs of one single set implementation class.
seanizer
@seanizer: That's not really true, there are precedents in the collections framework for class-specific methods that don't break the interface, e.g. `ArrayList.ensureCapacity()`
skaffman
@seanizer. `TreeSet` is still a set, but does have additional methods. If there is a need, there must be a way. The problem with `EnumSet` is really a problem with `Enum`. I would not have minded to coin MyEnumSet for my framework, but because `Enum` is so special I do not have access to its secret sauce ( they even called it SharedSecrets ) the way Java framework developers do.
Alexander Pogrebnyak
@alexander but treeset is backed by the navigablemap interface, which is implemented by more than one class. There are no non-interface public methods in treeset.
seanizer
+10  A: 

You can get an immutable EnumSet with Google collections (Guava).


Resources :

Colin Hebert
Guava is getting to be an awesome library. This rocks! +1
seanizer
Would have accepted it if `Sets.immutableEnumSet` would have returned a public `ImmutableEnumSet` class, not just `ImmutableSet`. Though I will try to make a request to do so.
Alexander Pogrebnyak
What benefit do you see this public ImmutableEnumSet type having, I wonder?
Kevin Bourrillion