From ae8fe0fe1e581a7796fa57cc310fd992dcca4622 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 23 Nov 2017 22:51:05 +0000 Subject: [PATCH] Refactor configurate dao --- .../storage/dao/file/ConfigurateDao.java | 23 ++++++++++-- .../common/storage/dao/file/HoconDao.java | 35 ++++-------------- .../common/storage/dao/file/JsonDao.java | 36 ++++--------------- .../common/storage/dao/file/YamlDao.java | 36 ++++--------------- 4 files changed, 39 insertions(+), 91 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/ConfigurateDao.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/ConfigurateDao.java index 84ddc4bff..af4e6ebf4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/ConfigurateDao.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/ConfigurateDao.java @@ -57,10 +57,12 @@ import me.lucko.luckperms.common.utils.ImmutableCollectors; import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.SimpleConfigurationNode; import ninja.leaping.configurate.Types; +import ninja.leaping.configurate.loader.ConfigurationLoader; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -104,13 +106,21 @@ public abstract class ConfigurateDao extends AbstractDao { this.dataFolderName = dataFolderName; } + protected abstract ConfigurationLoader loader(Path path); + private ConfigurationNode readFile(StorageLocation location, String name) throws IOException { File file = new File(getDirectory(location), name + fileExtension); registerFileAction(location, file); return readFile(file); } - protected abstract ConfigurationNode readFile(File file) throws IOException; + private ConfigurationNode readFile(File file) throws IOException { + if (!file.exists()) { + return null; + } + + return loader(file.toPath()).load(); + } private void saveFile(StorageLocation location, String name, ConfigurationNode node) throws IOException { File file = new File(getDirectory(location), name + fileExtension); @@ -118,7 +128,16 @@ public abstract class ConfigurateDao extends AbstractDao { saveFile(file, node); } - protected abstract void saveFile(File file, ConfigurationNode node) throws IOException; + private void saveFile(File file, ConfigurationNode node) throws IOException { + if (node == null) { + if (file.exists()) { + file.delete(); + } + return; + } + + loader(file.toPath()).save(node); + } private File getDirectory(StorageLocation location) { switch (location) { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/HoconDao.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/HoconDao.java index cd53486dd..4228a62d8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/HoconDao.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/HoconDao.java @@ -29,11 +29,11 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.hocon.HoconConfigurationLoader; +import ninja.leaping.configurate.loader.ConfigurationLoader; -import java.io.File; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; public class HoconDao extends ConfigurateDao { @@ -42,33 +42,10 @@ public class HoconDao extends ConfigurateDao { } @Override - protected ConfigurationNode readFile(File file) throws IOException { - if (!file.exists()) { - return null; - } - - HoconConfigurationLoader loader = HoconConfigurationLoader.builder() - .setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) - .setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) + protected ConfigurationLoader loader(Path path) { + return HoconConfigurationLoader.builder() + .setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8)) + .setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8)) .build(); - - return loader.load(); - } - - @Override - protected void saveFile(File file, ConfigurationNode node) throws IOException { - if (node == null) { - if (file.exists()) { - file.delete(); - } - return; - } - - HoconConfigurationLoader loader = HoconConfigurationLoader.builder() - .setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) - .setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) - .build(); - - loader.save(node); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/JsonDao.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/JsonDao.java index 826a94e2f..d09f74976 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/JsonDao.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/JsonDao.java @@ -29,11 +29,11 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.gson.GsonConfigurationLoader; +import ninja.leaping.configurate.loader.ConfigurationLoader; -import java.io.File; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; public class JsonDao extends ConfigurateDao { @@ -42,35 +42,11 @@ public class JsonDao extends ConfigurateDao { } @Override - protected ConfigurationNode readFile(File file) throws IOException { - if (!file.exists()) { - return null; - } - - GsonConfigurationLoader loader = GsonConfigurationLoader.builder() + protected ConfigurationLoader loader(Path path) { + return GsonConfigurationLoader.builder() .setIndent(2) - .setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) - .setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) + .setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8)) + .setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8)) .build(); - - return loader.load(); - } - - @Override - protected void saveFile(File file, ConfigurationNode node) throws IOException { - if (node == null) { - if (file.exists()) { - file.delete(); - } - return; - } - - GsonConfigurationLoader loader = GsonConfigurationLoader.builder() - .setIndent(2) - .setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) - .setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) - .build(); - - loader.save(node); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/YamlDao.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/YamlDao.java index 70d31af00..ee59d6197 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/YamlDao.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/YamlDao.java @@ -30,12 +30,12 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import org.yaml.snakeyaml.DumperOptions; import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.loader.ConfigurationLoader; import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; -import java.io.File; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; public class YamlDao extends ConfigurateDao { @@ -44,36 +44,12 @@ public class YamlDao extends ConfigurateDao { } @Override - protected ConfigurationNode readFile(File file) throws IOException { - if (!file.exists()) { - return null; - } - - YAMLConfigurationLoader loader = YAMLConfigurationLoader.builder() - .setFlowStyle(DumperOptions.FlowStyle.BLOCK) - .setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) - .setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) - .build(); - - return loader.load(); - } - - @Override - protected void saveFile(File file, ConfigurationNode node) throws IOException { - if (node == null) { - if (file.exists()) { - file.delete(); - } - return; - } - - YAMLConfigurationLoader loader = YAMLConfigurationLoader.builder() + protected ConfigurationLoader loader(Path path) { + return YAMLConfigurationLoader.builder() .setFlowStyle(DumperOptions.FlowStyle.BLOCK) .setIndent(2) - .setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) - .setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) + .setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8)) + .setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8)) .build(); - - loader.save(node); } }