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

org.aksw.commons.collections.trees.LabeledTree Maven / Gradle / Ivy

There is a newer version: 0.9.9
Show newest version
package org.aksw.commons.collections.trees;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import org.aksw.commons.collections.reversible.ReversibleMap;
import org.aksw.commons.collections.reversible.ReversibleMapImpl;


public class LabeledTree>
    implements Tree
{
    protected X rootNode;

    protected Map keyToNode;

    protected K root;
    //protected ListMultimap parentToChildren;
    //protected Map childToParent;
    protected ReversibleMap childToParent;
    //protected Map nodeToData;

    protected ReclaimingSupplier idSupplier;

    public LabeledTree() {
        this(null, new ReversibleMapImpl<>()); //, new HashMap<>());
    }
//
//    protected abstract LabeledNode createNode();
//
//    protected removeNode(K id) {
//
//    }

    public LabeledTree(K root, ReversibleMap childToParent) {// Map nodeToData) {
        super();
        this.root = root;
        this.childToParent = childToParent;
        this.keyToNode = new HashMap<>();
        //this.nodeToData = nodeToData;
    }

    @Override
    public K getRoot() {
        return root;
    }
//
//    public T getLabel(K key) {
//        T result = nodeToData.get(key);
//        return result;
//    }
//
//    public T setLabel(K key, T value) {
//        // TODO Validate that the node exists
//        T result = nodeToData.put(key, value);
//        return result;
//    }


    public X getNode(K key) {
        X result = keyToNode.get(key);
        return result;
    }


    @Override
    public Collection getChildren(K node) {
        return childToParent.reverse().get(node);
    }

    @Override
    public K getParent(K node) {
        return childToParent.get(node);
    }

    public X deleteNode(K node) {
        X result = keyToNode.get(node);

        // Call destroy before unlinking the node
        if(result != null) {
            result.destroy();
        }

        keyToNode.remove(node);
        childToParent.remove(node);

        return result;
    }
//
//    public static  LabeledTree create(T root, Function> getChildren, Supplier keySupplier) {
//
//        K rootKey = null;
//        ReversibleMap childToParent = new ReversibleMapImpl<>();
//        Map nodeToData = new HashMap<>();
//
//
//        LabeledTree result = new LabeledTree(rootKey, childToParent, nodeToData);
//        return result;
//    }

    @Override
    public K copy(K node, List children) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Tree createNew(K root) {
        throw new UnsupportedOperationException();
    }

    @Override
    public long nodeCount() {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toString() {
        return
            "LabeledTree [root=" + root + "]"
                + childToParent.reverse().toString();
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy