mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
feat: Enhanced text content replacer
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 4.1 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 22 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.4 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 21 KiB |
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
.idea/*
|
.idea/*
|
||||||
!.idea/icon.png
|
!.idea/icon.svg
|
||||||
**/target/
|
**/target/
|
||||||
**.iml
|
**.iml
|
||||||
Generated
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 4.1 KiB |
Generated
+80
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 22 KiB |
@@ -13,7 +13,7 @@ README LANGUAGES [ [**English**](README.md) | [中文](README_CN.md) ]
|
|||||||
|
|
||||||
# configured _(config framework)_
|
# configured _(config framework)_
|
||||||
|
|
||||||
<img src=".doc/images/logo-bg.png" alt="logo" align="right" style="float: right"/>
|
<img src=".doc/images/logo-bg.svg" width="192px" alt="logo" align="right" style="float: right"/>
|
||||||
|
|
||||||
_**"Once set, Simple get."**_
|
_**"Once set, Simple get."**_
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@ README LANGUAGES [ [English](README.md) | [**中文**](README_CN.md) ]
|
|||||||
|
|
||||||
# configured _(配置文件框架)_
|
# configured _(配置文件框架)_
|
||||||
|
|
||||||
<img src=".doc/images/logo-bg.png" alt="logo" align="right" style="float: right"/>
|
<img src=".doc/images/logo-bg.svg" width="192px" alt="logo" align="right" style="float: right"/>
|
||||||
|
|
||||||
**一次配置,轻松读取!**
|
**一次配置,轻松读取!**
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.function;
|
|
||||||
|
|
||||||
public interface ValueSupplier {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
+5
@@ -1,6 +1,7 @@
|
|||||||
package cc.carm.lib.configuration.value.text.function;
|
package cc.carm.lib.configuration.value.text.function;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.value.text.data.TextContents;
|
import cc.carm.lib.configuration.value.text.data.TextContents;
|
||||||
|
import cc.carm.lib.configuration.value.text.function.modifier.ContentReplacer;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -46,6 +47,7 @@ public abstract class ContentHandler<RECEIVER, SELF extends ContentHandler<RECEI
|
|||||||
"^\\?\\[(?<id>.+)](?<content>.*)$"
|
"^\\?\\[(?<id>.+)](?<content>.*)$"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
public static final @NotNull Function<String, String>
|
||||||
public static final @NotNull UnaryOperator<String> DEFAULT_PARAM_BUILDER = s -> "%(" + s + ")";
|
public static final @NotNull UnaryOperator<String> DEFAULT_PARAM_BUILDER = s -> "%(" + s + ")";
|
||||||
|
|
||||||
protected BiFunction<RECEIVER, String, String> parser = (receiver, value) -> value;
|
protected BiFunction<RECEIVER, String, String> parser = (receiver, value) -> value;
|
||||||
@@ -58,6 +60,9 @@ public abstract class ContentHandler<RECEIVER, SELF extends ContentHandler<RECEI
|
|||||||
protected @NotNull UnaryOperator<String> paramFormatter = DEFAULT_PARAM_BUILDER;
|
protected @NotNull UnaryOperator<String> paramFormatter = DEFAULT_PARAM_BUILDER;
|
||||||
protected @NotNull String[] params;
|
protected @NotNull String[] params;
|
||||||
|
|
||||||
|
protected @NotNull List<ContentReplacer> replacers = new ArrayList<>();
|
||||||
|
protected @NotNull List<ContentReplacer> inserters = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to store the insertion of the message
|
* Used to store the insertion of the message
|
||||||
*/
|
*/
|
||||||
|
|||||||
+82
@@ -0,0 +1,82 @@
|
|||||||
|
package cc.carm.lib.configuration.value.text.function.modifier;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class ContentReplacer<RECEIVER> {
|
||||||
|
|
||||||
|
public static <R> ContentReplacer.Builder<R> match(@NotNull Predicate<String> matcher) {
|
||||||
|
return new Builder<>(matcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <R> ContentReplacer.Builder<R> match(@NotNull String id) {
|
||||||
|
return match(text -> text.equalsIgnoreCase(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <R> ContentReplacer.Builder<R> match(@NotNull Pattern pattern) {
|
||||||
|
return match(text -> pattern.matcher(text).find());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final int priority;
|
||||||
|
protected final @NotNull Predicate<String> matcher;
|
||||||
|
protected final BiFunction<@NotNull RECEIVER, @NotNull String, @Nullable String> supplier;
|
||||||
|
|
||||||
|
public ContentReplacer(int priority,
|
||||||
|
@NotNull Predicate<String> matcher,
|
||||||
|
BiFunction<@NotNull RECEIVER, @NotNull String, @Nullable String> supplier) {
|
||||||
|
this.priority = priority;
|
||||||
|
this.matcher = matcher;
|
||||||
|
this.supplier = supplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Predicate<String> matcher() {
|
||||||
|
return this.matcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean check(@NotNull String param) {
|
||||||
|
return this.matcher.test(param);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @Nullable String content(@NotNull RECEIVER receiver, @NotNull String matchedParam) {
|
||||||
|
return this.supplier == null ? null : this.supplier.apply(receiver, matchedParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder<R> {
|
||||||
|
|
||||||
|
protected final @NotNull Predicate<String> matcher;
|
||||||
|
protected int priority = 0;
|
||||||
|
|
||||||
|
public Builder(@NotNull Predicate<String> matcher) {
|
||||||
|
this.matcher = matcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Builder<R> priority(int priority) {
|
||||||
|
this.priority = priority;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ContentReplacer<R> to(BiFunction<@NotNull R, @NotNull String, @Nullable String> supplier) {
|
||||||
|
return new ContentReplacer<>(this.priority, this.matcher, supplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ContentReplacer<R> to(Function<@NotNull R, @Nullable String> supplier) {
|
||||||
|
return to((receiver, matchedParam) -> supplier.apply(receiver));
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ContentReplacer<R> to(Supplier<@Nullable String> supplier) {
|
||||||
|
return to((receiver, matchedParam) -> supplier.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ContentReplacer<R> to(@NotNull String content) {
|
||||||
|
return to(() -> content);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
package cc.carm.lib.configuration.value.text.function.modifier;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class ParamHandler {
|
||||||
|
|
||||||
|
// %(<ID>)
|
||||||
|
public static final Pattern DEFAULT_PARAM_PATTERN = Pattern.compile("%\\((?<id>[^)]+)\\)");
|
||||||
|
public static final ParamHandler DEFAULT_PARAM = new ParamHandler(
|
||||||
|
s -> {
|
||||||
|
},
|
||||||
|
s -> {
|
||||||
|
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
protected final Function<String, String> extractor;
|
||||||
|
protected final Function<String, String> replacer;
|
||||||
|
|
||||||
|
public ParamHandler(Function<String, String> extractor, Function<String, String> replacer) {
|
||||||
|
this.extractor = extractor;
|
||||||
|
this.replacer = replacer;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user