1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | package com.github.javaparser.ast.stmt; |
22 | |
23 | import com.github.javaparser.ast.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.expr.BooleanLiteralExpr; |
25 | import com.github.javaparser.ast.expr.Expression; |
26 | import com.github.javaparser.ast.nodeTypes.NodeWithBody; |
27 | import com.github.javaparser.ast.nodeTypes.NodeWithCondition; |
28 | import com.github.javaparser.ast.observer.ObservableProperty; |
29 | import com.github.javaparser.ast.visitor.GenericVisitor; |
30 | import com.github.javaparser.ast.visitor.VoidVisitor; |
31 | import static com.github.javaparser.utils.Utils.assertNotNull; |
32 | import com.github.javaparser.ast.Node; |
33 | import com.github.javaparser.ast.visitor.CloneVisitor; |
34 | import com.github.javaparser.metamodel.WhileStmtMetaModel; |
35 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
36 | import com.github.javaparser.TokenRange; |
37 | import java.util.function.Consumer; |
38 | import java.util.Optional; |
39 | import com.github.javaparser.ast.Generated; |
40 | |
41 | /** |
42 | * A while statement. |
43 | * <br>{@code while(true) { ... }} |
44 | * |
45 | * @author Julio Vilmar Gesser |
46 | */ |
47 | public class WhileStmt extends Statement implements NodeWithBody<WhileStmt>, NodeWithCondition<WhileStmt> { |
48 | |
49 | private Expression condition; |
50 | |
51 | private Statement body; |
52 | |
53 | public WhileStmt() { |
54 | this(null, new BooleanLiteralExpr(), new ReturnStmt()); |
55 | } |
56 | |
57 | @AllFieldsConstructor |
58 | public WhileStmt(final Expression condition, final Statement body) { |
59 | this(null, condition, body); |
60 | } |
61 | |
62 | /** |
63 | * This constructor is used by the parser and is considered private. |
64 | */ |
65 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
66 | public WhileStmt(TokenRange tokenRange, Expression condition, Statement body) { |
67 | super(tokenRange); |
68 | setCondition(condition); |
69 | setBody(body); |
70 | customInitialization(); |
71 | } |
72 | |
73 | @Override |
74 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
75 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
76 | return v.visit(this, arg); |
77 | } |
78 | |
79 | @Override |
80 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
81 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
82 | v.visit(this, arg); |
83 | } |
84 | |
85 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
86 | public Statement getBody() { |
87 | return body; |
88 | } |
89 | |
90 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
91 | public Expression getCondition() { |
92 | return condition; |
93 | } |
94 | |
95 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
96 | public WhileStmt setBody(final Statement body) { |
97 | assertNotNull(body); |
98 | if (body == this.body) { |
99 | return (WhileStmt) this; |
100 | } |
101 | notifyPropertyChange(ObservableProperty.BODY, this.body, body); |
102 | if (this.body != null) |
103 | this.body.setParentNode(null); |
104 | this.body = body; |
105 | setAsParentNodeOf(body); |
106 | return this; |
107 | } |
108 | |
109 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
110 | public WhileStmt setCondition(final Expression condition) { |
111 | assertNotNull(condition); |
112 | if (condition == this.condition) { |
113 | return (WhileStmt) this; |
114 | } |
115 | notifyPropertyChange(ObservableProperty.CONDITION, this.condition, condition); |
116 | if (this.condition != null) |
117 | this.condition.setParentNode(null); |
118 | this.condition = condition; |
119 | setAsParentNodeOf(condition); |
120 | return this; |
121 | } |
122 | |
123 | @Override |
124 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
125 | public boolean remove(Node node) { |
126 | if (node == null) |
127 | return false; |
128 | return super.remove(node); |
129 | } |
130 | |
131 | @Override |
132 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
133 | public WhileStmt clone() { |
134 | return (WhileStmt) accept(new CloneVisitor(), null); |
135 | } |
136 | |
137 | @Override |
138 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
139 | public WhileStmtMetaModel getMetaModel() { |
140 | return JavaParserMetaModel.whileStmtMetaModel; |
141 | } |
142 | |
143 | @Override |
144 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
145 | public boolean replace(Node node, Node replacementNode) { |
146 | if (node == null) |
147 | return false; |
148 | if (node == body) { |
149 | setBody((Statement) replacementNode); |
150 | return true; |
151 | } |
152 | if (node == condition) { |
153 | setCondition((Expression) replacementNode); |
154 | return true; |
155 | } |
156 | return super.replace(node, replacementNode); |
157 | } |
158 | |
159 | @Override |
160 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
161 | public boolean isWhileStmt() { |
162 | return true; |
163 | } |
164 | |
165 | @Override |
166 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
167 | public WhileStmt asWhileStmt() { |
168 | return this; |
169 | } |
170 | |
171 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
172 | public void ifWhileStmt(Consumer<WhileStmt> action) { |
173 | action.accept(this); |
174 | } |
175 | |
176 | @Override |
177 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
178 | public Optional<WhileStmt> toWhileStmt() { |
179 | return Optional.of(this); |
180 | } |
181 | } |
182 |
Members