Allows the list join delimiter in FormatBuilder to be customized in some cases
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
This commit is contained in:
@@ -98,6 +98,8 @@ public final class FormatBuilder {
|
||||
/**
|
||||
* Replaces placeholders for the current string
|
||||
*
|
||||
* <p>If the placeholder or replacement is a list, it will be joined using the default delimiter: ",".</p>
|
||||
*
|
||||
* @param placeholder <p>The placeholder to replace</p>
|
||||
* @param replacement <p>The replacement</p>
|
||||
* @return <p>This format builder</p>
|
||||
@@ -106,7 +108,26 @@ public final class FormatBuilder {
|
||||
*/
|
||||
@NotNull
|
||||
public <K, L> FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement) throws IllegalStateException {
|
||||
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder), asString(replacement));
|
||||
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder, LIST_DELIMITER),
|
||||
asString(replacement, LIST_DELIMITER));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces placeholders for the current string
|
||||
*
|
||||
* @param placeholder <p>The placeholder to replace</p>
|
||||
* @param replacement <p>The replacement</p>
|
||||
* @param delimiter <p>The delimiter used for joining if the replacement is some kind of list</p>
|
||||
* @return <p>This format builder</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set, and the placeholder or replacement is
|
||||
* a translatable message</p>
|
||||
*/
|
||||
@NotNull
|
||||
public <K, L> FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement,
|
||||
@NotNull String delimiter) throws IllegalStateException {
|
||||
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder, LIST_DELIMITER),
|
||||
asString(replacement, delimiter));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -157,13 +178,29 @@ public final class FormatBuilder {
|
||||
/**
|
||||
* Appends the given string to this format builder
|
||||
*
|
||||
* <p>If the input is a list, it will be joined using the default delimiter: ",".</p>
|
||||
*
|
||||
* @param input <p>The input to append</p>
|
||||
* @return <p>This format builder</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message</p>
|
||||
*/
|
||||
@NotNull
|
||||
public <K> FormatBuilder append(@NotNull K input) throws IllegalStateException {
|
||||
this.toFormat += asString(input);
|
||||
this.toFormat += asString(input, LIST_DELIMITER);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the given string to this format builder
|
||||
*
|
||||
* @param input <p>The input to append</p>
|
||||
* @param delimiter <p>The delimiter used for joining if the input is some kind of list</p>
|
||||
* @return <p>This format builder</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message</p>
|
||||
*/
|
||||
@NotNull
|
||||
public <K> FormatBuilder append(@NotNull K input, @NotNull String delimiter) throws IllegalStateException {
|
||||
this.toFormat += asString(input, delimiter);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -216,11 +253,12 @@ public final class FormatBuilder {
|
||||
* Converts the given input to a string
|
||||
*
|
||||
* @param input <p>The input to convert</p>
|
||||
* @param delimiter <p>The delimiter to use if the input is some kind of list</p>
|
||||
* @return <p>The corresponding string</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message.</p>
|
||||
*/
|
||||
@NotNull
|
||||
private <K> String asString(@NotNull K input) {
|
||||
private <K> String asString(@NotNull K input, @NotNull String delimiter) {
|
||||
if (input instanceof String string) {
|
||||
return string;
|
||||
} else if (input instanceof TranslatableMessage translatableMessage) {
|
||||
@@ -237,14 +275,14 @@ public final class FormatBuilder {
|
||||
for (Object item : collection) {
|
||||
output.add(String.valueOf(item));
|
||||
}
|
||||
return String.join(LIST_DELIMITER, output);
|
||||
return String.join(delimiter, output);
|
||||
} else if (input.getClass().isArray()) {
|
||||
int length = Array.getLength(input);
|
||||
List<String> output = new ArrayList<>(length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
output.add(String.valueOf(Array.get(input, i)));
|
||||
}
|
||||
return String.join(LIST_DELIMITER, output);
|
||||
return String.join(delimiter, output);
|
||||
}
|
||||
return String.valueOf(input);
|
||||
}
|
||||
|
Reference in New Issue
Block a user