Archived
0

добавлено тестирование Text

This commit is contained in:
2018-09-22 20:24:10 +03:00
parent 80a7e7aaa8
commit 969503cc45
2 changed files with 113 additions and 21 deletions

View File

@@ -1,20 +1,11 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-06-11
*/
package mc.core.text;
import com.google.common.collect.ImmutableList;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import java.util.*;
@Getter
@EqualsAndHashCode
public class Text {
private static final Text EMPTY = new Text();
private static final Text NEW_LINE = new Text("\n", null, null, null);
@@ -39,7 +30,7 @@ public class Text {
}
public boolean isEmpty() {
boolean result = content.isEmpty();
boolean result = (content == null || content.isEmpty());
if (children != null && !children.isEmpty()) {
for (Text child : children) {
@@ -60,6 +51,19 @@ public class Text {
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Text text = (Text) o;
return Objects.equals(toPlain(), text.toPlain());
}
@Override
public int hashCode() {
return Objects.hash(toPlain());
}
public static class Builder {
@Getter
private String content;
@@ -81,6 +85,8 @@ public class Text {
}
public Builder(Object... objects) {
this.children = new ArrayList<>();
for(Object obj : objects) {
if (obj instanceof String) {
if (this.content == null) {
@@ -96,10 +102,10 @@ public class Text {
}
} else if (obj instanceof TextColor) {
this.color = (TextColor) obj;
} else if (obj instanceof Text) {
children.add((Text) obj);
}
}
this.children = new ArrayList<>();
}
public List<Text> getChildren() {
@@ -133,8 +139,14 @@ public class Text {
return this;
}
public Builder append(String string) {
return append(Text.of(string));
}
public Builder append(Text child) {
this.children.add(child);
if (child != null) {
this.children.add(child);
}
return this;
}
@@ -144,16 +156,20 @@ public class Text {
}
public Text build() {
if (children.isEmpty()) {
if (children.isEmpty() && (content == null || content.isEmpty())) {
return Text.EMPTY;
}
return new Text(
content,
color,
style,
ImmutableList.copyOf(children)
);
if (children.size() == 1 && children.get(0) != null) {
return children.get(0);
} else {
return new Text(
content,
color,
style,
ImmutableList.copyOf(children)
);
}
}
}