I have the following class structure:
abstract class Role;
class Employee extends Role;
class Student extends Role;
class Employer extends Role;
Also I have an enum in which I store the acceptable roles.
public enum RoleEnum
{
EMPLOYEE ("Employee", Employee.class),
STUDENT ("Student", Student.class),
EMPLOYER ("Employer", Employer.class);
final private String name;
final private Class<? extends Role> pRoleClass;
private RoleEnum(final String name, final Class<? extends Role> pRoleClass)
{
this.name = name;
this.pRoleClass = pRoleClass;
}
}
What I want is to get an array of elements of type Class<? extends Role>
from a list of RoleEnums. I am using FluentIterable from Guava libraries and what I am trying to do is something like FluentIterable.from(list).transform(function).toArray(Class<? extends PartyRole>)
. However what I managed to do is a workaround and kinda of a hack. It looks something like this:
(Class<R>[]) FluentIterable.from(roles)
.transform(new Function<RoleEnum, Class<R>>()
{
@Override public Class<R> apply(final RoleEnum role)
{
return (Class<R>) role.getRoleClass();
}
})
.toList().toArray(new Class[0]));
where
class RoleComboWidget<R extends Role>
is the class from where I call the method and
ImmutableList<RoleEnum> roles;