英文:
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;
}
}
我创建了执行相同操作的类 - 解析,将文本解析为段落,句子,标记,符号。
public class IntoParagraphParser implements ActionParser {
// 调用 IntoSentenceParser
}
public class IntoSentenceParser implements ActionParser {
// 调用 IntoLexemeParser
}
public class IntoLexemeParser implements ActionParser {
// 调用 IntoSymbolParser
}
public class IntoSymbolParser implements ActionParser {
}
所有数据存储在类-复合体CompositeText
的List<ComponentText> childComponents
中。
如何正确创建一个方法以便打印复合体内部的所有数据?
我认为这将是在CompositeText
中的toString()
方法。
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;
}
}
英文:
I have 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;
}
}
I created classes that perform the same action - parsing, parsing text into paragraphs, into sentences, into tokens, into symbols.
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 {
}
All data is stored in List <ComponentText> childComponents
in class-Composite - CompositeText
.
How to properly create a method so that it prints all the data that is inside the composite?
I think this will be the method toString()
in CompositeText
.
Class IntoParagraphParser
look:
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
得分: -1
Sure, here's the translated code snippet:
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (ComponentText component : childComponents) {
builder.append(component.toString());
}
return builder.toString();
}
英文:
Need @Override
the method toString()
in CompositeText
like this:
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (ComponentText component : childComponents) {
builder.append(component.toString());
}
return builder.toString();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论