Class CustomConversions

java.lang.Object
org.springframework.data.convert.CustomConversions

public class CustomConversions extends Object
Value object to capture custom conversion. That is essentially a List of converters and some additional logic around them. The converters build up two sets of types which store-specific basic types can be converted into and from. These types will be considered simple ones (which means they neither need deeper inspection nor nested conversion. Thus, the CustomConversions also act as factory for SimpleTypeHolder.

Custom conversions supports the following converters:

Note that CustomConversions does not support ConditionalConverter as custom conversions maintains a registry of convertible type pairs; We cannot determine convertible types based on ConditionalConverter#matches, instead, we require a converter to provide its convertible types either through the class declaration or GenericConverter#getConvertibleTypes.
Since:
2.0
Author:
Oliver Gierke, Thomas Darimont, Christoph Strobl, Mark Paluch, Xeno Amess, Johannes Englmeier
  • Constructor Details

  • Method Details

    • getSimpleTypeHolder

      public SimpleTypeHolder getSimpleTypeHolder()
      Returns the underlying SimpleTypeHolder.
      Returns:
      the underlying SimpleTypeHolder.
      See Also:
    • hasValueConverter

      public boolean hasValueConverter(PersistentProperty<?> property)
      Determines whether the given, required property has a value-specific converter registered. Returns false if no PropertyValueConversions have been configured for the underlying store.

      This method protects against null when not PropertyValueConversions have been configured for the underlying data store, and is a shortcut for: customConversions.getPropertyValueConversions().hasValueConverter(property);

      Parameters:
      property - PersistentProperty to evaluate; must not be null.
      Returns:
      a boolean value indicating whether PropertyValueConverter has been configured and registered for the property.
      See Also:
    • isSimpleType

      public boolean isSimpleType(Class<?> type)
      Returns whether the given type is considered to be simple. That means it's either a general simple type or we have a writing Converter registered for a particular type.
      Parameters:
      type - Class to evaluate as a simple type, such as a primitive type.
      Returns:
      a boolean value indicating whether the given, required type is simple.
      See Also:
    • registerConvertersIn

      public void registerConvertersIn(ConverterRegistry conversionService)
      Populates the given GenericConversionService with the converters registered.
      Parameters:
      conversionService - ConverterRegistry to populate; must not be null.
      See Also:
    • getPropertyValueConversions

      @Nullable public PropertyValueConversions getPropertyValueConversions()
      Gets a reference to the configured PropertyValueConversions if property value conversions are supported by the underlying data store.
      Returns:
      a reference to the configured PropertyValueConversions; may be null if the underlying data store does not support property value conversions.
      See Also:
    • getCustomWriteTarget

      public Optional<Class<?>> getCustomWriteTarget(Class<?> sourceType)
      Returns the target type to convert to in case we have a custom conversion registered to convert the given source type into a store native one.
      Parameters:
      sourceType - must not be null
      Returns:
      the target type to convert to in case we have a custom conversion registered to convert the given source type into a store native one.
    • getCustomWriteTarget

      public Optional<Class<?>> getCustomWriteTarget(Class<?> sourceType, Class<?> requestedTargetType)
      Returns the target type we can read an inject of the given source type to. The returned type might be a subclass of the given expected type though. If requestedTargetType is null we will simply return the first target type matching or null if no conversion can be found.
      Parameters:
      sourceType - must not be null
      requestedTargetType - must not be null.
      Returns:
      the target type we can read an inject of the given source type to.
    • hasCustomWriteTarget

      public boolean hasCustomWriteTarget(Class<?> sourceType)
      Returns whether we have a custom conversion registered to read sourceType into a native type. The returned type might be a subclass of the given expected type though.
      Parameters:
      sourceType - must not be null
      Returns:
      whether we have a custom conversion registered to read sourceType into a native type.
    • hasCustomWriteTarget

      public boolean hasCustomWriteTarget(Class<?> sourceType, Class<?> targetType)
      Returns whether we have a custom conversion registered to read an object of the given source type into an object of the given native target type.
      Parameters:
      sourceType - must not be null.
      targetType - must not be null.
      Returns:
      whether we have a custom conversion registered to read an object of the given source type into an object of the given native target type.
    • hasCustomReadTarget

      public boolean hasCustomReadTarget(Class<?> sourceType, Class<?> targetType)
      Returns whether we have a custom conversion registered to read the given source into the given target type.
      Parameters:
      sourceType - must not be null
      targetType - must not be null
      Returns:
      whether we have a custom conversion registered to read the given source into the given target type.