mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
feat: Enhanced text content replacer
This commit is contained in:
+5
@@ -1,6 +1,7 @@
|
||||
package cc.carm.lib.configuration.value.text.function;
|
||||
|
||||
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.Nullable;
|
||||
|
||||
@@ -46,6 +47,7 @@ public abstract class ContentHandler<RECEIVER, SELF extends ContentHandler<RECEI
|
||||
"^\\?\\[(?<id>.+)](?<content>.*)$"
|
||||
);
|
||||
|
||||
public static final @NotNull Function<String, String>
|
||||
public static final @NotNull UnaryOperator<String> DEFAULT_PARAM_BUILDER = s -> "%(" + s + ")";
|
||||
|
||||
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 String[] params;
|
||||
|
||||
protected @NotNull List<ContentReplacer> replacers = new ArrayList<>();
|
||||
protected @NotNull List<ContentReplacer> inserters = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 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