/simulatest-environment/src/main/java/org/simulatest/environment/tree/Node.java
Java | 79 lines | 58 code | 21 blank | 0 comment | 5 complexity | 134789df0af667428583d35036c8bb2a MD5 | raw file
1package org.simulatest.environment.tree; 2 3import java.util.LinkedList; 4 5public class Node<T> { 6 7 private T value; 8 private Node<T> parent; 9 private LinkedList<Node<T>> children; 10 11 public Node(T value) { 12 this.value = value; 13 this.children = new LinkedList<Node<T>>(); 14 } 15 16 public void addChild(Node<T> child) { 17 if (children.contains(child)) return; 18 19 child.setParent(this); 20 children.add(child); 21 } 22 23 public T getValue() { 24 return value; 25 } 26 27 public Node<T> getParent() { 28 return parent; 29 } 30 31 protected void setParent(Node<T> parent) { 32 this.parent = parent; 33 } 34 35 public boolean hasParent() { 36 return parent != null; 37 } 38 39 public LinkedList<Node<T>> getChildren() { 40 return children; 41 } 42 43 public Node<T> getFirstChild() { 44 return children.isEmpty() ? null : children.getFirst(); 45 } 46 47 public Node<T> getLastChild() { 48 return children.isEmpty() ? null : children.getLast(); 49 } 50 51 public boolean isLastChild() { 52 return hasParent() ? isTheLastChildOfParent() : false; 53 } 54 55 private boolean isTheLastChildOfParent() { 56 return this.equals(getParent().getLastChild()); 57 } 58 59 public int totalQuantityOfNodes() { 60 int size = 1; 61 for (Node<T> child : children) size += child.totalQuantityOfNodes(); 62 63 return size; 64 } 65 66 public void print(StringBuilder builder) { 67 print(builder, ""); 68 } 69 70 private void print(StringBuilder builder, String margin) { 71 builder.append(margin).append("-").append(value.toString()).append("\n"); 72 for (Node<T> child : children) child.print(builder, margin + " "); 73 } 74 75 public T getParentValue() { 76 return (getParent() != null) ? getParent().getValue() : null; 77 } 78 79}