2 package net.pterodactylus.util.data;
4 import java.util.Comparator;
5 import java.util.Iterator;
8 * A node that can be stored in a {@link Tree}. A node has exactly one parent
9 * (which is <code>null</code> if the node is the {@link Tree#getRootNode()}
10 * of the tree) and an arbitrary amount of child nodes.
12 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
14 * The type of the element to store
16 public interface Node<E extends Comparable<E>> extends Iterable<Node<E>>, Comparable<Node<E>> {
19 * Returns the parent node of the node.
21 * @return The parent node
23 public Node<E> getParent();
26 * Returns the element that is stored in the node.
28 * @return The node’s element
30 public E getElement();
33 * Adds an element as a child to this node and returns the created node.
36 * The child node’s element
37 * @return The created child node
39 public Node<E> addChild(E child);
42 * Returns the number of children this node has.
44 * @return The number of children
49 * Returns the child at the given index.
52 * The index of the child
53 * @return The child at the given index
55 public Node<E> getChild(int index);
58 * Returns the direct child node that contains the given element.
62 * @return The direct child node containing the given element, or
63 * <code>null</code> if this node does not have a child node
64 * containing the given element
66 public Node<E> getChild(E element);
69 * Returns whether the given node is a direct child of this node.
73 * @return <code>true</code> if the given node is a direct child of this
74 * node, <code>false</code> otherwise
76 public boolean hasChild(Node<E> childNode);
79 * Returns whether this node contains a child node containing the given
84 * @return <code>true</code> if this node contains a direct child node
85 * containing the given element, <code>false</code> otherwise
87 public boolean hasChild(E element);
90 * Returns the index of the given child node.
94 * @return The index of the child node, or <code>-1</code> if the child
95 * node is not a child node of this node
97 public int getIndexOfChild(Node<E> childNode);
100 * Returns the index of the child node containing the given element.
104 * @return The index of the child node, or <code>-1</code> if the child
105 * node is not a child node of this node
107 public int getIndexOfChild(E element);
110 * Remove the given child node from this node. If the given node is not a
111 * child of this node, nothing happens.
114 * The child node to remove
116 public void removeChild(Node<E> childNode);
119 * Removes the child node that contains the given element. The element in
120 * the node is checked using {@link Object#equals(Object)}.
123 * The child element to remove
125 public void removeChild(E child);
128 * Removes the child at the given index.
131 * The index of the child to remove
133 public void removeChild(int childIndex);
136 * Removes all children from this node.
138 public void removeAllChildren();
143 public Iterator<Node<E>> iterator();
146 * Searches this node’s children recursively for a node that contains the
150 * The element to search
151 * @return The node that contains the given element, or <code>null</code>
152 * if no node could be found
154 public Node<E> findChild(E element);
157 * Sorts all children according to their natural order.
159 public void sortChildren();
162 * Sorts all children with the given comparator.
165 * The comparator used to sort the children
167 public void sortChildren(Comparator<Node<E>> comparator);