I need to create an EnumSet from a Set. I decided to use the EnumSet#copyOf method. However, because of a restriction on this method:
the specified collection must contain at least one element (in order to determine the new enum set's element type)
I need to ensure that the collection is not empty. The code then becomes:
enum Color {RED, GREEN, BLUE};
Set<Color> set = ... // get it from somewhere
if (set.isEmpty()) {
return EnumSet.noneOf(Color.class);
else
return EnumSet.copyOf(set);
Perhaps there is a real limitation on javac to determine the correct type of members of the collection passed to copyOf
method, but I can't get over the feeling that I have to resort to something like above to cater for the empty collection. Here are my questions then:
Exactly what is the limitation that the empty collection can't be accepted here?
Would a method signature like
copyOf(Collection<Enum<E>>)
have solved this problem?If yes, what other problems would it have created?