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

org.eclipse.jnosql.mapping.graph.DefaultEntityTree Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
/*
 *  Copyright (c) 2022 Contributors to the Eclipse Foundation
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */
package org.eclipse.jnosql.mapping.graph;

import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
import org.apache.tinkerpop.gremlin.structure.Vertex;

import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * The default implementation of {@link EntityTree}
 */
final class DefaultEntityTree implements EntityTree {

    private final GraphConverter converter;

    private final Tree tree;

    DefaultEntityTree(GraphConverter converter, Tree tree) {
        this.converter = converter;
        this.tree = tree;
    }

    @Override
    public  Stream getLeaf() {
        return tree.getLeafObjects()
                .stream()
                .map(converter::toEntity);
    }

    @Override
    public  Stream getRoots() {
        return tree.keySet()
                .stream()
                .map(converter::toEntity);
    }

    @Override
    public  Stream> getRootsIds() {
        return tree.keySet().stream()
                .map(v -> TreeEntry.of(v, converter));
    }

    @Override
    public  Optional getTreeFromRoot(T id) {
        Objects.requireNonNull(id, "id is required");
        return tree.keySet().stream()
                .filter(v -> id.equals(v.id()))
                .findFirst()
                .map(tree::get)
                .map(t -> new DefaultEntityTree(converter, t));
    }

    @Override
    public Stream getLeafTrees() {
        return tree.getLeafTrees()
                .stream()
                .map(t -> new DefaultEntityTree(converter, t));
    }

    @Override
    public Stream getTreesAtDepth(int depth) {
        return tree.getTreesAtDepth(depth)
                .stream()
                .map(t -> new DefaultEntityTree(converter, t));
    }

    @Override
    public  Stream getLeafsAtDepth(int depth) {
        return tree.getObjectsAtDepth(depth)
                .stream()
                .map(converter::toEntity);
    }

    @Override
    public boolean isLeaf() {
        return tree.isLeaf();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy