add getChild(E)
[jSite2.git] / src / net / pterodactylus / util / data / Node.java
1
2 package net.pterodactylus.util.data;
3
4 import java.util.Iterator;
5
6 /**
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.
10  * 
11  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
12  * @param <E>
13  *            The type of the element to store
14  */
15 public interface Node<E> extends Iterable<Node<E>> {
16
17         /**
18          * Returns the parent node of the node.
19          * 
20          * @return The parent node
21          */
22         public Node<E> getParent();
23
24         /**
25          * Returns the element that is stored in the node.
26          * 
27          * @return The node’s element
28          */
29         public E getElement();
30
31         /**
32          * Adds an element as a child to this node and returns the created node.
33          * 
34          * @param child
35          *            The child node’s element
36          * @return The created child node
37          */
38         public Node<E> addChild(E child);
39
40         /**
41          * Returns the number of children this node has.
42          * 
43          * @return The number of children
44          */
45         public int size();
46
47         /**
48          * Returns the child at the given index.
49          * 
50          * @param index
51          *            The index of the child
52          * @return The child at the given index
53          */
54         public Node<E> getChild(int index);
55
56         /**
57          * Returns the direct child node that contains the given element.
58          * 
59          * @param element
60          *            The element
61          * @return The direct child node containing the given element, or
62          *         <code>null</code> if this node does not have a child node
63          *         containing the given element
64          */
65         public Node<E> getChild(E element);
66
67         /**
68          * Returns whether the given node is a direct child of this node.
69          * 
70          * @param childNode
71          *            The child node
72          * @return <code>true</code> if the given node is a direct child of this
73          *         node, <code>false</code> otherwise
74          */
75         public boolean hasChild(Node<E> childNode);
76
77         /**
78          * Returns whether this node contains a child node containing the given
79          * element.
80          * 
81          * @param element
82          *            The element
83          * @return <code>true</code> if this node contains a direct child node
84          *         containing the given element, <code>false</code> otherwise
85          */
86         public boolean hasChild(E element);
87
88         /**
89          * Returns the index of the given child node.
90          * 
91          * @param childNode
92          *            The child node
93          * @return The index of the child node, or <code>-1</code> if the child
94          *         node is not a child node of this node
95          */
96         public int getIndexOfChild(Node<E> childNode);
97
98         /**
99          * Returns the index of the child node containing the given element.
100          * 
101          * @param element
102          *            The element
103          * @return The index of the child node, or <code>-1</code> if the child
104          *         node is not a child node of this node
105          */
106         public int getIndexOfChild(E element);
107
108         /**
109          * Remove the given child node from this node. If the given node is not a
110          * child of this node, nothing happens.
111          * 
112          * @param childNode
113          *            The child node to remove
114          */
115         public void removeChild(Node<E> childNode);
116
117         /**
118          * Removes the child node that contains the given element. The element in
119          * the node is checked using {@link Object#equals(Object)}.
120          * 
121          * @param child
122          *            The child element to remove
123          */
124         public void removeChild(E child);
125
126         /**
127          * Removes the child at the given index.
128          * 
129          * @param childIndex
130          *            The index of the child to remove
131          */
132         public void removeChild(int childIndex);
133
134         /**
135          * Removes all children from this node.
136          */
137         public void removeAllChildren();
138
139         /**
140          * {@inheritDoc}
141          */
142         public Iterator<Node<E>> iterator();
143
144         /**
145          * Searches this node’s children recursively for a node that contains the
146          * given element.
147          * 
148          * @param element
149          *            The element to search
150          * @return The node that contains the given element, or <code>null</code>
151          *         if no node could be found
152          */
153         public Node<E> findChild(E element);
154
155 }