Skip to content

Commit ed2fd9c

Browse files
committed
#9 - Add type conversion for boolean, Boolean, BigDecimal, Instant, OffsetDateTime
1 parent 835b592 commit ed2fd9c

File tree

1 file changed

+99
-15
lines changed

1 file changed

+99
-15
lines changed

src/main/java/io/dinject/javalin/generator/TypeMap.java

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,30 @@ private static void add(TypeHandler h) {
1717
}
1818

1919
static {
20-
types.put("int", new IntHander());
21-
types.put("long", new LongHander());
22-
types.put("java.lang.String", new StringHander());
23-
types.put("java.lang.Integer", new IntegerHander());
20+
types.put("int", new IntHandler());
21+
types.put("long", new PLongHandler());
22+
types.put("boolean", new BoolHandler());
23+
24+
types.put("java.lang.String", new StringHandler());
25+
types.put("java.lang.Integer", new IntegerHandler());
26+
types.put("java.lang.Long", new LongHander());
27+
types.put("java.lang.Boolean", new BooleanHandler());
28+
2429
add(new UuidHandler());
30+
add(new BigDecimalHandler());
2531
add(new LocalDateHandler());
32+
add(new LocalTimeHandler());
33+
add(new LocalDateTimeHandler());
34+
add(new InstantHandler());
35+
add(new OffsetDateTimeHandler());
2636
}
2737

2838
static TypeHandler get(String type) {
2939
return types.get(type);
3040
}
3141

32-
static class StringHander extends JavaLangType {
33-
StringHander() {
42+
static class StringHandler extends JavaLangType {
43+
StringHandler() {
3444
super("String");
3545
}
3646

@@ -45,8 +55,8 @@ public String toMethod() {
4555
}
4656
}
4757

48-
static class IntegerHander extends JavaLangType {
49-
IntegerHander() {
58+
static class IntegerHandler extends JavaLangType {
59+
IntegerHandler() {
5060
super("Integer");
5161
}
5262

@@ -61,16 +71,54 @@ public String toMethod() {
6171
}
6272
}
6373

64-
static class IntHander extends Primitive {
65-
IntHander() {
74+
static class IntHandler extends Primitive {
75+
IntHandler() {
6676
super("Int");
6777
}
6878
}
6979

70-
static class LongHander extends Primitive {
80+
static class LongHander extends JavaLangType {
7181
LongHander() {
7282
super("Long");
7383
}
84+
85+
@Override
86+
public String asMethod() {
87+
return "asLong(";
88+
}
89+
90+
@Override
91+
public String toMethod() {
92+
return "toLong(";
93+
}
94+
}
95+
96+
static class PLongHandler extends Primitive {
97+
PLongHandler() {
98+
super("Long");
99+
}
100+
}
101+
102+
static class BooleanHandler extends JavaLangType {
103+
BooleanHandler() {
104+
super("Boolean");
105+
}
106+
107+
@Override
108+
public String asMethod() {
109+
return "asBool(";
110+
}
111+
112+
@Override
113+
public String toMethod() {
114+
return "toBoolean(";
115+
}
116+
}
117+
118+
static class BoolHandler extends Primitive {
119+
BoolHandler() {
120+
super("asBool(", "boolean");
121+
}
74122
}
75123

76124
static abstract class JavaLangType implements TypeHandler {
@@ -103,6 +151,11 @@ static abstract class Primitive implements TypeHandler {
103151
this.type = asType.toLowerCase();
104152
}
105153

154+
Primitive(String asMethod, String type) {
155+
this.asMethod = asMethod;
156+
this.type = type;
157+
}
158+
106159
@Override
107160
public String shortName() {
108161
return type;
@@ -124,26 +177,57 @@ public String getImportType() {
124177
}
125178
}
126179

127-
static class UuidHandler extends ObjectHander {
180+
static class UuidHandler extends ObjectHandler {
128181
UuidHandler() {
129182
super("java.util.UUID", "UUID");
130183
}
131184
}
132185

133-
static class LocalDateHandler extends ObjectHander {
186+
static class BigDecimalHandler extends ObjectHandler {
187+
BigDecimalHandler() {
188+
super("java.math.BigDecimal", "BigDecimal");
189+
}
190+
}
191+
192+
static class LocalDateHandler extends ObjectHandler {
134193
LocalDateHandler() {
135194
super("java.time.LocalDate", "LocalDate");
136195
}
137196
}
138197

139-
static abstract class ObjectHander implements TypeHandler {
198+
static class InstantHandler extends ObjectHandler {
199+
InstantHandler() {
200+
super("java.time.Instant", "Instant");
201+
}
202+
}
203+
204+
static class OffsetDateTimeHandler extends ObjectHandler {
205+
OffsetDateTimeHandler() {
206+
super("java.time.OffsetDateTime", "OffsetDateTime");
207+
}
208+
}
209+
210+
static class LocalTimeHandler extends ObjectHandler {
211+
LocalTimeHandler() {
212+
super("java.time.LocalTime", "LocalTime");
213+
}
214+
}
215+
216+
static class LocalDateTimeHandler extends ObjectHandler {
217+
LocalDateTimeHandler() {
218+
super("java.time.LocalDateTime", "LocalDateTime");
219+
}
220+
}
221+
222+
223+
static abstract class ObjectHandler implements TypeHandler {
140224

141225
private final String importType;
142226
private final String shortName;
143227
private final String asMethod;
144228
private final String toMethod;
145229

146-
ObjectHander(String importType, String shortName) {
230+
ObjectHandler(String importType, String shortName) {
147231
this.importType = importType;
148232
this.shortName = shortName;
149233
this.asMethod = "as" + shortName + "(";

0 commit comments

Comments
 (0)