001/*
002 * Copyright (C) 2009-2011 Mathias Doenitz
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.parboiled.trees;
018
019/**
020 * A {@link TreeNode} specialiation that allow for mutability of the tree structure.
021 * The three defined methods are all expected to properly uphold the trees "linking back" contract, where children
022 * have their parent fields point to the node actually holding them in their children list at all times.
023 * The three defined methods are the basic ones required, other convenience methods (like a simple addChild(child)
024 * without index) are defined as static methods of the {@link TreeUtils} class.
025 *
026 * @param <T> the actual implementation type of this TreeNode
027 */
028public interface MutableTreeNode<T extends MutableTreeNode<T>> extends TreeNode<T> {
029
030    /**
031     * Adds the given child to this nodes children list and setting the childs parent field to this node.
032     * If the child is currently attached to another node it is first removed.
033     *
034     * @param index the index under which to insert this child into the children list
035     * @param child the child node to add
036     */
037    void addChild(int index, T child);
038
039    /**
040     * Sets the child node at the given index to the given node. The node previously existing at the given child index
041     * is first properly removed by setting its parent field to null. If the child is currently attached to another
042     * node it is first removed from its old parent.
043     *
044     * @param index the index under which to set this child into the children list
045     * @param child the child node to set
046     */
047    void setChild(int index, T child);
048
049    /**
050     * Removes the child with the given index.
051     *
052     * @param index the index of the child to remove.
053     * @return the removed child
054     */
055    T removeChild(int index);
056
057}