refactor(creation): use stream for file collection creation

This commit is contained in:
2026-09-02 13:08:32 +04:00
parent 5e9eb6b46a
commit d7f4d30cc3
@@ -2,8 +2,11 @@ package ru.project.ui.creation.file;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; 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.collection.MyList;
import ru.project.collectors.MyCollectors;
import ru.project.io.StudentCsvReader; import ru.project.io.StudentCsvReader;
import ru.project.student.Student; import ru.project.student.Student;
import ru.project.ui.creation.FillStrategy; import ru.project.ui.creation.FillStrategy;
@@ -18,20 +21,16 @@ public class FileFillStrategy implements FillStrategy {
@Override @Override
public MyList<Student> create() { public MyList<Student> create() {
MyList<Student> collection = new MyLinkedList<>();
Path path = Path.of(settings.getAbsoluteFilePath()); Path path = Path.of(settings.getAbsoluteFilePath());
try (StudentCsvReader reader = new StudentCsvReader(path)) { try (StudentCsvReader reader = new StudentCsvReader(path)) {
while (reader.hasNext()) { return StreamSupport.stream(
collection.add(reader.next()); Spliterators.spliteratorUnknownSize(reader, Spliterator.ORDERED), false)
} .collect(MyCollectors.toMyList());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Не удалось прочитать коллекцию из файла.", e); throw new RuntimeException("Не удалось прочитать коллекцию из файла.", e);
} }
return collection;
} }
} }