From d807dd8f99a7b06e5fbbb9b5b0a66c65d21c9929 Mon Sep 17 00:00:00 2001 From: Leonhard Date: Tue, 21 Jul 2020 21:13:18 +0200 Subject: [PATCH] Multiple improvements --- pom.xml | 2 +- src/main/java/de/leonhard/storage/Config.java | 3 +- src/main/java/de/leonhard/storage/Json.java | 21 +++++------- src/main/java/de/leonhard/storage/Toml.java | 10 +++--- src/main/java/de/leonhard/storage/Yaml.java | 32 +++++++------------ .../de/leonhard/storage/util/FileUtils.java | 29 +++++++++-------- 6 files changed, 41 insertions(+), 56 deletions(-) diff --git a/pom.xml b/pom.xml index 4285f880..7748580c 100755 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.javafactorydev lightningstorage - 3.1.5 + 3.1.6 LightningStorage Store data in a better way https://github.com/JavaFactoryDev/LightningStorage diff --git a/src/main/java/de/leonhard/storage/Config.java b/src/main/java/de/leonhard/storage/Config.java index 392214c3..78b5928a 100755 --- a/src/main/java/de/leonhard/storage/Config.java +++ b/src/main/java/de/leonhard/storage/Config.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.InputStream; import java.util.List; +import lombok.NonNull; import org.jetbrains.annotations.Nullable; @SuppressWarnings({"unused"}) @@ -13,7 +14,7 @@ public class Config extends Yaml { private List header; - public Config(final Config config) { + public Config(@NonNull final Config config) { super(config); } diff --git a/src/main/java/de/leonhard/storage/Json.java b/src/main/java/de/leonhard/storage/Json.java index fa02d1ab..7c1410b5 100755 --- a/src/main/java/de/leonhard/storage/Json.java +++ b/src/main/java/de/leonhard/storage/Json.java @@ -42,15 +42,12 @@ public Json( @Nullable final ReloadSettings reloadSettings) { super(name, path, FileType.JSON); - if (create() || file.length() == 0) { - if (inputStream != null) { + if (create() || file.length() == 0) + if (inputStream != null) FileUtils.writeToFile(file, inputStream); - } - } - if (reloadSettings != null) { + if (reloadSettings != null) this.reloadSettings = reloadSettings; - } forceReload(); } @@ -73,15 +70,14 @@ public Json(final File file) { @Override public Map getMap(final String key) { final String finalKey = (pathPrefix == null) ? key : pathPrefix + "." + key; - if (!contains(finalKey)) { + if (!contains(finalKey)) return new HashMap<>(); - } else { + else { final Object map = get(key); - if (map instanceof Map) { + if (map instanceof Map) return (Map) fileData.get(key); - } else if (map instanceof JSONObject) { + else if (map instanceof JSONObject) return ((JSONObject) map).toMap(); - } // Exception in casting throw new IllegalArgumentException( "ClassCastEx: Json contains key: '" + key + "' but it is not a Map"); @@ -94,9 +90,8 @@ public Map getMap(final String key) { @Override protected Map readToMap() throws IOException { - if (file.length() == 0) { + if (file.length() == 0) Files.write(file.toPath(), Collections.singletonList("{}")); - } final JSONTokener jsonTokener = new JSONTokener(FileUtils.createInputStream(file)); return new JSONObject(jsonTokener).toMap(); diff --git a/src/main/java/de/leonhard/storage/Toml.java b/src/main/java/de/leonhard/storage/Toml.java index 64aa3549..1bef90e5 100755 --- a/src/main/java/de/leonhard/storage/Toml.java +++ b/src/main/java/de/leonhard/storage/Toml.java @@ -6,15 +6,15 @@ import de.leonhard.storage.internal.editor.toml.TomlManager; import de.leonhard.storage.internal.settings.ReloadSettings; import de.leonhard.storage.util.FileUtils; - import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Map; +import lombok.NonNull; public class Toml extends FlatFile { - public Toml(final Toml toml) { + public Toml(@NonNull final Toml toml) { super(toml.getFile()); fileData = toml.getFileData(); } @@ -34,13 +34,11 @@ public Toml( final ReloadSettings reloadSettings) { super(name, path, FileType.TOML); - if (create() && inputStream != null) { + if (create() && inputStream != null) FileUtils.writeToFile(file, inputStream); - } - if (reloadSettings != null) { + if (reloadSettings != null) this.reloadSettings = reloadSettings; - } forceReload(); } diff --git a/src/main/java/de/leonhard/storage/Yaml.java b/src/main/java/de/leonhard/storage/Yaml.java index f730dc19..3805cad1 100755 --- a/src/main/java/de/leonhard/storage/Yaml.java +++ b/src/main/java/de/leonhard/storage/Yaml.java @@ -11,17 +11,14 @@ import de.leonhard.storage.internal.settings.DataType; import de.leonhard.storage.internal.settings.ReloadSettings; import de.leonhard.storage.util.FileUtils; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Optional; import lombok.Cleanup; import lombok.Getter; +import lombok.NonNull; import lombok.Setter; import org.jetbrains.annotations.Nullable; @@ -34,7 +31,7 @@ public class Yaml extends FlatFile { @Setter private ConfigSettings configSettings = ConfigSettings.SKIP_COMMENTS; - public Yaml(final Yaml yaml) { + public Yaml(@NonNull final Yaml yaml) { super(yaml.getFile()); fileData = yaml.getFileData(); yamlEditor = yaml.getYamlEditor(); @@ -64,26 +61,22 @@ public Yaml( super(name, path, FileType.YAML); this.inputStream = inputStream; - if (create() && inputStream != null) { + if (create() && inputStream != null) FileUtils.writeToFile(file, inputStream); - } yamlEditor = new YamlEditor(file); parser = new YamlParser(yamlEditor); - if (reloadSettings != null) { + if (reloadSettings != null) this.reloadSettings = reloadSettings; - } - if (configSettings != null) { + if (configSettings != null) this.configSettings = configSettings; - } - if (dataType != null) { + if (dataType != null) this.dataType = dataType; - } else { + else this.dataType = DataType.fromConfigSettings(configSettings); - } forceReload(); } @@ -103,9 +96,8 @@ public Yaml addDefaultsFromInputStream() { public Yaml addDefaultsFromInputStream(@Nullable final InputStream inputStream) { reloadIfNeeded(); // Creating & setting defaults - if (inputStream == null) { + if (inputStream == null) return this; - } try { final Map data = new SimpleYamlReader( @@ -113,11 +105,9 @@ public Yaml addDefaultsFromInputStream(@Nullable final InputStream inputStream) final FileData newData = new FileData(data, DataType.UNSORTED); - for (final String key : newData.keySet()) { - if (!fileData.containsKey(key)) { + for (final String key : newData.keySet()) + if (!fileData.containsKey(key)) fileData.insert(key, newData.get(key)); - } - } write(); } catch (final Exception ex) { diff --git a/src/main/java/de/leonhard/storage/util/FileUtils.java b/src/main/java/de/leonhard/storage/util/FileUtils.java index 22936bc5..d4e29b39 100755 --- a/src/main/java/de/leonhard/storage/util/FileUtils.java +++ b/src/main/java/de/leonhard/storage/util/FileUtils.java @@ -1,12 +1,6 @@ package de.leonhard.storage.util; import de.leonhard.storage.internal.provider.LightningProviders; -import lombok.Cleanup; -import lombok.NonNull; -import lombok.SneakyThrows; -import lombok.experimental.UtilityClass; -import org.jetbrains.annotations.Nullable; - import java.io.*; import java.nio.file.Files; import java.nio.file.Path; @@ -18,6 +12,11 @@ import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import lombok.Cleanup; +import lombok.NonNull; +import lombok.SneakyThrows; +import lombok.experimental.UtilityClass; +import org.jetbrains.annotations.Nullable; /** * Class for easier, more convenient & strait interaction with files @@ -60,7 +59,8 @@ public List listFiles( return result; } - public File getAndMake(@NonNull final String name, + public File getAndMake( + @NonNull final String name, @NonNull final String path) { return getAndMake(new File(path, name)); } @@ -107,8 +107,8 @@ public String getParentDirPath(@NonNull final File file) { } /** - * Since file.getParentFile can be null we created an extension function to get the path - * of the parent file + * Since file.getParentFile can be null we created an extension function to get the path of the + * parent file * * @param fileOrDirPath Path to file * @return Path to file as String @@ -219,10 +219,10 @@ private byte[] readAllBytes(@NonNull final File file) { } } - public List readAllLines(final File file) { + public List readAllLines(@NonNull final File file) { final byte[] fileBytes = readAllBytes(file); final String asString = new String(fileBytes); - return new ArrayList<>(Arrays.asList(asString.split("\n"))); + return new ArrayList<>(Arrays.asList(asString.split(System.lineSeparator()))); } // ---------------------------------------------------------------------------------------------------- @@ -312,9 +312,10 @@ public void extractResource( if (target.exists() && !replace) return; - try (final InputStream inputStream = LightningProviders - .inputStreamProvider() - .createInputStreamFromInnerResource(resourcePath)) { + try ( + final InputStream inputStream = LightningProviders + .inputStreamProvider() + .createInputStreamFromInnerResource(resourcePath)) { Valid.notNull( inputStream,