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.expr; |
22 | |
23 | import com.github.javaparser.ast.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.nodeTypes.NodeWithCondition; |
25 | import com.github.javaparser.ast.observer.ObservableProperty; |
26 | import com.github.javaparser.ast.visitor.GenericVisitor; |
27 | import com.github.javaparser.ast.visitor.VoidVisitor; |
28 | import static com.github.javaparser.utils.Utils.assertNotNull; |
29 | import com.github.javaparser.ast.Node; |
30 | import com.github.javaparser.ast.visitor.CloneVisitor; |
31 | import com.github.javaparser.metamodel.ConditionalExprMetaModel; |
32 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
33 | import com.github.javaparser.TokenRange; |
34 | import java.util.function.Consumer; |
35 | import java.util.Optional; |
36 | import com.github.javaparser.ast.Generated; |
37 | |
38 | /** |
39 | * The ternary conditional expression. |
40 | * In {@code b==0?x:y}, b==0 is the condition, x is thenExpr, and y is elseExpr. |
41 | * |
42 | * @author Julio Vilmar Gesser |
43 | */ |
44 | public class ConditionalExpr extends Expression implements NodeWithCondition<ConditionalExpr> { |
45 | |
46 | private Expression condition; |
47 | |
48 | private Expression thenExpr; |
49 | |
50 | private Expression elseExpr; |
51 | |
52 | public ConditionalExpr() { |
53 | this(null, new BooleanLiteralExpr(), new StringLiteralExpr(), new StringLiteralExpr()); |
54 | } |
55 | |
56 | @AllFieldsConstructor |
57 | public ConditionalExpr(Expression condition, Expression thenExpr, Expression elseExpr) { |
58 | this(null, condition, thenExpr, elseExpr); |
59 | } |
60 | |
61 | /** |
62 | * This constructor is used by the parser and is considered private. |
63 | */ |
64 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
65 | public ConditionalExpr(TokenRange tokenRange, Expression condition, Expression thenExpr, Expression elseExpr) { |
66 | super(tokenRange); |
67 | setCondition(condition); |
68 | setThenExpr(thenExpr); |
69 | setElseExpr(elseExpr); |
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 Expression getCondition() { |
87 | return condition; |
88 | } |
89 | |
90 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
91 | public Expression getElseExpr() { |
92 | return elseExpr; |
93 | } |
94 | |
95 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
96 | public Expression getThenExpr() { |
97 | return thenExpr; |
98 | } |
99 | |
100 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
101 | public ConditionalExpr setCondition(final Expression condition) { |
102 | assertNotNull(condition); |
103 | if (condition == this.condition) { |
104 | return (ConditionalExpr) this; |
105 | } |
106 | notifyPropertyChange(ObservableProperty.CONDITION, this.condition, condition); |
107 | if (this.condition != null) |
108 | this.condition.setParentNode(null); |
109 | this.condition = condition; |
110 | setAsParentNodeOf(condition); |
111 | return this; |
112 | } |
113 | |
114 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
115 | public ConditionalExpr setElseExpr(final Expression elseExpr) { |
116 | assertNotNull(elseExpr); |
117 | if (elseExpr == this.elseExpr) { |
118 | return (ConditionalExpr) this; |
119 | } |
120 | notifyPropertyChange(ObservableProperty.ELSE_EXPR, this.elseExpr, elseExpr); |
121 | if (this.elseExpr != null) |
122 | this.elseExpr.setParentNode(null); |
123 | this.elseExpr = elseExpr; |
124 | setAsParentNodeOf(elseExpr); |
125 | return this; |
126 | } |
127 | |
128 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
129 | public ConditionalExpr setThenExpr(final Expression thenExpr) { |
130 | assertNotNull(thenExpr); |
131 | if (thenExpr == this.thenExpr) { |
132 | return (ConditionalExpr) this; |
133 | } |
134 | notifyPropertyChange(ObservableProperty.THEN_EXPR, this.thenExpr, thenExpr); |
135 | if (this.thenExpr != null) |
136 | this.thenExpr.setParentNode(null); |
137 | this.thenExpr = thenExpr; |
138 | setAsParentNodeOf(thenExpr); |
139 | return this; |
140 | } |
141 | |
142 | @Override |
143 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
144 | public boolean remove(Node node) { |
145 | if (node == null) |
146 | return false; |
147 | return super.remove(node); |
148 | } |
149 | |
150 | @Override |
151 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
152 | public ConditionalExpr clone() { |
153 | return (ConditionalExpr) accept(new CloneVisitor(), null); |
154 | } |
155 | |
156 | @Override |
157 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
158 | public ConditionalExprMetaModel getMetaModel() { |
159 | return JavaParserMetaModel.conditionalExprMetaModel; |
160 | } |
161 | |
162 | @Override |
163 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
164 | public boolean replace(Node node, Node replacementNode) { |
165 | if (node == null) |
166 | return false; |
167 | if (node == condition) { |
168 | setCondition((Expression) replacementNode); |
169 | return true; |
170 | } |
171 | if (node == elseExpr) { |
172 | setElseExpr((Expression) replacementNode); |
173 | return true; |
174 | } |
175 | if (node == thenExpr) { |
176 | setThenExpr((Expression) replacementNode); |
177 | return true; |
178 | } |
179 | return super.replace(node, replacementNode); |
180 | } |
181 | |
182 | @Override |
183 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
184 | public boolean isConditionalExpr() { |
185 | return true; |
186 | } |
187 | |
188 | @Override |
189 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
190 | public ConditionalExpr asConditionalExpr() { |
191 | return this; |
192 | } |
193 | |
194 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
195 | public void ifConditionalExpr(Consumer<ConditionalExpr> action) { |
196 | action.accept(this); |
197 | } |
198 | |
199 | @Override |
200 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
201 | public Optional<ConditionalExpr> toConditionalExpr() { |
202 | return Optional.of(this); |
203 | } |
204 | } |
205 |
Members