2 package net.pterodactylus.util.data;
4 import java.util.Iterator;
7 * A node that can be stored in a {@link Tree}. A node has exactly one parent
8 * (which is <code>null</code> if the node is the {@link Tree#getRootNode()}
9 * of the tree) and an arbitrary amount of child nodes.
11 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
13 * The type of the element to store
15 public interface Node<E> extends Iterable<Node<E>> {
18 * Returns the parent node of the node.
20 * @return The parent node
22 public Node<E> getParent();
25 * Returns the element that is stored in the node.
27 * @return The node’s element
29 public E getElement();
32 * Adds an element as a child to this node and returns the created node.
35 * The child node’s element
36 * @return The created child node
38 public Node<E> addChild(E child);
41 * Returns the number of children this node has.
43 * @return The number of children
48 * Returns the child at the given index.
51 * The index of the child
52 * @return The child at the given index
54 public Node<E> getChild(int index);
57 * Returns whether the given node is a direct child of this node.
61 * @return <code>true</code> if the given node is a direct child of this
62 * node, <code>false</code> otherwise
64 public boolean hasChild(Node<E> childNode);
67 * Returns whether this node contains a child node containing the given
72 * @return <code>true</code> if this node contains a direct child node
73 * containing the given element, <code>false</code> otherwise
75 public boolean hasChild(E element);
78 * Returns the index of the given child node.
82 * @return The index of the child node, or <code>-1</code> if the child
83 * node is not a child node of this node
85 public int getIndexOfChild(Node<E> childNode);
88 * Returns the index of the child node containing the given element.
92 * @return The index of the child node, or <code>-1</code> if the child
93 * node is not a child node of this node
95 public int getIndexOfChild(E element);
98 * Remove the given child node from this node. If the given node is not a
99 * child of this node, nothing happens.
102 * The child node to remove
104 public void removeChild(Node<E> childNode);
107 * Removes the child node that contains the given element. The element in
108 * the node is checked using {@link Object#equals(Object)}.
111 * The child element to remove
113 public void removeChild(E child);
116 * Removes the child at the given index.
119 * The index of the child to remove
121 public void removeChild(int childIndex);
124 * Removes all children from this node.
126 public void removeAllChildren();
131 public Iterator<Node<E>> iterator();
134 * Searches this node’s children recursively for a node that contains the
138 * The element to search
139 * @return The node that contains the given element, or <code>null</code>
140 * if no node could be found
142 public Node<E> findChild(E element);