Skip to content

Commit 37d39e6

Browse files
committed
Add support for increment and decrement operator
1 parent 54a0569 commit 37d39e6

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.jcodegen.java;
2+
3+
public class Decrement extends IncrementDecrement<Decrement> {
4+
5+
public Decrement(String variable) {
6+
this(variable, IncDecPosition.BEFORE);
7+
}
8+
9+
public Decrement(String variable, IncDecPosition position) {
10+
this(variable, position, 0);
11+
}
12+
13+
public Decrement(String variable, int indentationLevel) {
14+
this(variable, IncDecPosition.BEFORE, indentationLevel);
15+
}
16+
17+
public Decrement(String variable, IncDecPosition position, int indentationLevel) {
18+
super("--", variable, position, indentationLevel);
19+
}
20+
21+
22+
@Override
23+
protected Decrement getThis() {
24+
return this;
25+
}
26+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.jcodegen.java;
2+
3+
public enum IncDecPosition {
4+
BEFORE, AFTER
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.jcodegen.java;
2+
3+
public class Increment extends IncrementDecrement<Increment> {
4+
5+
public Increment(String variable) {
6+
this(variable, IncDecPosition.BEFORE);
7+
}
8+
9+
public Increment(String variable, IncDecPosition position) {
10+
this(variable, position, 0);
11+
}
12+
13+
public Increment(String variable, int indentationLevel) {
14+
this(variable, IncDecPosition.BEFORE, indentationLevel);
15+
}
16+
17+
public Increment(String variable, IncDecPosition position, int indentationLevel) {
18+
super("++", variable, position, indentationLevel);
19+
}
20+
21+
22+
@Override
23+
protected Increment getThis() {
24+
return this;
25+
}
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jcodegen.java;
2+
3+
abstract class IncrementDecrement<T extends IncrementDecrement<T>> extends Expression<T> {
4+
5+
private final String operation;
6+
private final String variable;
7+
private final IncDecPosition position;
8+
9+
IncrementDecrement(String operation, String variable, IncDecPosition position, int indentationLevel) {
10+
super(variable, indentationLevel);
11+
this.operation = operation;
12+
this.variable = variable;
13+
this.position = position;
14+
}
15+
16+
17+
@Override
18+
public String toString() {
19+
final StringBuilder buf = new StringBuilder();
20+
21+
startExpression(buf);
22+
23+
if (position.equals(IncDecPosition.BEFORE))
24+
buf.append(operation);
25+
buf.append(variable);
26+
if (position.equals(IncDecPosition.AFTER))
27+
buf.append(operation);
28+
29+
endExpression(buf);
30+
31+
return buf.toString();
32+
}
33+
}

0 commit comments

Comments
 (0)