All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.gematik.rbellogger.util.GenericPrettyPrinter Maven / Gradle / Ivy

There is a newer version: 0.28.2
Show newest version
package de.gematik.rbellogger.util;

import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class GenericPrettyPrinter {

    private final Predicate isLeaf;
    private final Function nodePrinter;
    private final Function> leafRetriever;
    private String openingBrace = "[";
    private String closingBrace = "]";
    private String depthStepper = "\t";

    public String prettyPrint(T root) {
        return prettyPrint(root, 0);
    }

    private String prettyPrint(T node, int depth) {
        if (isLeaf.test(node)) {
            return depthStepper.repeat(depth) + openingBrace + "\n"
                + leafRetriever.apply(node)
                    .map(leaf -> prettyPrint(leaf, depth + 1))
                    .collect(Collectors.joining(",\n"))
                + "\n" + depthStepper.repeat(depth) + closingBrace;
        } else {
            return depthStepper.repeat(depth) + nodePrinter.apply(node);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy