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

org.parboiled.trees.ImmutableTreeNode Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2009-2011 Mathias Doenitz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.parboiled.trees;

import java.util.List;

/**
 * An {@link ImmutableGraphNode} specialization representing a tree node with a parent field linking back to the nodes
 * (only) parent.
 *
 * @param  the actual implementation type of this ImmutableTreeNode
 */
public class ImmutableTreeNode> extends ImmutableGraphNode implements TreeNode {

    // we cannot make the parent field final since otherwise we can't create a tree hierarchy with parents linking to
    // their children and vice versa. So we design this for a bottom up tree construction strategy were children
    // are created first and then "acquired" by their parents
    private T parent;

    public ImmutableTreeNode() {
    }

    public ImmutableTreeNode(List children) {
        super(children);
        acquireChildren();
    }

    public T getParent() {
        return parent;
    }

    @SuppressWarnings({"unchecked"})
    protected void acquireChildren() {
        for (T children : getChildren()) {
            ((ImmutableTreeNode) children).parent = this;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy