aboutsummaryrefslogtreecommitdiff
path: root/project/JavaCommon/src/com/modulus/dataread/expressions/AbstractStatement.java
blob: c2ec699005c584434a8a45462312b6d9b52f703c (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.modulus.dataread.expressions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * this class outlines a rough diagram of what a statement
 * looks like. The major importance is how it already
 * correctly implements the tree fasion that statements
 * should in theory be ordered in.
 * 
 * @author jrahm
 *
 */
public abstract class AbstractStatement implements Statement, Serializable{
	private static final long serialVersionUID = -6717726729821743941L;
	private Collection<Statement> children;
	private int line = -1;
	
	/**
	 * the header of this statement.
	 */
	protected String header;
	
	/**
	 * Creates a new AbstractStatement with the
	 * default header being an empty string.
	 */
	public AbstractStatement(){
		this.children = new ArrayList<Statement>();
		this.header = "";
	}
	
	@Override
	public void addChild(Statement child) {
		this.children.add(child);
	}

	@Override
	public Statement[] getChildren() {
		return children.toArray(new Statement[children.size()]);
	}

	@Override
	public String getHeader() {
		return header;
	}

	@Override
	public void removeChild(Statement child) {
		this.children.remove(child);
	}
	
	@Override
	public String toString(){
		return toString(0);
	}
	
	public String toString( int recur ){
		StringBuffer buffer = new StringBuffer();
		String tab = "";
		for(int i = 0;i < recur;i++)
			tab += '\t';
		
		
		buffer.append(tab + header);
		
		if(children.size() > 0){
			buffer.append("{\n");
			for(Statement child : children){
				buffer.append(child.toString( recur + 1 ) + "\n");
			}
			buffer.append(tab + "}");
		} else{
			buffer.append(";");
		}
		
		return buffer.toString();
	}
	
	@Override
	public Statement getChildByHeader(String header){
		
		for(Statement child : children){
			String chHeader = child.getHeader();
			
			if(chHeader.equals(header))
				return child;
		}
		
		return null;
	}
	
	@Override
	public Statement[] getChildrenByHeader(String header){
		List<Statement> ret = new ArrayList<Statement>();
		
		for(Statement child : children){
			String chHeader = child.getHeader();
			
			if(chHeader.equals(header))
				ret.add(child);
		}
		
		return ret.toArray(new Statement[ret.size()]);
	}
	
	public int getLineNumber(){
		return line;
	}
	
	public void setLineNumber(int line){
		this.line = line;
	}
	
	public boolean hasChildren(){
		return !children.isEmpty();
	}
	
	public void clearChildren(){
		this.children.clear();
	}
}