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

feat(parser): Supported parse functions

This commit is contained in:
2025-02-17 02:46:18 +08:00
parent bf6ea97b99
commit 05f504a347
2 changed files with 52 additions and 10 deletions
@@ -55,11 +55,33 @@ public class ConfiguredText<MSG, RECEIVER> extends ConfiguredValue<TextContents>
.dispatcher(this.dispatcher).placeholders(values);
}
/**
* Parse the message for the receiver.
*
* @param receiver The receiver of the message.
* @param values The values to replace the {@link #params}.
* @return The parsed message.
*/
public List<String> parse(@NotNull RECEIVER receiver, @NotNull Object... values) {
return prepare(values).parse(receiver);
}
/**
* Parse the message for the receiver and send it.
*
* @param receiver The receiver of the message.
* @param values The values to replace the {@link #params}.
* @return The parsed message.
*/
public String parseLine(@NotNull RECEIVER receiver, @NotNull Object... values) {
return prepare(values).parseLine(receiver);
}
/**
* Compile the message for the receiver.
*
* @param receiver The receiver of the message.
* @param values The values to replace the placeholders.
* @param values The values to replace the {@link #params}.
* @return The compiled message.
*/
public List<MSG> compile(@NotNull RECEIVER receiver, @NotNull Object... values) {
@@ -70,7 +92,7 @@ public class ConfiguredText<MSG, RECEIVER> extends ConfiguredValue<TextContents>
* Compile the message for the receiver and send it.
*
* @param receiver The receiver of the message.
* @param values The values to replace the placeholders.
* @param values The values to replace the {@link #params}.
* @return The compiled message.
*/
public MSG compileLine(@NotNull RECEIVER receiver, @NotNull Object... values) {
@@ -81,7 +103,7 @@ public class ConfiguredText<MSG, RECEIVER> extends ConfiguredValue<TextContents>
* Send the message to the receiver.
*
* @param receiver The receiver of the message.
* @param values The values to replace the placeholders.
* @param values The values to replace the {@link #params}.
*/
public void sendTo(@NotNull RECEIVER receiver, @NotNull Object... values) {
prepare(values).to(receiver);
@@ -91,7 +113,7 @@ public class ConfiguredText<MSG, RECEIVER> extends ConfiguredValue<TextContents>
* Send the message to the multiple receivers.
*
* @param receivers The receivers of the message.
* @param values The values to replace the placeholders.
* @param values The values to replace the {@link #params}.
*/
public void sendToAll(@NotNull Iterable<? extends RECEIVER> receivers, @NotNull Object... values) {
prepare(values).to(receivers);
@@ -113,7 +113,7 @@ public abstract class TextParser<RECEIVER, SELF extends TextParser<RECEIVER, SEL
/**
* Set the placeholders for the text.
*
* @param values the values of the placeholders
* @param values The values to replace the {@link #params(String...)}.
* @return the current {@link TextParser} instance
*/
public SELF placeholders(@Nullable Object... values) {
@@ -193,6 +193,18 @@ public abstract class TextParser<RECEIVER, SELF extends TextParser<RECEIVER, SEL
return self();
}
/**
* Parse the texts for the receiver.
*
* @param receiver the receiver
* @return the parsed line
*/
public List<String> parse(@Nullable RECEIVER receiver) {
List<String> result = new ArrayList<>();
handleTexts(receiver, result::add);
return result;
}
/**
* Parse the texts for the receiver.
*
@@ -211,17 +223,26 @@ public abstract class TextParser<RECEIVER, SELF extends TextParser<RECEIVER, SEL
* Parse the texts as a single line for the receiver.
*
* @param receiver the receiver
* @param compiler the compiler
* @param <V> the type of the message
* @return the parsed line
*/
public <V> @Nullable V parseLine(@Nullable RECEIVER receiver, @NotNull BiFunction<RECEIVER, String, V> compiler) {
public @Nullable String parseLine(@Nullable RECEIVER receiver) {
if (this.texts.isEmpty()) return null;
StringBuilder builder = new StringBuilder();
handleTexts(receiver, s -> builder.append(s).append(this.lineSeparator));
// Remove the last line separator, if it exists
if (builder.length() > 0) builder.delete(builder.length() - this.lineSeparator.length(), builder.length());
return compiler.apply(receiver, builder.toString());
return builder.toString();
}
/**
* Parse the texts as a single line for the receiver.
*
* @param receiver the receiver
* @param <V> the type of the message
* @return the parsed line
*/
public <V> @Nullable V parseLine(@Nullable RECEIVER receiver, @NotNull BiFunction<RECEIVER, String, V> compiler) {
return Optional.ofNullable(parseLine(receiver)).map(s -> compiler.apply(receiver, s)).orElse(null);
}
@@ -236,7 +257,6 @@ public abstract class TextParser<RECEIVER, SELF extends TextParser<RECEIVER, SEL
return this.parser.apply(receiver, setPlaceholders(text, this.placeholders));
}
public void handleTexts(@Nullable RECEIVER receiver, @NotNull Consumer<String> lineConsumer) {
if (this.texts.isEmpty()) return; // Nothing to parse