Java pass class variable inside Diamond operator

albert :

I have a doubt in a Java project:

public class example {

   public Data getData() {
        List<Users> users = usersService.findByClinicId(id);
        Type targetListType = new TypeToken<List<UserDTO>>() {}.getType();
        List<UserDTO> usersDTO = modelMapper.map(users, targetListType);

        List<Personas> profesionales = personasService.findProfesionalsByClinicId(id);
        targetListType = new TypeToken<List<PersonaDTO>>() {}.getType();
        List<PersonaDTO> profesionalesDTO = modelMapper.map(profesionales, targetListType);

        List<Personas> auxiliares = personasService.findAuxiliarsByClinicId(id);
        targetListType = new TypeToken<List<PersonaDTO>>() {}.getType();
        List<PersonaDTO> auxiliaresDTO = modelMapper.map(auxiliares, targetListType);

        List<Prescripciones> prescripciones = prescripcionesService.findProfesionalsByClinicId(id);
        targetListType = new TypeToken<List<PrescriptionNameDTO>>() {}.getType();
        List<PrescriptionNameDTO> prescripcionesDTO = modelMapper.map(prescripciones, targetListType);

       profesionales = entityToDTO(PersonaDTO.class, profesionales);

    ...
   }

    private <T> List<T> entityToDTO(Class<T> clazz, List<T> list) {
        Type targetListType = new TypeToken<List<clazz>>() {}.getType();
        List<PersonaDTO> auxiliaresDTO = modelMapper.map(list, targetListType);

    }
}

The idea is to reduce the code of the getData function using the entityToDTO function.

The problem I have is:

  • In the method entityToDTO, I want to pass in TypeToken first parameter the type of List that I want, but it doesn't accept List, where clazz is the variable that has the Class name.

Is there a way to pass inside the List diamond operator a variable with the type of class I want to use?

UPDATE: The idea is to reduce the number of lines of getData function.

hasnae :

you can update your entityToDTO method as follow :

public static <D, T> List<D> entityToDTO(final Collection<T> sourceList, Class<D> destinationCLass) {
        if (Objects.isNull(sourceList)) {
            return null;
        }
        return sourceList.stream().map(entity -> modelMapper.map(entity, destinationCLass))
            .collect(Collectors.toList());

   }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=114040&siteId=1