A little bit refactor

This commit is contained in:
Aleksey 2020-12-31 12:51:25 +03:00
parent 5133628dba
commit cef6cf0fd2
3 changed files with 21 additions and 18 deletions

View file

@ -46,7 +46,6 @@ public final class ConfigKeeper {
for (String group: path) { for (String group: path) {
JsonElement element = obj.get(group); JsonElement element = obj.get(group);
if (element == null || !element.isJsonObject()) { if (element == null || !element.isJsonObject()) {
changed = true;
element = new JsonObject(); element = new JsonObject();
obj.add(group, element); obj.add(group, element);
} }
@ -57,7 +56,7 @@ public final class ConfigKeeper {
String paramKey = key.getEntry(); String paramKey = key.getEntry();
paramKey += " [default: " + entry.getDefault() + "]"; paramKey += " [default: " + entry.getDefault() + "]";
changed = entry.setLocation(obj, paramKey) || changed; this.changed = entry.setLocation(obj, paramKey);
} }
private <T, E extends Entry<T>> void storeValue(E entry, T value) { private <T, E extends Entry<T>> void storeValue(E entry, T value) {

View file

@ -8,7 +8,7 @@ public class ConfigKey {
private final boolean root; private final boolean root;
public ConfigKey(String entry, String... path) { public ConfigKey(String entry, String... path) {
validate(entry, path); this.validate(entry);
this.path = path; this.path = path;
this.entry = entry; this.entry = entry;
this.root = path.length == 0 || (path.length == 1 && path[0].isEmpty()); this.root = path.length == 0 || (path.length == 1 && path[0].isEmpty());
@ -74,7 +74,7 @@ public class ConfigKey {
return String.format("%s:%s", p, entry); return String.format("%s:%s", p, entry);
} }
private void validate(String entry, String... path) { private void validate(String entry) {
if (entry == null) { if (entry == null) {
throw new NullPointerException("Config key must be not null!"); throw new NullPointerException("Config key must be not null!");
} }

View file

@ -19,6 +19,10 @@ public class PathConfig extends Config {
return new ConfigKey(key, category.split("\\.")); return new ConfigKey(key, category.split("\\."));
} }
protected ConfigKey createKey(String key) {
return new ConfigKey(key, "");
}
@Nullable @Nullable
public <T, E extends Entry<T>> E getEntry(String category, String key, Class<E> type) { public <T, E extends Entry<T>> E getEntry(String category, String key, Class<E> type) {
return this.getEntry(createKey(category, key), type); return this.getEntry(createKey(category, key), type);
@ -88,58 +92,58 @@ public class PathConfig extends Config {
// From Root // From Root
public String getStringRoot(String key, String defaultValue) { public String getStringRoot(String key, String defaultValue) {
return this.getString(createKey("", key), defaultValue); return this.getString(createKey(key), defaultValue);
} }
public String getStringRoot(String key) { public String getStringRoot(String key) {
return this.getString(createKey("", key)); return this.getString(createKey(key));
} }
public boolean setStringRoot(String key, String value) { public boolean setStringRoot(String key, String value) {
return this.setString(createKey("", key), value); return this.setString(createKey(key), value);
} }
public int getIntRoot(String key, int defaultValue) { public int getIntRoot(String key, int defaultValue) {
return this.getInt(createKey("", key), defaultValue); return this.getInt(createKey(key), defaultValue);
} }
public int getIntRoot(String key) { public int getIntRoot(String key) {
return this.getInt(createKey("", key)); return this.getInt(createKey(key));
} }
public boolean setIntRoot(String key, int value) { public boolean setIntRoot(String key, int value) {
return this.setInt(createKey("", key), value); return this.setInt(createKey(key), value);
} }
public boolean setRangedIntRoot(String key, int value) { public boolean setRangedIntRoot(String key, int value) {
return this.setRanged(createKey("", key), value, IntegerRange.class); return this.setRanged(createKey(key), value, IntegerRange.class);
} }
public boolean setRangedFloatRoot(String key, float value) { public boolean setRangedFloatRoot(String key, float value) {
return this.setRanged(createKey("", key), value, FloatRange.class); return this.setRanged(createKey(key), value, FloatRange.class);
} }
public float getFloatRoot(String key, float defaultValue) { public float getFloatRoot(String key, float defaultValue) {
return this.getFloat(createKey("", key), defaultValue); return this.getFloat(createKey(key), defaultValue);
} }
public float getFloatRoot(String key) { public float getFloatRoot(String key) {
return this.getFloat(createKey("", key)); return this.getFloat(createKey(key));
} }
public boolean setFloatRoot(String key, float value) { public boolean setFloatRoot(String key, float value) {
return this.setFloat(createKey("", key), value); return this.setFloat(createKey(key), value);
} }
public boolean getBooleanRoot(String key, boolean defaultValue) { public boolean getBooleanRoot(String key, boolean defaultValue) {
return this.getBoolean(createKey("", key), defaultValue); return this.getBoolean(createKey(key), defaultValue);
} }
public boolean getBooleanRoot(String key) { public boolean getBooleanRoot(String key) {
return this.getBoolean(createKey("", key)); return this.getBoolean(createKey(key));
} }
public boolean setBooleanRoot(String key, boolean value) { public boolean setBooleanRoot(String key, boolean value) {
return this.setBoolean(createKey("", key), value); return this.setBoolean(createKey(key), value);
} }
} }