Package io.microsphere.convert
Interface Converter<S,T>
-
- Type Parameters:
S
- The source typeT
- The target type
- All Superinterfaces:
java.lang.Comparable<Prioritized>
,Prioritized
- All Known Subinterfaces:
StringConverter<T>
- All Known Implementing Classes:
AbstractConverter
,ByteArrayToObjectConverter
,MapToPropertiesConverter
,NumberToByteConverter
,NumberToCharacterConverter
,NumberToDoubleConverter
,NumberToFloatConverter
,NumberToIntegerConverter
,NumberToLongConverter
,NumberToShortConverter
,ObjectToBooleanConverter
,ObjectToByteArrayConverter
,ObjectToByteConverter
,ObjectToCharacterConverter
,ObjectToDoubleConverter
,ObjectToFloatConverter
,ObjectToIntegerConverter
,ObjectToLongConverter
,ObjectToOptionalConverter
,ObjectToShortConverter
,ObjectToStringConverter
,PropertiesToStringConverter
,StringToBooleanConverter
,StringToByteConverter
,StringToCharacterConverter
,StringToCharArrayConverter
,StringToClassConverter
,StringToDoubleConverter
,StringToDurationConverter
,StringToFloatConverter
,StringToInputStreamConverter
,StringToIntegerConverter
,StringToLongConverter
,StringToShortConverter
,StringToStringConverter
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface Converter<S,T> extends Prioritized
A class to convert the source-typed value to the target-typed value- Since:
- 1.0.0
-
-
Field Summary
-
Fields inherited from interface io.microsphere.lang.Prioritized
COMPARATOR, MAX_PRIORITY, MIN_PRIORITY, NORMAL_PRIORITY
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default boolean
accept(java.lang.Class<?> sourceType, java.lang.Class<?> targetType)
Accept the source type and target type or notT
convert(S source)
Convert the source-typed value to the target-typed valuestatic <T> T
convertIfPossible(java.lang.Object source, java.lang.Class<T> targetType)
Convert the value of source to target-type value if possiblestatic <S,T>
Converter<S,T>getConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType)
Get the Converter instance fromServiceLoader
with the specified source and target typedefault java.lang.Class<S>
getSourceType()
Get the source typedefault java.lang.Class<T>
getTargetType()
Get the target type-
Methods inherited from interface io.microsphere.lang.Prioritized
compareTo, getPriority
-
-
-
-
Method Detail
-
accept
default boolean accept(java.lang.Class<?> sourceType, java.lang.Class<?> targetType)
Accept the source type and target type or not- Parameters:
sourceType
- the source typetargetType
- the target type- Returns:
- if accepted, return
true
, orfalse
-
convert
@Nullable T convert(@Nullable S source)
Convert the source-typed value to the target-typed value- Parameters:
source
- the source-typed value- Returns:
- the target-typed value
-
getSourceType
default java.lang.Class<S> getSourceType()
Get the source type- Returns:
- non-null
-
getTargetType
default java.lang.Class<T> getTargetType()
Get the target type- Returns:
- non-null
-
getConverter
static <S,T> Converter<S,T> getConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType)
Get the Converter instance fromServiceLoader
with the specified source and target type- Parameters:
sourceType
- the source typetargetType
- the target type- Returns:
-
convertIfPossible
static <T> T convertIfPossible(java.lang.Object source, java.lang.Class<T> targetType)
Convert the value of source to target-type value if possible- Type Parameters:
T
- the target type- Parameters:
source
- the value of sourcetargetType
- the target type- Returns:
null
if can't be converted
-
-