Auto-default EnumSet as a standard collection.
See original GitHub issueIf I attempt to make an immutable with an EnumSet
field, it will not allow a default build without specifying the EnumSet
or establishing a @Default
. An EnumSet
is standard enough I’d expect it to provide a default empty collection the same way immutables does for Set
, ImmutableSortedMultiset
, OptionalLong
, etc. The performance & GC benefits of native support for EnumSet are even more important in a pervasive immutable world.
demonstration code:
public class EnumTest {
public static enum MyEnum {
FOO, BAR
}
@Immutable
public interface TestImmutableIF {
EnumSet<MyEnum> getVariants();
}
@Test
public void itGetsCollection() {
TestImmutable immutable = TestImmutable.builder().build();
// fails with: Cannot build TestImmutable, some of required attributes are not set [variants]
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Guide to EnumSet - Baeldung
In this tutorial, we'll explore the EnumSet collection from the java.util package and discuss its peculiarities.
Read more >EnumSet (Java Platform SE 8 ) - Oracle Help Center
A specialized Set implementation for use with enum types. All of the elements in an enum set must come from a single enum...
Read more >EnumSet in Java - GeeksforGeeks
EnumSet class is a member of the Java Collections Framework & is not synchronized. It's a high-performance set implementation, much faster than ...
Read more >java - What does EnumSet really mean? - Stack Overflow
An EnumSet is a Set which contains enum instance of a specific enum type, in a more efficient way than other Set implementations...
Read more >How to use EnumSet in Java with Example - Javarevisited
It has an Item on EnumSet, which highlights some typical use-cases for this collection class instead of using int variable and bitwise operator....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The advantage of
ImmutableSetEncoding
encoding (linked above from HubSpot) is to avoid creating a collection builder in some cases where zero-copy assignment will do (if it’s already an ImmutableSet). The default templates in Immutables would force creating set builder every time. That was a tradeoff to make generated code less heavy (for conditions and branches). The idea was that if there’s a need for zero-copy, collections can be passed towithAttribute
at the cost of shallow copy of the immutable object itself. I cannot judge if that was is a good decision, but I’m glad that folks can create optimized encodings which work for their cases.Look, the default templates (without any encodings) discover the
Set<Enum>
orImmutableSet<Enum>
and use code like thisSo you already using immutable enum set and it should be very efficient as you’ve described. Well, the only difference is a single object indirection of an immutable wrapper. I only suggested using encoding if you want mutable
EnumSet
to have all the “goodies” directly, and that encoding you would write specifically forEnumSet<E>
.In addition, in past we have been concerned with efficient handling of interned enum-like objects and have some support for them. You can take a look at module
ordinal
(code and javadocs): https://github.com/immutables/immutables/tree/master/ordinal/src/org/immutables/ordinal