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

com.github.gumtreediff.tree.AbstractTree Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of GumTree.
 *
 * GumTree is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GumTree 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GumTree.  If not, see .
 *
 * Copyright 2011-2015 Jean-Rémy Falleri 
 * Copyright 2011-2015 Floréal Morandat 
 */

package com.github.gumtreediff.tree;

import com.github.gumtreediff.io.TreeIoUtils;

import java.util.*;

public abstract class AbstractTree implements Tree {
    protected Tree parent;

    protected List children;

    protected TreeMetrics metrics;

    @Override
    public String toString() {
        if (hasLabel())
            return String.format("%s: %s [%d,%d]",
                    getType(), getLabel(), getPos(), getEndPos());
        else
            return String.format("%s [%d,%d]",
                    getType(), getPos(), getEndPos());
    }

    @Override
    public String toTreeString() {
        return TreeIoUtils.toShortText(this).toString();
    }

    @Override
    public Tree getParent() {
        return parent;
    }

    @Override
    public void setParent(Tree parent) {
        this.parent = parent;
    }

    @Override
    public void setParentAndUpdateChildren(Tree parent) {
        if (this.parent != null)
            this.parent.getChildren().remove(this);
        this.parent = parent;
        if (this.parent != null)
            parent.getChildren().add(this);
    }

    @Override
    public List getChildren() {
        return children;
    }

    @Override
    public void setChildren(List children) {
        this.children = children;
        for (Tree c : children)
            c.setParent(this);
    }

    @Override
    public void addChild(Tree t) {
        children.add(t);
        t.setParent(this);
    }

    @Override
    public void insertChild(Tree t, int position) {
        children.add(position, t);
        t.setParent(this);
    }

    public TreeMetrics getMetrics() {
        if (metrics == null) {
            Tree root = this;
            if (!this.isRoot()) {
                List parents = this.getParents();
                root = parents.get(parents.size() - 1);
            }
            TreeVisitor.visitTree(root, new TreeMetricComputer());
        }

        return metrics;
    }

    public void setMetrics(TreeMetrics metrics) {
        this.metrics = metrics;
    }

    protected static class EmptyEntryIterator implements Iterator> {
        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public Map.Entry next() {
            throw new NoSuchElementException();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy