mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
feat(loader): Refactor loaders and metadata.
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
package cc.carm.lib.configuration.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;
|
||||
|
||||
/**
|
||||
* Header Comments.
|
||||
* Add a comment to the top of the corresponding configuration for easy reading and viewing.
|
||||
* <p>e.g.
|
||||
* <blockquote><pre>
|
||||
* # The first line of the comment
|
||||
* # The second line of the comment
|
||||
* foo: "bar"
|
||||
* </pre></blockquote>
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface HeaderComment {
|
||||
|
||||
/**
|
||||
* If the content of the note is 0, it will be treated as a blank line.
|
||||
* <p> e.g. <b>{"foo","","bar"}</b>
|
||||
* Will be set as
|
||||
* <blockquote><pre>
|
||||
* # foo
|
||||
*
|
||||
* # bar
|
||||
* foo: "bar"
|
||||
* </pre></blockquote>
|
||||
*
|
||||
* @return The content of this comment
|
||||
*/
|
||||
@NotNull
|
||||
String[] value() default "";
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package cc.carm.lib.configuration.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;
|
||||
|
||||
/**
|
||||
* Inline comments,
|
||||
* add comments to the rows of the corresponding configurations for easy reading and viewing.
|
||||
* e.g.
|
||||
* <blockquote><pre>
|
||||
* foo: "bar" # Comment Contents
|
||||
* </pre></blockquote>
|
||||
*/
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface InlineComment {
|
||||
|
||||
/**
|
||||
* If the content length is 0, the comment will not be added.
|
||||
* <p> e.g. <b>"foobar"</b> will be set
|
||||
* <blockquote><pre>
|
||||
* foo: "bar" # foobar
|
||||
* </pre></blockquote>
|
||||
*
|
||||
* @return The content of this comment
|
||||
*/
|
||||
@NotNull
|
||||
String value() default "";
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package cc.carm.lib.configuration.commentable;
|
||||
|
||||
import cc.carm.lib.configuration.annotation.HeaderComment;
|
||||
import cc.carm.lib.configuration.annotation.InlineComment;
|
||||
import cc.carm.lib.configuration.value.meta.ValueMetaType;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public interface CommentableMetaTypes {
|
||||
|
||||
/**
|
||||
* Configuration's {@link HeaderComment}
|
||||
*/
|
||||
ValueMetaType<List<String>> HEADER_COMMENTS = ValueMetaType.of(Collections.emptyList());
|
||||
|
||||
/**
|
||||
* Configuration's {@link InlineComment}
|
||||
*/
|
||||
ValueMetaType<String> INLINE_COMMENT_VALUE = ValueMetaType.of();
|
||||
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package cc.carm.lib.configuration.commentable;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ConfigurationComments {
|
||||
|
||||
protected final @NotNull Map<String, List<String>> headerComments = new HashMap<>();
|
||||
protected final @NotNull Map<String, String> inlineComments = new HashMap<>();
|
||||
|
||||
protected @NotNull Map<String, List<String>> getHeaderComments() {
|
||||
return headerComments;
|
||||
}
|
||||
|
||||
protected @NotNull Map<String, String> getInlineComments() {
|
||||
return inlineComments;
|
||||
}
|
||||
|
||||
public void setHeaderComments(@Nullable String path, @Nullable List<String> comments) {
|
||||
if (comments == null) {
|
||||
getHeaderComments().remove(path);
|
||||
} else {
|
||||
getHeaderComments().put(path, comments);
|
||||
}
|
||||
}
|
||||
|
||||
public void setInlineComment(@NotNull String path, @Nullable String comment) {
|
||||
if (comment == null) {
|
||||
getInlineComments().remove(path);
|
||||
} else {
|
||||
getInlineComments().put(path, comment);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Unmodifiable
|
||||
public List<String> getHeaderComment(@Nullable String path) {
|
||||
return Optional.ofNullable(getHeaderComments().get(path)).map(Collections::unmodifiableList).orElse(null);
|
||||
}
|
||||
|
||||
public @Nullable String getInlineComment(@NotNull String path) {
|
||||
return getInlineComments().get(path);
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package cc.carm.lib.configuration.option;
|
||||
|
||||
import cc.carm.lib.easyoptions.OptionType;
|
||||
|
||||
public interface CommentableOptions {
|
||||
|
||||
/**
|
||||
* Whether to keep modified comments in configuration,
|
||||
* that means we only set comments for values that are not exists in configuration.
|
||||
*/
|
||||
OptionType<Boolean> KEEP_COMMENTS = OptionType.of(true);
|
||||
|
||||
/**
|
||||
* Whether to comment values name that are not exists in configuration and no default value offered.
|
||||
* <br>If true, a value without default value is not exists in configuration, we will comment its name,
|
||||
* <p>e.g. a value named "foo" without default value will be put as:
|
||||
* <blockquote><pre>
|
||||
* # Value comments
|
||||
* # foo:
|
||||
* </pre></blockquote>
|
||||
*/
|
||||
OptionType<Boolean> COMMENT_NO_DEFAULT = OptionType.of(true);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user