Archived
0

Text: упрощен builder

This commit is contained in:
2021-04-28 22:12:21 +03:00
parent b255980c05
commit c722da2b05
4 changed files with 103 additions and 54 deletions

View File

@@ -1,10 +1,11 @@
package mc.protocol.model.text;
import com.google.common.collect.ImmutableList;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@EqualsAndHashCode
@ToString
@@ -12,16 +13,32 @@ public class Text {
public static final Text EMPTY = of("");
private TextColor color;
private TextStyle style;
private String content;
private ImmutableList<Text> children;
private List<Text> children;
Text(String content, ImmutableList<Text> children) {
Text(TextColor color, TextStyle style, String content, List<Text> children) {
this.color = color;
this.style = style;
this.content = content;
this.children = children;
}
public static Text of(String content) {
return new Text(content, null);
return new Text(null, null, content, null);
}
public static Text of(TextColor color, String content) {
return new Text(color, null, content, null);
}
public static Text of(TextStyle style, String content) {
return new Text(null, style, content, null);
}
public static Text of(TextColor color, TextStyle style, String content) {
return new Text(color, style, content, null);
}
public static Text.Builder builder() {
@@ -29,12 +46,7 @@ public class Text {
}
public static class Builder {
private final LinkedList<Object> chain = new LinkedList<>();
public Builder append(String content) {
chain.add(content);
return this;
}
private final LinkedList<Text> chain = new LinkedList<>();
public Builder append(Text text) {
if (text == null || EMPTY.equals(text)) {
@@ -50,28 +62,14 @@ public class Text {
return EMPTY;
}
StringBuilder contentBuilder = null;
ImmutableList.Builder<Text> childrenBuilder = null;
Text rootText = chain.pollFirst();
for (Object element : chain) {
if (element instanceof String) {
if (contentBuilder == null) {
contentBuilder = new StringBuilder((String) element);
} else {
contentBuilder.append((String) element);
}
} else if (element instanceof Text) {
if (childrenBuilder == null) {
childrenBuilder = ImmutableList.builder();
if (!chain.isEmpty()) {
rootText.children = new ArrayList<>();
rootText.children.addAll(chain);
}
childrenBuilder.add((Text) element);
}
}
return new Text(
contentBuilder == null ? null : contentBuilder.toString(),
childrenBuilder == null ? null : childrenBuilder.build());
return rootText;
}
}
}

View File

@@ -0,0 +1,31 @@
package mc.protocol.model.text;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Getter
public enum TextColor {
//@formatter:off
BLACK ("black", '0'),
DARK_BLUE ("dark_blue", '1'),
DARK_GREEN ("dark_green", '2'),
DARK_AQUA ("dark_aqua", '3'),
DARK_RED ("dark_red", '4'),
DARK_PUEPLE("dark_purple", '5'),
GOLD ("gold", '6'),
GRAY ("gray", '7'),
DARK_GRAY ("dark_gray", '8'),
BLUE ("blue", '9'),
GREEN ("green", 'a'),
AQUA ("aqua", 'b'),
RED ("red", 'c'),
PUEPLE ("light_purple",'d'),
YELLOW ("yellow", 'e'),
WHITE ("white", 'f'),
RESET ("reset", 'r');
//@formatter:on
private final String name;
private final char code;
}

View File

@@ -0,0 +1,38 @@
package mc.protocol.model.text;
import lombok.*;
import lombok.experimental.Accessors;
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder(builderClassName = "Builder")
@Accessors(fluent = true)
@Getter
@Setter(AccessLevel.PACKAGE)
@EqualsAndHashCode
@ToString
@SuppressWarnings("java:S1845")
public class TextStyle {
public static final TextStyle BOLD = new TextStyle(true, null, null, null, null);
public static final TextStyle ITALIC = new TextStyle(null, true, null, null, null);
public static final TextStyle UNDERLINE = new TextStyle(null, null, true, null, null);
public static final TextStyle STRIKETHOUGH = new TextStyle(null, null, null, true, null);
public static final TextStyle OBFUSCATED = new TextStyle(null, null, null, null, true);
public static final TextStyle RESET = new TextStyle(false, false, false, false, false);
public static final TextStyle NONE = new TextStyle(null, null, null, null, null);
private Boolean bold;
private Boolean italic;
private Boolean underline;
private Boolean strikethrough;
private Boolean obfuscated;
void merge(TextStyle style) {
if (style.bold != null) this.bold = style.bold;
if (style.italic != null) this.italic = style.italic;
if (style.underline != null) this.underline = style.underline;
if (style.strikethrough != null) this.strikethrough = style.strikethrough;
if (style.obfuscated != null) this.obfuscated = style.obfuscated;
}
}

View File

@@ -1,9 +1,10 @@
package mc.protocol.model.text;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
class TextTest {
@@ -20,31 +21,12 @@ class TextTest {
Text actual;
Text expected;
actual = Text.builder().append("123").build();
expected = new Text("123", null);
actual = Text.builder().append(Text.of("123")).build();
expected = new Text(null, null, "123", null);
assertEquals(expected, actual);
actual = Text.builder().append("123").append("456").build();
expected = new Text("123456", null);
assertEquals(expected, actual);
}
@Test
void childrenTest() {
Text actual;
Text expected;
actual = Text.builder().append("123").append((Text) null).build();
expected = new Text("123", null);
assertEquals(expected, actual);
actual = Text.builder().append("123").append(Text.EMPTY).build();
expected = new Text("123", null);
assertEquals(expected, actual);
Text child = Text.of("456");
actual = Text.builder().append("123").append(child).build();
expected = new Text("123", ImmutableList.of(child));
actual = Text.builder().append(Text.of("123")).append(Text.of("456")).build();
expected = new Text(null, null, "123", List.of(Text.of("456")));
assertEquals(expected, actual);
}
}