如何打印使用Composite创建的整个数据结构?

huangapple go评论74阅读模式
英文:

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 {
}

所有数据存储在类-复合体CompositeTextList<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&lt;ComponentText&gt; childComponents;

    private CompositeText() {
        childComponents = new ArrayList&lt;&gt;();
    }

    public CompositeText(String value, TypeComponent typeComponent) {
        this.value = value;
        this.type = typeComponent;
        childComponents = new ArrayList&lt;&gt;();
    }

    @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 &lt;ComponentText&gt; 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 = &quot;(?m)(?=^\\s{4})&quot;;
    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();
    }

huangapple
  • 本文由 发表于 2020年9月7日 00:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63766168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定