public Node<E> getChild(int index);
/**
+ * Returns whether the given node is a direct child of this node.
+ *
+ * @param childNode
+ * The child node
+ * @return <code>true</code> if the given node is a direct child of this
+ * node, <code>false</code> otherwise
+ */
+ public boolean hasChildNode(Node<E> childNode);
+
+ /**
+ * Returns the index of the given child node.
+ *
+ * @param childNode
+ * The child node
+ * @return The index of the child node, or <code>-1</code> if the child
+ * node is not a child node of this node
+ */
+ public int getIndexOfChild(Node<E> childNode);
+
+ /**
+ * Returns the index of the child node containing the given element.
+ *
+ * @param element
+ * The element
+ * @return The index of the child node, or <code>-1</code> if the child
+ * node is not a child node of this node
+ */
+ public int getIndexOfChild(E element);
+
+ /**
* Remove the given child node from this node. If the given node is not a
* child of this node, nothing happens.
*
*/
public Iterator<Node<E>> iterator();
+ /**
+ * Searches this node’s children recursively for a node that contains the
+ * given element.
+ *
+ * @param element
+ * The element to search
+ * @return The node that contains the given element, or <code>null</code>
+ * if no node could be found
+ */
+ public Node<E> findChild(E element);
+
}
\ No newline at end of file
/**
* {@inheritDoc}
*/
+ public boolean hasChildNode(Node<E> childNode) {
+ return children.contains(childNode);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getIndexOfChild(Node<E> childNode) {
+ int childIndex = 0;
+ for (Node<E> node: children) {
+ if (node.equals(childNode)) {
+ return childIndex;
+ }
+ childIndex++;
+ }
+ return -1;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getIndexOfChild(E element) {
+ int childIndex = 0;
+ for (Node<E> node: children) {
+ if (node.getElement().equals(element)) {
+ return childIndex;
+ }
+ childIndex++;
+ }
+ return -1;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public void removeChild(Node<E> childNode) {
children.remove(childNode);
}
return children.iterator();
}
+ /**
+ * {@inheritDoc}
+ */
+ public Node<E> findChild(E element) {
+ for (Node<E> childNode: children) {
+ Node<E> wantedNode = childNode.findChild(element);
+ if (wantedNode != null) {
+ return wantedNode;
+ }
+ }
+ if (this.element.equals(element)) {
+ return this;
+ }
+ return null;
+ }
+
}
\ No newline at end of file