node/README.org
Paul Campbell 5fb827adf6
Clean up changelog and readme, and remove external build dependencies (#38)
* [changelog] convert to org-mode format

* [readme] remove sonarcloud and codacy badges

* [jenkins] don't build with jdk 9 or 10

* [jenkins] add build with jdk 12

* [jenkins] remove codacy submission

* [jenkins] remove sonarcloud submission
2018-12-24 08:51:18 +00:00

91 lines
2.2 KiB
Org Mode

* Node
[[https://oss.sonatype.org/content/repositories/releases/net/kemitix/node][file:https://img.shields.io/nexus/r/https/oss.sonatype.org/net.kemitix/node.svg?style=for-the-badge]]
[[https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.kemitix%22%20AND%20a%3A%22node%22][file:https://img.shields.io/maven-central/v/net.kemitix/node.svg?style=for-the-badge]]
[[http://i.jpeek.org/net.kemitix/node/index.html][file:http://i.jpeek.org/net.kemitix/node/badge.svg]]
* A parent/children data structure
* Usage
Add as a dependency in your =pom.xml=:
#+BEGIN_SRC xml
<dependency>
<groupId>net.kemitix</groupId>
<artifactId>node</artifactId>
<version>${node.version}</version>
</dependency>
#+END_SRC
The library consits of an interface =Node= and an implementation =NodeItem=.
** Create a root node
#+BEGIN_SRC java
Node<String> root = new NodeItem<>("[root]");
#+END_SRC
** Get the contents of the node
#+BEGIN_SRC java
String rootData = root.getData(); // returns "[root]"
#+END_SRC
** Add a child node
#+BEGIN_SRC java
Node<String> child = root.createChild("child");
#+END_SRC
Which is shorthand for:
#+BEGIN_SRC java
Node<String> child = new NodeItem<>("child");
root.addChild(child);
#+END_SRC
The tree now looks like:
#+BEGIN_EXAMPLE
"[root]"
\-> "child"
#+END_EXAMPLE
** Get the child node
#+BEGIN_SRC java
Node<String> childNode = root.getChild("child");
#+END_SRC
** Create a chain of nodes
#+BEGIN_SRC java
root.createDescendantLine(Arrays.asList("alpha", "beta", "gamma"));
#+END_SRC
#+BEGIN_EXAMPLE
"[root]"
\-> "alpha"
\-> "beta"
\-> "gamma"
#+END_EXAMPLE
** Walk the tree to find a node
#+BEGIN_SRC java
Optional<Node<String>> foundNode = root.walkTree(Arrays.asList("alpha", "beta", "gamma"));
if (foundNode.isPresent()) {
String betaData = foundNode.get().getParent().getData();
// returns "beta"
}
#+END_SRC
** Get all children of a node
#+BEGIN_SRC java
Set<Node<String>> children = root.getChildren();
children.size();
// returns 2 ("child" and "alpha")
#+END_SRC