Given data:
I am presented with a collection:
Collection<?> collection
This collection may contain anything. It could be "normal" classes, or it could be enum values.
The class is known at this point in my code. I am given:
Class<?> clazz
Which is guaranteed to be the class of objects contained in the collection. There is no class-mixing.
Goal:
Should the clazz
of the objects in the collection
be any kind of enum, I wish to create an EnumSet
of the objects contained. For my purposes, I could just take the given collection an run with it. However enum sets are just way more efficient, AFAIK.
What I have achieved:
- Determined if I am dealing with an
Enum
type of class by using:
if (Enum.class.isAssignableFrom(clazz)) {
//noinspection unchecked
Class<? extends Enum<?>> enumClass = ((Class<? extends Enum<?>>) clazz);
System.out.println("enumClass: " + enumClass.getSimpleName()); // prints the correct class!
// what now? :-(
}
What I am struggling with:
Anything beyond that, it feels like I have already tried every way of casting things at the wall and seeing what sticks. And when it comes to trying to create a generic EnumSet
, I have never even gotten to the point where my IDE would let me compile.
Answer found
From the answer of @JayC667 below (thank you very much for your effort), I have abstracted the following answer for my purposes:
if (Enum.class.isAssignableFrom(clazz)) {
EnumSet<?> enumSet = collection.isEmpty() ? EnumSet.noneOf(((Class<? extends Enum>) clazz)) : EnumSet.copyOf((Collection<Enum>) collection);
}
I am not sure if this answers your question...
Central piece is
EnumSet.copyOf((Collection<TestEnum1>) collection)
. Code works OK for the test, but I'm not sure where you're going with it, so... good luck ;-)The output is