Skip to content

Commit 4cab589

Browse files
committed
#210 Implemented single boolean expression
1 parent d7d43ad commit 4cab589

File tree

8 files changed

+197
-162
lines changed

8 files changed

+197
-162
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.javalite.templator;
2+
3+
import java.util.Map;
4+
import org.javalite.common.Convert;
5+
6+
/**
7+
* @author Eric Nielsen
8+
*/
9+
class BooleanId extends Exp {
10+
private final ChainedIds chainedIds;
11+
12+
BooleanId(ChainedIds chainedIds) {
13+
this.chainedIds = chainedIds;
14+
}
15+
16+
@Override
17+
boolean resultWith(Map values) {
18+
return Convert.toBoolean(chainedIds.valueFrom(values));
19+
}
20+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.javalite.templator;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import static org.javalite.common.Inflector.*;
6+
7+
/**
8+
* @author Eric Nielsen
9+
*/
10+
class ChainedIds {
11+
private final String firstId;
12+
private final List<String> ids;
13+
14+
ChainedIds(String firstId, List<String> ids) {
15+
this.firstId = firstId;
16+
this.ids = ids;
17+
}
18+
19+
private Object valueOf(Object obj, String id) {
20+
try {
21+
if (id.endsWith("()")) {
22+
return obj.getClass().getMethod(id.substring(0, id.length() - 2)).invoke(obj);
23+
}
24+
} catch (Exception e) {
25+
throw new TemplateException(e);
26+
}
27+
if (obj instanceof Map) {
28+
return ((Map) obj).get(id);
29+
}
30+
try {
31+
// try generic get method
32+
return obj.getClass().getMethod("get", String.class).invoke(obj, id);
33+
} catch (Exception e) {
34+
try {
35+
// try javabean property
36+
return obj.getClass().getMethod("get" + capitalize(id)).invoke(obj);
37+
} catch (Exception e1) {
38+
// try public field
39+
try {
40+
return obj.getClass().getDeclaredField(id).get(obj);
41+
} catch (Exception e2) {
42+
}
43+
}
44+
}
45+
return null;
46+
}
47+
48+
Object valueFrom(Map values) {
49+
Object obj = values.get(firstId);
50+
for (String id : ids) {
51+
obj = valueOf(obj, id);
52+
}
53+
return obj;
54+
}
55+
}

javalite-templator/src/main/java/org/javalite/templator/Comparison.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,57 @@
66
* @author Eric Nielsen
77
*/
88
class Comparison extends Exp {
9-
private final Identifiers leftIdent;
10-
private final Op op;
11-
private final Identifiers rightIdent;
9+
enum Operator {
10+
eq {
11+
@Override boolean resultWith(Object obj1, Object obj2) {
12+
return obj1 == null ? obj2 == null : (obj2 == null ? false : obj1.equals(obj2));
13+
}
14+
},
15+
gt {
16+
@Override boolean resultWith(Object obj1, Object obj2) {
17+
return compare(obj1, obj2) > 0;
18+
}
19+
},
20+
gte {
21+
@Override boolean resultWith(Object obj1, Object obj2) {
22+
return compare(obj1, obj2) >= 0;
23+
}
24+
},
25+
lt {
26+
@Override boolean resultWith(Object obj1, Object obj2) {
27+
return compare(obj1, obj2) < 0;
28+
}
29+
},
30+
lte {
31+
@Override boolean resultWith(Object obj1, Object obj2) {
32+
return compare(obj1, obj2) <= 0;
33+
}
34+
},
35+
neq {
36+
@Override boolean resultWith(Object obj1, Object obj2) {
37+
return obj1 == null ? obj2 != null : (obj2 == null ? true : !obj1.equals(obj2));
38+
}
39+
};
1240

13-
Comparison(Identifiers leftIdent, Op op, Identifiers rightIdent) {
14-
this.leftIdent = leftIdent;
15-
this.op = op;
16-
this.rightIdent = rightIdent;
41+
abstract boolean resultWith(Object obj1, Object obj2);
42+
43+
int compare(Object obj1, Object obj2) {
44+
return ((Comparable) obj1).compareTo(obj2);
45+
}
46+
}
47+
48+
private final ChainedIds leftOperand;
49+
private final Operator operator;
50+
private final ChainedIds rightOperand;
51+
52+
Comparison(ChainedIds leftOperand, Operator operator, ChainedIds rightOperand) {
53+
this.leftOperand = leftOperand;
54+
this.operator = operator;
55+
this.rightOperand = rightOperand;
1756
}
1857

1958
@Override
2059
boolean resultWith(Map values) {
21-
return op.resultWith(leftIdent.valueFrom(values), rightIdent.valueFrom(values));
60+
return operator.resultWith(leftOperand.valueFrom(values), rightOperand.valueFrom(values));
2261
}
2362
}

javalite-templator/src/main/java/org/javalite/templator/Identifiers.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

javalite-templator/src/main/java/org/javalite/templator/Op.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)