Storage Repository : système simplifié

This commit is contained in:
Maximilien LEDOUX
2022-03-11 15:52:02 +01:00
parent 56a8121782
commit 1a3b796f61
2 changed files with 24 additions and 13 deletions

View File

@@ -10,11 +10,22 @@ import java.nio.file.StandardOpenOption;
public class Repository {
private final String filePath;
private final Adapter adapter;
/**
* @param filePath The path where the file must be saved
* @param adapter The service that converts Objects to Strings
* @param filePath The path to the configuration file
* @param adapter The adapter that converts objects to string and vice-versa
*/
static void save(String filePath, Adapter adapter) {
public Repository(String filePath, Adapter adapter) {
this.filePath = filePath;
this.adapter = adapter;
}
/**
* Saves configuration file
*/
public void save() {
if (filePath != null) {
String jsonAppData = adapter.toString();
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
@@ -26,17 +37,18 @@ public class Repository {
}
}
/**
* @param filePath The path where the file is stored
* @param adapter The service that converts Strings to objects
* @return
* Loads configuration file
*
* @return AppData
*/
static AppData load(String filePath, Adapter adapter) {
public AppData load() {
String jsonString = readFile(filePath);
return adapter.fromString(jsonString);
}
private static String readFile(String filePath) {
private String readFile(String filePath) {
StringBuilder builder = new StringBuilder();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8)) {
while (reader.ready()) {