Skip to content

Commit c8b75e9

Browse files
authored
Tidy generator-core TypeMap with final modifiers and add unit tests (#556)
1 parent 62218b4 commit c8b75e9

File tree

2 files changed

+106
-59
lines changed

2 files changed

+106
-59
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* <p/>
1111
* These types convert from String to types on controller methods.
1212
*/
13-
class TypeMap {
13+
final class TypeMap {
1414

1515
private static final Map<String, TypeHandler> types = new HashMap<>();
1616

@@ -48,40 +48,25 @@ static TypeHandler get(String type) {
4848

4949
static TypeHandler collectionHandler(UType type, boolean isEnum) {
5050
final var handler = types.get(type.param0());
51-
5251
if (!isEnum && handler == null) {
5352
return null;
5453
}
55-
56-
return types.computeIfAbsent(
57-
type.full(),
58-
k ->
59-
new CollectionHandler(
60-
isEnum ? enumParamHandler(type.paramRaw()) : handler,
61-
type.mainType().startsWith("java.util.Set"),
62-
isEnum));
54+
return types.computeIfAbsent(type.full(), k -> CollectionHandler.of(type, isEnum, handler));
6355
}
6456

6557
static TypeHandler optionalHandler(UType type, boolean isEnum) {
6658
final var handler = types.get(type.param0());
67-
6859
if (!isEnum && handler == null) {
6960
return null;
7061
}
71-
72-
return types.computeIfAbsent(
73-
type.full(),
74-
k ->
75-
new OptionalHandler(
76-
isEnum ? enumParamHandler(type.paramRaw()) : handler,
77-
isEnum));
62+
return types.computeIfAbsent(type.full(), k -> OptionalHandler.of(type, isEnum, handler));
7863
}
7964

8065
static TypeHandler enumParamHandler(UType type) {
8166
return new EnumHandler(type);
8267
}
8368

84-
static class StringHandler extends JavaLangType {
69+
static final class StringHandler extends JavaLangType {
8570
StringHandler() {
8671
super("String");
8772
}
@@ -97,7 +82,7 @@ public String toMethod() {
9782
}
9883
}
9984

100-
static class IntegerHandler extends JavaLangType {
85+
static final class IntegerHandler extends JavaLangType {
10186
IntegerHandler() {
10287
super("Integer");
10388
}
@@ -113,13 +98,13 @@ public String toMethod() {
11398
}
11499
}
115100

116-
static class IntHandler extends Primitive {
101+
static final class IntHandler extends Primitive {
117102
IntHandler() {
118103
super("Int");
119104
}
120105
}
121106

122-
static class LongHandler extends JavaLangType {
107+
static final class LongHandler extends JavaLangType {
123108
LongHandler() {
124109
super("Long");
125110
}
@@ -135,13 +120,13 @@ public String toMethod() {
135120
}
136121
}
137122

138-
static class PLongHandler extends Primitive {
123+
static final class PLongHandler extends Primitive {
139124
PLongHandler() {
140125
super("Long");
141126
}
142127
}
143128

144-
static class FloatHandler extends JavaLangType {
129+
static final class FloatHandler extends JavaLangType {
145130
FloatHandler() {
146131
super("Float");
147132
}
@@ -157,13 +142,13 @@ public String toMethod() {
157142
}
158143
}
159144

160-
static class PFloatHandler extends Primitive {
145+
static final class PFloatHandler extends Primitive {
161146
PFloatHandler() {
162147
super("Float");
163148
}
164149
}
165150

166-
static class DoubleHandler extends JavaLangType {
151+
static final class DoubleHandler extends JavaLangType {
167152
DoubleHandler() {
168153
super("Double");
169154
}
@@ -179,13 +164,13 @@ public String toMethod() {
179164
}
180165
}
181166

182-
static class PDoubleHandler extends Primitive {
167+
static final class PDoubleHandler extends Primitive {
183168
PDoubleHandler() {
184169
super("Double");
185170
}
186171
}
187172

188-
static class BooleanHandler extends JavaLangType {
173+
static final class BooleanHandler extends JavaLangType {
189174
BooleanHandler() {
190175
super("Boolean");
191176
}
@@ -201,7 +186,7 @@ public String toMethod() {
201186
}
202187
}
203188

204-
static class BoolHandler extends Primitive {
189+
static final class BoolHandler extends Primitive {
205190
BoolHandler() {
206191
super("asBool(", "boolean");
207192
}
@@ -231,7 +216,7 @@ public List<String> importTypes() {
231216
}
232217
}
233218

234-
abstract static class Primitive implements TypeHandler {
219+
static abstract class Primitive implements TypeHandler {
235220

236221
private final String type;
237222

@@ -273,55 +258,55 @@ public List<String> importTypes() {
273258
}
274259
}
275260

276-
static class UuidHandler extends ObjectHandler {
261+
static final class UuidHandler extends ObjectHandler {
277262
UuidHandler() {
278263
super("java.util.UUID", "UUID");
279264
}
280265
}
281266

282-
static class BigDecimalHandler extends ObjectHandler {
267+
static final class BigDecimalHandler extends ObjectHandler {
283268
BigDecimalHandler() {
284269
super("java.math.BigDecimal", "BigDecimal");
285270
}
286271
}
287272

288-
static class BigIntegerHandler extends ObjectHandler {
273+
static final class BigIntegerHandler extends ObjectHandler {
289274
BigIntegerHandler() {
290275
super("java.math.BigInteger", "BigInteger");
291276
}
292277
}
293278

294-
static class LocalDateHandler extends ObjectHandler {
279+
static final class LocalDateHandler extends ObjectHandler {
295280
LocalDateHandler() {
296281
super("java.time.LocalDate", "LocalDate");
297282
}
298283
}
299284

300-
static class InstantHandler extends ObjectHandler {
285+
static final class InstantHandler extends ObjectHandler {
301286
InstantHandler() {
302287
super("java.time.Instant", "Instant");
303288
}
304289
}
305290

306-
static class OffsetDateTimeHandler extends ObjectHandler {
291+
static final class OffsetDateTimeHandler extends ObjectHandler {
307292
OffsetDateTimeHandler() {
308293
super("java.time.OffsetDateTime", "OffsetDateTime");
309294
}
310295
}
311296

312-
static class LocalTimeHandler extends ObjectHandler {
297+
static final class LocalTimeHandler extends ObjectHandler {
313298
LocalTimeHandler() {
314299
super("java.time.LocalTime", "LocalTime");
315300
}
316301
}
317302

318-
static class LocalDateTimeHandler extends ObjectHandler {
303+
static final class LocalDateTimeHandler extends ObjectHandler {
319304
LocalDateTimeHandler() {
320305
super("java.time.LocalDateTime", "LocalDateTime");
321306
}
322307
}
323308

324-
static class EnumHandler extends ObjectHandler {
309+
static final class EnumHandler extends ObjectHandler {
325310
private final UType type;
326311

327312
EnumHandler(UType type) {
@@ -340,25 +325,31 @@ public String asMethod() {
340325
}
341326
}
342327

343-
static class CollectionHandler implements TypeHandler {
328+
static final class CollectionHandler implements TypeHandler {
344329

345330
private final List<String> importTypes;
346331
private final String shortName;
347-
private String toMethod;
332+
private final String toMethod;
348333

349-
CollectionHandler(TypeHandler handler, boolean set, boolean isEnum) {
334+
private static CollectionHandler of(UType type, boolean isEnum, TypeHandler sourceHandler) {
335+
final var handler = isEnum ? enumParamHandler(type.paramRaw()) : sourceHandler;
336+
final var isSet = type.mainType().startsWith("java.util.Set");
337+
return new CollectionHandler(handler, isSet, isEnum);
338+
}
339+
340+
private CollectionHandler(TypeHandler handler, boolean set, boolean isEnum) {
350341
this.importTypes = new ArrayList<>(handler.importTypes());
351342
this.importTypes.add("io.avaje.http.api.PathTypeConversion");
352343
this.shortName = handler.shortName();
353-
this.toMethod =
354-
(set ? "set" : "list")
355-
+ "("
356-
+ (isEnum
357-
? "qp -> " + handler.toMethod() + " qp)"
358-
: "PathTypeConversion::as" + shortName)
359-
+ ", ";
344+
String _toMethod =
345+
(set ? "set" : "list")
346+
+ "("
347+
+ (isEnum
348+
? "qp -> " + handler.toMethod() + " qp)"
349+
: "PathTypeConversion::as" + shortName)
350+
+ ", ";
360351

361-
this.toMethod = toMethod.replace("PathTypeConversion::asString", "Object::toString");
352+
this.toMethod = _toMethod.replace("PathTypeConversion::asString", "Object::toString");
362353
}
363354

364355
@Override
@@ -387,13 +378,18 @@ public String toMethod() {
387378
}
388379
}
389380

390-
static class OptionalHandler implements TypeHandler {
381+
static final class OptionalHandler implements TypeHandler {
391382

392383
private final List<String> importTypes;
393384
private final String shortName;
394385
private final String toMethod;
395386

396-
OptionalHandler(TypeHandler handler, boolean isEnum) {
387+
private static OptionalHandler of(UType type, boolean isEnum, TypeHandler sourceHandler) {
388+
final var handler = isEnum ? enumParamHandler(type.paramRaw()) : sourceHandler;
389+
return new OptionalHandler(handler, isEnum);
390+
}
391+
392+
private OptionalHandler(TypeHandler handler, boolean isEnum) {
397393
this.importTypes = new ArrayList<>(handler.importTypes());
398394
this.importTypes.add("io.avaje.http.api.PathTypeConversion");
399395
this.shortName = handler.shortName();
@@ -437,7 +433,7 @@ public String toMethod() {
437433
}
438434
}
439435

440-
abstract static class ObjectHandler implements TypeHandler {
436+
static abstract class ObjectHandler implements TypeHandler {
441437

442438
private final String importType;
443439
private final String shortName;

http-generator-core/src/test/java/io/avaje/http/generator/core/TypeMapTest.java

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,73 @@ void get_BigInt() {
7272

7373
@Test
7474
void get_OptionalInteger() {
75-
TypeHandler handler = TypeMap.get("java.lang.Integer");
76-
TypeMap.OptionalHandler optionalHandler = new TypeMap.OptionalHandler(handler, false);
75+
UType uType = UType.parse("java.util.Optional<java.lang.Integer>");
76+
TypeHandler optionalHandler = TypeMap.optionalHandler(uType, false);
77+
assertThat(optionalHandler).isNotNull();
7778
assertThat(optionalHandler.toMethod()).isEqualTo("optional(PathTypeConversion::asInteger, ");
7879
}
7980

8081
@Test
8182
void get_OptionalString() {
82-
TypeHandler handler = TypeMap.get("java.lang.String");
83-
TypeMap.OptionalHandler optionalHandler = new TypeMap.OptionalHandler(handler, false);
83+
UType uType = UType.parse("java.util.Optional<java.lang.String>");
84+
TypeHandler optionalHandler = TypeMap.optionalHandler(uType, false);
85+
assertThat(optionalHandler).isNotNull();
8486
assertThat(optionalHandler.toMethod()).isEqualTo("optional(");
8587
}
8688

8789
@Test
8890
void get_OptionalEnum() {
89-
TypeHandler handler = TypeMap.enumParamHandler(UType.parse("org.my.MyEnum"));
90-
TypeMap.OptionalHandler optionalHandler = new TypeMap.OptionalHandler(handler, true);
91+
UType uType = UType.parse("java.util.Optional<org.my.MyEnum>");
92+
TypeHandler optionalHandler = TypeMap.optionalHandler(uType, true);
93+
assertThat(optionalHandler).isNotNull();
9194
assertThat(optionalHandler.toMethod()).isEqualTo("optional(qp -> (MyEnum) toEnum(MyEnum.class, qp), ");
9295
}
96+
97+
@Test
98+
void get_ListInteger() {
99+
UType uType = UType.parse("java.util.List<java.lang.Integer>");
100+
TypeHandler handler = TypeMap.collectionHandler(uType, false);
101+
assertThat(handler).isNotNull();
102+
assertThat(handler.toMethod()).isEqualTo("list(PathTypeConversion::asInteger, ");
103+
}
104+
105+
@Test
106+
void get_SetInteger() {
107+
UType uType = UType.parse("java.util.Set<java.lang.Integer>");
108+
TypeHandler handler = TypeMap.collectionHandler(uType, false);
109+
assertThat(handler).isNotNull();
110+
assertThat(handler.toMethod()).isEqualTo("set(PathTypeConversion::asInteger, ");
111+
}
112+
113+
@Test
114+
void get_ListString() {
115+
UType uType = UType.parse("java.util.List<java.lang.String>");
116+
TypeHandler handler = TypeMap.collectionHandler(uType, false);
117+
assertThat(handler).isNotNull();
118+
assertThat(handler.toMethod()).isEqualTo("list(Object::toString, ");
119+
}
120+
121+
@Test
122+
void get_SetString() {
123+
UType uType = UType.parse("java.util.Set<java.lang.String>");
124+
TypeHandler handler = TypeMap.collectionHandler(uType, false);
125+
assertThat(handler).isNotNull();
126+
assertThat(handler.toMethod()).isEqualTo("set(Object::toString, ");
127+
}
128+
129+
@Test
130+
void get_ListEnum() {
131+
UType uType = UType.parse("java.util.List<org.my.MyEnum>");
132+
TypeHandler handler = TypeMap.collectionHandler(uType, true);
133+
assertThat(handler).isNotNull();
134+
assertThat(handler.toMethod()).isEqualTo("list(qp -> (MyEnum) toEnum(MyEnum.class, qp), ");
135+
}
136+
137+
@Test
138+
void get_SetEnum() {
139+
UType uType = UType.parse("java.util.Set<org.my.MyEnum>");
140+
TypeHandler handler = TypeMap.collectionHandler(uType, true);
141+
assertThat(handler).isNotNull();
142+
assertThat(handler.toMethod()).isEqualTo("set(qp -> (MyEnum) toEnum(MyEnum.class, qp), ");
143+
}
93144
}

0 commit comments

Comments
 (0)