Package io.micronaut.core.convert
Interface ConversionService<Impl extends ConversionService>
-
- Type Parameters:
Impl- The type
- All Known Implementing Classes:
DefaultConversionService
public interface ConversionService<Impl extends ConversionService>A service for allowing conversion from one type to another.- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description static ConversionService<?>SHAREDDeprecated.This will be removed in the next major version.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description <S,T>
ImpladdConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, TypeConverter<S,T> typeConverter)Adds a type converter.<S,T>
ImpladdConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, java.util.function.Function<S,T> typeConverter)Adds a type converter.<S,T>
booleancanConvert(java.lang.Class<S> sourceType, java.lang.Class<T> targetType)Return whether the given source type is convertible to the given target type.default <T> java.util.Optional<T>convert(java.lang.Object object, ArgumentConversionContext<T> context)Attempts to convert the given object to the given target type.default <T> java.util.Optional<T>convert(java.lang.Object object, Argument<T> targetType)Attempts to convert the given object to the given target type.default <T> java.util.Optional<T>convert(java.lang.Object object, java.lang.Class<T> targetType)Attempts to convert the given object to the given target type.<T> java.util.Optional<T>convert(java.lang.Object object, java.lang.Class<T> targetType, ConversionContext context)Attempts to convert the given object to the given target type.default <T> TconvertRequired(java.lang.Object value, Argument<T> argument)Convert the value to the given type.default <T> TconvertRequired(java.lang.Object value, java.lang.Class<T> type)Convert the value to the given type.
-
-
-
Field Detail
-
SHARED
@Deprecated static final ConversionService<?> SHARED
Deprecated.This will be removed in the next major version. Use an injectedConversionServiceinstead.The default shared conversion service.
-
-
Method Detail
-
addConverter
<S,T> Impl addConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, java.util.function.Function<S,T> typeConverter)
Adds a type converter.- Type Parameters:
S- The source generic typeT- The target generic type- Parameters:
sourceType- The source typetargetType- The target typetypeConverter- The type converter- Returns:
- This conversion service
-
addConverter
<S,T> Impl addConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, TypeConverter<S,T> typeConverter)
Adds a type converter.- Type Parameters:
S- The source generic typeT- The target generic type- Parameters:
sourceType- The source typetargetType- The target typetypeConverter- The type converter- Returns:
- This conversion service
-
convert
<T> java.util.Optional<T> convert(java.lang.Object object, java.lang.Class<T> targetType, ConversionContext context)Attempts to convert the given object to the given target type. If conversion fails or is not possible an emptyOptionalis returned.- Type Parameters:
T- The generic type- Parameters:
object- The object to converttargetType- The target typecontext- The conversion context- Returns:
- The optional
-
canConvert
<S,T> boolean canConvert(java.lang.Class<S> sourceType, java.lang.Class<T> targetType)Return whether the given source type is convertible to the given target type.- Type Parameters:
S- The generic source typeT- The target source type- Parameters:
sourceType- The source typetargetType- The target type- Returns:
- True if it can be converted
-
convert
default <T> java.util.Optional<T> convert(java.lang.Object object, java.lang.Class<T> targetType)Attempts to convert the given object to the given target type. If conversion fails or is not possible an emptyOptionalis returned.- Type Parameters:
T- The generic type- Parameters:
object- The object to converttargetType- The target type- Returns:
- The optional
-
convert
default <T> java.util.Optional<T> convert(java.lang.Object object, Argument<T> targetType)Attempts to convert the given object to the given target type. If conversion fails or is not possible an emptyOptionalis returned.- Type Parameters:
T- The generic type- Parameters:
object- The object to converttargetType- The target type- Returns:
- The optional
-
convert
default <T> java.util.Optional<T> convert(java.lang.Object object, ArgumentConversionContext<T> context)Attempts to convert the given object to the given target type. If conversion fails or is not possible an emptyOptionalis returned.- Type Parameters:
T- The generic type- Parameters:
object- The object to convertcontext- TheArgumentConversionContext- Returns:
- The optional
-
convertRequired
@Nullable default <T> T convertRequired(@Nullable java.lang.Object value, java.lang.Class<T> type)
Convert the value to the given type.- Type Parameters:
T- The generic type- Parameters:
value- The valuetype- The type- Returns:
- The converted value
- Throws:
ConversionErrorException- if the value cannot be converted- Since:
- 1.1.4
-
convertRequired
@Nullable default <T> T convertRequired(@Nullable java.lang.Object value, Argument<T> argument)
Convert the value to the given type.- Type Parameters:
T- The generic type- Parameters:
value- The valueargument- The argument- Returns:
- The converted value
- Throws:
ConversionErrorException- if the value cannot be converted- Since:
- 1.1.4
-
-