Class CachingConverter

java.lang.Object
org.apache.juneau.commons.conversion.CachingConverter
All Implemented Interfaces:
Converter
Direct Known Subclasses:
BasicConverter

public abstract class CachingConverter extends Object implements Converter
An abstract 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:

public class MyConverter extends CachingConverter { @Override protected <I,O> Conversion<I,O> findConversion(Class<I> inType, Class<O> outType) { if (inType == String.class && outType == Integer.class) return (Conversion<I,O>) (Conversion<String,Integer>) (s, memberOf, session, args) -> Integer.valueOf(s); return null; } }

Thread Safety:

This class is thread-safe. The cache uses ConcurrentHashMap for safe concurrent access.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canConvert(Class<?> inType, Class<?> outType)
    Returns true 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> T
    to(Object o, Class<T> type)
    Converts the specified object to the specified type.
    <T> T
    to(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> T
    to(Object o, Object memberOf, ConverterSession session, Type mainType, Type... args)
    Converts the specified object to the specified parameterized type, using the given outer instance and converter session.
    <T> T
    to(Object o, Type mainType, Type... args)
    Converts the specified object to the specified parameterized type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • findConversion

      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.

      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 cause to(Object, Class) and to(Object, Type, Type...) to throw InvalidConversionException for 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 Conversion function, or null if no conversion is available.
    • canConvert

      public boolean canConvert(Class<?> inType, Class<?> outType)
      Description copied from interface: Converter
      Returns true if this converter can convert from the specified input type to the specified output type.
      Specified by:
      canConvert in interface Converter
      Parameters:
      inType - The input type.
      outType - The output type.
      Returns:
      true if a conversion path exists.
    • to

      public <T> T to(Object o, Class<T> type)
      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:
      to in interface Converter
      Type Parameters:
      T - The target type.
      Parameters:
      o - The object to convert. Can be null.
      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, or null otherwise.
      Throws:
      InvalidConversionException - If no conversion path exists from the input type to the target type.
    • to

      public <T> T to(Object o, Type mainType, Type... args)
      Converts the specified object to the specified parameterized type.

      The raw class is extracted from mainType (supporting both Class and ParameterizedType values). Type arguments are extracted from args the same way and passed through to the cached Conversion function at call time.

      The cache is keyed on the raw input and output classes only. The args are not part of the cache key — a single cached Conversion handles all parameterizations of a given output type.

      Specified by:
      to in interface Converter
      Type Parameters:
      T - The target type.
      Parameters:
      o - The object to convert. Can be null.
      mainType - The target type. May be a Class or ParameterizedType.
      args - The type arguments of the target type (e.g. element type for collections).
      Returns:
      The converted object, or null if the input is null.
      Throws:
      InvalidConversionException - If no conversion path exists from the input type to the target type.
    • to

      public <T> T to(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.

      The session is forwarded to the cached Conversion function so conversion lambdas can access contextual objects such as TimeZone or Locale.

      Specified by:
      to in interface Converter
      Type Parameters:
      T - The target type.
      Parameters:
      o - The object to convert. Can be null.
      memberOf - The outer instance for non-static inner class construction, or null.
      session - The converter session providing contextual objects, or null.
      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, or null otherwise.
      Throws:
      InvalidConversionException - If no conversion path exists from the input type to the target type.
    • to

      public <T> T to(Object o, Object memberOf, ConverterSession session, Type mainType, Type... args)
      Converts the specified object to the specified parameterized type, using the given outer instance and converter session.

      The session is forwarded to the cached Conversion function so conversion lambdas can access contextual objects such as TimeZone or Locale.

      Specified by:
      to in interface Converter
      Type Parameters:
      T - The target type.
      Parameters:
      o - The object to convert. Can be null.
      memberOf - The outer instance for non-static inner class construction, or null.
      session - The converter session providing contextual objects, or null.
      mainType - The target type. May be a Class or ParameterizedType.
      args - The type arguments of the target type (e.g. element type for collections).
      Returns:
      The converted object, or null if the input is null.
      Throws:
      InvalidConversionException - If no conversion path exists from the input type to the target type.