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

com.kenshoo.pl.entity.Hierarchy Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.entity;

import org.apache.commons.lang3.tuple.Pair;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toSet;
import static org.jooq.lambda.Seq.seq;


class Hierarchy {

    private final EntityType root;
    private final Set, EntityType>> parentChildRelations;

    private Hierarchy(EntityType root, Set, EntityType>> parentChildRelations) {
        this.root = root;
        this.parentChildRelations = parentChildRelations;
    }

    static Hierarchy build(ChangeFlowConfig rootFlow) {
        Set, EntityType>> relations = getRelationsRecursively(rootFlow).collect(toSet());
        return new Hierarchy(rootFlow.getEntityType(), relations);
    }

    Collection> childrenTypes(EntityType parent) {
        return seq(parentChildRelations)
                .filter(pair -> pair.getLeft().equals(parent))
                .map(Pair::getRight)
                .toList();
    }

    EntityType root() {
        return root;
    }

    boolean contains(EntityField field) {
        return seq(parentChildRelations).anyMatch(
                pair -> field.getEntityType().equals(pair.getLeft()) ||
                        field.getEntityType().equals(pair.getRight())
        );
    }

    private static Stream, EntityType>> getRelationsRecursively(ChangeFlowConfig flow) {
        Stream, EntityType>> myRelations = flow.childFlows().stream().map(childFlow -> Pair.of(flow.getEntityType(), childFlow.getEntityType()));
        return Stream.concat(myRelations, flow.childFlows().stream().flatMap(f -> getRelationsRecursively(f)));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy