summaryrefslogtreecommitdiff
path: root/src/org/catacombae/rarx/Tree.java
blob: 1ec9eb068e5171cf65e245524a2f7d1efdfe8ff0 (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
47
48
49
50
51
52
53
54
55
package org.catacombae.rarx;
import java.util.LinkedList;

public class Tree<A> extends TreeNode<A> {
    public Tree(A value) { super(value); }
    public Tree() { super(); }
    
    private LinkedList<Pair<String, TreeNode<A>>> children = new LinkedList<Pair<String, TreeNode<A>>>();
    
    private static class Pair<A, B> {
	public A a;
	public B b;
	public Pair(A a, B b) {
	    this.a = a;
	    this.b = b;
	}
    }
    
    public void put(String entryName, TreeNode<A> tn) {
	children.addLast(new Pair<String, TreeNode<A>>(entryName, tn));
    }
    public TreeNode<A> get(String entryName) {
	for(Pair<String, TreeNode<A>> p : children) {
	    if(p.a.equals(entryName))
		return p.b;
	}
	return null;
    }
    
    public int childCount() {
	return children.size();
    }
    
    public String[] listChildrenNames() {
	String[] targetArray = new String[children.size()];
	int i = 0;
	for(Pair<String, TreeNode<A>> p : children)
	    targetArray[i++] = p.a;
	
	return targetArray;
	
    }
    
    public A[] listChildren(A[] targetArray) {
	if(targetArray.length != children.size())
	    throw new IllegalArgumentException("target array not matching number of children");
	else {
	    int i = 0;
	    for(Pair<String, TreeNode<A>> p : children) {
		targetArray[i++] = p.b.getValue();
	    }
	    return targetArray;
	}
    }
}