mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
refactor(collection): Refactor builders of collection types (including List and Set).
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>configured-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>4.1.8</version>
|
||||
<version>4.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package cc.carm.lib.configuration.builder.collection;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueType;
|
||||
import cc.carm.lib.configuration.builder.impl.AbstractSectionBuilder;
|
||||
import cc.carm.lib.configuration.function.ValueHandler;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSection;
|
||||
import cc.carm.lib.configuration.value.impl.CollectionConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class SectionCollectionBuilder<
|
||||
V, C extends Collection<V>,
|
||||
RESULT extends CollectionConfigValue<V, C, ?>,
|
||||
SELF extends SectionCollectionBuilder<V, C, RESULT, SELF>
|
||||
> extends AbstractSectionBuilder<C, V, RESULT, SELF> {
|
||||
|
||||
protected @NotNull Supplier<? extends C> constructor;
|
||||
|
||||
public SectionCollectionBuilder(@NotNull Supplier<? extends C> constructor,
|
||||
@NotNull ValueType<V> paramType,
|
||||
@NotNull ValueHandler<ConfigureSection, V> parser,
|
||||
@NotNull ValueHandler<V, ? extends Map<String, Object>> serializer) {
|
||||
super(new ValueType<C>() {
|
||||
}, paramType, parser, serializer);
|
||||
this.constructor = constructor;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final @NotNull SELF defaults(@NotNull V... values) {
|
||||
return defaults(c -> c.addAll(Arrays.asList(values)));
|
||||
}
|
||||
|
||||
public final @NotNull SELF defaults(@NotNull Consumer<C> constructor) {
|
||||
return defaults(() -> {
|
||||
C collection = this.constructor.get();
|
||||
constructor.accept(collection);
|
||||
return collection;
|
||||
});
|
||||
}
|
||||
|
||||
public SELF constructor(@NotNull Supplier<? extends C> constructor) {
|
||||
this.constructor = constructor;
|
||||
return self();
|
||||
}
|
||||
|
||||
public SELF construct(@NotNull C collection) {
|
||||
return constructor(() -> collection);
|
||||
}
|
||||
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package cc.carm.lib.configuration.builder.collection;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueAdapter;
|
||||
import cc.carm.lib.configuration.adapter.ValueType;
|
||||
import cc.carm.lib.configuration.function.ValueHandler;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSection;
|
||||
import cc.carm.lib.configuration.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.value.impl.CollectionConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class SimpleCollectionCreator<V, C extends Collection<V>, RESULT extends CollectionConfigValue<V, C, ?>> {
|
||||
|
||||
public static <V, C extends Collection<V>, RESULT extends CollectionConfigValue<V, C, ?>>
|
||||
@NotNull SimpleCollectionCreator<V, C, RESULT> create(
|
||||
@NotNull ValueType<V> type,
|
||||
@NotNull Supplier<? extends C> defaultConstructor,
|
||||
@NotNull CollectionValueFactory<V, C, RESULT> factory) {
|
||||
return new SimpleCollectionCreator<>(type, defaultConstructor, factory);
|
||||
}
|
||||
|
||||
protected final @NotNull Supplier<? extends C> defaultConstructor;
|
||||
protected final @NotNull ValueType<V> type;
|
||||
|
||||
protected final @NotNull CollectionValueFactory<V, C, RESULT> factory;
|
||||
|
||||
public SimpleCollectionCreator(@NotNull ValueType<V> type,
|
||||
@NotNull Supplier<? extends C> defaultConstructor,
|
||||
@NotNull CollectionValueFactory<V, C, RESULT> factory) {
|
||||
this.defaultConstructor = defaultConstructor;
|
||||
this.type = type;
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public <S> @NotNull Source<S, V, C, RESULT> from(@NotNull Class<S> sourceType) {
|
||||
return from(ValueType.of(sourceType));
|
||||
}
|
||||
|
||||
public <S> @NotNull Source<S, V, C, RESULT> from(@NotNull ValueType<S> sourceType) {
|
||||
return new Source<S, V, C, RESULT>(
|
||||
defaultConstructor, sourceType, type,
|
||||
ValueHandler.required(type),
|
||||
ValueHandler.required(sourceType),
|
||||
factory
|
||||
);
|
||||
}
|
||||
|
||||
public @NotNull SimpleCollectionCreator.Source<Object, V, C, RESULT> fromObject() {
|
||||
return new Source<Object, V, C, RESULT>(
|
||||
defaultConstructor, ValueType.OBJECT, type,
|
||||
ValueHandler.deserialize(type), ValueHandler.toObject(),
|
||||
factory
|
||||
);
|
||||
}
|
||||
|
||||
public @NotNull SimpleCollectionCreator.Source<String, V, C, RESULT> fromString() {
|
||||
return new Source<String, V, C, RESULT>(
|
||||
defaultConstructor, ValueType.STRING, type,
|
||||
ValueHandler.required(type), ValueHandler.stringValue(),
|
||||
factory
|
||||
);
|
||||
}
|
||||
|
||||
public @NotNull SimpleCollectionCreator.Section<V, C, RESULT> fromSection() {
|
||||
return new Section<V, C, RESULT>(
|
||||
defaultConstructor, type,
|
||||
ValueHandler.required(type), ValueHandler.required(),
|
||||
factory
|
||||
);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CollectionValueFactory<V, C, RESULT> {
|
||||
@NotNull RESULT build(
|
||||
@NotNull ValueManifest<C, V> manifest,
|
||||
@NotNull Supplier<? extends C> constructor,
|
||||
@NotNull ValueAdapter<V> paramAdapter
|
||||
);
|
||||
}
|
||||
|
||||
public static class Source<SOURCE, V, C extends Collection<V>, RESULT extends CollectionConfigValue<V, C, ?>>
|
||||
extends SourceCollectionBuilder<SOURCE, V, C, RESULT, Source<SOURCE, V, C, RESULT>> {
|
||||
|
||||
protected final @NotNull CollectionValueFactory<V, C, RESULT> factory;
|
||||
|
||||
public Source(
|
||||
@NotNull Supplier<? extends C> constructor,
|
||||
@NotNull ValueType<SOURCE> sourceType,
|
||||
@NotNull ValueType<V> paramType,
|
||||
@NotNull ValueHandler<SOURCE, V> parser,
|
||||
@NotNull ValueHandler<V, SOURCE> serializer,
|
||||
@NotNull CollectionValueFactory<V, C, RESULT> factory) {
|
||||
super(constructor, sourceType, paramType, parser, serializer);
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull SimpleCollectionCreator.Source<SOURCE, V, C, RESULT> self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull RESULT build() {
|
||||
return factory.build(buildManifest(), constructor, buildAdapter());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Section<V, C extends Collection<V>, RESULT extends CollectionConfigValue<V, C, ?>>
|
||||
extends SectionCollectionBuilder<V, C, RESULT, Section<V, C, RESULT>> {
|
||||
protected final @NotNull CollectionValueFactory<V, C, RESULT> factory;
|
||||
|
||||
public Section(
|
||||
@NotNull Supplier<? extends C> constructor,
|
||||
@NotNull ValueType<V> paramType,
|
||||
@NotNull ValueHandler<ConfigureSection, V> parser,
|
||||
@NotNull ValueHandler<V, ? extends Map<String, Object>> serializer,
|
||||
@NotNull CollectionValueFactory<V, C, RESULT> factory) {
|
||||
super(constructor, paramType, parser, serializer);
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull SimpleCollectionCreator.Section<V, C, RESULT> self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull RESULT build() {
|
||||
return factory.build(buildManifest(), constructor, buildAdapter());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package cc.carm.lib.configuration.builder.collection;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueType;
|
||||
import cc.carm.lib.configuration.builder.impl.AbstractSourceBuilder;
|
||||
import cc.carm.lib.configuration.function.ValueHandler;
|
||||
import cc.carm.lib.configuration.value.impl.CollectionConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class SourceCollectionBuilder<
|
||||
SOURCE, V, C extends Collection<V>,
|
||||
RESULT extends CollectionConfigValue<V, C, ?>,
|
||||
SELF extends SourceCollectionBuilder<SOURCE, V, C, RESULT, SELF>
|
||||
>
|
||||
extends AbstractSourceBuilder<C, SOURCE, V, RESULT, SELF> {
|
||||
|
||||
protected @NotNull Supplier<? extends C> constructor;
|
||||
|
||||
public SourceCollectionBuilder(@NotNull Supplier<? extends C> constructor,
|
||||
@NotNull ValueType<SOURCE> sourceType, @NotNull ValueType<V> paramType,
|
||||
@NotNull ValueHandler<SOURCE, V> parser, @NotNull ValueHandler<V, SOURCE> serializer) {
|
||||
super(new ValueType<C>() {
|
||||
}, sourceType, paramType, parser, serializer);
|
||||
this.constructor = constructor;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final @NotNull SELF defaults(@NotNull V... values) {
|
||||
return defaults(c -> c.addAll(Arrays.asList(values)));
|
||||
}
|
||||
|
||||
public final @NotNull SELF defaults(@NotNull Consumer<C> constructor) {
|
||||
return defaults(() -> {
|
||||
C collection = this.constructor.get();
|
||||
constructor.accept(collection);
|
||||
return collection;
|
||||
});
|
||||
}
|
||||
|
||||
public SELF constructor(@NotNull Supplier<? extends C> constructor) {
|
||||
this.constructor = constructor;
|
||||
return self();
|
||||
}
|
||||
|
||||
public SELF construct(@NotNull C collection) {
|
||||
return constructor(() -> collection);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package cc.carm.lib.configuration.builder.list;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueType;
|
||||
import cc.carm.lib.configuration.function.ValueHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ConfigListBuilder<V> {
|
||||
|
||||
protected final @NotNull ValueType<V> type;
|
||||
|
||||
public ConfigListBuilder(@NotNull ValueType<V> type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public <S> @NotNull SourceListBuilder<S, V> from(@NotNull Class<S> sourceType) {
|
||||
return from(ValueType.of(sourceType));
|
||||
}
|
||||
|
||||
public <S> @NotNull SourceListBuilder<S, V> from(@NotNull ValueType<S> sourceType) {
|
||||
return new SourceListBuilder<>(
|
||||
ArrayList::new, sourceType, type,
|
||||
ValueHandler.required(type),
|
||||
ValueHandler.required(sourceType)
|
||||
);
|
||||
}
|
||||
|
||||
public @NotNull SourceListBuilder<Object, V> fromObject() {
|
||||
return new SourceListBuilder<>(
|
||||
ArrayList::new, ValueType.OBJECT, type,
|
||||
ValueHandler.deserialize(type), ValueHandler.toObject()
|
||||
);
|
||||
}
|
||||
|
||||
public @NotNull SourceListBuilder<String, V> fromString() {
|
||||
return new SourceListBuilder<>(
|
||||
ArrayList::new, ValueType.STRING, type,
|
||||
ValueHandler.required(type), ValueHandler.stringValue()
|
||||
);
|
||||
}
|
||||
|
||||
public @NotNull SectionListBuilder<V> fromSection() {
|
||||
return new SectionListBuilder<>(
|
||||
ArrayList::new, type,
|
||||
ValueHandler.required(type), ValueHandler.required()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cc.carm.lib.configuration.builder.list;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueType;
|
||||
import cc.carm.lib.configuration.builder.collection.SimpleCollectionCreator;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigListCreator<V> extends SimpleCollectionCreator<V, List<V>, ConfiguredList<V>> {
|
||||
|
||||
public ConfigListCreator(@NotNull ValueType<V> type) {
|
||||
super(type, ArrayList::new, ConfiguredList::new);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package cc.carm.lib.configuration.builder.list;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueType;
|
||||
import cc.carm.lib.configuration.builder.impl.AbstractSectionBuilder;
|
||||
import cc.carm.lib.configuration.function.ValueHandler;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSection;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class SectionListBuilder<V>
|
||||
extends AbstractSectionBuilder<List<V>, V, ConfiguredList<V>, SectionListBuilder<V>> {
|
||||
|
||||
protected @NotNull Supplier<? extends List<V>> constructor;
|
||||
|
||||
public SectionListBuilder(@NotNull Supplier<? extends List<V>> constructor,
|
||||
@NotNull ValueType<V> paramType,
|
||||
@NotNull ValueHandler<ConfigureSection, V> parser,
|
||||
@NotNull ValueHandler<V, ? extends Map<String, Object>> serializer) {
|
||||
super(new ValueType<List<V>>() {
|
||||
}, paramType, parser, serializer);
|
||||
this.constructor = constructor;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final @NotNull SectionListBuilder<V> defaults(@NotNull V... values) {
|
||||
return defaults(new ArrayList<>(Arrays.asList(values)));
|
||||
}
|
||||
|
||||
public final @NotNull SectionListBuilder<V> defaults(@NotNull Collection<V> values) {
|
||||
return defaults(new ArrayList<>(values));
|
||||
}
|
||||
|
||||
public final @NotNull SectionListBuilder<V> defaults(@NotNull Consumer<List<V>> constructor) {
|
||||
return defaults(() -> {
|
||||
List<V> list = new ArrayList<>();
|
||||
constructor.accept(list);
|
||||
return list;
|
||||
});
|
||||
}
|
||||
|
||||
public SectionListBuilder<V> constructor(@NotNull Supplier<? extends List<V>> constructor) {
|
||||
this.constructor = constructor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public <LIST extends List<V>> SectionListBuilder<V> construct(@NotNull LIST list) {
|
||||
return constructor(() -> list);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull SectionListBuilder<V> self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredList<V> build() {
|
||||
return new ConfiguredList<>(buildManifest(), constructor, buildAdapter());
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package cc.carm.lib.configuration.builder.list;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueType;
|
||||
import cc.carm.lib.configuration.builder.impl.AbstractSourceBuilder;
|
||||
import cc.carm.lib.configuration.function.ValueHandler;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class SourceListBuilder<SOURCE, V>
|
||||
extends AbstractSourceBuilder<List<V>, SOURCE, V, ConfiguredList<V>, SourceListBuilder<SOURCE, V>> {
|
||||
|
||||
protected @NotNull Supplier<? extends List<V>> constructor;
|
||||
|
||||
public SourceListBuilder(@NotNull Supplier<? extends List<V>> constructor,
|
||||
@NotNull ValueType<SOURCE> sourceType, @NotNull ValueType<V> paramType,
|
||||
@NotNull ValueHandler<SOURCE, V> parser, @NotNull ValueHandler<V, SOURCE> serializer) {
|
||||
super(new ValueType<List<V>>() {
|
||||
}, sourceType, paramType, parser, serializer);
|
||||
this.constructor = constructor;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final @NotNull SourceListBuilder<SOURCE, V> defaults(@NotNull V... values) {
|
||||
return defaults(new ArrayList<>(Arrays.asList(values)));
|
||||
}
|
||||
|
||||
public final @NotNull SourceListBuilder<SOURCE, V> defaults(@NotNull Collection<V> values) {
|
||||
return defaults(new ArrayList<>(values));
|
||||
}
|
||||
|
||||
public final @NotNull SourceListBuilder<SOURCE, V> defaults(@NotNull Consumer<List<V>> constructor) {
|
||||
return defaults(() -> {
|
||||
List<V> list = new ArrayList<>();
|
||||
constructor.accept(list);
|
||||
return list;
|
||||
});
|
||||
}
|
||||
|
||||
public SourceListBuilder<SOURCE, V> constructor(@NotNull Supplier<? extends List<V>> constructor) {
|
||||
this.constructor = constructor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public <LIST extends List<V>> SourceListBuilder<SOURCE, V> construct(@NotNull LIST list) {
|
||||
return constructor(() -> list);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull SourceListBuilder<SOURCE, V> self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredList<V> build() {
|
||||
return new ConfiguredList<>(buildManifest(), this.constructor, buildAdapter());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package cc.carm.lib.configuration.value.impl;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueAdapter;
|
||||
import cc.carm.lib.configuration.adapter.ValueParser;
|
||||
import cc.carm.lib.configuration.adapter.ValueSerializer;
|
||||
import cc.carm.lib.configuration.adapter.ValueType;
|
||||
import cc.carm.lib.configuration.value.ValueManifest;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Base implementation of a collection config value, like {@link List} or {@link Set}.
|
||||
*
|
||||
* @param <V> Value type
|
||||
* @param <C> Collection type
|
||||
* @param <SELF> Self reference type (used for internal call or recursive generics)
|
||||
*/
|
||||
public abstract class CollectionConfigValue<
|
||||
V, C extends Collection<V>,
|
||||
SELF extends CollectionConfigValue<V, C, SELF>
|
||||
> extends CachedConfigValue<C, V> implements Collection<V> {
|
||||
|
||||
protected final @NotNull Supplier<? extends C> constructor;
|
||||
protected final @NotNull ValueAdapter<V> paramAdapter;
|
||||
|
||||
public CollectionConfigValue(@NotNull ValueManifest<C, V> manifest,
|
||||
@NotNull Supplier<? extends C> constructor,
|
||||
@NotNull ValueAdapter<V> paramAdapter) {
|
||||
super(manifest);
|
||||
this.constructor = constructor;
|
||||
this.paramAdapter = paramAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Adapter of this value.
|
||||
*/
|
||||
public @NotNull ValueAdapter<V> adapter() {
|
||||
return this.paramAdapter;
|
||||
}
|
||||
|
||||
public @NotNull ValueType<V> paramType() {
|
||||
return adapter().type();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Value's parser, parse base object to value.
|
||||
*/
|
||||
public @Nullable ValueParser<V> parser() {
|
||||
return parserFor(adapter());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Value's serializer, parse value to base object.
|
||||
*/
|
||||
public @Nullable ValueSerializer<V> serializer() {
|
||||
return serializerFor(adapter());
|
||||
}
|
||||
|
||||
private @NotNull C createCollection() {
|
||||
return constructor.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull C get() {
|
||||
if (!cacheExpired()) return getCachedOrDefault(createCollection());
|
||||
// Data that is outdated and needs to be parsed again.
|
||||
C set = createCollection();
|
||||
try {
|
||||
List<?> data = config().contains(path()) ? config().getList(path()) : null;
|
||||
if (data == null) return getDefaultFirst(set);
|
||||
|
||||
ValueParser<V> parser = parser();
|
||||
if (parser == null) return getDefaultFirst(set);
|
||||
|
||||
int i = 0;
|
||||
for (Object dataVal : data) {
|
||||
if (dataVal == null) continue;
|
||||
try {
|
||||
set.add(withValidated(parser.parse(holder(), paramType(), dataVal)));
|
||||
} catch (Exception e) {
|
||||
throwing(path + "[" + i + "]", e);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throwing(ex);
|
||||
}
|
||||
return updateCache(set);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@Nullable C collection) {
|
||||
updateCache(collection);
|
||||
if (collection == null) {
|
||||
setData(null);
|
||||
return;
|
||||
}
|
||||
|
||||
ValueSerializer<V> serializer = serializer();
|
||||
if (serializer == null) return;
|
||||
|
||||
List<Object> data = new ArrayList<>();
|
||||
for (V val : collection) {
|
||||
if (val == null) continue;
|
||||
try {
|
||||
data.add(serializer.serialize(holder(), paramType(), withValidated(val)));
|
||||
} catch (Exception ex) {
|
||||
throwing(ex);
|
||||
}
|
||||
}
|
||||
setData(data);
|
||||
}
|
||||
|
||||
public @NotNull C copy() {
|
||||
C other = createCollection();
|
||||
other.addAll(resolve());
|
||||
return other;
|
||||
}
|
||||
|
||||
public abstract @NotNull SELF self();
|
||||
|
||||
public <T> @NotNull T handle(Function<C, T> function) {
|
||||
C list = resolve();
|
||||
T result = function.apply(list);
|
||||
set(list);
|
||||
return result;
|
||||
}
|
||||
|
||||
public @NotNull SELF modify(Consumer<C> consumer) {
|
||||
C list = resolve();
|
||||
consumer.accept(list);
|
||||
set(list);
|
||||
return self();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return resolve().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return resolve().isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return resolve().contains(o);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<V> iterator() {
|
||||
return resolve().iterator();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Object @NotNull [] toArray() {
|
||||
return resolve().toArray();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T> T @NotNull [] toArray(@NotNull T[] a) {
|
||||
return resolve().toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(@NotNull Collection<?> c) {
|
||||
return new HashSet<>(resolve()).containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(V v) {
|
||||
handle(list -> list.add(v));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NotNull Collection<? extends V> c) {
|
||||
return handle(list -> list.addAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return handle(list -> list.remove(o));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(@NotNull Collection<?> c) {
|
||||
return handle(list -> list.removeAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(@NotNull Collection<?> c) {
|
||||
return handle(list -> list.retainAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
modify(Collection::clear);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +1,33 @@
|
||||
package cc.carm.lib.configuration.value.standard;
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueAdapter;
|
||||
import cc.carm.lib.configuration.adapter.ValueParser;
|
||||
import cc.carm.lib.configuration.adapter.ValueSerializer;
|
||||
import cc.carm.lib.configuration.adapter.ValueType;
|
||||
import cc.carm.lib.configuration.builder.list.ConfigListBuilder;
|
||||
import cc.carm.lib.configuration.builder.list.SourceListBuilder;
|
||||
import cc.carm.lib.configuration.builder.collection.SimpleCollectionCreator;
|
||||
import cc.carm.lib.configuration.builder.list.ConfigListCreator;
|
||||
import cc.carm.lib.configuration.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.value.impl.CachedConfigValue;
|
||||
import cc.carm.lib.configuration.value.impl.CollectionConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ConfiguredList<V> extends CachedConfigValue<List<V>, V> implements List<V> {
|
||||
public class ConfiguredList<V> extends CollectionConfigValue<V, List<V>, ConfiguredList<V>> implements List<V> {
|
||||
|
||||
public static <T> @NotNull ConfigListBuilder<T> builderOf(@NotNull Class<T> type) {
|
||||
public static <T> @NotNull ConfigListCreator<T> builderOf(@NotNull Class<T> type) {
|
||||
return builderOf(ValueType.of(type));
|
||||
}
|
||||
|
||||
public static <T> @NotNull ConfigListBuilder<T> builderOf(@NotNull ValueType<T> type) {
|
||||
return new ConfigListBuilder<>(type);
|
||||
public static <T> @NotNull ConfigListCreator<T> builderOf(@NotNull ValueType<T> type) {
|
||||
return new ConfigListCreator<>(type);
|
||||
}
|
||||
|
||||
public static <T> @NotNull SourceListBuilder<Object, T> with(@NotNull Class<T> registeredType) {
|
||||
public static <T>
|
||||
@NotNull SimpleCollectionCreator.Source<Object, T, List<T>, ConfiguredList<T>> with(@NotNull Class<T> registeredType) {
|
||||
return with(ValueType.of(registeredType));
|
||||
}
|
||||
|
||||
public static <T> @NotNull SourceListBuilder<Object, T> with(@NotNull ValueType<T> registeredType) {
|
||||
return new ConfigListBuilder<>(registeredType).fromObject();
|
||||
public static <T> @NotNull SimpleCollectionCreator.Source<Object, T, List<T>, ConfiguredList<T>> with(@NotNull ValueType<T> registeredType) {
|
||||
return builderOf(registeredType).fromObject();
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
@@ -42,94 +38,10 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>, V> implements
|
||||
return with(ValueType.of(value)).defaults(list).build();
|
||||
}
|
||||
|
||||
protected final @NotNull Supplier<? extends List<V>> constructor;
|
||||
protected final @NotNull ValueAdapter<V> paramAdapter;
|
||||
|
||||
public ConfiguredList(@NotNull ValueManifest<List<V>, V> manifest,
|
||||
@NotNull Supplier<? extends List<V>> constructor,
|
||||
@NotNull ValueAdapter<V> paramAdapter) {
|
||||
super(manifest);
|
||||
this.constructor = constructor;
|
||||
this.paramAdapter = paramAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Adapter of this value.
|
||||
*/
|
||||
public @NotNull ValueAdapter<V> adapter() {
|
||||
return this.paramAdapter;
|
||||
}
|
||||
|
||||
public @NotNull ValueType<V> paramType() {
|
||||
return adapter().type();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Value's parser, parse base object to value.
|
||||
*/
|
||||
public @Nullable ValueParser<V> parser() {
|
||||
return parserFor(adapter());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Value's serializer, parse value to base object.
|
||||
*/
|
||||
public @Nullable ValueSerializer<V> serializer() {
|
||||
return serializerFor(adapter());
|
||||
}
|
||||
|
||||
private @NotNull List<V> createList() {
|
||||
return constructor.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<V> get() {
|
||||
if (!cacheExpired()) return getCachedOrDefault(createList());
|
||||
// Data that is outdated and needs to be parsed again.
|
||||
List<V> list = createList();
|
||||
try {
|
||||
List<?> data = config().contains(path()) ? config().getList(path()) : null;
|
||||
if (data == null) return getDefaultFirst(list);
|
||||
|
||||
ValueParser<V> parser = parser();
|
||||
if (parser == null) return getDefaultFirst(list);
|
||||
|
||||
int i = 0;
|
||||
for (Object dataVal : data) {
|
||||
if (dataVal == null) continue;
|
||||
try {
|
||||
list.add(withValidated(parser.parse(holder(), paramType(), dataVal)));
|
||||
} catch (Exception e) {
|
||||
throwing(path + "[" + i + "]", e);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throwing(ex);
|
||||
}
|
||||
return updateCache(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@Nullable List<V> list) {
|
||||
updateCache(list);
|
||||
if (list == null) {
|
||||
setData(null);
|
||||
return;
|
||||
}
|
||||
|
||||
ValueSerializer<V> serializer = serializer();
|
||||
if (serializer == null) return;
|
||||
|
||||
List<Object> data = new ArrayList<>();
|
||||
for (V val : list) {
|
||||
if (val == null) continue;
|
||||
try {
|
||||
data.add(serializer.serialize(holder(), paramType(), withValidated(val)));
|
||||
} catch (Exception ex) {
|
||||
throwing(ex);
|
||||
}
|
||||
}
|
||||
setData(data);
|
||||
super(manifest, constructor, paramAdapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -137,21 +49,8 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>, V> implements
|
||||
return resolve().get(index);
|
||||
}
|
||||
|
||||
public @NotNull List<V> copy() {
|
||||
return new ArrayList<>(resolve());
|
||||
}
|
||||
|
||||
public <T> @NotNull T handle(Function<List<V>, T> function) {
|
||||
List<V> list = resolve();
|
||||
T result = function.apply(list);
|
||||
set(list);
|
||||
return result;
|
||||
}
|
||||
|
||||
public @NotNull ConfiguredList<V> modify(Consumer<List<V>> consumer) {
|
||||
List<V> list = resolve();
|
||||
consumer.accept(list);
|
||||
set(list);
|
||||
@Override
|
||||
public @NotNull ConfiguredList<V> self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -160,89 +59,22 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>, V> implements
|
||||
return handle(list -> list.set(index, element));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return resolve().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return resolve().isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return resolve().contains(o);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<V> iterator() {
|
||||
return resolve().iterator();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Object @NotNull [] toArray() {
|
||||
return resolve().toArray();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T> T @NotNull [] toArray(@NotNull T[] a) {
|
||||
return resolve().toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(@NotNull Collection<?> c) {
|
||||
return new HashSet<>(resolve()).containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(V v) {
|
||||
handle(list -> list.add(v));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, V element) {
|
||||
modify(list -> list.add(index, element));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NotNull Collection<? extends V> c) {
|
||||
return handle(list -> list.addAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, @NotNull Collection<? extends V> c) {
|
||||
return handle(list -> list.addAll(index, c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return handle(list -> list.remove(o));
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(int index) {
|
||||
return handle(list -> list.remove(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(@NotNull Collection<?> c) {
|
||||
return handle(list -> list.removeAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(@NotNull Collection<?> c) {
|
||||
return handle(list -> list.retainAll(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
modify(List::clear);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
@@ -272,4 +104,5 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>, V> implements
|
||||
return resolve().subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user