Skip to content

Commit 345c3d5

Browse files
committed
Add ternary operator to code that can be generated
1 parent 2534e20 commit 345c3d5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.jcodegen.java;
2+
3+
public class TernaryOperator extends Expression<TernaryOperator> {
4+
5+
private final Condition condition;
6+
private final StringOrCode<Expression> trueResult;
7+
private final StringOrCode<Expression> falseResult;
8+
9+
10+
public TernaryOperator(final Condition condition, final Expression trueResult, final Expression falseResult) {
11+
super("Ternary", 0);
12+
this.condition = condition;
13+
this.trueResult = new StringOrCode<Expression>(trueResult);
14+
this.falseResult = new StringOrCode<Expression>(falseResult);
15+
}
16+
17+
public TernaryOperator(final Condition condition, final String trueResult, final Expression falseResult) {
18+
super("Ternary", 0);
19+
this.condition = condition;
20+
this.trueResult = new StringOrCode<Expression>(trueResult);
21+
this.falseResult = new StringOrCode<Expression>(falseResult);
22+
}
23+
24+
public TernaryOperator(final Condition condition, final Expression trueResult, final String falseResult) {
25+
super("Ternary", 0);
26+
this.condition = condition;
27+
this.trueResult = new StringOrCode<Expression>(trueResult);
28+
this.falseResult = new StringOrCode<Expression>(falseResult);
29+
}
30+
31+
public TernaryOperator(final Condition condition, final String trueResult, final String falseResult) {
32+
super("Ternary", 0);
33+
this.condition = condition;
34+
this.trueResult = new StringOrCode<Expression>(trueResult);
35+
this.falseResult = new StringOrCode<Expression>(falseResult);
36+
}
37+
38+
39+
@Override
40+
protected TernaryOperator getThis() {
41+
return this;
42+
}
43+
44+
45+
@Override
46+
public String toString() {
47+
return condition.toString() + " ? " + trueResult.toString() + " : " + falseResult.toString();
48+
}
49+
}

0 commit comments

Comments
 (0)