Archived
0

Text: дополнен children

This commit is contained in:
2021-04-28 21:27:38 +03:00
parent 30bb894b7c
commit df4c1b7c71
2 changed files with 44 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
package mc.protocol.model.text; package mc.protocol.model.text;
import com.google.common.collect.ImmutableList;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
@@ -10,13 +11,15 @@ public class Text {
public static final Text EMPTY = of(""); public static final Text EMPTY = of("");
private String content; private String content;
private ImmutableList<Text> children;
Text(String content) { Text(String content, ImmutableList<Text> children) {
this.content = content; this.content = content;
this.children = children;
} }
public static Text of(String content) { public static Text of(String content) {
return new Text(content); return new Text(content, null);
} }
public static Text.Builder builder() { public static Text.Builder builder() {
@@ -25,6 +28,7 @@ public class Text {
public static class Builder { public static class Builder {
private String content; private String content;
private ImmutableList.Builder<Text> childrenBuilder;
public Builder append(String content) { public Builder append(String content) {
if (this.content == null) { if (this.content == null) {
@@ -35,11 +39,25 @@ public class Text {
return this; return this;
} }
public Builder append(Text text) {
if (text == null || EMPTY.equals(text)) {
return this;
}
if (this.childrenBuilder == null) {
this.childrenBuilder = ImmutableList.builder();
}
this.childrenBuilder.add(text);
return this;
}
public Text build() { public Text build() {
if (content == null) { if (content == null && childrenBuilder == null) {
return EMPTY; return EMPTY;
} else { } else {
return new Text(content); return new Text(content,
childrenBuilder == null ? null : childrenBuilder.build());
} }
} }
} }

View File

@@ -1,5 +1,6 @@
package mc.protocol.model.text; package mc.protocol.model.text;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@@ -20,11 +21,30 @@ class TextTest {
Text expected; Text expected;
actual = Text.builder().append("123").build(); actual = Text.builder().append("123").build();
expected = new Text("123"); expected = new Text("123", null);
assertEquals(expected, actual); assertEquals(expected, actual);
actual = Text.builder().append("123").append("456").build(); actual = Text.builder().append("123").append("456").build();
expected = new Text("123456"); 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));
assertEquals(expected, actual); assertEquals(expected, actual);
} }
} }