2016-06-03 16:48:49 +01:00
[![Build Status ](https://travis-ci.org/kemitix/node.svg?branch=develop )](https://travis-ci.org/kemitix/node)
[![Coverage Status ](https://coveralls.io/repos/github/kemitix/node/badge.svg?branch=develop )](https://coveralls.io/github/kemitix/node?branch=develop)
2016-01-09 17:10:37 +00:00
# node
2016-05-18 07:51:26 +01:00
[![Join the chat at https://gitter.im/kemitix/node ](https://badges.gitter.im/kemitix/node.svg )](https://gitter.im/kemitix/node?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2016-01-09 17:10:37 +00:00
A parent/children data structure
2016-01-09 17:51:36 +00:00
# Usage
Add as a dependency in your `pom.xml` :
< dependency >
< groupId > net.kemitix< / groupId >
< artifactId > node< / artifactId >
< version > ${node.version}< / version >
< / dependency >
The library consits of an interface `Node` and an implementation `NodeItem` .
## Create a root node
Node< String > root = new NodeItem< >("[root]");
## Get the contents of the node
String rootData = root.getData(); // returns "[root]"
## Add a child node
2016-01-09 17:55:24 +00:00
Node< String > child = root.createChild("child");
2016-01-09 17:53:46 +00:00
Which is shorthand for:
2016-01-09 17:55:24 +00:00
Node< String > child = new NodeItem< >("child");
root.addChild(child);
2016-01-09 17:51:36 +00:00
The tree now looks like:
"[root]"
\-> "child"
## Get the child node
Node< String > childNode = root.getChild("child");
## Create a chain of nodes
root.createDescendantLine(Arrays.asList("alpha", "beta", "gamma"));
"[root]"
|-> "child"
\-> "alpha"
\-> "beta"
\-> "gamma"
## Walk the tree to find a node
Optional< Node < String > > foundNode = root.walkTree(Arrays.asList(
"alpha", "beta", "gamma"));
if (foundNode.isPresent()) {
String betaData = foundNode.get().getParent().getData();
// returns "beta"
}
## Get all children of a node
Set< Node < String > > children = root.getChildren();
children.size(); // returns 2 ("child" and "alpha")