如何打印使用 Composite 创建的整个数据结构?
How print the entire data structure created with Composite?
我有 class-Composite
:
public class CompositeText implements ComponentText {
private TypeComponent type;
private String value;
private final List<ComponentText> childComponents;
private CompositeText() {
childComponents = new ArrayList<>();
}
public CompositeText(String value, TypeComponent typeComponent) {
this.value = value;
this.type = typeComponent;
childComponents = new ArrayList<>();
}
@Override
public void add(ComponentText componentText) {
childComponents.add(componentText);
}
@Override
public void remove(ComponentText componentText) {
childComponents.remove(componentText);
}
@Override
public TypeComponent getComponentType() {
return this.type;
}
@Override
public ComponentText getChild(int index) {
return childComponents.get(index);
}
@Override
public int getCountChildElements() {
return childComponents.size();
}
@Override
public int getCountAllElements() {
return childComponents.stream()
.mapToInt(ComponentText::getCountAllElements)
.sum();
}
@Override
public String toString() {
return null;
}
}
我创建了执行相同操作的 classes - 解析,将文本解析为段落、句子、标记、符号。
public class IntoParagraphParser implements ActionParser {
// call IntoSentenceParser
}
public class IntoSentenceParser implements ActionParser {
// call IntoLexemeParser
}
public class IntoLexemeParser implements ActionParser {
// call IntoSymbolParser
}
public class IntoSymbolParser implements ActionParser {
}
所有数据都存储在 List <ComponentText> childComponents
中 class-Composite - CompositeText
.
如何正确创建一个方法,以便它打印复合内的所有数据?
我认为这将是 CompositeText
中的方法 toString()
。
ClassIntoParagraphParser
看:
public class IntoParagraphParser implements ActionParser {
private static final String PARAGRAPH_SPLIT_REGEX = "(?m)(?=^\s{4})";
private static final IntoParagraphParser paragraphParser = new IntoParagraphParser();
private static final IntoSentenceParser sentenceParser = IntoSentenceParser.getInstance();
private IntoParagraphParser() {
}
public static IntoParagraphParser getInstance() {
return paragraphParser;
}
public ComponentText parse(String text) throws TextException {
ComponentText oneParagraph;
ComponentText componentParagraph = new CompositeText(text, TypeComponent.PARAGRAPH);
String[] arrayParagraph = text.split(PARAGRAPH_SPLIT_REGEX);
for(String element: arrayParagraph) {
oneParagraph = new CompositeText(element, TypeComponent.PARAGRAPH);
oneParagraph.add(sentenceParser.parse(element));
componentParagraph.add(oneParagraph);
}
return componentParagraph;
}
}
需要 @Override
CompositeText
中的方法 toString()
像这样:
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (ComponentText component : childComponents) {
builder.append(component.toString());
}
return builder.toString();
}
但是如何用Stream API
正确地写这段代码呢?
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
childComponents.stream().map(...????
return builder.toString();
}
我有 class-Composite
:
public class CompositeText implements ComponentText {
private TypeComponent type;
private String value;
private final List<ComponentText> childComponents;
private CompositeText() {
childComponents = new ArrayList<>();
}
public CompositeText(String value, TypeComponent typeComponent) {
this.value = value;
this.type = typeComponent;
childComponents = new ArrayList<>();
}
@Override
public void add(ComponentText componentText) {
childComponents.add(componentText);
}
@Override
public void remove(ComponentText componentText) {
childComponents.remove(componentText);
}
@Override
public TypeComponent getComponentType() {
return this.type;
}
@Override
public ComponentText getChild(int index) {
return childComponents.get(index);
}
@Override
public int getCountChildElements() {
return childComponents.size();
}
@Override
public int getCountAllElements() {
return childComponents.stream()
.mapToInt(ComponentText::getCountAllElements)
.sum();
}
@Override
public String toString() {
return null;
}
}
我创建了执行相同操作的 classes - 解析,将文本解析为段落、句子、标记、符号。
public class IntoParagraphParser implements ActionParser {
// call IntoSentenceParser
}
public class IntoSentenceParser implements ActionParser {
// call IntoLexemeParser
}
public class IntoLexemeParser implements ActionParser {
// call IntoSymbolParser
}
public class IntoSymbolParser implements ActionParser {
}
所有数据都存储在 List <ComponentText> childComponents
中 class-Composite - CompositeText
.
如何正确创建一个方法,以便它打印复合内的所有数据?
我认为这将是 CompositeText
中的方法 toString()
。
ClassIntoParagraphParser
看:
public class IntoParagraphParser implements ActionParser {
private static final String PARAGRAPH_SPLIT_REGEX = "(?m)(?=^\s{4})";
private static final IntoParagraphParser paragraphParser = new IntoParagraphParser();
private static final IntoSentenceParser sentenceParser = IntoSentenceParser.getInstance();
private IntoParagraphParser() {
}
public static IntoParagraphParser getInstance() {
return paragraphParser;
}
public ComponentText parse(String text) throws TextException {
ComponentText oneParagraph;
ComponentText componentParagraph = new CompositeText(text, TypeComponent.PARAGRAPH);
String[] arrayParagraph = text.split(PARAGRAPH_SPLIT_REGEX);
for(String element: arrayParagraph) {
oneParagraph = new CompositeText(element, TypeComponent.PARAGRAPH);
oneParagraph.add(sentenceParser.parse(element));
componentParagraph.add(oneParagraph);
}
return componentParagraph;
}
}
需要 @Override
CompositeText
中的方法 toString()
像这样:
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (ComponentText component : childComponents) {
builder.append(component.toString());
}
return builder.toString();
}
但是如何用Stream API
正确地写这段代码呢?
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
childComponents.stream().map(...????
return builder.toString();
}