From 1088b303994997e63251d4c8f5d8805c787dd7f8 Mon Sep 17 00:00:00 2001 From: A5H73Y Date: Mon, 8 Nov 2021 22:02:38 +0000 Subject: [PATCH 1/2] Small performance improvements Update project dependency versions --- pom.xml | 4 +- src/main/java/de/leonhard/storage/Config.java | 2 - .../storage/internal/DataStorage.java | 2 +- .../leonhard/storage/internal/FileData.java | 17 ++++---- .../internal/editor/toml/TomlReader.java | 7 ++- .../internal/provider/LightningProviders.java | 43 ++++++++----------- .../leonhard/storage/util/ClassWrapper.java | 14 +++--- .../de/leonhard/storage/util/FileUtils.java | 1 - .../java/de/leonhard/storage/ConfigTest.java | 2 +- .../java/de/leonhard/storage/JsonTest.java | 2 +- .../java/de/leonhard/storage/TomlTest.java | 4 +- .../java/de/leonhard/storage/YamlTest.java | 4 +- 12 files changed, 48 insertions(+), 54 deletions(-) diff --git a/pom.xml b/pom.xml index bba654d4..0751d594 100755 --- a/pom.xml +++ b/pom.xml @@ -154,7 +154,7 @@ lombok org.projectlombok provided - 1.18.20 + 1.18.22 @@ -166,7 +166,7 @@ org.junit.jupiter junit-jupiter - 5.8.0 + 5.8.1 test diff --git a/src/main/java/de/leonhard/storage/Config.java b/src/main/java/de/leonhard/storage/Config.java index 187f794a..2a70bf2b 100755 --- a/src/main/java/de/leonhard/storage/Config.java +++ b/src/main/java/de/leonhard/storage/Config.java @@ -14,8 +14,6 @@ @SuppressWarnings({"unused"}) public class Config extends Yaml { - private List header; - public Config(@NonNull final Config config) { super(config); } diff --git a/src/main/java/de/leonhard/storage/internal/DataStorage.java b/src/main/java/de/leonhard/storage/internal/DataStorage.java index f7ac3c9f..d94402fa 100755 --- a/src/main/java/de/leonhard/storage/internal/DataStorage.java +++ b/src/main/java/de/leonhard/storage/internal/DataStorage.java @@ -319,7 +319,7 @@ default T getOrSetDefault(final String key, final T def) { set(key, def); return def; } else { - return (T) ClassWrapper.getFromDef(raw, def); + return ClassWrapper.getFromDef(raw, def); } } } diff --git a/src/main/java/de/leonhard/storage/internal/FileData.java b/src/main/java/de/leonhard/storage/internal/FileData.java index f60bb832..60f2272d 100755 --- a/src/main/java/de/leonhard/storage/internal/FileData.java +++ b/src/main/java/de/leonhard/storage/internal/FileData.java @@ -234,14 +234,13 @@ public Set keySet(final String key) { */ private Set multiLayerKeySet(final Map map) { final Set out = new HashSet<>(); - for (final String key : map.keySet()) { - if (map.get(key) instanceof Map) { - for (final String tempKey : multiLayerKeySet( - (Map) map.get(key))) { - out.add(key + "." + tempKey); + for (final Map.Entry entry : map.entrySet()) { + if (entry.getValue() instanceof Map) { + for (final String tempKey : multiLayerKeySet((Map) entry.getValue())) { + out.add(entry.getKey() + "." + tempKey); } } else { - out.add(key); + out.add(entry.getKey()); } } return out; @@ -307,9 +306,9 @@ public void putAll(final Map map) { private int size(final Map map) { int size = map.size(); - for (final String key : map.keySet()) { - if (map.get(key) instanceof Map) { - size += size((Map) map.get(key)); + for (final Map.Entry entry : map.entrySet()) { + if (entry.getValue() instanceof Map) { + size += size((Map) entry.getValue()); } } return size; diff --git a/src/main/java/de/leonhard/storage/internal/editor/toml/TomlReader.java b/src/main/java/de/leonhard/storage/internal/editor/toml/TomlReader.java index 6c2fbdd2..b175fab6 100644 --- a/src/main/java/de/leonhard/storage/internal/editor/toml/TomlReader.java +++ b/src/main/java/de/leonhard/storage/internal/editor/toml/TomlReader.java @@ -446,7 +446,9 @@ private Map nextTableContent() { } private Object nextNumberOrDate(final char first) { - boolean maybeDouble = true, maybeInteger = true, maybeDate = true; + boolean maybeDouble = true; + boolean maybeInteger = true; + boolean maybeDate = true; final StringBuilder sb = new StringBuilder(); sb.append(first); char c; @@ -614,7 +616,8 @@ private String nextBasicString() { private String nextBasicMultilineString() { final StringBuilder sb = new StringBuilder(); - boolean first = true, escape = false; + boolean first = true; + boolean escape = false; while (hasNext()) { final char c = next(); if (first && (c == '\r' || c == '\n')) { diff --git a/src/main/java/de/leonhard/storage/internal/provider/LightningProviders.java b/src/main/java/de/leonhard/storage/internal/provider/LightningProviders.java index 94f460cc..3f311b52 100644 --- a/src/main/java/de/leonhard/storage/internal/provider/LightningProviders.java +++ b/src/main/java/de/leonhard/storage/internal/provider/LightningProviders.java @@ -24,44 +24,39 @@ public class LightningProviders { private ExceptionHandler exceptionHandler; public MapProvider mapProvider() { - - if (mapProvider != null) { - return mapProvider; + if (mapProvider == null) { + mapProvider = new MapProvider() {}; } - - return mapProvider = new MapProvider() { - }; + return mapProvider; } public YamlConfig yamlConfig() { - if (yamlConfig != null) { - return yamlConfig; + if (yamlConfig == null) { + final YamlConfig config = new YamlConfig(); + // Use unicode + config.writeConfig.setEscapeUnicode(false); + // Don't use anchors + config.writeConfig.setAutoAnchor(false); + // Never use write the classname above keys + config.writeConfig.setWriteClassname(YamlConfig.WriteClassName.NEVER); + yamlConfig = config; } - final YamlConfig config = new YamlConfig(); - // Use unicode - config.writeConfig.setEscapeUnicode(false); - // Don't use anchors - config.writeConfig.setAutoAnchor(false); - // Never use write the classname above keys - config.writeConfig.setWriteClassname(YamlConfig.WriteClassName.NEVER); - return yamlConfig = config; + return yamlConfig; } public InputStreamProvider inputStreamProvider() { - if (inputStreamProvider != null) { - return inputStreamProvider; + if (inputStreamProvider == null) { + inputStreamProvider = new InputStreamProvider() {}; } - return inputStreamProvider = new InputStreamProvider() { - }; + return inputStreamProvider; } public ExceptionHandler exceptionHandler() { - if (exceptionHandler != null) { - return exceptionHandler; + if (exceptionHandler == null) { + exceptionHandler = new ExceptionHandler() {}; } - return exceptionHandler = new ExceptionHandler() { - }; + return exceptionHandler; } } diff --git a/src/main/java/de/leonhard/storage/util/ClassWrapper.java b/src/main/java/de/leonhard/storage/util/ClassWrapper.java index 846df2b0..ce1181d3 100755 --- a/src/main/java/de/leonhard/storage/util/ClassWrapper.java +++ b/src/main/java/de/leonhard/storage/util/ClassWrapper.java @@ -82,7 +82,7 @@ public T getFromDef(final Object obj, final Class clazz) { } @UtilityClass - public class LONG { + public static class LONG { public Long[] getLongArray(final Object obj) { if (obj instanceof List) { @@ -105,7 +105,7 @@ public Long getLong(final Object obj) { } @UtilityClass - public class DOUBLE { + public static class DOUBLE { public Double[] getDoubleArray(final Object obj) { if (obj instanceof List) { @@ -128,7 +128,7 @@ public Double getDouble(final Object obj) { } @UtilityClass - public class FLOAT { + public static class FLOAT { public Float[] getFloatArray(final Object obj) { if (obj instanceof List) { @@ -151,7 +151,7 @@ public Float getFloat(final Object obj) { } @UtilityClass - public class INTEGER { + public static class INTEGER { public Integer[] getIntArray(final Object obj) { if (obj instanceof List) { @@ -175,7 +175,7 @@ public Integer getInt(final Object obj) { @UtilityClass @SuppressWarnings("unused") - public class SHORT { + public static class SHORT { public Short[] getShortArray(final Object obj) { if (obj instanceof List) { @@ -198,7 +198,7 @@ public Short getShort(final Object obj) { } @UtilityClass - public class BYTE { + public static class BYTE { public Byte[] getByteArray(final Object obj) { if (obj instanceof List) { @@ -221,7 +221,7 @@ public Byte getByte(final Object obj) { } @UtilityClass - public class STRING { + public static class STRING { public String[] getStringArray(final Object obj) { if (obj instanceof List) { diff --git a/src/main/java/de/leonhard/storage/util/FileUtils.java b/src/main/java/de/leonhard/storage/util/FileUtils.java index 99fe5ce1..1e520c45 100755 --- a/src/main/java/de/leonhard/storage/util/FileUtils.java +++ b/src/main/java/de/leonhard/storage/util/FileUtils.java @@ -10,7 +10,6 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; diff --git a/src/test/java/de/leonhard/storage/ConfigTest.java b/src/test/java/de/leonhard/storage/ConfigTest.java index a46f8ffc..60d84b6b 100644 --- a/src/test/java/de/leonhard/storage/ConfigTest.java +++ b/src/test/java/de/leonhard/storage/ConfigTest.java @@ -77,7 +77,7 @@ void testSet() { @AfterAll @Test - static void tearDown() { + void tearDown() { config.clear(); Assertions.assertTrue(config.getFile().delete()); } diff --git a/src/test/java/de/leonhard/storage/JsonTest.java b/src/test/java/de/leonhard/storage/JsonTest.java index 7fc7fbd6..4e6d72fc 100644 --- a/src/test/java/de/leonhard/storage/JsonTest.java +++ b/src/test/java/de/leonhard/storage/JsonTest.java @@ -69,7 +69,7 @@ void testSet() { @AfterAll @Test - static void tearDown() { + void tearDown() { json.clear(); Assertions.assertTrue(json.getFile().delete()); } diff --git a/src/test/java/de/leonhard/storage/TomlTest.java b/src/test/java/de/leonhard/storage/TomlTest.java index 9b27bf21..8f8ee569 100644 --- a/src/test/java/de/leonhard/storage/TomlTest.java +++ b/src/test/java/de/leonhard/storage/TomlTest.java @@ -22,7 +22,7 @@ void setUp() { @Test void testGetDataType() { - Assertions.assertEquals(toml.getDataType(), DataType.SORTED); + Assertions.assertEquals(DataType.SORTED, toml.getDataType()); } @Test @@ -69,7 +69,7 @@ void testSet() { @AfterAll @Test - static void tearDown() { + void tearDown() { toml.clear(); Assertions.assertTrue(toml.getFile().delete()); } diff --git a/src/test/java/de/leonhard/storage/YamlTest.java b/src/test/java/de/leonhard/storage/YamlTest.java index 21df4997..b459c5cc 100644 --- a/src/test/java/de/leonhard/storage/YamlTest.java +++ b/src/test/java/de/leonhard/storage/YamlTest.java @@ -26,7 +26,7 @@ void setUp() { @Test void testGetDataType() { - Assertions.assertEquals(yaml.getDataType(), DataType.SORTED); + Assertions.assertEquals(DataType.SORTED, yaml.getDataType()); } @Test @@ -79,7 +79,7 @@ void testSet() { @AfterAll @Test - static void tearDown() { + void tearDown() { yaml.clear(); Assertions.assertTrue(yaml.getFile().delete()); } From 3581c1bf5cdeb6c2e0e81f088c26c0de7096c1a9 Mon Sep 17 00:00:00 2001 From: A5H73Y Date: Tue, 9 Nov 2021 12:11:15 +0000 Subject: [PATCH 2/2] Changes following review --- .../internal/provider/LightningProviders.java | 2 +- .../de/leonhard/storage/util/ClassWrapper.java | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/leonhard/storage/internal/provider/LightningProviders.java b/src/main/java/de/leonhard/storage/internal/provider/LightningProviders.java index 3f311b52..18508a99 100644 --- a/src/main/java/de/leonhard/storage/internal/provider/LightningProviders.java +++ b/src/main/java/de/leonhard/storage/internal/provider/LightningProviders.java @@ -25,7 +25,7 @@ public class LightningProviders { public MapProvider mapProvider() { if (mapProvider == null) { - mapProvider = new MapProvider() {}; + mapProvider = new MapProvider() {}; } return mapProvider; } diff --git a/src/main/java/de/leonhard/storage/util/ClassWrapper.java b/src/main/java/de/leonhard/storage/util/ClassWrapper.java index ce1181d3..846df2b0 100755 --- a/src/main/java/de/leonhard/storage/util/ClassWrapper.java +++ b/src/main/java/de/leonhard/storage/util/ClassWrapper.java @@ -82,7 +82,7 @@ public T getFromDef(final Object obj, final Class clazz) { } @UtilityClass - public static class LONG { + public class LONG { public Long[] getLongArray(final Object obj) { if (obj instanceof List) { @@ -105,7 +105,7 @@ public Long getLong(final Object obj) { } @UtilityClass - public static class DOUBLE { + public class DOUBLE { public Double[] getDoubleArray(final Object obj) { if (obj instanceof List) { @@ -128,7 +128,7 @@ public Double getDouble(final Object obj) { } @UtilityClass - public static class FLOAT { + public class FLOAT { public Float[] getFloatArray(final Object obj) { if (obj instanceof List) { @@ -151,7 +151,7 @@ public Float getFloat(final Object obj) { } @UtilityClass - public static class INTEGER { + public class INTEGER { public Integer[] getIntArray(final Object obj) { if (obj instanceof List) { @@ -175,7 +175,7 @@ public Integer getInt(final Object obj) { @UtilityClass @SuppressWarnings("unused") - public static class SHORT { + public class SHORT { public Short[] getShortArray(final Object obj) { if (obj instanceof List) { @@ -198,7 +198,7 @@ public Short getShort(final Object obj) { } @UtilityClass - public static class BYTE { + public class BYTE { public Byte[] getByteArray(final Object obj) { if (obj instanceof List) { @@ -221,7 +221,7 @@ public Byte getByte(final Object obj) { } @UtilityClass - public static class STRING { + public class STRING { public String[] getStringArray(final Object obj) { if (obj instanceof List) {