Skip to content
This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
/ node Public archive

[MOVED] A parent/children data structure

License

Notifications You must be signed in to change notification settings

kemitix/node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node

https://img.shields.io/nexus/r/https/oss.sonatype.org/net.kemitix/node.svg?style=for-the-badge https://img.shields.io/maven-central/v/net.kemitix/node.svg?style=for-the-badge

http://i.jpeek.org/net.kemitix/node/badge.svg

A parent/children data structure

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

Node<String> child = root.createChild("child");

Which is shorthand for:

Node<String> child = new NodeItem<>("child");
root.addChild(child);

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]"
\-> "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")