Improves efficiency when appending many strings to a format builder
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:
@@ -20,13 +20,14 @@ public final class FormatBuilder {
|
|||||||
private static @Nullable StringFormatter stringFormatter = null;
|
private static @Nullable StringFormatter stringFormatter = null;
|
||||||
private static final IllegalStateException NOT_SETUP_EXCEPTION = new IllegalStateException("String formatter has not been set!");
|
private static final IllegalStateException NOT_SETUP_EXCEPTION = new IllegalStateException("String formatter has not been set!");
|
||||||
private static final String LIST_DELIMITER = ",";
|
private static final String LIST_DELIMITER = ",";
|
||||||
private @NotNull String toFormat;
|
private StringBuilder stringBuilder = null;
|
||||||
|
private @NotNull String stringToFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new format builder
|
* Instantiates a new format builder
|
||||||
*/
|
*/
|
||||||
public FormatBuilder() {
|
public FormatBuilder() {
|
||||||
this.toFormat = "";
|
this.stringToFormat = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,7 +39,7 @@ public final class FormatBuilder {
|
|||||||
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message</p>
|
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message</p>
|
||||||
*/
|
*/
|
||||||
public <K> FormatBuilder(@NotNull K input) throws IllegalStateException {
|
public <K> FormatBuilder(@NotNull K input) throws IllegalStateException {
|
||||||
this.toFormat = asString(input, LIST_DELIMITER);
|
this.stringToFormat = asString(input, LIST_DELIMITER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,7 +50,7 @@ public final class FormatBuilder {
|
|||||||
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message</p>
|
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message</p>
|
||||||
*/
|
*/
|
||||||
public <K> FormatBuilder(@NotNull K input, @NotNull String delimiter) throws IllegalStateException {
|
public <K> FormatBuilder(@NotNull K input, @NotNull String delimiter) throws IllegalStateException {
|
||||||
this.toFormat = asString(input, delimiter);
|
this.stringToFormat = asString(input, delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,7 +71,7 @@ public final class FormatBuilder {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public FormatBuilder replace(@NotNull List<String> placeholders, @NotNull List<String> replacements) {
|
public FormatBuilder replace(@NotNull List<String> placeholders, @NotNull List<String> replacements) {
|
||||||
this.toFormat = StringFormatter.replacePlaceholders(this.toFormat, placeholders, replacements);
|
this.stringToFormat = StringFormatter.replacePlaceholders(getCurrentString(), placeholders, replacements);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +84,7 @@ public final class FormatBuilder {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public FormatBuilder replace(@NotNull String[] placeholders, @NotNull String[] replacements) {
|
public FormatBuilder replace(@NotNull String[] placeholders, @NotNull String[] replacements) {
|
||||||
this.toFormat = StringFormatter.replacePlaceholders(this.toFormat, placeholders, replacements);
|
this.stringToFormat = StringFormatter.replacePlaceholders(getCurrentString(), placeholders, replacements);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,9 +101,7 @@ public final class FormatBuilder {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public <K, L> FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement) throws IllegalStateException {
|
public <K, L> FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement) throws IllegalStateException {
|
||||||
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder, LIST_DELIMITER),
|
return replace(placeholder, replacement, LIST_DELIMITER);
|
||||||
asString(replacement, LIST_DELIMITER));
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -118,7 +117,7 @@ public final class FormatBuilder {
|
|||||||
@NotNull
|
@NotNull
|
||||||
public <K, L> FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement,
|
public <K, L> FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement,
|
||||||
@NotNull String delimiter) throws IllegalStateException {
|
@NotNull String delimiter) throws IllegalStateException {
|
||||||
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder, LIST_DELIMITER),
|
this.stringToFormat = StringFormatter.replacePlaceholder(getCurrentString(), asString(placeholder, LIST_DELIMITER),
|
||||||
asString(replacement, delimiter));
|
asString(replacement, delimiter));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -131,7 +130,7 @@ public final class FormatBuilder {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public FormatBuilder replace(@NotNull StringReplacer stringReplacer) {
|
public FormatBuilder replace(@NotNull StringReplacer stringReplacer) {
|
||||||
this.toFormat = stringReplacer.replace(this.toFormat);
|
this.stringToFormat = stringReplacer.replace(getCurrentString());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +148,7 @@ public final class FormatBuilder {
|
|||||||
if (FormatBuilder.stringFormatter == null) {
|
if (FormatBuilder.stringFormatter == null) {
|
||||||
throw NOT_SETUP_EXCEPTION;
|
throw NOT_SETUP_EXCEPTION;
|
||||||
}
|
}
|
||||||
this.toFormat = FormatBuilder.stringFormatter.getUnFormattedColoredMessage(this.toFormat);
|
this.stringToFormat = FormatBuilder.stringFormatter.getUnFormattedColoredMessage(getCurrentString());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +162,7 @@ public final class FormatBuilder {
|
|||||||
* @return <p>This format builder</p>
|
* @return <p>This format builder</p>
|
||||||
*/
|
*/
|
||||||
public FormatBuilder color(@NotNull ColorConversion colorConversion) {
|
public FormatBuilder color(@NotNull ColorConversion colorConversion) {
|
||||||
this.toFormat = ColorHelper.translateColorCodes(this.toFormat, colorConversion);
|
this.stringToFormat = ColorHelper.translateColorCodes(getCurrentString(), colorConversion);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,8 +177,7 @@ public final class FormatBuilder {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public <K> FormatBuilder append(@NotNull K input) throws IllegalStateException {
|
public <K> FormatBuilder append(@NotNull K input) throws IllegalStateException {
|
||||||
this.toFormat += asString(input, LIST_DELIMITER);
|
return append(input, LIST_DELIMITER);
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,7 +190,10 @@ public final class FormatBuilder {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public <K> FormatBuilder append(@NotNull K input, @NotNull String delimiter) throws IllegalStateException {
|
public <K> FormatBuilder append(@NotNull K input, @NotNull String delimiter) throws IllegalStateException {
|
||||||
this.toFormat += asString(input, delimiter);
|
if (this.stringBuilder == null) {
|
||||||
|
this.stringBuilder = new StringBuilder();
|
||||||
|
}
|
||||||
|
this.stringBuilder.append(asString(input, delimiter));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,7 +207,7 @@ public final class FormatBuilder {
|
|||||||
if (FormatBuilder.stringFormatter == null) {
|
if (FormatBuilder.stringFormatter == null) {
|
||||||
throw NOT_SETUP_EXCEPTION;
|
throw NOT_SETUP_EXCEPTION;
|
||||||
}
|
}
|
||||||
FormatBuilder.stringFormatter.displaySuccessMessage(commandSender, this.toFormat);
|
FormatBuilder.stringFormatter.displaySuccessMessage(commandSender, getCurrentString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -219,7 +220,7 @@ public final class FormatBuilder {
|
|||||||
if (FormatBuilder.stringFormatter == null) {
|
if (FormatBuilder.stringFormatter == null) {
|
||||||
throw NOT_SETUP_EXCEPTION;
|
throw NOT_SETUP_EXCEPTION;
|
||||||
}
|
}
|
||||||
FormatBuilder.stringFormatter.displayErrorMessage(commandSender, this.toFormat);
|
FormatBuilder.stringFormatter.displayErrorMessage(commandSender, getCurrentString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -232,13 +233,26 @@ public final class FormatBuilder {
|
|||||||
if (FormatBuilder.stringFormatter == null) {
|
if (FormatBuilder.stringFormatter == null) {
|
||||||
throw NOT_SETUP_EXCEPTION;
|
throw NOT_SETUP_EXCEPTION;
|
||||||
}
|
}
|
||||||
FormatBuilder.stringFormatter.displayNeutralMessage(commandSender, this.toFormat);
|
FormatBuilder.stringFormatter.displayNeutralMessage(commandSender, getCurrentString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.toFormat;
|
return getCurrentString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the currently generated string
|
||||||
|
*
|
||||||
|
* @return <p>The current string</p>
|
||||||
|
*/
|
||||||
|
private String getCurrentString() {
|
||||||
|
if (this.stringBuilder != null) {
|
||||||
|
this.stringToFormat += this.stringBuilder.toString();
|
||||||
|
this.stringBuilder = null;
|
||||||
|
}
|
||||||
|
return this.stringToFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -38,6 +38,10 @@ public class FormatBuilderTest {
|
|||||||
assertEquals("Test1,2,3,4,5abck z", builder.toString());
|
assertEquals("Test1,2,3,4,5abck z", builder.toString());
|
||||||
builder.append(List.of(ParticleMode.SINGLE, ParticleMode.CIRCLE, ParticleMode.CUBE));
|
builder.append(List.of(ParticleMode.SINGLE, ParticleMode.CIRCLE, ParticleMode.CUBE));
|
||||||
assertEquals("Test1,2,3,4,5abck zSINGLE,CIRCLE,CUBE", builder.toString());
|
assertEquals("Test1,2,3,4,5abck zSINGLE,CIRCLE,CUBE", builder.toString());
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
builder.append(i);
|
||||||
|
}
|
||||||
|
assertEquals("Test1,2,3,4,5abck zSINGLE,CIRCLE,CUBE0123456789", builder.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user