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

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

/*
 * 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
 */
// TODO: rename; this class IS NOT immutable
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(final List children)
    {
        super(children);
        // TODO: fix that! This absolutely sucks
        acquireChildren();
    }

    @Override
    public final T getParent()
    {
        return parent;
    }

    // TODO: make generic! Looks like a chore...
    protected final void acquireChildren()
    {
        final List children = getChildren();
        for (final T child : children)
            ((ImmutableTreeNode) child).parent = this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy