1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-05 02:58:20 +08:00

Compare commits

..

5 Commits

Author SHA1 Message Date
carm 727c26a2fb feat(value): 单独提出 ”ValueManifest“ 以解决每个实现类中初始参数复杂的问题,方便后续开发。 2023-03-15 22:24:08 +08:00
dependabot[bot] 9c95a16d90 chore(deps): Bump maven-surefire-plugin from 2.22.2 to 3.0.0 (#26)
Bumps [maven-surefire-plugin](https://github.com/apache/maven-surefire) from 2.22.2 to 3.0.0.
- [Release notes](https://github.com/apache/maven-surefire/releases)
- [Commits](https://github.com/apache/maven-surefire/compare/surefire-2.22.2...surefire-3.0.0)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-surefire-plugin
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-15 14:01:18 +08:00
dependabot[bot] 92c05f1a59 chore(deps): Bump annotations from 24.0.0 to 24.0.1 (#25)
Bumps [annotations](https://github.com/JetBrains/java-annotations) from 24.0.0 to 24.0.1.
- [Release notes](https://github.com/JetBrains/java-annotations/releases)
- [Changelog](https://github.com/JetBrains/java-annotations/blob/master/CHANGELOG.md)
- [Commits](https://github.com/JetBrains/java-annotations/commits)

---
updated-dependencies:
- dependency-name: org.jetbrains:annotations
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-05 12:09:12 +08:00
dependabot[bot] 739ed41885 chore(deps): Bump maven-compiler-plugin from 3.10.1 to 3.11.0 (#24)
Bumps [maven-compiler-plugin](https://github.com/apache/maven-compiler-plugin) from 3.10.1 to 3.11.0.
- [Release notes](https://github.com/apache/maven-compiler-plugin/releases)
- [Commits](https://github.com/apache/maven-compiler-plugin/compare/maven-compiler-plugin-3.10.1...maven-compiler-plugin-3.11.0)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-compiler-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-01 00:07:08 +08:00
carm a66da01996 style: improve code readability. 2023-02-19 19:54:08 +08:00
23 changed files with 305 additions and 245 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.4.0</version> <version>3.5.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
@@ -1,6 +1,7 @@
package cc.carm.lib.configuration.core.builder; package cc.carm.lib.configuration.core.builder;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.ConfigValue; import cc.carm.lib.configuration.core.value.ConfigValue;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -66,4 +67,11 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
return defaults(defaultValueSupplier.get()); return defaults(defaultValueSupplier.get());
} }
protected @NotNull ValueManifest<T> buildManifest() {
return ValueManifest.of(
this.provider, this.path,
this.headerComments, this.inlineComment, this.defaultValue
);
}
} }
@@ -65,9 +65,7 @@ public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, Source
@Override @Override
public @NotNull ConfiguredList<V> build() { public @NotNull ConfiguredList<V> build() {
return new ConfiguredList<>( return new ConfiguredList<>(
this.provider, this.path, buildManifest(), this.valueClass,
this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.sourceParser.andThen(this.valueParser), this.sourceParser.andThen(this.valueParser),
this.valueSerializer.andThen(sourceSerializer) this.valueSerializer.andThen(sourceSerializer)
); );
@@ -2,6 +2,7 @@ package cc.carm.lib.configuration.core.builder.map;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder; import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction; import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.type.ConfiguredMap; import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -96,10 +97,8 @@ public class SourceMapBuilder<M extends Map<K, V>, S, K, V> extends CommonConfig
@Override @Override
public @NotNull ConfiguredMap<K, V> build() { public @NotNull ConfiguredMap<K, V> build() {
return new ConfiguredMap<>( return new ConfiguredMap<>(
this.provider, this.path, new ValueManifest<>(provider, path, headerComments, inlineComment, defaultValue),
this.headerComments, this.inlineComment, this.supplier, this.keyClass, this.keyParser,
this.defaultValue, this.supplier,
this.keyClass, this.keyParser,
this.valueClass, this.sourceParser.andThen(this.valueParser), this.valueClass, this.sourceParser.andThen(this.valueParser),
this.keySerializer, this.valueSerializer.andThen(this.sourceSerializer) this.keySerializer, this.valueSerializer.andThen(this.sourceSerializer)
); );
@@ -44,12 +44,7 @@ public class SectionValueBuilder<V>
@Override @Override
public @NotNull ConfiguredSection<V> build() { public @NotNull ConfiguredSection<V> build() {
return new ConfiguredSection<>( return new ConfiguredSection<>(buildManifest(), this.valueClass, this.parser, this.serializer);
this.provider, this.path,
this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.parser, this.serializer
);
} }
} }
@@ -57,9 +57,7 @@ public class SourceValueBuilder<S, V> extends CommonConfigBuilder<V, SourceValue
@Override @Override
public @NotNull ConfiguredValue<V> build() { public @NotNull ConfiguredValue<V> build() {
return new ConfiguredValue<>( return new ConfiguredValue<>(
this.provider, this.path, buildManifest(), this.valueClass,
this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.valueParser.compose(this.sourceParser), this.valueParser.compose(this.sourceParser),
this.valueSerializer.andThen(sourceSerializer) this.valueSerializer.andThen(sourceSerializer)
); );
@@ -62,6 +62,94 @@ public interface ConfigDataFunction<T, R> {
}; };
} }
@Contract(pure = true)
static <V> @NotNull ConfigDataFunction<String, V> parseString(Class<V> valueClass) {
return (input) -> {
if (valueClass.isInstance(input)) return valueClass.cast(input);
else throw new IllegalArgumentException("Cannot cast string to " + valueClass.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Integer> intValue() {
return (input) -> {
if (input instanceof Integer) {
return (Integer) input;
} else if (input instanceof Number) {
return ((Number) input).intValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Integer.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Short> shortValue() {
return (input) -> {
if (input instanceof Short) {
return (Short) input;
} else if (input instanceof Number) {
return ((Number) input).shortValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Short.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Double> doubleValue() {
return (input) -> {
if (input instanceof Double) {
return (Double) input;
} else if (input instanceof Number) {
return ((Number) input).doubleValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Double.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Byte> byteValue() {
return (input) -> {
if (input instanceof Byte) {
return (Byte) input;
} else if (input instanceof Number) {
return ((Number) input).byteValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Byte.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Float> floatValue() {
return (input) -> {
if (input instanceof Float) {
return (Float) input;
} else if (input instanceof Number) {
return ((Number) input).floatValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Float.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Long> longValue() {
return (input) -> {
if (input instanceof Long) {
return (Long) input;
} else if (input instanceof Number) {
return ((Number) input).longValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Long.class.getName());
};
}
@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Boolean> booleanValue() {
return (input) -> {
if (input instanceof Boolean) {
return (Boolean) input;
} else if (input instanceof String) {
String s = (String) input;
return Boolean.parseBoolean(s) || "yes".equalsIgnoreCase(s);
} else if (input instanceof Integer) {
return ((Integer) input) == 1;
} else throw new IllegalArgumentException("Cannot cast value to " + Boolean.class.getName());
};
}
} }
@@ -90,92 +90,39 @@ public interface ConfigValueParser<T, R> {
}; };
} }
@Contract(pure = true)
static @NotNull <T> ConfigValueParser<T, String> castToString(Class<T> clazz) {
return (input, defaultValue) -> {
if (input instanceof String) return (String) input;
else return input.toString();
};
}
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Integer> intValue() { static @NotNull ConfigValueParser<Object, Integer> intValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.intValue().parse(input);
if (input instanceof Integer) {
return (Integer) input;
} else if (input instanceof Number) {
return ((Number) input).intValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Integer.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Short> shortValue() { static @NotNull ConfigValueParser<Object, Short> shortValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.shortValue().parse(input);
if (input instanceof Short) {
return (Short) input;
} else if (input instanceof Number) {
return ((Number) input).shortValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Short.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Double> doubleValue() { static @NotNull ConfigValueParser<Object, Double> doubleValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.doubleValue().parse(input);
if (input instanceof Double) {
return (Double) input;
} else if (input instanceof Number) {
return ((Number) input).doubleValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Double.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Byte> byteValue() { static @NotNull ConfigValueParser<Object, Byte> byteValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.byteValue().parse(input);
if (input instanceof Byte) {
return (Byte) input;
} else if (input instanceof Number) {
return ((Number) input).byteValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Byte.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Float> floatValue() { static @NotNull ConfigValueParser<Object, Float> floatValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.floatValue().parse(input);
if (input instanceof Float) {
return (Float) input;
} else if (input instanceof Number) {
return ((Number) input).floatValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Float.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Long> longValue() { static @NotNull ConfigValueParser<Object, Long> longValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.longValue().parse(input);
if (input instanceof Long) {
return (Long) input;
} else if (input instanceof Number) {
return ((Number) input).longValue();
} else throw new IllegalArgumentException("Cannot cast value to " + Long.class.getName());
};
} }
@Contract(pure = true) @Contract(pure = true)
static @NotNull ConfigValueParser<Object, Boolean> booleanValue() { static @NotNull ConfigValueParser<Object, Boolean> booleanValue() {
return (input, defaultValue) -> { return (input, defaultValue) -> ConfigDataFunction.booleanValue().parse(input);
if (input instanceof Boolean) {
return (Boolean) input;
} else if (input instanceof String) {
String s = (String) input;
return Boolean.parseBoolean(s) || "yes".equalsIgnoreCase(s);
} else if (input instanceof Integer) {
return ((Integer) input) == 1;
} else throw new IllegalArgumentException("Cannot cast value to " + Boolean.class.getName());
};
} }
} }
@@ -2,62 +2,34 @@ package cc.carm.lib.configuration.core.value;
import cc.carm.lib.configuration.core.builder.ConfigBuilder; import cc.carm.lib.configuration.core.builder.ConfigBuilder;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
public abstract class ConfigValue<T> { public abstract class ConfigValue<T> extends ValueManifest<T> {
public static @NotNull ConfigBuilder builder() { public static @NotNull ConfigBuilder builder() {
return new ConfigBuilder(); return new ConfigBuilder();
} }
protected @Nullable ConfigurationProvider<?> provider; public ConfigValue(@NotNull ValueManifest<T> manifest) {
protected @Nullable String configPath; super(manifest.provider, manifest.configPath, manifest.headerComments, manifest.inlineComment, manifest.defaultValue);
}
protected @Nullable List<String> headerComments;
protected @Nullable String inlineComments;
protected @Nullable T defaultValue;
@Deprecated
public ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath, public ConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments, @Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable T defaultValue) { @Nullable T defaultValue) {
this.provider = provider; super(provider, configPath, headerComments, inlineComments, defaultValue);
this.configPath = configPath;
this.headerComments = headerComments;
this.inlineComments = inlineComments;
this.defaultValue = defaultValue;
} }
public void initialize(@NotNull ConfigurationProvider<?> provider, boolean saveDefault, @NotNull String configPath, public void initialize(@NotNull ConfigurationProvider<?> provider, boolean saveDefault, @NotNull String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments) { @Nullable List<String> headerComments, @Nullable String inlineComments) {
if (this.provider == null) this.provider = provider; this.initialize(provider, configPath, headerComments, inlineComments);
if (this.configPath == null) this.configPath = configPath;
if (this.headerComments == null) this.headerComments = headerComments;
if (this.inlineComments == null) this.inlineComments = inlineComments;
if (saveDefault) setDefault(); if (saveDefault) setDefault();
if (getHeaderComments() != null) {
this.provider.setHeaderComment(getConfigPath(), getHeaderComments());
}
if (getInlineComments() != null) {
this.provider.setInlineComment(getConfigPath(), getInlineComments());
}
}
public @Nullable T getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(@Nullable T defaultValue) {
this.defaultValue = defaultValue;
} }
/** /**
@@ -131,39 +103,4 @@ public abstract class ConfigValue<T> {
else return false; else return false;
} }
public @NotNull ConfigurationProvider<?> getProvider() {
return Optional.ofNullable(this.provider)
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
}
public final @NotNull ConfigurationWrapper<?> getConfiguration() {
try {
return getProvider().getConfiguration();
} catch (Exception ex) {
throw new IllegalStateException("Value(" + configPath + ") has not been initialized", ex);
}
}
public @NotNull String getConfigPath() {
return Optional.ofNullable(this.configPath)
.orElseThrow(() -> new IllegalStateException("No section path provided."));
}
protected Object getValue() {
return getConfiguration().get(getConfigPath());
}
protected void setValue(@Nullable Object value) {
getConfiguration().set(getConfigPath(), value);
}
public @Nullable String getInlineComments() {
return inlineComments;
}
@Unmodifiable
public @Nullable List<String> getHeaderComments() {
return headerComments;
}
} }
@@ -0,0 +1,103 @@
package cc.carm.lib.configuration.core.value;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.List;
import java.util.Optional;
public class ValueManifest<T> {
public static <V> ValueManifest<V> of(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments) {
return new ValueManifest<>(provider, configPath, headerComments, inlineComments, null);
}
public static <V> ValueManifest<V> of(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@Nullable V defaultValue) {
return new ValueManifest<>(provider, configPath, headerComments, inlineComments, defaultValue);
}
protected @Nullable ConfigurationProvider<?> provider;
protected @Nullable String configPath;
protected @Nullable List<String> headerComments;
protected @Nullable String inlineComment;
protected @Nullable T defaultValue;
public ValueManifest(@Nullable ConfigurationProvider<?> provider, @Nullable String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComment,
@Nullable T defaultValue) {
this.provider = provider;
this.configPath = configPath;
this.headerComments = headerComments;
this.inlineComment = inlineComment;
this.defaultValue = defaultValue;
}
protected void initialize(@NotNull ConfigurationProvider<?> provider, @NotNull String configPath,
@Nullable List<String> headerComments, @Nullable String inlineComments) {
if (this.provider == null) this.provider = provider;
if (this.configPath == null) this.configPath = configPath;
if (this.headerComments == null) this.headerComments = headerComments;
if (this.inlineComment == null) this.inlineComment = inlineComments;
if (getHeaderComments() != null) {
this.provider.setHeaderComment(getConfigPath(), getHeaderComments());
}
if (getInlineComment() != null) {
this.provider.setInlineComment(getConfigPath(), getInlineComment());
}
}
public @Nullable T getDefaultValue() {
return this.defaultValue;
}
public void setDefaultValue(@Nullable T defaultValue) {
this.defaultValue = defaultValue;
}
public @NotNull ConfigurationProvider<?> getProvider() {
return Optional.ofNullable(this.provider)
.orElseThrow(() -> new IllegalStateException("Value(" + configPath + ") does not have a provider."));
}
public final @NotNull ConfigurationWrapper<?> getConfiguration() {
try {
return getProvider().getConfiguration();
} catch (Exception ex) {
throw new IllegalStateException("Value(" + configPath + ") has not been initialized", ex);
}
}
public @NotNull String getConfigPath() {
return Optional.ofNullable(this.configPath)
.orElseThrow(() -> new IllegalStateException("No section path provided."));
}
protected Object getValue() {
String path = getConfigPath(); // 当未指定路径时,优先抛出异常
return getConfiguration().get(path);
}
protected void setValue(@Nullable Object value) {
getConfiguration().set(getConfigPath(), value);
}
public @Nullable String getInlineComment() {
return inlineComment;
}
@Unmodifiable
public @Nullable List<String> getHeaderComments() {
return headerComments;
}
}
@@ -1,21 +1,18 @@
package cc.carm.lib.configuration.core.value.impl; package cc.carm.lib.configuration.core.value.impl;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ConfigValue; import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.lib.configuration.core.value.ValueManifest;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
public abstract class CachedConfigValue<T> extends ConfigValue<T> { public abstract class CachedConfigValue<T> extends ConfigValue<T> {
protected @Nullable T cachedValue; protected @Nullable T cachedValue;
protected long parsedTime = -1; protected long parsedTime = -1;
public CachedConfigValue(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath, public CachedConfigValue(@NotNull ValueManifest<T> manifest) {
@Nullable List<String> headerComments, @Nullable String inlineComments, super(manifest);
@Nullable T defaultValue) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
} }
protected T updateCache(T value) { protected T updateCache(T value) {
@@ -2,7 +2,7 @@ package cc.carm.lib.configuration.core.value.type;
import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder; import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction; import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -22,12 +22,10 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
protected final @NotNull ConfigDataFunction<Object, V> parser; protected final @NotNull ConfigDataFunction<Object, V> parser;
protected final @NotNull ConfigDataFunction<V, Object> serializer; protected final @NotNull ConfigDataFunction<V, Object> serializer;
public ConfiguredList(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath, public ConfiguredList(@NotNull ValueManifest<List<V>> manifest, @NotNull Class<V> valueClass,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<V> valueClass, @Nullable List<V> defaultValue,
@NotNull ConfigDataFunction<Object, V> parser, @NotNull ConfigDataFunction<Object, V> parser,
@NotNull ConfigDataFunction<V, Object> serializer) { @NotNull ConfigDataFunction<V, Object> serializer) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue); super(manifest);
this.valueClass = valueClass; this.valueClass = valueClass;
this.parser = parser; this.parser = parser;
this.serializer = serializer; this.serializer = serializer;
@@ -54,6 +52,25 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
else return new ArrayList<>(); else return new ArrayList<>();
} }
@Override
public V get(int index) {
return get().get(index);
}
public <T> @NotNull T modifyValue(Function<List<V>, T> function) {
List<V> list = get();
T result = function.apply(list);
set(list);
return result;
}
public @NotNull List<V> modifyList(Consumer<List<V>> consumer) {
List<V> list = get();
consumer.accept(list);
set(list);
return list;
}
@Override @Override
public void set(@Nullable List<V> value) { public void set(@Nullable List<V> value) {
updateCache(value); updateCache(value);
@@ -72,18 +89,9 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
} }
} }
public <T> @NotNull T modifyValue(Function<List<V>, T> function) { @Override
List<V> list = get(); public V set(int index, V element) {
T result = function.apply(list); return modifyValue(list -> list.set(index, element));
set(list);
return result;
}
public @NotNull List<V> modifyList(Consumer<List<V>> consumer) {
List<V> list = get();
consumer.accept(list);
set(list);
return list;
} }
@Override @Override
@@ -119,6 +127,11 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
return get().toArray(a); return get().toArray(a);
} }
@Override
public boolean containsAll(@NotNull Collection<?> c) {
return new HashSet<>(get()).containsAll(c);
}
@Override @Override
public boolean add(V v) { public boolean add(V v) {
modifyValue(list -> list.add(v)); modifyValue(list -> list.add(v));
@@ -126,13 +139,8 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
} }
@Override @Override
public boolean remove(Object o) { public void add(int index, V element) {
return modifyValue(list -> list.remove(o)); modifyList(list -> list.add(index, element));
}
@Override
public boolean containsAll(@NotNull Collection<?> c) {
return new HashSet<>(get()).containsAll(c);
} }
@Override @Override
@@ -145,6 +153,16 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
return modifyValue(list -> list.addAll(index, c)); return modifyValue(list -> list.addAll(index, c));
} }
@Override
public boolean remove(Object o) {
return modifyValue(list -> list.remove(o));
}
@Override
public V remove(int index) {
return modifyValue(list -> list.remove(index));
}
@Override @Override
public boolean removeAll(@NotNull Collection<?> c) { public boolean removeAll(@NotNull Collection<?> c) {
return modifyValue(list -> list.removeAll(c)); return modifyValue(list -> list.removeAll(c));
@@ -160,26 +178,6 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
modifyList(List::clear); modifyList(List::clear);
} }
@Override
public V get(int index) {
return get().get(index);
}
@Override
public V set(int index, V element) {
return modifyValue(list -> list.set(index, element));
}
@Override
public void add(int index, V element) {
modifyList(list -> list.add(index, element));
}
@Override
public V remove(int index) {
return modifyValue(list -> list.remove(index));
}
@Override @Override
public int indexOf(Object o) { public int indexOf(Object o) {
return get().indexOf(o); return get().indexOf(o);
@@ -2,14 +2,17 @@ package cc.carm.lib.configuration.core.value.type;
import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder; import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction; import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable; import org.jetbrains.annotations.Unmodifiable;
import java.util.*; import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
@@ -32,15 +35,17 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
protected final @NotNull ConfigDataFunction<K, String> keySerializer; protected final @NotNull ConfigDataFunction<K, String> keySerializer;
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer; protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
public ConfiguredMap(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath,
@Nullable List<String> headerComments, @Nullable String inlineComments, public ConfiguredMap(@NotNull ValueManifest<Map<K, V>> manifest
@Nullable Map<K, V> defaultValue, @NotNull Supplier<? extends Map<K, V>> supplier, ,
@NotNull Supplier<? extends Map<K, V>> mapObjSupplier,
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser, @NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser, @NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser,
@NotNull ConfigDataFunction<K, String> keySerializer, @NotNull ConfigDataFunction<K, String> keySerializer,
@NotNull ConfigDataFunction<V, Object> valueSerializer) { @NotNull ConfigDataFunction<V, Object> valueSerializer) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue); super(manifest
this.supplier = supplier; );
this.supplier = mapObjSupplier;
this.keyClass = keyClass; this.keyClass = keyClass;
this.valueClass = valueClass; this.valueClass = valueClass;
this.keyParser = keyParser; this.keyParser = keyParser;
@@ -102,6 +107,11 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
else return supplier.get(); else return supplier.get();
} }
@Override
public V get(Object key) {
return get().get(key);
}
@Override @Override
public void set(@Nullable Map<K, V> value) { public void set(@Nullable Map<K, V> value) {
updateCache(value); updateCache(value);
@@ -156,11 +166,6 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
return get().containsValue(value); return get().containsValue(value);
} }
@Override
public V get(Object key) {
return get().get(key);
}
@Nullable @Nullable
@Override @Override
public V put(K key, V value) { public V put(K key, V value) {
@@ -3,18 +3,18 @@ package cc.carm.lib.configuration.core.value.type;
import cc.carm.lib.configuration.core.builder.value.SectionValueBuilder; import cc.carm.lib.configuration.core.builder.value.SectionValueBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction; import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.function.ConfigValueParser; import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class ConfiguredSection<V> extends CachedConfigValue<V> { public class ConfiguredSection<V> extends CachedConfigValue<V> {
public static <V> @NotNull SectionValueBuilder<V> builder(@NotNull Class<V> valueClass) { public static <V> @NotNull SectionValueBuilder<V> builder(@NotNull Class<V> valueClass) {
return builder().asValue(valueClass).fromSection(); return builder().asValue(valueClass).fromSection();
} }
@@ -24,12 +24,10 @@ public class ConfiguredSection<V> extends CachedConfigValue<V> {
protected final @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser; protected final @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser;
protected final @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer; protected final @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer;
public ConfiguredSection(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath, public ConfiguredSection(@NotNull ValueManifest<V> manifest, @NotNull Class<V> valueClass,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<V> valueClass, @Nullable V defaultValue,
@NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser, @NotNull ConfigValueParser<ConfigurationWrapper<?>, V> parser,
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) { @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
super(provider, sectionPath, headerComments, inlineComments, defaultValue); super(manifest);
this.valueClass = valueClass; this.valueClass = valueClass;
this.parser = parser; this.parser = parser;
this.serializer = serializer; this.serializer = serializer;
@@ -3,12 +3,11 @@ package cc.carm.lib.configuration.core.value.type;
import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder; import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction; import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.function.ConfigValueParser; import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public class ConfiguredValue<V> extends CachedConfigValue<V> { public class ConfiguredValue<V> extends CachedConfigValue<V> {
@@ -30,13 +29,10 @@ public class ConfiguredValue<V> extends CachedConfigValue<V> {
protected final @NotNull ConfigValueParser<Object, V> parser; protected final @NotNull ConfigValueParser<Object, V> parser;
protected final @NotNull ConfigDataFunction<V, Object> serializer; protected final @NotNull ConfigDataFunction<V, Object> serializer;
public ConfiguredValue(@Nullable ConfigurationProvider<?> provider, @Nullable String sectionPath, public ConfiguredValue(@NotNull ValueManifest<V> manifest, @NotNull Class<V> valueClass,
@Nullable List<String> headerComments, @Nullable String inlineComments,
@NotNull Class<V> valueClass, @Nullable V defaultValue,
@NotNull ConfigValueParser<Object, V> parser, @NotNull ConfigValueParser<Object, V> parser,
@NotNull ConfigDataFunction<V, Object> serializer) { @NotNull ConfigDataFunction<V, Object> serializer) {
super(manifest);
super(provider, sectionPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass; this.valueClass = valueClass;
this.parser = parser; this.parser = parser;
this.serializer = serializer; this.serializer = serializer;
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.4.0</version> <version>3.5.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.4.0</version> <version>3.5.0</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.4.0</version> <version>3.5.0</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>3.4.0</version> <version>3.5.0</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -1,12 +1,10 @@
package cc.carm.lib.configuration.yaml; package cc.carm.lib.configuration.yaml;
import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
import cc.carm.lib.configuration.yaml.builder.YAMLConfigBuilder; import cc.carm.lib.configuration.yaml.builder.YAMLConfigBuilder;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public abstract class YAMLValue<T> extends CachedConfigValue<T> { public abstract class YAMLValue<T> extends CachedConfigValue<T> {
@@ -14,10 +12,8 @@ public abstract class YAMLValue<T> extends CachedConfigValue<T> {
return new YAMLConfigBuilder(); return new YAMLConfigBuilder();
} }
public YAMLValue(@Nullable YAMLConfigProvider provider, @Nullable String configPath, public YAMLValue(@NotNull ValueManifest<T> manifest) {
@Nullable List<String> headerComments, @Nullable String inlineComments, super(manifest);
@Nullable T defaultValue) {
super(provider, configPath, headerComments, inlineComments, defaultValue);
} }
public YAMLConfigProvider getYAMLProvider() { public YAMLConfigProvider getYAMLProvider() {
@@ -21,7 +21,7 @@ public class SerializableBuilder<T extends ConfigurationSerializable>
@Override @Override
public @NotNull ConfiguredSerializable<T> build() { public @NotNull ConfiguredSerializable<T> build() {
return new ConfiguredSerializable<>(this.provider, this.path, this.headerComments, this.inlineComment, this.valueClass, this.defaultValue); return new ConfiguredSerializable<>(buildManifest(), this.valueClass);
} }
@@ -1,12 +1,11 @@
package cc.carm.lib.configuration.yaml.value; package cc.carm.lib.configuration.yaml.value;
import cc.carm.lib.configuration.yaml.YAMLConfigProvider; import cc.carm.lib.configuration.core.value.ValueManifest;
import cc.carm.lib.configuration.yaml.YAMLValue; import cc.carm.lib.configuration.yaml.YAMLValue;
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable; import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends YAMLValue<T> { public class ConfiguredSerializable<T extends ConfigurationSerializable> extends YAMLValue<T> {
@@ -22,10 +21,8 @@ public class ConfiguredSerializable<T extends ConfigurationSerializable> extends
protected final @NotNull Class<T> valueClass; protected final @NotNull Class<T> valueClass;
public ConfiguredSerializable(@Nullable YAMLConfigProvider provider, @Nullable String configPath, public ConfiguredSerializable(@NotNull ValueManifest<T> manifest, @NotNull Class<T> valueClass) {
@Nullable List<String> headerComments, @Nullable String inlineComments, super(manifest);
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
super(provider, configPath, headerComments, inlineComments, defaultValue);
this.valueClass = valueClass; this.valueClass = valueClass;
} }
+4 -4
View File
@@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>3.4.0</version> <version>3.5.0</version>
<modules> <modules>
<module>core</module> <module>core</module>
<module>demo</module> <module>demo</module>
@@ -109,7 +109,7 @@
<dependency> <dependency>
<groupId>org.jetbrains</groupId> <groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>24.0.0</version> <version>24.0.1</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
@@ -122,7 +122,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version> <version>3.0.0</version>
<configuration> <configuration>
<useSystemClassLoader>false</useSystemClassLoader> <useSystemClassLoader>false</useSystemClassLoader>
</configuration> </configuration>
@@ -193,7 +193,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version> <version>3.11.0</version>
<configuration> <configuration>
<source>${java.version}</source> <source>${java.version}</source>
<target>${java.version}</target> <target>${java.version}</target>