Search code examples
javatypesguava

Guava: How to type a List of powersets of generic Enums


I'm writing a function which needs a list of all of the powerSets of three different enums. I'm using Guava's Sets class and I'm having trouble with the typing.

Set<? extends Enum> allSlots = EnumSet.allOf(Slot.class);
Set<Set<? extends Enum>> slotsPS = Sets.powerSet(allSlots);

Set<Set<Day>> daysPS = Sets.powerSet(EnumSet.allOf(Day.class));
Set<Set<Time>> timesPS = Sets.powerSet(EnumSet.allOf(Time.class));
List<Set<Set<? extends Enum>>> allPS = new ArrayList<>();
allPS.add(slotsPS);

I've narrowed it down to Guava's powerSet function as the source of my frustration.

Set<Set<? extends Enum>> slotsPS = Sets.powerSet(allSlots); gives the error:

incompatible types: no instance(s) of type variable(s) E exist so that Set<Set<E>> conforms to Set<Set<? extends Enum>>
  where E is a type-variable:
    E extends Object declared in method <E>powerSet(Set<E>)

Solution

  • Sets.powerSet(allSlots) is a Set<Set<Slot>>, so you'd need to use:

    Set<? extends Set<? extends Enum<?>>>
    

    if you want to drop the actual enum type.