From a0b65f341bca680ad048b98303ad6c86dfe591ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Fri, 22 Mar 2019 13:21:32 +0400 Subject: [PATCH] #13 streams refactoring --- .../ru/ulstu/file/service/FileService.java | 27 +++++++++---------- .../ru/ulstu/paper/service/PaperService.java | 16 ++++++----- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 7c40b60..ceb4503 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -11,10 +11,10 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; @@ -84,10 +84,13 @@ public class FileService { fileRepository.delete(fileData); } - public List saveOrCreate(List files) { - return files.stream().map(fileDataDto -> { - return fileDataDto.getId() != null ? update(fileDataDto) : create(fileDataDto); - }).collect(Collectors.toList()); + public List saveOrCreate(List fileDtos) throws IOException { + List files = new ArrayList<>(); + for (FileDataDto file : fileDtos) { + if (file.getId() != null) files.add(update(file)); + else files.add(create(file)); + } + return files; } @Transactional @@ -98,12 +101,9 @@ public class FileService { } @Transactional - public FileData create(FileDataDto fileDataDto) { - FileData newFile = new FileData(); - try { - newFile = createFileFromTmp(fileDataDto.getTmpFileName()); - } catch (IOException e) { - } + public FileData create(FileDataDto fileDataDto) throws IOException { + FileData newFile = null; + newFile = createFileFromTmp(fileDataDto.getTmpFileName()); copyFromDto(newFile, fileDataDto); newFile = fileRepository.save(newFile); return newFile; @@ -115,9 +115,8 @@ public class FileService { } @Transactional - public void delete(Integer fileId) throws IOException { - FileData file = fileRepository.findOne(fileId); - fileRepository.delete(file); + public void delete(Integer fileId) { + fileRepository.delete(fileRepository.findOne(fileId)); } public FileDataDto createFromMultipartFile(MultipartFile multipartFile) throws IOException { diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 7c5df22..89a087d 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -20,8 +20,8 @@ import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; +import static java.util.stream.Collectors.toList; import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.paper.model.Paper.PaperStatus.ATTENTION; @@ -67,7 +67,7 @@ public class PaperService { return findAllDto() .stream() .filter(paper -> paper.getStatus() != COMPLETED && paper.getStatus() != FAILED) - .collect(Collectors.toList()); + .collect(toList()); } public PaperDto findOneDto(Integer id) { @@ -90,7 +90,9 @@ public class PaperService { paper.setTitle(paperDto.getTitle()); paper.setUpdateDate(new Date()); paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines())); - paper.setFiles(fileService.saveOrCreate(paperDto.getFiles().stream().filter(f -> !f.isDeleted()).collect(Collectors.toList()))); + paper.setFiles(fileService.saveOrCreate(paperDto.getFiles().stream() + .filter(f -> !f.isDeleted()) + .collect(toList()))); paper.getAuthors().clear(); if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); @@ -104,7 +106,9 @@ public class PaperService { Paper.PaperStatus oldStatus = paper.getStatus(); Set oldAuthors = new HashSet<>(paper.getAuthors()); - for (FileDataDto file : paperDto.getFiles().stream().filter(f -> f.isDeleted() && f.getId() != null).collect(Collectors.toList())) { + for (FileDataDto file : paperDto.getFiles().stream() + .filter(f -> f.isDeleted() && f.getId() != null) + .collect(toList())) { fileService.delete(file.getId()); } @@ -164,7 +168,7 @@ public class PaperService { return statusCompareResult; } return paper1.getTitle().compareTo(paper2.getTitle()); - }).collect(Collectors.toList()); + }).collect(toList()); } public PaperDto findPaper(int id) { @@ -178,7 +182,7 @@ public class PaperService { && (paper.getStatus() == ON_PREPARATION || paper.getStatus() == DRAFT || paper.getStatus() == ATTENTION)) - .collect(Collectors.toList()); + .collect(toList()); papers.forEach(paper -> { Paper.PaperStatus oldStatus = paper.getStatus(); paper.setStatus(Paper.PaperStatus.FAILED);