Class CachingConverter
- All Implemented Interfaces:
Converter
- Direct Known Subclasses:
BasicConverter
Converter implementation that caches previously determined type conversions.
Conversion functions are discovered lazily on the first call for a given input/output type pair and
stored in a two-level ConcurrentHashMap for subsequent fast lookup.
Subclasses implement findConversion(Class, Class) to provide the actual conversion logic.
The result is cached so that reflection or other expensive discovery happens only once per type pair.
Example:
Thread Safety:
This class is thread-safe. The cache uses ConcurrentHashMap for safe concurrent access.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancanConvert(Class<?> inType, Class<?> outType) Returnstrue if this converter can convert from the specified input type to the specified output type.protected abstract <I,O> Conversion<I, O> findConversion(Class<I> inType, Class<O> outType) Finds a conversion function from the specified input type to the specified output type.<T> TConverts the specified object to the specified type.<T> Tto(Object o, Object memberOf, ConverterSession session, Class<T> type) Converts the specified object to the specified type, using the given outer instance and converter session.<T> TConverts the specified object to the specified parameterized type, using the given outer instance and converter session.<T> TConverts the specified object to the specified parameterized type.
-
Constructor Details
-
CachingConverter
public CachingConverter()
-
-
Method Details
-
findConversion
Finds a conversion function from the specified input type to the specified output type.This method is called lazily the first time a conversion between a given type pair is requested. The result is cached so subsequent calls for the same type pair bypass this method entirely. When this method returns
null , a sentinel is stored so that unconvertable type pairs are also only evaluated once.Implementations should return
null if no conversion is possible for the given types, which will causeto(Object, Class)andto(Object, Type, Type...)to throwInvalidConversionExceptionfor that type pair.- Type Parameters:
I- The input type.O- The output type.- Parameters:
inType- The runtime class of the input object.outType- The target output class.- Returns:
- A
Conversionfunction, ornull if no conversion is available.
-
canConvert
Description copied from interface:ConverterReturnstrue if this converter can convert from the specified input type to the specified output type.- Specified by:
canConvertin interfaceConverter- Parameters:
inType- The input type.outType- The output type.- Returns:
true if a conversion path exists.
-
to
Converts the specified object to the specified type.On the first call for a given input/output type pair,
findConversion(Class, Class)is invoked and the result is cached. Subsequent calls use the cached function directly.- Specified by:
toin interfaceConverter- Type Parameters:
T- The target type.- Parameters:
o- The object to convert. Can benull .type- The target type.- Returns:
- The converted object, or the primitive zero-value if the input is
null and the target is a primitive type, ornull otherwise. - Throws:
InvalidConversionException- If no conversion path exists from the input type to the target type.
-
to
Converts the specified object to the specified parameterized type.The raw class is extracted from
mainType(supporting bothClassandParameterizedTypevalues). Type arguments are extracted fromargsthe same way and passed through to the cachedConversionfunction at call time.The cache is keyed on the raw input and output classes only. The
argsare not part of the cache key — a single cachedConversionhandles all parameterizations of a given output type.- Specified by:
toin interfaceConverter- Type Parameters:
T- The target type.- Parameters:
o- The object to convert. Can benull .mainType- The target type. May be aClassorParameterizedType.args- The type arguments of the target type (e.g. element type for collections).- Returns:
- The converted object, or
null if the input isnull . - Throws:
InvalidConversionException- If no conversion path exists from the input type to the target type.
-
to
Converts the specified object to the specified type, using the given outer instance and converter session.The session is forwarded to the cached
Conversionfunction so conversion lambdas can access contextual objects such asTimeZoneorLocale.- Specified by:
toin interfaceConverter- Type Parameters:
T- The target type.- Parameters:
o- The object to convert. Can benull .memberOf- The outer instance for non-static inner class construction, ornull .session- The converter session providing contextual objects, ornull .type- The target type.- Returns:
- The converted object, or the primitive zero-value if the input is
null and the target is a primitive type, ornull otherwise. - Throws:
InvalidConversionException- If no conversion path exists from the input type to the target type.
-
to
Converts the specified object to the specified parameterized type, using the given outer instance and converter session.The session is forwarded to the cached
Conversionfunction so conversion lambdas can access contextual objects such asTimeZoneorLocale.- Specified by:
toin interfaceConverter- Type Parameters:
T- The target type.- Parameters:
o- The object to convert. Can benull .memberOf- The outer instance for non-static inner class construction, ornull .session- The converter session providing contextual objects, ornull .mainType- The target type. May be aClassorParameterizedType.args- The type arguments of the target type (e.g. element type for collections).- Returns:
- The converted object, or
null if the input isnull . - Throws:
InvalidConversionException- If no conversion path exists from the input type to the target type.
-