readHeaderComments(@Nullable HeaderComment annotation) {
+ if (annotation == null) return null;
+ String[] value = annotation.value();
+ return value.length > 0 ? Arrays.asList(value) : null;
+ }
+
+
+ protected static @Nullable String readInlineComments(@Nullable InlineComment annotation) {
+ if (annotation == null) return null;
+ String value = annotation.value();
+ return value.length() > 0 ? value : null;
}
protected static @Nullable String getClassPath(@NotNull Class> clazz,
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java b/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java
deleted file mode 100644
index 5874024..0000000
--- a/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package cc.carm.lib.configuration.core.annotation;
-
-import org.jetbrains.annotations.NotNull;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-@Target({ElementType.TYPE, ElementType.FIELD})
-@Retention(RetentionPolicy.RUNTIME)
-public @interface ConfigComment {
-
- @NotNull
- String[] value() default "";
-
- /**
- * 首行换行,即会在注释开始前进行一次换行,与上方配置分离,优化观感。
- * 如:
- *
- * some-key: "SomeValue"
- *
- * # 注释第一行
- * # 注释第二行
- * startWrap: true
- *
- *
- * @return 是否在结尾添加换行符
- */
- boolean startWrap() default true;
-
- /**
- * 末尾换行,即会在注释结束后进行一次换行,如:
- *
- * # 注释第一行
- * # 注释第二行
- *
- * endWrap: true
- *
- * 该功能可用于编写配置文件的顶部注释。
- *
- * @return 是否在结尾添加换行符
- */
- boolean endWrap() default false;
-}
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/annotation/HeaderComment.java b/core/src/main/java/cc/carm/lib/configuration/core/annotation/HeaderComment.java
new file mode 100644
index 0000000..04702e2
--- /dev/null
+++ b/core/src/main/java/cc/carm/lib/configuration/core/annotation/HeaderComment.java
@@ -0,0 +1,39 @@
+package cc.carm.lib.configuration.core.annotation;
+
+import org.jetbrains.annotations.NotNull;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * 顶部注释,用于给对应配置的顶部添加注释,便于使用者阅读查看。
+ *
如:
+ *
+ * # 注释第一行
+ * # 注释第二行
+ * foo: "bar"
+ *
+ */
+@Target({ElementType.TYPE, ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface HeaderComment {
+
+ /**
+ * 注释内容,若内容长度为0则会视为一个空行。
+ * 如 {"foo","","bar"}
+ * 会被添加为
+ *
+ * # foo
+ *
+ * # bar
+ * foo: "bar"
+ *
+ *
+ * @return 注释内容
+ */
+ @NotNull
+ String[] value() default "";
+
+}
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/annotation/InlineComment.java b/core/src/main/java/cc/carm/lib/configuration/core/annotation/InlineComment.java
new file mode 100644
index 0000000..94a4ae6
--- /dev/null
+++ b/core/src/main/java/cc/carm/lib/configuration/core/annotation/InlineComment.java
@@ -0,0 +1,33 @@
+package cc.carm.lib.configuration.core.annotation;
+
+import org.jetbrains.annotations.NotNull;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * 行内注释,用于给对应配置的所在行添加注释,便于使用者阅读查看。
+ * 如:
+ *
+ * foo: "bar" # 注释内容
+ *
+ */
+@Target({ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface InlineComment {
+
+ /**
+ * 注释内容,若内容长度为0则不会添加注释
+ * 如 "foobar" 将被设定为
+ *
+ * foo: "bar" # foobar
+ *
+ *
+ * @return 注释内容
+ */
+ @NotNull
+ String value() default "";
+
+}
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java
index 0be89fd..0b368c4 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java
@@ -1,11 +1,13 @@
package cc.carm.lib.configuration.core.builder;
-import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ConfigValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import java.util.Arrays;
+import java.util.List;
+
public abstract class AbstractConfigBuilder, P extends ConfigurationProvider>> {
protected final Class super P> providerClass;
@@ -13,9 +15,8 @@ public abstract class AbstractConfigBuilder headerComments;
+ protected @Nullable String inlineComment;
protected @Nullable T defaultValue;
@@ -38,38 +39,26 @@ public abstract class AbstractConfigBuilder comments) {
+ this.headerComments = comments;
return getThis();
}
- public @NotNull B setStartWarp(boolean enable) {
- this.startWrap = enable;
+ public @NotNull B inlineComment(@NotNull String comment) {
+ this.inlineComment = comment;
return getThis();
}
- public @NotNull B startWarp() {
- return setStartWarp(true);
- }
-
- public @NotNull B setEndWarp(boolean enable) {
- this.endWrap = enable;
- return getThis();
- }
-
- public @NotNull B endWarp() {
- return setEndWarp(true);
- }
-
public @NotNull B defaults(@Nullable T defaultValue) {
this.defaultValue = defaultValue;
return getThis();
}
- protected @Nullable ConfigCommentInfo buildComments() {
- ConfigCommentInfo info = ConfigCommentInfo.of(this.comments, this.startWrap, this.endWrap);
- if (info.equals(ConfigCommentInfo.defaults())) return null;
- else return info;
- }
-
-
}
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/ConfigListBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/ConfigListBuilder.java
index 68e539b..ab6d7ec 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/ConfigListBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/ConfigListBuilder.java
@@ -1,8 +1,10 @@
package cc.carm.lib.configuration.core.builder.list;
+import cc.carm.lib.configuration.core.annotation.InlineComment;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import org.jetbrains.annotations.NotNull;
+
public class ConfigListBuilder {
protected final @NotNull Class valueClass;
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java
index 3a09cda..f9b96fc 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java
@@ -65,7 +65,8 @@ public class SourceListBuilder extends CommonConfigBuilder, Source
@Override
public @NotNull ConfiguredList build() {
return new ConfiguredList<>(
- this.provider, this.path, this.buildComments(),
+ this.provider, this.path,
+ this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.sourceParser.andThen(this.valueParser),
this.valueSerializer.andThen(sourceSerializer)
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java
index 3345c6d..8fdd916 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java
@@ -89,7 +89,8 @@ public class SourceMapBuilder, S, K, V> extends CommonConfig
@Override
public @NotNull ConfiguredMap build() {
return new ConfiguredMap<>(
- this.provider, this.path,this.buildComments(),
+ this.provider, this.path,
+ this.headerComments, this.inlineComment,
this.defaultValue, this.supplier,
this.keyClass, this.keyParser,
this.valueClass, this.sourceParser.andThen(this.valueParser),
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SectionValueBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SectionValueBuilder.java
index 4366f73..6e7a0c3 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SectionValueBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SectionValueBuilder.java
@@ -45,7 +45,8 @@ public class SectionValueBuilder
@Override
public @NotNull ConfiguredSection build() {
return new ConfiguredSection<>(
- this.provider, this.path,this.buildComments(),
+ this.provider, this.path,
+ this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.parser, this.serializer
);
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SourceValueBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SourceValueBuilder.java
index f52f0ee..5d6caea 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SourceValueBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SourceValueBuilder.java
@@ -3,7 +3,6 @@ package cc.carm.lib.configuration.core.builder.value;
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
-import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import org.jetbrains.annotations.NotNull;
@@ -58,7 +57,8 @@ public class SourceValueBuilder extends CommonConfigBuilder build() {
return new ConfiguredValue<>(
- this.provider, this.path, this.buildComments(),
+ this.provider, this.path,
+ this.headerComments, this.inlineComment,
this.valueClass, this.defaultValue,
this.valueParser.compose(this.sourceParser),
this.valueSerializer.andThen(sourceSerializer)
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigCommentInfo.java b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigCommentInfo.java
deleted file mode 100644
index 47818a3..0000000
--- a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigCommentInfo.java
+++ /dev/null
@@ -1,80 +0,0 @@
-package cc.carm.lib.configuration.core.source;
-
-import cc.carm.lib.configuration.core.annotation.ConfigComment;
-import cc.carm.lib.configuration.core.value.ConfigValue;
-import org.jetbrains.annotations.Contract;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.Arrays;
-import java.util.Objects;
-import java.util.Optional;
-
-public class ConfigCommentInfo {
-
- public static @NotNull ConfigCommentInfo DEFAULT_INFO = of(new String[0], true, false);
-
- protected final @NotNull String[] comments;
- protected final boolean startWrap;
- protected final boolean endWrap;
-
- public ConfigCommentInfo(@NotNull String[] comments, boolean startWrap, boolean endWrap) {
- this.comments = comments;
- this.startWrap = startWrap;
- this.endWrap = endWrap;
- }
-
- public @NotNull String[] getComments() {
- return comments;
- }
-
- public boolean endWrap() {
- return endWrap;
- }
-
- public boolean startWrap() {
- return startWrap;
- }
-
- public static @NotNull ConfigCommentInfo of(@NotNull String[] comments, boolean startWrap, boolean endWrap) {
- return new ConfigCommentInfo(comments, startWrap, endWrap);
- }
-
- public static @NotNull ConfigCommentInfo defaults() {
- return DEFAULT_INFO;
- }
-
- @Contract("!null->!null")
- public static @Nullable ConfigCommentInfo fromAnnotation(@Nullable ConfigComment comment) {
- if (comment == null) return null;
- else return new ConfigCommentInfo(comment.value(), comment.startWrap(), comment.endWrap());
- }
-
- public static @NotNull ConfigCommentInfo fromValue(@NotNull ConfigValue> value) {
- return Optional.ofNullable(value.getComments()).orElse(defaults());
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- ConfigCommentInfo that = (ConfigCommentInfo) o;
- return startWrap == that.startWrap && endWrap == that.endWrap && Arrays.equals(getComments(), that.getComments());
- }
-
- @Override
- public int hashCode() {
- int result = Objects.hash(startWrap, endWrap);
- result = 31 * result + Arrays.hashCode(getComments());
- return result;
- }
-
- @Override
- public String toString() {
- return "ConfigCommentInfo{" +
- "comments=" + Arrays.toString(comments) +
- ", startWrap=" + startWrap +
- ", endWrap=" + endWrap +
- '}';
- }
-}
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationProvider.java b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationProvider.java
index 66153e6..8ed0b53 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationProvider.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationProvider.java
@@ -4,6 +4,9 @@ import cc.carm.lib.configuration.core.ConfigInitializer;
import cc.carm.lib.configuration.core.ConfigurationRoot;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.annotations.Unmodifiable;
+
+import java.util.List;
public abstract class ConfigurationProvider {
@@ -27,9 +30,15 @@ public abstract class ConfigurationProvider {
public abstract void save() throws Exception;
- public abstract void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment);
+ public abstract void setHeaderComment(@Nullable String path, @Nullable List comments);
- public abstract @Nullable ConfigCommentInfo getComment(@Nullable String path);
+ public abstract void setInlineComment(@NotNull String path, @Nullable String comment);
+
+ @Nullable
+ @Unmodifiable
+ public abstract List getHeaderComment(@Nullable String path);
+
+ public abstract @Nullable String getInlineComment(@NotNull String path);
public abstract @NotNull ConfigInitializer extends ConfigurationProvider> getInitializer();
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationReader.java b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationReader.java
index 2985e33..ba4deba 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationReader.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationReader.java
@@ -4,6 +4,11 @@ import cc.carm.lib.configuration.core.function.ConfigValueParser;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.annotations.Unmodifiable;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
interface ConfigurationReader {
@@ -26,7 +31,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Byte.class);
}
-
default @Nullable Byte getByte(@NotNull String path) {
return getByte(path, (byte) 0);
}
@@ -36,12 +40,10 @@ interface ConfigurationReader {
return getWrapper().get(path, def, ConfigValueParser.byteValue());
}
-
default boolean isShort(@NotNull String path) {
return getWrapper().isType(path, Short.class);
}
-
default @Nullable Short getShort(@NotNull String path) {
return getShort(path, (short) 0);
}
@@ -56,7 +58,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Integer.class);
}
-
default @Nullable Integer getInt(@NotNull String path) {
return getInt(path, 0);
}
@@ -71,7 +72,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Long.class);
}
-
default @Nullable Long getLong(@NotNull String path) {
return getLong(path, 0L);
}
@@ -86,7 +86,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Float.class);
}
-
default @Nullable Float getFloat(@NotNull String path) {
return getFloat(path, 0.0F);
}
@@ -101,7 +100,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Double.class);
}
-
default @Nullable Double getDouble(@NotNull String path) {
return getDouble(path, 0.0D);
}
@@ -116,7 +114,6 @@ interface ConfigurationReader {
return getWrapper().isType(path, Boolean.class);
}
-
default @Nullable Character getChar(@NotNull String path) {
return getChar(path, null);
}
@@ -140,5 +137,54 @@ interface ConfigurationReader {
return getWrapper().get(path, def, String.class);
}
+ @Unmodifiable
+ default @NotNull List getStringList(@NotNull String path) {
+ return parseList(getWrapper().getList(path), ConfigValueParser.castToString());
+ }
+
+ @Unmodifiable
+ default @NotNull List getIntegerList(@NotNull String path) {
+ return parseList(getWrapper().getList(path), ConfigValueParser.intValue());
+ }
+
+ @Unmodifiable
+ default @NotNull List getLongList(@NotNull String path) {
+ return parseList(getWrapper().getList(path), ConfigValueParser.longValue());
+ }
+
+ @Unmodifiable
+ default @NotNull List getDoubleList(@NotNull String path) {
+ return parseList(getWrapper().getList(path), ConfigValueParser.doubleValue());
+ }
+
+ @Unmodifiable
+ default @NotNull List getFloatList(@NotNull String path) {
+ return parseList(getWrapper().getList(path), ConfigValueParser.floatValue());
+ }
+
+ @Unmodifiable
+ default @NotNull List getByteList(@NotNull String path) {
+ return parseList(getWrapper().getList(path), ConfigValueParser.byteValue());
+ }
+
+ @Unmodifiable
+ default @NotNull List getCharList(@NotNull String path) {
+ return parseList(getWrapper().getList(path), ConfigValueParser.castObject(Character.class));
+ }
+
+ @Unmodifiable
+ static @NotNull List parseList(@Nullable List> list, ConfigValueParser