From: David ‘Bombe’ Roden Date: Mon, 26 May 2008 18:27:05 +0000 (+0200) Subject: add more methods X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=364659907c0364092e78824d3eea4a13fecd831b;p=jSite2.git add more methods --- diff --git a/src/net/pterodactylus/util/data/Node.java b/src/net/pterodactylus/util/data/Node.java index 7737d50..3ba6004 100644 --- a/src/net/pterodactylus/util/data/Node.java +++ b/src/net/pterodactylus/util/data/Node.java @@ -54,6 +54,36 @@ public interface Node extends Iterable> { public Node getChild(int index); /** + * Returns whether the given node is a direct child of this node. + * + * @param childNode + * The child node + * @return true if the given node is a direct child of this + * node, false otherwise + */ + public boolean hasChildNode(Node childNode); + + /** + * Returns the index of the given child node. + * + * @param childNode + * The child node + * @return The index of the child node, or -1 if the child + * node is not a child node of this node + */ + public int getIndexOfChild(Node childNode); + + /** + * Returns the index of the child node containing the given element. + * + * @param element + * The element + * @return The index of the child node, or -1 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. * @@ -84,4 +114,15 @@ public interface Node extends Iterable> { */ public Iterator> 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 null + * if no node could be found + */ + public Node findChild(E element); + } \ No newline at end of file diff --git a/src/net/pterodactylus/util/data/NodeImpl.java b/src/net/pterodactylus/util/data/NodeImpl.java index a30f76e..2d57876 100644 --- a/src/net/pterodactylus/util/data/NodeImpl.java +++ b/src/net/pterodactylus/util/data/NodeImpl.java @@ -87,6 +87,41 @@ class NodeImpl implements Node { /** * {@inheritDoc} */ + public boolean hasChildNode(Node childNode) { + return children.contains(childNode); + } + + /** + * {@inheritDoc} + */ + public int getIndexOfChild(Node childNode) { + int childIndex = 0; + for (Node node: children) { + if (node.equals(childNode)) { + return childIndex; + } + childIndex++; + } + return -1; + } + + /** + * {@inheritDoc} + */ + public int getIndexOfChild(E element) { + int childIndex = 0; + for (Node node: children) { + if (node.getElement().equals(element)) { + return childIndex; + } + childIndex++; + } + return -1; + } + + /** + * {@inheritDoc} + */ public void removeChild(Node childNode) { children.remove(childNode); } @@ -117,4 +152,20 @@ class NodeImpl implements Node { return children.iterator(); } + /** + * {@inheritDoc} + */ + public Node findChild(E element) { + for (Node childNode: children) { + Node wantedNode = childNode.findChild(element); + if (wantedNode != null) { + return wantedNode; + } + } + if (this.element.equals(element)) { + return this; + } + return null; + } + } \ No newline at end of file