refactor(creation): use stream for file collection creation
This commit is contained in:
@@ -2,8 +2,11 @@ package ru.project.ui.creation.file;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import java.util.Spliterator;
|
||||
import java.util.Spliterators;
|
||||
import java.util.stream.StreamSupport;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.collectors.MyCollectors;
|
||||
import ru.project.io.StudentCsvReader;
|
||||
import ru.project.student.Student;
|
||||
import ru.project.ui.creation.FillStrategy;
|
||||
@@ -18,20 +21,16 @@ public class FileFillStrategy implements FillStrategy {
|
||||
|
||||
@Override
|
||||
public MyList<Student> create() {
|
||||
MyList<Student> collection = new MyLinkedList<>();
|
||||
|
||||
Path path = Path.of(settings.getAbsoluteFilePath());
|
||||
|
||||
try (StudentCsvReader reader = new StudentCsvReader(path)) {
|
||||
|
||||
while (reader.hasNext()) {
|
||||
collection.add(reader.next());
|
||||
}
|
||||
return StreamSupport.stream(
|
||||
Spliterators.spliteratorUnknownSize(reader, Spliterator.ORDERED), false)
|
||||
.collect(MyCollectors.toMyList());
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Не удалось прочитать коллекцию из файла.", e);
|
||||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user