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:
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<version>3.9.1</version>
|
||||
</parent>
|
||||
<properties>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
|
||||
<artifactId>easyconfiguration-commentable</artifactId>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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 "";
|
||||
|
||||
}
|
||||
@@ -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 "";
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package cc.carm.lib.configuration.commentable;
|
||||
|
||||
import cc.carm.lib.configuration.annotation.HeaderComment;
|
||||
import cc.carm.lib.configuration.annotation.InlineComment;
|
||||
import cc.carm.lib.easyannotation.AnnotatedMetaType;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public interface CommentableMetaTypes {
|
||||
|
||||
/**
|
||||
* Configuration's {@link HeaderComment}
|
||||
*/
|
||||
AnnotatedMetaType<HeaderComment, List<String>> HEADER_COMMENT = AnnotatedMetaType.of(
|
||||
HeaderComment.class, h -> h.value().length == 0 ? null : Arrays.asList(h.value())
|
||||
);
|
||||
|
||||
/**
|
||||
* Configuration's {@link InlineComment}
|
||||
*/
|
||||
AnnotatedMetaType<InlineComment, String> INLINE_COMMENT = AnnotatedMetaType.of(
|
||||
InlineComment.class, c -> c.value().isEmpty() ? null : c.value()
|
||||
);
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package cc.carm.lib.configuration.commentable;
|
||||
|
||||
import cc.carm.lib.easyoptions.OptionType;
|
||||
|
||||
import static cc.carm.lib.easyoptions.OptionType.of;
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
+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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user