Class AbstractConverter<S,T>
- java.lang.Object
-
- io.microsphere.convert.AbstractConverter<S,T>
-
- Type Parameters:
S
- The source typeT
- The target type
- All Implemented Interfaces:
Converter<S,T>
,Prioritized
,java.lang.Comparable<Prioritized>
- Direct Known Subclasses:
ByteArrayToObjectConverter
,MapToPropertiesConverter
,NumberToByteConverter
,NumberToCharacterConverter
,NumberToDoubleConverter
,NumberToFloatConverter
,NumberToIntegerConverter
,NumberToLongConverter
,NumberToShortConverter
,ObjectToBooleanConverter
,ObjectToByteArrayConverter
,ObjectToByteConverter
,ObjectToCharacterConverter
,ObjectToDoubleConverter
,ObjectToFloatConverter
,ObjectToIntegerConverter
,ObjectToLongConverter
,ObjectToShortConverter
,ObjectToStringConverter
,PropertiesToStringConverter
,StringToBooleanConverter
,StringToByteConverter
,StringToCharacterConverter
,StringToCharArrayConverter
,StringToClassConverter
,StringToDoubleConverter
,StringToDurationConverter
,StringToFloatConverter
,StringToInputStreamConverter
,StringToIntegerConverter
,StringToLongConverter
,StringToShortConverter
,StringToStringConverter
public abstract class AbstractConverter<S,T> extends java.lang.Object implements Converter<S,T>
An abstract base class for implementing theConverter
interface.This class provides a default implementation for priority resolution based on the inheritance hierarchy depth of source and target types, ensuring more specific converters (those handling more derived types) receive higher priority. It also includes common functionality such as logging support, null safety in conversion, and proper equals and hashcode behavior.
Example Usage
public class StringToIntegerConverter extends AbstractConverter<String, Integer> { public boolean accept(Class<?> sourceType, Class<?> targetType) { return sourceType.isAssignableFrom(String.class) && targetType.isAssignableFrom(Integer.class); } public Integer doConvert(String source) { return Integer.parseInt(source); } }
Prioritized Behavior Example
{@code public class HighPriorityStringToIntegerConverter extends AbstractConverter
{ public boolean accept(Class> sourceType, Class> targetType) { return sourceType.isAssignableFrom(String.class) && targetType.isAssignableFrom(Integer.class); } public Integer doConvert(String source) { return Integer.parseInt(source); } - Since:
- 1.0.0
- Author:
- Mercy
- See Also:
Converter
,Prioritized
-
-
Field Summary
Fields Modifier and Type Field Description protected Logger
logger
-
Fields inherited from interface io.microsphere.lang.Prioritized
COMPARATOR, MAX_PRIORITY, MIN_PRIORITY, NORMAL_PRIORITY
-
-
Constructor Summary
Constructors Constructor Description AbstractConverter()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description T
convert(S source)
Convert the source-typed value to the target-typed valueprotected abstract T
doConvert(S source)
Converts the non-null source-typed value to the target-typed value.boolean
equals(java.lang.Object o)
int
getPriority()
Get the priorityint
hashCode()
protected java.lang.Integer
resolvePriority()
Resolve the priority based on the inheritance hierarchy depth of source and target types.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.microsphere.convert.Converter
accept, getSourceType, getTargetType
-
Methods inherited from interface io.microsphere.lang.Prioritized
compareTo
-
-
-
-
Field Detail
-
logger
protected final Logger logger
-
-
Method Detail
-
convert
@Nullable public final T convert(@Nullable S source)
Description copied from interface:Converter
Convert the source-typed value to the target-typed value
-
doConvert
@Nullable protected abstract T doConvert(@Nonnull S source) throws java.lang.Throwable
Converts the non-null source-typed value to the target-typed value.This method is called by
convert(Object)
after checking if the source is null. Subclasses must implement this method to provide the actual conversion logic.- Parameters:
source
- the non-null source-typed value- Returns:
- the converted target-typed value, or
null
if conversion is not possible - Throws:
java.lang.Throwable
- if an error occurs during conversion
-
resolvePriority
protected java.lang.Integer resolvePriority()
Resolve the priority based on the inheritance hierarchy depth of source and target types.The priority is calculated by combining the number of superclasses and interfaces from both the source and target types. This ensures more specific converters (those handling more derived types) receive higher priority.
- Returns:
- the resolved priority as an
Integer
, negative value indicates higher priority
-
getPriority
public int getPriority()
Description copied from interface:Prioritized
Get the priority- Specified by:
getPriority
in interfacePrioritized
- Returns:
- the default is
minimum one
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-