我有 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;
}
}
我创建了执行相同操作的类—解析,将文本解析为段落、句子、标记和符号。
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
类内复合- CompositeText
.
如何正确地创建一个方法,以便它打印出组合中的所有数据?我想这就是方法 toString()
在 CompositeText
.
班级 IntoParagraphParser
看:
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;
}
}
1条答案
按热度按时间von4xj4u1#
需要
@Override
方法toString()
在CompositeText
这样地:但是如何用
Stream API
?