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
2010-08-30 20:05:11
It doesn't have all methods of an `EnumSet`
Colin Hebert
2010-08-30 20:08:33
@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
2010-08-30 20:38:57
@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
2010-08-30 20:44:23
@seanizer, Hum, I didn't noticed that the new methods where static.
Colin Hebert
2010-08-30 20:53:38
@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
2010-08-30 21:12:54
@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
2010-08-30 21:31:52
@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
2010-08-30 21:37:15
@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
2010-08-30 22:01:00
+10
A:
You can get an immutable EnumSet with Google collections (Guava).
Resources :
Colin Hebert
2010-08-30 20:05:16
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
2010-08-30 22:12:06
What benefit do you see this public ImmutableEnumSet type having, I wonder?
Kevin Bourrillion
2010-09-01 23:25:09