refactor: remove obsolete data source infrastructure
- remove legacy DataSource implementations - remove obsolete file and random data source classes
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
package ru.project.datasource;
|
||||
|
||||
import ru.project.student.Student;
|
||||
import ru.project.collection.MyList;
|
||||
|
||||
|
||||
public interface DataSource {
|
||||
MyList<Student> create();
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package ru.project.datasource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.NoSuchElementException;
|
||||
import ru.project.student.Student;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
|
||||
public class FileDataSource implements DataSource{
|
||||
private final String fileName;
|
||||
|
||||
public FileDataSource(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyList<Student> create() {
|
||||
MyList<Student> resultList = new MyLinkedList<>();
|
||||
try {
|
||||
StudentCsvReader reader = new StudentCsvReader(fileName);
|
||||
while (reader.hasNext()) {
|
||||
resultList.add(reader.next());
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
catch (IllegalArgumentException e){
|
||||
System.out.println(e.getMessage());
|
||||
return null;
|
||||
}
|
||||
catch (NoSuchElementException e){
|
||||
System.out.println(e.getMessage() + "No such element");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package ru.project.datasource;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import ru.project.student.Student;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
//import ru.project.collection.Iterator;
|
||||
|
||||
public class FileWriter {
|
||||
private Path path;
|
||||
|
||||
public FileWriter(String absolutePath) {
|
||||
this.path = Path.of(absolutePath);
|
||||
}
|
||||
|
||||
public void write(MyList<Student> iist){
|
||||
//int sizeOfCollection = list.size();
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(
|
||||
path,
|
||||
StandardOpenOption.CREATE,
|
||||
StandardOpenOption.APPEND
|
||||
)) {
|
||||
/*Iterator iterator = list.getIterator();
|
||||
while (iterator.hasNext()){
|
||||
writer.write(iterator.next().toFileString());
|
||||
writer.newLine();
|
||||
}*/
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package ru.project.datasource;
|
||||
|
||||
import ru.project.student.Student;
|
||||
import ru.project.student.StudentBuilder;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomDataSource implements DataSource{
|
||||
private final int size;
|
||||
//private final String CHARS = "АБВГДЕЖХИКЛМНОПРСТУФХЦЧЩЭЮЯ";
|
||||
private int upperBounRecordBookNumber;
|
||||
|
||||
public RandomDataSource(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public void setUpperBounRecordBookNumber(int upperBounRecordBookNumber) {
|
||||
this.upperBounRecordBookNumber = upperBounRecordBookNumber;
|
||||
}
|
||||
|
||||
private String getRandomGroupNumber(Random random){
|
||||
char prefix = (char)(random.nextInt(26) + (int)'A');
|
||||
String result = prefix + String.format("%02d", random.nextInt(100));
|
||||
return result;
|
||||
}
|
||||
/*private String getRandomRecordBookNumber(Random random){
|
||||
StringBuilder buildRes = new StringBuilder(CHARS.charAt(random.nextInt(CHARS.length())));
|
||||
buildRes.append(String.format("%08d", random.nextInt(99999999)));
|
||||
return buildRes.toString();
|
||||
}*/
|
||||
|
||||
@Override
|
||||
public MyList<Student> create() {
|
||||
if (upperBounRecordBookNumber < 0)
|
||||
throw new IllegalArgumentException("The upper bound is negative!!!");
|
||||
Random random = new Random();
|
||||
MyList<Student> result = new MyLinkedList<>();
|
||||
for (short i = 0; i<size; i++){
|
||||
result.add(new StudentBuilder()
|
||||
.setGroupNumber(getRandomGroupNumber(random))
|
||||
.setAverageGrade(Math.random() * 5.0)
|
||||
.setRecordBookNumber(random.nextInt(upperBounRecordBookNumber + 1))
|
||||
.build());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package ru.project.datasource;
|
||||
|
||||
public class StudentBuffer {
|
||||
private String groupNumber;
|
||||
private double averageGrade;
|
||||
private int recordBookNumber;
|
||||
|
||||
public StudentBuffer(String groupNumber, double averageGrade, Integer recordBookNumber) {
|
||||
this.groupNumber = groupNumber;
|
||||
this.averageGrade = averageGrade;
|
||||
this.recordBookNumber = recordBookNumber;
|
||||
}
|
||||
|
||||
public String getGroupNumber() {
|
||||
return groupNumber;
|
||||
}
|
||||
|
||||
public void setGroupNumber(String groupNumber) {
|
||||
this.groupNumber = groupNumber;
|
||||
}
|
||||
|
||||
public double getAverageGrade() {
|
||||
return averageGrade;
|
||||
}
|
||||
|
||||
public void setAverageGrade(double averageGrade) {
|
||||
this.averageGrade = averageGrade;
|
||||
}
|
||||
|
||||
public int getRecordBookNumber() {
|
||||
return recordBookNumber;
|
||||
}
|
||||
|
||||
public void setRecordBookNumber(int recordBookNumber) {
|
||||
this.recordBookNumber = recordBookNumber;
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package ru.project.datasource;
|
||||
|
||||
import org.apache.commons.csv.CSVFormat;
|
||||
import org.apache.commons.csv.CSVPrinter;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import ru.project.student.Student;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
|
||||
|
||||
public class StudentCsvWriter{
|
||||
private static final String[] HEADER = {
|
||||
"groupNumber", "averageGrade", "recordBookNumber"
|
||||
};
|
||||
private static final DateTimeFormatter FILE_NAME_FORMAT =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm");
|
||||
|
||||
private final Path requestedPath;
|
||||
private Path actualPath;
|
||||
|
||||
public StudentCsvWriter(String absoluteFilePath) {
|
||||
Path path = Paths.get(absoluteFilePath);
|
||||
if (!path.isAbsolute()) {
|
||||
throw new IllegalArgumentException("File path must be absolute");
|
||||
}
|
||||
this.requestedPath = path;
|
||||
}
|
||||
|
||||
public void write(Student student) throws IOException {
|
||||
Path target = resolveTargetPath();
|
||||
boolean fileExists = Files.exists(target);
|
||||
|
||||
StandardOpenOption[] options = fileExists
|
||||
? new StandardOpenOption[]{StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND}
|
||||
: new StandardOpenOption[]{StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE};
|
||||
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(target, StandardCharsets.UTF_8, options);
|
||||
CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT)) {
|
||||
if (!fileExists) {
|
||||
printer.printRecord((Object[]) HEADER);
|
||||
}
|
||||
printer.printRecord(
|
||||
student.getGroupNumber(),
|
||||
Double.toString(student.getAverageGrade()),
|
||||
student.getRecordBookNumber()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private Path resolveTargetPath() {
|
||||
if (actualPath != null) {
|
||||
return actualPath;
|
||||
}
|
||||
if (Files.exists(requestedPath)) {
|
||||
actualPath = requestedPath;
|
||||
return actualPath;
|
||||
}
|
||||
|
||||
Path parent = requestedPath.getParent();
|
||||
String fileName = "students-" + LocalDateTime.now().format(FILE_NAME_FORMAT) + ".csv";
|
||||
actualPath = parent == null ? Paths.get(fileName).toAbsolutePath() : parent.resolve(fileName);
|
||||
return actualPath;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user