From b76d18743ec063dddd11414be0f3b39fae05b8ec Mon Sep 17 00:00:00 2001 From: xDec0de_ Date: Tue, 29 Oct 2024 20:09:26 +0100 Subject: [PATCH] Config is now an interface, not an abstract class --- .../net/codersky/mcutils/storage/Config.java | 176 ++++++++---------- .../net/codersky/mcutils/storage/Storage.java | 24 ++- .../storage/files/yaml/YamlConfig.java | 9 +- .../storage/files/yaml/YamlManager.java | 4 - 4 files changed, 109 insertions(+), 104 deletions(-) diff --git a/shared/src/main/java/net/codersky/mcutils/storage/Config.java b/shared/src/main/java/net/codersky/mcutils/storage/Config.java index 60b5890..336c32b 100644 --- a/shared/src/main/java/net/codersky/mcutils/storage/Config.java +++ b/shared/src/main/java/net/codersky/mcutils/storage/Config.java @@ -2,6 +2,7 @@ import net.codersky.mcutils.Reloadable; import net.codersky.mcutils.java.MCCollections; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -14,9 +15,7 @@ import java.util.UUID; import java.util.function.Predicate; -public abstract class Config implements Reloadable { - - protected final HashMap keys = new HashMap<>(); +public interface Config extends Reloadable { /** * Does any necessary tasks in order to set up this {@link Config}. @@ -29,7 +28,7 @@ public abstract class Config implements Reloadable { * * @since MCUtils 1.0.0 */ - public abstract boolean setup(); + boolean setup(); /** * Saves the cached data of this {@link Config} to @@ -43,7 +42,7 @@ public abstract class Config implements Reloadable { * * @since MCUtils 1.0.0 */ - public abstract boolean save(); + boolean save(); /** * Loads the data stored on this config to the cache @@ -57,29 +56,27 @@ public abstract class Config implements Reloadable { * * @since MCUtils 1.0.0 */ - public abstract boolean reload(); + boolean reload(); /* * Key access */ @NotNull - public HashMap getMap() { - return new HashMap<>(keys); - } + HashMap getMap(); - public Set> getEntries() { - return keys.entrySet(); + default Set> getEntries() { + return getMap().entrySet(); } - public Set> getEntries(@NotNull Predicate filter) { + default Set> getEntries(@NotNull Predicate filter) { return MCCollections.clone(getEntries(), entry -> filter.test(entry.getKey())); } @NotNull - public Config removeEntries(@NotNull String... keys) { + default Config removeEntries(@NotNull String... keys) { for (String key : keys) - this.keys.remove(key); + getMap().remove(key); return this; } @@ -100,25 +97,25 @@ public Config removeEntries(@NotNull String... keys) { * @see #removeEntries(String...) * @see #containsKey(String...) */ - public Set getKeys() { - return keys.keySet(); + default Set getKeys() { + return getMap().keySet(); } @NotNull - public Set getKeys(@NotNull Predicate filter) { - return MCCollections.clone(keys.keySet(), filter); + default Set getKeys(@NotNull Predicate filter) { + return MCCollections.clone(getKeys(), filter); } - public boolean containsKey(@NotNull String... keys) { + default boolean containsKey(@NotNull String... keys) { for (String key : keys) - if (!this.keys.containsKey(key)) + if (!getMap().containsKey(key)) return false; return true; } @NotNull - public Config clear() { - keys.clear(); + default Config clear() { + getMap().clear(); return this; } @@ -129,105 +126,107 @@ public Config clear() { // - Utility - // @NotNull - protected T set(@NotNull String key, @NotNull T value) { - keys.put(Objects.requireNonNull(key), Objects.requireNonNull(value)); + @ApiStatus.Internal + default T set(@NotNull String key, @NotNull T value) { + getMap().put(Objects.requireNonNull(key), Objects.requireNonNull(value)); return value; } @NotNull - protected List setList(@NotNull String key, @NotNull List list) { + @ApiStatus.Internal + default List setList(@NotNull String key, @NotNull List list) { Objects.requireNonNull(list); final ArrayList lst = list instanceof ArrayList ? (ArrayList) list : new ArrayList<>(list); - keys.put(Objects.requireNonNull(key), lst); + getMap().put(Objects.requireNonNull(key), lst); return list; } // - Strings - // @NotNull - public String setString(@NotNull String key, @NotNull String value) { + default String setString(@NotNull String key, @NotNull String value) { return set(key, value); } @NotNull - public List setStrings(@NotNull String key, @NotNull List value) { + default List setStrings(@NotNull String key, @NotNull List value) { return setList(key, value); } // - Characters - // - public char setChar(@NotNull String key, char value) { + default char setChar(@NotNull String key, char value) { return set(key, value); } @NotNull - public List setChars(@NotNull String key, @NotNull List value) { + default List setChars(@NotNull String key, @NotNull List value) { return setList(key, value); } // - Booleans - // - public boolean setBoolean(@NotNull String key, boolean value) { + default boolean setBoolean(@NotNull String key, boolean value) { return set(key, value); } @NotNull - public List setBooleans(@NotNull String key, @NotNull List value) { + default List setBooleans(@NotNull String key, @NotNull List value) { return setList(key, value); } // - Integers - // - public int setInt(@NotNull String key, int value) { + default int setInt(@NotNull String key, int value) { return set(key, value); } @NotNull - public List setInts(@NotNull String key, @NotNull List value) { + default List setInts(@NotNull String key, @NotNull List value) { return setList(key, value); } // - Longs - // - public long setLong(@NotNull String key, long value) { + default long setLong(@NotNull String key, long value) { return set(key, value); } @NotNull - public List setLongs(@NotNull String key, @NotNull List value) { + default List setLongs(@NotNull String key, @NotNull List value) { return setList(key, value); } // - Floats - // - public float setFloat(@NotNull String key, float value) { + default float setFloat(@NotNull String key, float value) { return set(key, value); } - public List setFloats(@NotNull String key, @NotNull List value) { + default List setFloats(@NotNull String key, @NotNull List value) { return setList(key, value); } // - Doubles - // - public double setDouble(@NotNull String key, double value) { + default double setDouble(@NotNull String key, double value) { return set(key, value); } @NotNull - public List setDoubles(@NotNull String key, @NotNull List value) { + default List setDoubles(@NotNull String key, @NotNull List value) { return setList(key, value); } // - UUIDs - // @NotNull - public UUID setUUID(@NotNull String key, @NotNull UUID value) { + default UUID setUUID(@NotNull String key, @NotNull UUID value) { return set(key, value); } @NotNull - public List setUUIDs(@NotNull String key, @NotNull List value) { + default List setUUIDs(@NotNull String key, @NotNull List value) { return setList(key, value); } @@ -237,15 +236,17 @@ public List setUUIDs(@NotNull String key, @NotNull List value) { // - Utility - // + @ApiStatus.Internal @SuppressWarnings("unchecked") - protected T get(@NotNull String key, @NotNull Class type) { - final Object obj = keys.get(key); + default T get(@NotNull String key, @NotNull Class type) { + final Object obj = getMap().get(key); return (obj != null && obj.getClass().equals(type)) ? (T) obj : null; } + @ApiStatus.Internal @SuppressWarnings("unchecked") - protected List getList(@NotNull String key, @NotNull Class type) { - final Object obj = keys.get(key); + default List getList(@NotNull String key, @NotNull Class type) { + final Object obj = getMap().get(key); if (obj instanceof ArrayList lst) return lst.getClass().getTypeParameters()[0].getClass().equals(type) ? (List) lst : null; return null; @@ -266,23 +267,23 @@ protected List getList(@NotNull String key, @NotNull Class type) { * @since MCUtils 1.0.0 */ @Nullable - public String getString(@NotNull String key) { + default String getString(@NotNull String key) { return get(key, String.class); } @NotNull - public String getString(@NotNull String key, @NotNull String def) { + default String getString(@NotNull String key, @NotNull String def) { final String str = getString(key); return str == null ? def : str; } @Nullable - public List getStrings(@NotNull String key) { + default List getStrings(@NotNull String key) { return getList(key, String.class); } @NotNull - public List getStrings(@NotNull String key, @NotNull List def) { + default List getStrings(@NotNull String key, @NotNull List def) { final List lst = getStrings(key); return lst == null ? def : lst; } @@ -290,22 +291,22 @@ public List getStrings(@NotNull String key, @NotNull List def) { // - Characters - // @Nullable - public Character getChar(@NotNull String key) { + default Character getChar(@NotNull String key) { return get(key, Character.class); } - public char getChar(@NotNull String key, char def) { + default char getChar(@NotNull String key, char def) { final Character ch = getChar(key); return ch == null ? def : ch; } @Nullable - public List getChars(@NotNull String key) { + default List getChars(@NotNull String key) { return getList(key, Character.class); } @NotNull - public List getChars(@NotNull String key, @NotNull List def) { + default List getChars(@NotNull String key, @NotNull List def) { final List lst = getChars(key); return lst == null ? def : lst; } @@ -313,22 +314,22 @@ public List getChars(@NotNull String key, @NotNull List de // - Booleans - // @Nullable - public Boolean getBoolean(@NotNull String key) { + default Boolean getBoolean(@NotNull String key) { return get(key, Boolean.class); } - public boolean getBoolean(@NotNull String key, boolean def) { + default boolean getBoolean(@NotNull String key, boolean def) { final Boolean bool = getBoolean(key); return bool == null ? def : bool; } @Nullable - public List getBooleans(@NotNull String key) { + default List getBooleans(@NotNull String key) { return getList(key, Boolean.class); } @NotNull - public List getBooleans(@NotNull String key, @NotNull List def) { + default List getBooleans(@NotNull String key, @NotNull List def) { final List lst = getBooleans(key); return lst == null ? def : lst; } @@ -336,22 +337,22 @@ public List getBooleans(@NotNull String key, @NotNull List def // - Integers - // @Nullable - public Integer getInt(@NotNull String key) { + default Integer getInt(@NotNull String key) { return get(key, Integer.class); } - public int getInt(@NotNull String key, int def) { + default int getInt(@NotNull String key, int def) { final Integer i = getInt(key); return i == null ? def : i; } @Nullable - public List getInts(@NotNull String key) { + default List getInts(@NotNull String key) { return getList(key, Integer.class); } @NotNull - public List getInts(@NotNull String key, @NotNull List def) { + default List getInts(@NotNull String key, @NotNull List def) { final List lst = getInts(key); return lst == null ? def : lst; } @@ -359,22 +360,22 @@ public List getInts(@NotNull String key, @NotNull List def) { // - Longs - // @Nullable - public Long getLong(@NotNull String key) { + default Long getLong(@NotNull String key) { return get(key, Long.class); } - public long getLong(@NotNull String key, long def) { + default long getLong(@NotNull String key, long def) { final Long l = getLong(key); return l == null ? def : l; } @Nullable - public List getLongs(@NotNull String key) { + default List getLongs(@NotNull String key) { return getList(key, Long.class); } @NotNull - public List getLongs(@NotNull String key, @NotNull List def) { + default List getLongs(@NotNull String key, @NotNull List def) { final List lst = getLongs(key); return lst == null ? def : lst; } @@ -382,22 +383,22 @@ public List getLongs(@NotNull String key, @NotNull List def) { // - Floats - // @Nullable - public Float getFloat(@NotNull String key) { + default Float getFloat(@NotNull String key) { return get(key, Float.class); } - public float getFloat(@NotNull String key, float def) { + default float getFloat(@NotNull String key, float def) { final Float f = getFloat(key); return f == null ? def : f; } @Nullable - public List getFloats(@NotNull String key) { + default List getFloats(@NotNull String key) { return getList(key, Float.class); } @NotNull - public List getFloats(@NotNull String key, @NotNull List def) { + default List getFloats(@NotNull String key, @NotNull List def) { final List lst = getFloats(key); return lst == null ? def : lst; } @@ -405,22 +406,22 @@ public List getFloats(@NotNull String key, @NotNull List def) { // - Doubles - // @Nullable - public Double getDouble(@NotNull String key) { + default Double getDouble(@NotNull String key) { return get(key, Double.class); } - public double getDouble(@NotNull String key, double def) { + default double getDouble(@NotNull String key, double def) { final Double d = getDouble(key); return d == null ? def : d; } @Nullable - public List getDoubles(@NotNull String key) { + default List getDoubles(@NotNull String key) { return getList(key, Double.class); } @NotNull - public List getDoubles(@NotNull String key, @NotNull List def) { + default List getDoubles(@NotNull String key, @NotNull List def) { final List lst = getDoubles(key); return lst == null ? def : lst; } @@ -428,43 +429,24 @@ public List getDoubles(@NotNull String key, @NotNull List def) { // - UUID - // @Nullable - public UUID getUUID(@NotNull String key) { + default UUID getUUID(@NotNull String key) { return get(key, UUID.class); } @NotNull - public UUID getUUID(@NotNull String key, @NotNull UUID def) { + default UUID getUUID(@NotNull String key, @NotNull UUID def) { final UUID uuid = getUUID(key); return uuid == null ? def : uuid; } @Nullable - public List getUUIDs(@NotNull String key) { + default List getUUIDs(@NotNull String key) { return getList(key, UUID.class); } @NotNull - public List getUUIDs(@NotNull String key, @NotNull List def) { + default List getUUIDs(@NotNull String key, @NotNull List def) { final List lst = getUUIDs(key); return lst == null ? def : lst; } - - /* - * Object class - */ - - @Override - public boolean equals(@Nullable Object obj) { - return obj instanceof final Config cfg && cfg.keys.equals(this.keys); - } - - @Override - public int hashCode() { - return Objects.hashCode(keys); - } - - @Override - public String toString() { - return "Config" + keys; - } } diff --git a/shared/src/main/java/net/codersky/mcutils/storage/Storage.java b/shared/src/main/java/net/codersky/mcutils/storage/Storage.java index 3bdcc24..1ae173e 100644 --- a/shared/src/main/java/net/codersky/mcutils/storage/Storage.java +++ b/shared/src/main/java/net/codersky/mcutils/storage/Storage.java @@ -1,7 +1,9 @@ package net.codersky.mcutils.storage; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Objects; import java.util.UUID; import net.codersky.mcutils.java.MCCollections; @@ -44,7 +46,15 @@ * * @see FlatStorage */ -public abstract class Storage extends Config { +public abstract class Storage implements Config { + + protected final HashMap keys = new HashMap<>(); + + @NotNull + @Override + public HashMap getMap() { + return keys; + } /** * Does any necessary tasks in order to set up this {@link Storage}. @@ -191,8 +201,18 @@ public List getDates(@NotNull String key, @Nullable List def) { * Object class */ + @Override + public boolean equals(@Nullable Object obj) { + return obj instanceof final Config cfg && cfg.getMap().equals(this.getMap()); + } + + @Override + public int hashCode() { + return Objects.hashCode(getMap()); + } + @Override public String toString() { - return "Storage" + keys; + return "Storage" + getMap(); } } diff --git a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlConfig.java b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlConfig.java index 4b6d0fa..fab890b 100644 --- a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlConfig.java +++ b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlConfig.java @@ -6,9 +6,11 @@ import org.jetbrains.annotations.NotNull; import java.io.File; +import java.util.HashMap; -public class YamlConfig extends Config implements Reloadable { +public class YamlConfig implements Config { + protected final HashMap keys = new HashMap<>(); private final YamlManager manager; public YamlConfig(String path) { @@ -42,6 +44,11 @@ public boolean reload() { return manager.reload(keys); } + @Override + public @NotNull HashMap getMap() { + return keys; + } + @Override public boolean save() { return manager.save(keys); diff --git a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlManager.java b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlManager.java index a1097b3..a298eba 100644 --- a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlManager.java +++ b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlManager.java @@ -1,7 +1,6 @@ package net.codersky.mcutils.storage.files.yaml; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -11,14 +10,11 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.Set; class YamlManager {