Add generic DAO with CSV implementation
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
868e640ec6
commit
a453a01ff9
71
0-Plugin/src/main/java/persistence/GenericCSVDAO.java
Normal file
71
0-Plugin/src/main/java/persistence/GenericCSVDAO.java
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
package persistence;
|
||||||
|
|
||||||
|
import persistence.csv.CSVSerializable;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
|
||||||
|
|
||||||
|
private final File file;
|
||||||
|
private final Function<String[], T> constructor;
|
||||||
|
|
||||||
|
public GenericCSVDAO(File file, Function<String[], T> constructor) {
|
||||||
|
this.file = file;
|
||||||
|
this.constructor = constructor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<T> getALl() {
|
||||||
|
var lines = new ArrayList<String>();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(this.file))) {
|
||||||
|
for (String line; (line = br.readLine()) != null; ) {
|
||||||
|
lines.add(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
.stream()
|
||||||
|
.map(line -> constructor.apply(line.split(CSVSerializable.seperator)))
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(T value) {
|
||||||
|
var allEntities = this.getALl();
|
||||||
|
this.removeAll();
|
||||||
|
|
||||||
|
allEntities.stream().filter(entity -> !entity.equals(value)).forEach(this::add);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(T value) {
|
||||||
|
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file, true))) {
|
||||||
|
bufferedWriter.write(value.toCSVString());
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAll() {
|
||||||
|
|
||||||
|
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file, true))) {
|
||||||
|
bufferedWriter.write("");
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
1-Adapter/src/main/java/persistence/GenericDAO.java
Normal file
16
1-Adapter/src/main/java/persistence/GenericDAO.java
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
package persistence;
|
||||||
|
|
||||||
|
import persistence.category.CategoryEntity;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public interface GenericDAO<T> {
|
||||||
|
|
||||||
|
public Set<T> getALl();
|
||||||
|
|
||||||
|
void remove(T value);
|
||||||
|
|
||||||
|
void add(T value);
|
||||||
|
|
||||||
|
}
|
10
1-Adapter/src/main/java/persistence/csv/CSVSerializable.java
Normal file
10
1-Adapter/src/main/java/persistence/csv/CSVSerializable.java
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package persistence.csv;
|
||||||
|
|
||||||
|
public interface CSVSerializable {
|
||||||
|
|
||||||
|
static String seperator = ",";
|
||||||
|
|
||||||
|
String[] getHeaders();
|
||||||
|
|
||||||
|
String toCSVString();
|
||||||
|
}
|
Reference in a new issue