|
25 | 25 | import java.time.LocalDateTime; |
26 | 26 | import java.time.ZoneOffset; |
27 | 27 | import java.time.format.DateTimeParseException; |
| 28 | +import java.util.EnumSet; |
28 | 29 | import java.util.List; |
29 | 30 | import java.util.Map; |
30 | 31 | import java.util.Optional; |
|
47 | 48 | public enum BuiltinParser implements Parser { |
48 | 49 |
|
49 | 50 | Basic { |
50 | | - private final Map<Class<?>, Function<String, Object>> parsers = |
51 | | - ImmutableMap.<Class<?>, Function<String, Object>> builder() |
52 | | - .put(BigDecimal.class, NOT_EMPTY.andThen(BigDecimal::new)) |
53 | | - .put(BigInteger.class, NOT_EMPTY.andThen(BigInteger::new)) |
54 | | - .put(Byte.class, NOT_EMPTY.andThen(Byte::valueOf)) |
55 | | - .put(byte.class, NOT_EMPTY.andThen(Byte::valueOf)) |
56 | | - .put(Double.class, NOT_EMPTY.andThen(Double::valueOf)) |
57 | | - .put(double.class, NOT_EMPTY.andThen(Double::valueOf)) |
58 | | - .put(Float.class, NOT_EMPTY.andThen(Float::valueOf)) |
59 | | - .put(float.class, NOT_EMPTY.andThen(Float::valueOf)) |
60 | | - .put(Integer.class, NOT_EMPTY.andThen(Integer::valueOf)) |
61 | | - .put(int.class, NOT_EMPTY.andThen(Integer::valueOf)) |
62 | | - .put(Long.class, NOT_EMPTY.andThen(this::toLong)) |
63 | | - .put(long.class, NOT_EMPTY.andThen(this::toLong)) |
64 | | - .put(Short.class, NOT_EMPTY.andThen(Short::valueOf)) |
65 | | - .put(short.class, NOT_EMPTY.andThen(Short::valueOf)) |
66 | | - .put(Boolean.class, NOT_EMPTY.andThen(this::toBoolean)) |
67 | | - .put(boolean.class, NOT_EMPTY.andThen(this::toBoolean)) |
68 | | - .put(Character.class, NOT_EMPTY.andThen(this::toCharacter)) |
69 | | - .put(char.class, NOT_EMPTY.andThen(this::toCharacter)) |
70 | | - .put(String.class, this::toString) |
71 | | - .build(); |
| 51 | + private final Map<Class<?>, Function<String, Object>> parsers = ImmutableMap |
| 52 | + .<Class<?>, Function<String, Object>> builder() |
| 53 | + .put(BigDecimal.class, NOT_EMPTY.andThen(BigDecimal::new)) |
| 54 | + .put(BigInteger.class, NOT_EMPTY.andThen(BigInteger::new)) |
| 55 | + .put(Byte.class, NOT_EMPTY.andThen(Byte::valueOf)) |
| 56 | + .put(byte.class, NOT_EMPTY.andThen(Byte::valueOf)) |
| 57 | + .put(Double.class, NOT_EMPTY.andThen(Double::valueOf)) |
| 58 | + .put(double.class, NOT_EMPTY.andThen(Double::valueOf)) |
| 59 | + .put(Float.class, NOT_EMPTY.andThen(Float::valueOf)) |
| 60 | + .put(float.class, NOT_EMPTY.andThen(Float::valueOf)) |
| 61 | + .put(Integer.class, NOT_EMPTY.andThen(Integer::valueOf)) |
| 62 | + .put(int.class, NOT_EMPTY.andThen(Integer::valueOf)) |
| 63 | + .put(Long.class, NOT_EMPTY.andThen(this::toLong)) |
| 64 | + .put(long.class, NOT_EMPTY.andThen(this::toLong)) |
| 65 | + .put(Short.class, NOT_EMPTY.andThen(Short::valueOf)) |
| 66 | + .put(short.class, NOT_EMPTY.andThen(Short::valueOf)) |
| 67 | + .put(Boolean.class, NOT_EMPTY.andThen(this::toBoolean)) |
| 68 | + .put(boolean.class, NOT_EMPTY.andThen(this::toBoolean)) |
| 69 | + .put(Character.class, NOT_EMPTY.andThen(this::toCharacter)) |
| 70 | + .put(char.class, NOT_EMPTY.andThen(this::toCharacter)) |
| 71 | + .put(String.class, this::toString) |
| 72 | + .build(); |
72 | 73 |
|
73 | 74 | @Override |
74 | 75 | public Object parse(final TypeLiteral<?> type, final Parser.Context ctx) throws Throwable { |
75 | 76 | Function<String, Object> parser = parsers.get(type.getRawType()); |
76 | 77 | if (parser != null) { |
77 | 78 | return ctx |
78 | | - .param(values -> |
79 | | - parser.apply(values.get(0)) |
80 | | - ).body(body -> |
81 | | - parser.apply(body.text()) |
82 | | - ); |
| 79 | + .param(values -> parser.apply(values.get(0))).body(body -> parser.apply(body.text())); |
83 | 80 | } |
84 | 81 | return ctx.next(); |
85 | 82 | } |
@@ -119,12 +116,11 @@ private Long toLong(final String value) { |
119 | 116 | }, |
120 | 117 |
|
121 | 118 | Collection { |
122 | | - private final Map<Class<?>, Supplier<ImmutableCollection.Builder<?>>> parsers = |
123 | | - ImmutableMap.<Class<?>, Supplier<ImmutableCollection.Builder<?>>> builder() |
124 | | - .put(List.class, ImmutableList.Builder::new) |
125 | | - .put(Set.class, ImmutableSet.Builder::new) |
126 | | - .put(SortedSet.class, ImmutableSortedSet::naturalOrder) |
127 | | - .build(); |
| 119 | + private final Map<Class<?>, Supplier<ImmutableCollection.Builder<?>>> parsers = ImmutableMap.<Class<?>, Supplier<ImmutableCollection.Builder<?>>> builder() |
| 120 | + .put(List.class, ImmutableList.Builder::new) |
| 121 | + .put(Set.class, ImmutableSet.Builder::new) |
| 122 | + .put(SortedSet.class, ImmutableSortedSet::naturalOrder) |
| 123 | + .build(); |
128 | 124 |
|
129 | 125 | private boolean matches(final TypeLiteral<?> toType) { |
130 | 126 | return parsers.containsKey(toType.getRawType()) |
@@ -194,15 +190,20 @@ public Object parse(final TypeLiteral<?> type, final Parser.Context ctx) |
194 | 190 | Class rawType = type.getRawType(); |
195 | 191 | if (Enum.class.isAssignableFrom(rawType)) { |
196 | 192 | return ctx |
197 | | - .param(values -> |
198 | | - java.lang.Enum.valueOf(rawType, values.get(0).toUpperCase()) |
199 | | - ).body(body -> |
200 | | - java.lang.Enum.valueOf(rawType, body.text().toUpperCase()) |
201 | | - ); |
| 193 | + .param(values -> toEnum(rawType, values.get(0))) |
| 194 | + .body(body -> toEnum(rawType, body.text())); |
202 | 195 | } else { |
203 | 196 | return ctx.next(); |
204 | 197 | } |
205 | 198 | } |
| 199 | + |
| 200 | + Object toEnum(final Class type, final String value) { |
| 201 | + Set<Enum> set = EnumSet.allOf(type); |
| 202 | + return set.stream() |
| 203 | + .filter(e -> e.name().equalsIgnoreCase(value)) |
| 204 | + .findFirst() |
| 205 | + .orElseGet(() -> java.lang.Enum.valueOf(type, value)); |
| 206 | + } |
206 | 207 | }, |
207 | 208 |
|
208 | 209 | Upload { |
|
0 commit comments