question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-default EnumSet as a standard collection.

See original GitHub issue

If 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:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
elucashcommented, Aug 14, 2021

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 to withAttribute 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.

0reactions
elucashcommented, Aug 14, 2021

Look, the default templates (without any encodings) discover the Set<Enum> or ImmutableSet<Enum> and use code like this

/// FOR GUAVA
Sets.immutableEnumSet(enumSet.build()),

/// FOR JDK ONLY
  private static <T extends Enum<T>> Set<T> createUnmodifiableEnumSet(Iterable<T> iterable) {
    if (iterable instanceof EnumSet<?>) {
      return Collections.unmodifiableSet(EnumSet.copyOf((EnumSet<T>) iterable));
    }
    List<T> list = createSafeList(iterable, true, false);
    switch(list.size()) {
    case 0: return Collections.emptySet();
    case 1: return Collections.singleton(list.get(0));
    default: return Collections.unmodifiableSet(EnumSet.copyOf(list));
    }
  }

So 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 for EnumSet<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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found