summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/ForStatement.java
blob: d4854515e73a153f4242a1645c54f96333f1e816 (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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * ForStatement.java 1.0
 *
 *
 * Jun 20, 1997 by mich
 * Sep 29, 1997 by bv
 * Oct 10, 1997 by mich
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @version 1.0 last updated:  Oct 10, 1997
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

import io.devnulllabs.openjava.ptree.util.ParseTreeVisitor;

/**
 * The <code>ForStatement</code> class represents a for statement
 * node of parse tree.
 * <br>
 * The specification of the initialization part may be modified
 * in the later version of OpenJava.
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @see io.devnulllabs.openjava.ptree.NonLeaf
 * @see io.devnulllabs.openjava.ptree.Statement
 * @see io.devnulllabs.openjava.ptree.Expression
 * @see io.devnulllabs.openjava.ptree.ExpressionList
 * @see io.devnulllabs.openjava.ptree.StatementList
 */
public class ForStatement extends NonLeaf implements Statement, ParseTree {

    /**
     * Allocates a new ForStatement object.
     *
     */
    ForStatement() {
        super();
    }

    /**
     * Allocates a new ForStatement object.
     *
     * @param  init  the initialization part.
     * @param  expr  the condition part.
     * @param  iterator  the increments part.
     * @param  stmts  the stement list of the body.
     */
    public ForStatement(
        ExpressionList init,
        Expression expr,
        ExpressionList iterator,
        StatementList stmts) {
        super();
        if (iterator == null)
            iterator = new ExpressionList();
        if (stmts == null)
            stmts = new StatementList();
        set(null, null, init, expr, iterator, stmts);
    }

    public ForStatement(
        TypeName tspec,
        VariableDeclarator[] vdecls,
        Expression expr,
        ExpressionList iterator,
        StatementList stmts) {
        super();
        if (stmts == null)
            stmts = new StatementList();
        if (tspec == null || vdecls == null || vdecls.length == 0) {
            set(null, null, null, expr, iterator, stmts);
        } else {
            set(tspec, vdecls, null, expr, iterator, stmts);
        }
    }

    /**
     * Gets the initialization part of this for-statement.
     *
     * @return  the initialization part.
     */
    public ExpressionList getInit() {
        return (ExpressionList) elementAt(2);
    }

    /**
     * Sets the initialization part of this for-statement.
     *
     * @param  init  the initialization part.
     */
    public void setInit(ExpressionList init) {
        setElementAt(null, 0);
        setElementAt(null, 1);
        setElementAt(init, 2);
    }

    /**
     * Gets the initialization part of this for-statement.
     *
     * @return  the initialization part.
     */
    public TypeName getInitDeclType() {
        return (TypeName) elementAt(0);
    }

    /**
     * Gets the initialization part of this for-statement.
     *
     * @return  the initialization part.
     */
    public VariableDeclarator[] getInitDecls() {
        return (VariableDeclarator[]) elementAt(1);
    }

    /**
     * Sets the initialization part of this for-statement.
     *
     * @param  init  the initialization part.
     */
    public void setInitDecl(
        TypeName type_specifier,
        VariableDeclarator[] init) {
        if (type_specifier == null || init == null || init.length == 0) {
            setElementAt(null, 0);
            setElementAt(null, 1);
        } else {
            setElementAt(type_specifier, 0);
            setElementAt(init, 1);
        }
        setElementAt(null, 2);
    }

    /**
     * Gets the condition part of this for-statement.
     *
     * @return  the expression of the condtion part.
     */
    public Expression getCondition() {
        return (Expression) elementAt(3);
    }

    /**
     * Sets the condition part of this for-statement.
     *
     * @param  cond  the expression of the condtion part.
     */
    public void setCondition(Expression cond) {
        setElementAt(cond, 3);
    }

    /**
     * Gets the increments part of this for-statement.
     *
     * @return  the expression list of the increments part.
     */
    public ExpressionList getIncrement() {
        return (ExpressionList) elementAt(4);
    }

    /**
     * Sets the increments part of this for-statement.
     *
     * @param  incs  the expression list of the increments part.
     */
    public void setIncrement(ExpressionList incs) {
        if (incs == null)
            incs = new ExpressionList();
        setElementAt(incs, 4);
    }

    /**
     * Gets the body of this for-statement.
     *
     * @return  the statement list of the body.
     */
    public StatementList getStatements() {
        return (StatementList) elementAt(5);
    }

    /**
     * Sets the body of this for-statement.
     *
     * @param  stmts  the statement list of the body.
     */
    public void setStatements(StatementList stmts) {
        if (stmts == null)
            stmts = new StatementList();
        setElementAt(stmts, 5);
    }

    public void accept(ParseTreeVisitor v) throws ParseTreeException {
        v.visit(this);
    }

}