Text: изменён builder
This commit is contained in:
@@ -4,6 +4,8 @@ import com.google.common.collect.ImmutableList;
|
|||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
@EqualsAndHashCode
|
@EqualsAndHashCode
|
||||||
@ToString
|
@ToString
|
||||||
public class Text {
|
public class Text {
|
||||||
@@ -27,15 +29,10 @@ public class Text {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private String content;
|
private final LinkedList<Object> chain = new LinkedList<>();
|
||||||
private ImmutableList.Builder<Text> childrenBuilder;
|
|
||||||
|
|
||||||
public Builder append(String content) {
|
public Builder append(String content) {
|
||||||
if (this.content == null) {
|
chain.add(content);
|
||||||
this.content = content;
|
|
||||||
} else {
|
|
||||||
this.content += content;
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,21 +41,37 @@ public class Text {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.childrenBuilder == null) {
|
chain.add(text);
|
||||||
this.childrenBuilder = ImmutableList.builder();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.childrenBuilder.add(text);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Text build() {
|
public Text build() {
|
||||||
if (content == null && childrenBuilder == null) {
|
if (chain.isEmpty()) {
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder contentBuilder = null;
|
||||||
|
ImmutableList.Builder<Text> childrenBuilder = null;
|
||||||
|
|
||||||
|
for (Object element : chain) {
|
||||||
|
if (element instanceof String) {
|
||||||
|
if (contentBuilder == null) {
|
||||||
|
contentBuilder = new StringBuilder((String) element);
|
||||||
} else {
|
} else {
|
||||||
return new Text(content,
|
contentBuilder.append((String) element);
|
||||||
|
}
|
||||||
|
} else if (element instanceof Text) {
|
||||||
|
if (childrenBuilder == null) {
|
||||||
|
childrenBuilder = ImmutableList.builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
childrenBuilder.add((Text) element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Text(
|
||||||
|
contentBuilder == null ? null : contentBuilder.toString(),
|
||||||
childrenBuilder == null ? null : childrenBuilder.build());
|
childrenBuilder == null ? null : childrenBuilder.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user