class DefectService {

    void updateDefectAssignations(List<Problem> problems, List<Defect> statuses) {
        final List<Problem> problemsToUpdate = new ArrayList<>();
        for (Problem problem : problems) {
            final Optional<Defect> defect = statuses.stream()
                    .filter(d -> problem.getDefectId().equals(d.getId()))
                    .findFirst();
            if (defect.isPresent()) {
                if (problem.getDefectExistence() != DefectExistence.EXISTS ||
                        problem.getStatus() != defect.get().getStatus() ||
                        !areEqualDownToSeconds(problem.getClosingDateTime(), defect.get().getCloseDateTime())) {
                    problem.setDefectExistence(DefectExistence.EXISTS);
                    problem.setStatus(defect.get().getStatus());
                    problem.setClosingDateTime(defect.get().getCloseDateTime());
                    problemsToUpdate.add(problem);
                }
            } else {
                if (problem.getDefectExistence() != DefectExistence.NONEXISTENT ||
                        problem.getStatus() != ProblemStatus.OPEN ||
                        problem.getClosingDateTime() != null) {
                    problem.setDefectExistence(DefectExistence.NONEXISTENT);
                    problem.setStatus(ProblemStatus.OPEN); // In case defect was closed and removed: it should be acted on
                    problem.setClosingDateTime(null); // Not CLOSED anymore (if it was)
                    problemsToUpdate.add(problem);
                }
            }
        }

        problemRepository.saveAll(problemsToUpdate);
    }

}