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 functional interface that defines a strategy for converting values from one type (S
) to another type (T
).Implementations of this interface can be used to encapsulate conversion logic between types, and they may optionally implement the
Prioritized
interface to control ordering when multiple converters are available.Example Usage
public class StringToIntegerConverter implements Converter<String, Integer> { public boolean accept(Class<?> sourceType, Class<?> targetType) { return sourceType.isAssignableFrom(String.class) && targetType.isAssignableFrom(Integer.class); } public Integer convert(String source) { return Integer.parseInt(source); } }
Prioritized Behavior Example
public class HighPriorityStringToIntegerConverter implements Converter<String, Integer>, Prioritized { public int getPriority() { return Prioritized.MAX_PRIORITY; // Highest priority } public boolean accept(Class<?> sourceType, Class<?> targetType) { return sourceType.isAssignableFrom(String.class) && targetType.isAssignableFrom(Integer.class); } public Integer convert(String source) { return Integer.parseInt(source); } }
- Since:
- 1.0.0
- Author:
- Mercy
-
-
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)
Converts the givensource
object to the specifiedtargetType
if a suitable converter is available.static <S,T>
Converter<S,T>getConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType)
Retrieves a converter instance that can convert from the specified source type to the target type.default 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)
Retrieves a converter instance that can convert from the specified source type to the target type.This method uses the service loader mechanism to find all available converters and sorts them based on their priority. The first converter that accepts the given source and target types will be returned.
Example Usage
Class<String> sourceType = String.class; Class<Integer> targetType = Integer.class; Converter<String, Integer> converter = getConverter(sourceType, targetType); if (converter != null) { Integer result = converter.convert("123"); }
- Type Parameters:
S
- the source typeT
- the target type- Parameters:
sourceType
- the class of the source typetargetType
- the class of the target type- Returns:
- a converter instance that can handle the specified types, or
null
if no suitable converter is found
-
convertIfPossible
static <T> T convertIfPossible(java.lang.Object source, java.lang.Class<T> targetType)
Converts the givensource
object to the specifiedtargetType
if a suitable converter is available.This method attempts to find a converter that can convert from the type of the source object to the target type using the service loader mechanism. If such a converter exists, it will be used to perform the conversion. If no suitable converter is found, this method returns
null
.Example Usage
String source = "123"; Integer result = convertIfPossible(source, Integer.class);
- Type Parameters:
T
- the type of the target- Parameters:
source
- the source object to be convertedtargetType
- the target type to convert to- Returns:
- the converted object of type
T
, ornull
if no suitable converter is found
-
-