|
15 | 15 | import java.net.URI;
|
16 | 16 | import java.nio.file.Path;
|
17 | 17 | import java.time.LocalDate;
|
18 |
| -import java.util.List; |
19 | 18 | import java.util.Optional;
|
20 | 19 | import java.util.regex.Pattern;
|
21 | 20 |
|
22 | 21 | public final class AutoMappings {
|
23 | 22 |
|
24 | 23 | private final TypeTool tool;
|
25 |
| - private final List<AutoMapping> mappings; |
26 | 24 |
|
27 | 25 | @Inject
|
28 | 26 | public AutoMappings(TypeTool tool) {
|
29 | 27 | this.tool = tool;
|
30 |
| - this.mappings = autoMappings(); |
31 | 28 | }
|
32 | 29 |
|
33 | 30 | <M extends Item>
|
34 | 31 | Optional<Mapping<M>> findAutoMapping(
|
35 | 32 | Match<M> match) {
|
36 | 33 | TypeMirror baseType = match.baseType();
|
37 |
| - for (AutoMapping conversion : mappings) { |
38 |
| - if (tool.isSameType(baseType, conversion.qualifiedName)) { |
39 |
| - Mapping<M> mapping = Mapping.create(conversion.createConverterExpression, match); |
40 |
| - return Optional.of(mapping); |
41 |
| - } |
| 34 | + if (tool.isSameType(baseType, String.class)) { |
| 35 | + return Optional.of(createMapping("asString", match)); |
| 36 | + } |
| 37 | + if (tool.isSameType(baseType, Integer.class)) { |
| 38 | + return Optional.of(createMapping("asInteger", match)); |
| 39 | + } |
| 40 | + if (tool.isSameType(baseType, Path.class)) { |
| 41 | + return Optional.of(createMapping("asPath", match)); |
| 42 | + } |
| 43 | + if (tool.isSameType(baseType, File.class)) { |
| 44 | + return Optional.of(createMapping("asExistingFile", match)); |
| 45 | + } |
| 46 | + if (tool.isSameType(baseType, URI.class)) { |
| 47 | + return Optional.of(createMapping("asURI", match)); |
| 48 | + } |
| 49 | + if (tool.isSameType(baseType, Pattern.class)) { |
| 50 | + return Optional.of(createMapping("asPattern", match)); |
| 51 | + } |
| 52 | + if (tool.isSameType(baseType, LocalDate.class)) { |
| 53 | + return Optional.of(createMapping("asLocalDate", match)); |
| 54 | + } |
| 55 | + if (tool.isSameType(baseType, Long.class)) { |
| 56 | + return Optional.of(createMapping("asLong", match)); |
| 57 | + } |
| 58 | + if (tool.isSameType(baseType, Short.class)) { |
| 59 | + return Optional.of(createMapping("asShort", match)); |
| 60 | + } |
| 61 | + if (tool.isSameType(baseType, Byte.class)) { |
| 62 | + return Optional.of(createMapping("asByte", match)); |
| 63 | + } |
| 64 | + if (tool.isSameType(baseType, Float.class)) { |
| 65 | + return Optional.of(createMapping("asFloat", match)); |
| 66 | + } |
| 67 | + if (tool.isSameType(baseType, Double.class)) { |
| 68 | + return Optional.of(createMapping("asDouble", match)); |
| 69 | + } |
| 70 | + if (tool.isSameType(baseType, Character.class)) { |
| 71 | + return Optional.of(createMapping("asCharacter", match)); |
| 72 | + } |
| 73 | + if (tool.isSameType(baseType, BigInteger.class)) { |
| 74 | + return Optional.of(createMapping("asBigInteger", match)); |
| 75 | + } |
| 76 | + if (tool.isSameType(baseType, BigDecimal.class)) { |
| 77 | + return Optional.of(createMapping("asBigDecimal", match)); |
42 | 78 | }
|
43 | 79 | return Optional.empty();
|
44 | 80 | }
|
45 | 81 |
|
46 |
| - private static AutoMapping create( |
47 |
| - Class<?> autoType, |
48 |
| - String methodName) { |
49 |
| - String canonicalName = autoType.getCanonicalName(); |
| 82 | + private static <M extends Item> Mapping<M> createMapping( |
| 83 | + String methodName, |
| 84 | + Match<M> match) { |
50 | 85 | CodeBlock createConverterExpression = CodeBlock.of("$T.$L()", StandardConverters.class, methodName);
|
51 |
| - return new AutoMapping(canonicalName, createConverterExpression); |
52 |
| - } |
53 |
| - |
54 |
| - private static List<AutoMapping> autoMappings() { |
55 |
| - return List.of( |
56 |
| - create(String.class, "asString"), |
57 |
| - create(Integer.class, "asInteger"), |
58 |
| - create(Path.class, "asPath"), |
59 |
| - create(File.class, "asExistingFile"), |
60 |
| - create(URI.class, "asURI"), |
61 |
| - create(Pattern.class, "asPattern"), |
62 |
| - create(LocalDate.class, "asLocalDate"), |
63 |
| - create(Long.class, "asLong"), |
64 |
| - create(Short.class, "asShort"), |
65 |
| - create(Byte.class, "asByte"), |
66 |
| - create(Float.class, "asFloat"), |
67 |
| - create(Double.class, "asDouble"), |
68 |
| - create(Character.class, "asCharacter"), |
69 |
| - create(BigInteger.class, "asBigInteger"), |
70 |
| - create(BigDecimal.class, "asBigDecimal")); |
71 |
| - } |
72 |
| - |
73 |
| - private static final class AutoMapping { |
74 |
| - final String qualifiedName; |
75 |
| - final CodeBlock createConverterExpression; |
76 |
| - |
77 |
| - AutoMapping(String qualifiedName, CodeBlock createConverterExpression) { |
78 |
| - this.qualifiedName = qualifiedName; |
79 |
| - this.createConverterExpression = createConverterExpression; |
80 |
| - } |
| 86 | + return Mapping.create(createConverterExpression, match); |
81 | 87 | }
|
82 | 88 | }
|
0 commit comments