@@ -328,10 +328,12 @@ public interface Type<T> {
328
328
@ SuppressWarnings ("unchecked" )
329
329
static <T > @ NotNull Type <T > custom (@ NotNull ObjectMapper <T > objectMapper ) {
330
330
Objects .requireNonNull (objectMapper );
331
- @ NotNull Map <@ NotNull String , @ NotNull Type <?>> properties = objectMapper .getProperties ();
332
- Objects .requireNonNull (properties );
331
+ Map <String , ObjectMapper .Property <?>> properties = new HashMap <>();
332
+ for (ObjectMapper .Property <?> property : objectMapper .getProperties ())
333
+ if (properties .put (property .getKey (), ObjectMapper .Property .of (property .getKey (), property .getType (), property .isOptional ())) != null )
334
+ throw new IllegalArgumentException ("Duplicate key: " + property .getKey ());
333
335
if (properties .isEmpty ())
334
- throw new IllegalArgumentException ("properties should not be empty" );
336
+ throw new IllegalArgumentException ("Properties should not be empty" );
335
337
return new Type <T >() {
336
338
@ Override
337
339
public @ NotNull T toEntryType (@ NotNull Object value ) throws InvalidTypeException , ValidationException {
@@ -343,8 +345,8 @@ public interface Type<T> {
343
345
Map <String , Object > newMap = new HashMap <>();
344
346
for (Map .Entry <?, ?> entry : mapValue .entrySet ()) {
345
347
String key = STRING .toEntryType (entry .getKey ());
346
- Type <?> type = this . getType (key );
347
- newMap .put (key , type .toEntryType (entry .getValue ()));
348
+ ObjectMapper . Property <?> property = properties . get (key );
349
+ newMap .put (key , property . getType () .toEntryType (entry .getValue ()));
348
350
}
349
351
350
352
return objectMapper .createInstance (Collections .unmodifiableMap (newMap ));
@@ -353,18 +355,13 @@ public interface Type<T> {
353
355
@ Override
354
356
public @ NotNull Object toWritable (@ NotNull T value , @ NotNull Function <@ NotNull Object , @ NotNull Object > keyPreprocessor ) throws InvalidTypeException {
355
357
Map <Object , Object > newMap = new HashMap <>();
356
- for (Map .Entry <String , Object > entry : objectMapper .readValues (value ).entrySet ())
357
- newMap .put (keyPreprocessor .apply (STRING .toWritable (entry .getKey (), keyPreprocessor )), this .getType (entry .getKey ()).toWritable (entry .getValue (), keyPreprocessor ));
358
+ for (Map .Entry <String , Object > entry : objectMapper .readValues (value ).entrySet ()) {
359
+ ObjectMapper .Property <Object > property = (ObjectMapper .Property <Object >) properties .get (entry .getKey ());
360
+ newMap .put (keyPreprocessor .apply (STRING .toWritable (entry .getKey (), keyPreprocessor )), property .getType ().toWritable (entry .getValue (), keyPreprocessor ));
361
+ }
358
362
359
363
return Collections .unmodifiableMap (newMap );
360
364
}
361
-
362
- private <U > Type <U > getType (String key ) {
363
- Type <U > type = (Type <U >) properties .get (key );
364
- if (type == null )
365
- throw new InvalidTypeException (key , properties .keySet ());
366
- return type ;
367
- }
368
365
};
369
366
}
370
367
}
0 commit comments