use of io.micronaut.core.convert.TypeConverter in project micronaut-core by micronaut-projects.
the class TimeConverterRegistrar method register.
@Override
public void register(ConversionService<?> conversionService) {
final BiFunction<CharSequence, ConversionContext, Optional<Duration>> durationConverter = (object, context) -> {
String value = object.toString().trim();
if (value.startsWith("P")) {
try {
return Optional.of(Duration.parse(value));
} catch (DateTimeParseException e) {
context.reject(value, e);
return Optional.empty();
}
} else {
Matcher matcher = DURATION_MATCHER.matcher(value);
if (matcher.find()) {
String amount = matcher.group(1);
final String g2 = matcher.group(2);
char type = g2.charAt(0);
try {
switch(type) {
case 's':
return Optional.of(Duration.ofSeconds(Integer.parseInt(amount)));
case 'm':
String ms = matcher.group(MILLIS);
if (StringUtils.hasText(ms)) {
return Optional.of(Duration.ofMillis(Integer.parseInt(amount)));
} else {
return Optional.of(Duration.ofMinutes(Integer.parseInt(amount)));
}
case 'h':
return Optional.of(Duration.ofHours(Integer.parseInt(amount)));
case 'd':
return Optional.of(Duration.ofDays(Integer.parseInt(amount)));
default:
final String seq = g2 + matcher.group(3);
switch(seq) {
case "ns":
return Optional.of(Duration.ofNanos(Integer.parseInt(amount)));
default:
context.reject(value, new DateTimeParseException("Unparseable date format (" + value + "). Should either be a ISO-8601 duration or a round number followed by the unit type", value, 0));
return Optional.empty();
}
}
} catch (NumberFormatException e) {
context.reject(value, e);
}
}
}
return Optional.empty();
};
// CharSequence -> Duration
conversionService.addConverter(CharSequence.class, Duration.class, (object, targetType, context) -> durationConverter.apply(object, context));
// CharSequence -> TemporalAmount
conversionService.addConverter(CharSequence.class, TemporalAmount.class, (object, targetType, context) -> durationConverter.apply(object, context).map(TemporalAmount.class::cast));
// CharSequence -> LocalDateTime
conversionService.addConverter(CharSequence.class, LocalDateTime.class, (object, targetType, context) -> {
try {
DateTimeFormatter formatter = resolveFormatter(context);
LocalDateTime result = LocalDateTime.parse(object, formatter);
return Optional.of(result);
} catch (DateTimeParseException e) {
context.reject(object, e);
return Optional.empty();
}
});
// TemporalAccessor - CharSequence
final TypeConverter<TemporalAccessor, CharSequence> temporalConverter = (object, targetType, context) -> {
try {
DateTimeFormatter formatter = resolveFormatter(context);
return Optional.of(formatter.format(object));
} catch (DateTimeParseException e) {
context.reject(object, e);
return Optional.empty();
}
};
conversionService.addConverter(TemporalAccessor.class, CharSequence.class, temporalConverter);
// CharSequence -> LocalDate
conversionService.addConverter(CharSequence.class, LocalDate.class, (object, targetType, context) -> {
try {
DateTimeFormatter formatter = resolveFormatter(context);
LocalDate result = LocalDate.parse(object, formatter);
return Optional.of(result);
} catch (DateTimeParseException e) {
context.reject(object, e);
return Optional.empty();
}
});
// CharSequence -> ZonedDateTime
conversionService.addConverter(CharSequence.class, ZonedDateTime.class, (object, targetType, context) -> {
try {
DateTimeFormatter formatter = resolveFormatter(context);
ZonedDateTime result = ZonedDateTime.parse(object, formatter);
return Optional.of(result);
} catch (DateTimeParseException e) {
context.reject(object, e);
return Optional.empty();
}
});
// CharSequence -> OffsetDateTime
conversionService.addConverter(CharSequence.class, OffsetDateTime.class, (object, targetType, context) -> {
try {
DateTimeFormatter formatter = resolveFormatter(context);
OffsetDateTime result = OffsetDateTime.parse(object, formatter);
return Optional.of(result);
} catch (DateTimeParseException e) {
context.reject(object, e);
return Optional.empty();
}
});
}
use of io.micronaut.core.convert.TypeConverter in project micronaut-core by micronaut-projects.
the class DefaultApplicationContext method initializeTypeConverters.
/**
* @param beanContext The bean context
*/
protected void initializeTypeConverters(BeanContext beanContext) {
Collection<BeanRegistration<TypeConverter>> typeConverters = beanContext.getBeanRegistrations(TypeConverter.class);
for (BeanRegistration<TypeConverter> typeConverterRegistration : typeConverters) {
TypeConverter typeConverter = typeConverterRegistration.getBean();
List<Argument<?>> typeArguments = typeConverterRegistration.getBeanDefinition().getTypeArguments(TypeConverter.class);
if (typeArguments.size() == 2) {
Class source = typeArguments.get(0).getType();
Class target = typeArguments.get(1).getType();
if (source != null && target != null && !(source == Object.class && target == Object.class)) {
getConversionService().addConverter(source, target, typeConverter);
}
}
}
Collection<TypeConverterRegistrar> registrars = beanContext.getBeansOfType(TypeConverterRegistrar.class);
for (TypeConverterRegistrar registrar : registrars) {
registrar.register(conversionService);
}
}
Aggregations