Why this fragment of code doesnt work but the others do?

49 Views Asked by At

This dont work: [Only changed the .map()]

return findTechnician.findWithUser()
                          .stream()
                          .map(this::toDTO)
                          .collect(Collectors.toSet());

Error: Method threw 'java.lang.TypeNotPresentException' exception.

Type com.grupoaev.aevgestion.domain.entities.solicitudes.Tecnico not present enter image description here

This is my actual class:

private static class TechniciansModel extends LoadableDetachableModel<Collection<TechnicianDatesDTO>> {

        private static final long serialVersionUID = 1L;

        @SpringBean
        private FindTechnician findTechnician;
        @SpringBean
        private FindTechnicalUserAbsences findTechnicalUserAbsences;

        public TechniciansModel() {
            Injector.get().inject(this);
        }

        @Override
        protected Collection<TechnicianDatesDTO> load() {

            return findTechnician.findWithUser()
                          .stream()
                          .map(t->toDTO(t))
                          .collect(Collectors.toSet());
        }

        @NotNull
        private TechnicianDatesDTO toDTO(Tecnico technician) {
            String displayingName = technician.getUsuario().getName();

            Set<Range<LocalDate>> invalidDates =
                    findTechnicalUserAbsences.findByTecnicoId(technician.getId())
                                             .stream()
                                             .map(absence -> new Range<>(toLocalDate(absence.getFechaInicio()),
                                                                         toLocalDate(absence.getFechaFin())))
                                             .collect(Collectors.toSet());

            return new TechnicianDatesDTO(displayingName, invalidDates);
        }

        private LocalDate toLocalDate(Date date) {
            return date != null ? date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() : null;
        }
    }

I guess this is a deep Java working way problem that i dont understand and i m probably far from it...

Should work the same way as:

return findTechnician.findWithUser()
                          .stream()
                          .map(t->toDTO(t))
                          .collect(Collectors.toSet());
1

There are 1 best solutions below

3
Nidhi257 On

try as below:

return findTechnician.findWithUser()
       .stream()
       .map(TechniciansModel::toDTO)
       .collect(Collectors.toSet());

:: is used with the class name not by the object.