summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/DefaultTreeNode.java
blob: 9d3764d89ea73dd3c20e9a2fa00a6ea6bde15302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* DefaultTreeNode.java */

public class DefaultTreeNode
implements SimpleTreeNode
{
  private int              CAPACITY;
  private String           name;
  private SimpleTreeNode[] childs;
  private int              childcnt;

  public DefaultTreeNode(String name)
  {
    this.CAPACITY = 5;
    this.name     = name;
    this.childs   = new SimpleTreeNode[CAPACITY];
    this.childcnt = 0;
  }

  public void addChild(SimpleTreeNode child)
  {
    if (childcnt >= CAPACITY) {
      CAPACITY *= 2;
      SimpleTreeNode[] newchilds = new SimpleTreeNode[CAPACITY];
      for (int i = 0; i < childcnt; ++i) {
        newchilds[i] = childs[i];
      }
      childs = newchilds;
    }
    childs[childcnt++] = child;
  }

  public int getChildCnt()
  {
    return childcnt;
  }

  public SimpleTreeNode getChild(int pos)
  {
    return childs[pos];
  }

  public String toString()
  {
    return name;
  }
}