1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 10:38:19 +08:00

feat(loader): Refactor loaders and metadata.

This commit is contained in:
2024-01-30 18:01:36 +08:00
parent b912ea369c
commit da3d4d1fd2
114 changed files with 1868 additions and 1772 deletions
+51
View File
@@ -0,0 +1,51 @@
<?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>
<relativePath>../../pom.xml</relativePath>
</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-feature-commentable</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>easyconfiguration-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</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 "";
}
@@ -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();
}
@@ -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);
}
}
@@ -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);
}
+50
View File
@@ -0,0 +1,50 @@
<?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>
<relativePath>../../pom.xml</relativePath>
</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-feature-file</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>easyconfiguration-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,15 @@
package cc.carm.lib.configuration.option;
import cc.carm.lib.easyoptions.OptionType;
import static cc.carm.lib.easyoptions.OptionType.of;
public class FileConfigOptions {
/**
* Whether to copy files from resource if exists.
*/
OptionType<Boolean> COPY_DEFAULTS = of(true);
}
@@ -0,0 +1,87 @@
//package cc.carm.lib.configuration.core.source.impl;
//
//import org.jetbrains.annotations.NotNull;
//import org.jetbrains.annotations.Nullable;
//
//import java.io.File;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.net.URL;
//import java.net.URLConnection;
//import java.nio.file.Files;
//import java.util.Objects;
//
//public abstract class FileConfigProvider<W extends ConfigurationWrapper<?>> extends ConfigurationProvider<W> {
//
// protected final @NotNull File file;
//
// protected FileConfigProvider(@NotNull File file) {
// this.file = file;
// }
//
// public @NotNull File getFile() {
// return file;
// }
//
// public void initializeFile(@Nullable String sourcePath) throws IOException {
// if (this.file.exists()) return;
//
// File parent = this.file.getParentFile();
// if (parent != null && !parent.exists() && !parent.mkdirs()) {
// throw new IOException("Failed to create directory " + file.getParentFile().getAbsolutePath());
// }
//
// if (!this.file.createNewFile()) {
// throw new IOException("Failed to create file " + file.getAbsolutePath());
// }
//
// if (sourcePath != null) {
// try {
// saveResource(sourcePath, true);
// } catch (IllegalArgumentException ignored) {
// }
// }
// }
//
// public void saveResource(@NotNull String resourcePath, boolean replace)
// throws IOException, IllegalArgumentException {
// Objects.requireNonNull(resourcePath, "ResourcePath cannot be null");
// if (resourcePath.isEmpty()) throw new IllegalArgumentException("ResourcePath cannot be empty");
//
// resourcePath = resourcePath.replace('\\', '/');
//
// URL url = this.getClass().getClassLoader().getResource(resourcePath);
// if (url == null) throw new IllegalArgumentException("The resource '" + resourcePath + "' not exists");
//
// File outDir = file.getParentFile();
//
// if (!outDir.exists() && !outDir.mkdirs()) throw new IOException("Failed to create directory " + outDir);
// if (!file.exists() || replace) {
// try (OutputStream out = Files.newOutputStream(file.toPath())) {
// URLConnection connection = url.openConnection();
// connection.setUseCaches(false);
// try (InputStream in = connection.getInputStream()) {
// byte[] buf = new byte[1024];
// int len;
// while ((len = in.read(buf)) > 0) {
// out.write(buf, 0, len);
// }
// }
// }
// }
// }
//
// @Nullable
// public InputStream getResource(@NotNull String filename) {
// try {
// URL url = this.getClass().getClassLoader().getResource(filename);
// if (url == null) return null;
// URLConnection connection = url.openConnection();
// connection.setUseCaches(false);
// return connection.getInputStream();
// } catch (IOException ex) {
// return null;
// }
// }
//}
@@ -0,0 +1,6 @@
package cc.carm.lib.configuration.source;
public class FileConfigSource {
}