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.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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user