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

fr.ird.observe.navigation.model.DtoModelNavigationNode Maven / Gradle / Ivy

There is a newer version: 4.34
Show newest version
package fr.ird.observe.navigation.model;

/*-
 * #%L
 * ObServe Toolkit :: Common Dto
 * %%
 * Copyright (C) 2017 - 2020 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import fr.ird.observe.dto.IdDto;
import io.ultreia.java4all.bean.JavaBean;

import java.util.Objects;

/**
 * Created by tchemit on 26/05/2018.
 *
 * @author Tony Chemit - [email protected]
 */
public interface DtoModelNavigationNode extends JavaBean {

    String PROPERTY_ID = "id";

    String PROPERTY_ENABLED = "enabled";

    Class getType();

    String getModel();

    int getLevel();

    boolean accept(IdDto dto);

    String getId();

    void setId(String id);

    DtoModelNavigationNode getParent();

    ImmutableList> getChildren();

    > void init(N parentNode, ImmutableList> nodeStore);

    @SuppressWarnings("unchecked")
    default DtoModelNavigationNode getRoot() {
        return isRoot() ? this : getParent().getRoot();
    }

    default boolean isRoot() {
        return getParent() == null;
    }

    default boolean isLeaf() {
        return getChildren().isEmpty();
    }

    default boolean isEnabled() {
        return getId() != null;
    }

    default boolean isDisabled() {
        return !isEnabled();
    }

    default boolean acceptModel(String model) {
        return Objects.equals(getModel(), model);
    }

    default void copyTo(DtoModelNavigationNode targetNode) {
        targetNode.setId(getId());
        DtoModelNavigationNode parent = getParent();
        DtoModelNavigationNode targetNodeParent = targetNode.getParent();
        if (parent != null) {
            parent.copyTo(targetNodeParent);
        }
    }

    @SuppressWarnings("unchecked")
    default > void getIds(ImmutableList.Builder builder) {
        if (this.isEnabled()) {
            builder.add((N) this);
            for (DtoModelNavigationNode child : getChildren()) {
                child.getIds(builder);
            }
        }
    }

    default DtoModelNavigationNode upToSharedAncestor(DtoModelNavigationNode otherNode) {
        if (!acceptModel(otherNode.getModel())) {
            return null;
        }
        if (isRoot()) {
            return null;
        }
        ImmutableMap, DtoModelNavigationNode> otherNodesToRootMapping = Maps.uniqueIndex(otherNode.getNodesToRoot(), DtoModelNavigationNode::getType);
        for (DtoModelNavigationNode thisNode : getParent().getNodesToRoot()) {
            DtoModelNavigationNode otherOptionalNode = otherNodesToRootMapping.get(thisNode.getType());
            if (otherOptionalNode != null) {
                return thisNode;
            }
        }
        return null;
    }

    > ImmutableList getNodesFromAncestor(DtoModelNavigationNode other);

    > ImmutableList getNodesToAncestor(DtoModelNavigationNode other);

    default > ImmutableList getNodesFromRoot() {
        return getNodesFromAncestor(getRoot());
    }

    default > ImmutableList getNodesToRoot() {
        return getNodesToAncestor(getRoot());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy