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

irlmq6kh  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(373)

我有 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;
    }
}
von4xj4u

von4xj4u1#

需要 @Override 方法 toString()CompositeText 这样地:

@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();
    }

相关问题