Class MappingUtils
java.lang.Object
com.broadleafcommerce.data.tracking.core.mapping.MappingUtils
Utility methods related to mapping activities in data tracking and ModelMapper
-
Field Summary
FieldsModifier and TypeFieldDescriptionSynthetic properties in the TypeMap when a Converter is primarily responsible for mapping. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
fromMeOptimizedTypeMapWithConverter
(org.modelmapper.ModelMapper mapper, Class<?> sourceType, Class<?> destinationType, Function<org.modelmapper.spi.MappingContext<?, ?>, Object> converter, NearbyNestedMapping... nestedMappings) Create aTypeMap
instance that uses an explicit converter function (as a ModelMapper preConverter) for performing the mapping conversion.getTrackingType
(Class<D> domainType) Get the data tracking type for the repository domain typegetTrackingType
(Object domainInstance) Get the data tracking type for the repository domain instancestatic void
includeBaseConverters
(org.modelmapper.ModelMapper mapper, org.modelmapper.TypeMap<?, ?> typeMap, Class<?> parentSource, Class<?> parentDestination) TypeMap.includeBase(Class, Class)
does not automatically include pre and post converters.static void
includeBaseConverters
(org.modelmapper.ModelMapper mapper, org.modelmapper.TypeMap<?, ?> typeMap, Class<?> parentSource, Class<?> parentDestination, org.modelmapper.Converter<?, ?> preOverride, org.modelmapper.Converter<?, ?> postOverride, boolean skipParentConverters) TypeMap.includeBase(Class, Class)
does not automatically include pre and post converters.static boolean
isResidentFromMe
(Class<? extends ModelMapperMappable> entityClass) Whether theModelMapperMappable.fromMe()
method declaration is resident in the entityClass param.static boolean
isResidentToMe
(Class<? extends ModelMapperMappable> entityClass) Whether theModelMapperMappable.toMe()
method declaration is resident in the entityClass param.static <T,
G> void Perform mapping for a particular field using getter/setter from a POJO to another POJO.static <T> void
Perform mapping for a particular field using getter/setter from a POJO to another POJO.static void
setupExtensions
(org.modelmapper.ModelMapper mapper, Class<?> extendedSource, Class<?> extendedDestination, Class<?> parentSource, Class<?> parentDestination) Creates aTypeMap
for the extended source and destination.static void
setupExtensions
(org.modelmapper.ModelMapper mapper, Class<?> extendedSource, Class<?> extendedDestination, Class<?> parentSource, Class<?> parentDestination, org.modelmapper.Converter<?, ?> preOverride, org.modelmapper.Converter<?, ?> postOverride, boolean skipParentConverters) Creates aTypeMap
for the extended source and destination.static void
toMeOptimizedTypeMapWithConverter
(org.modelmapper.ModelMapper mapper, Class<?> sourceType, Class<?> destinationType, Function<org.modelmapper.spi.MappingContext<?, ?>, ?> converter, NearbyNestedMapping... nestedMappings) Create aTypeMap
instance that uses an explicit converter function (as a ModelMapper preConverter) for performing the mapping conversion.
-
Field Details
-
CONVERTER_OPTIMIZED_PROPERTIES_BY_MODEL_TYPE
Synthetic properties in the TypeMap when a Converter is primarily responsible for mapping. In this case, these properties only serve to inform the construction ofChangeDetail
instances. -
KEPT_PROPERTIES_BY_MODEL_TYPE
-
-
Method Details
-
isResidentToMe
Whether theModelMapperMappable.toMe()
method declaration is resident in the entityClass param. If provided in a superclass, then false.- Parameters:
entityClass
- The class to investigate- Returns:
- Whether the
ModelMapperMappable.toMe()
method declaration is resident in the entityClass param.
-
isResidentFromMe
Whether theModelMapperMappable.fromMe()
method declaration is resident in the entityClass param. If provided in a superclass, then false.- Parameters:
entityClass
- The class to investigate- Returns:
- Whether the
ModelMapperMappable.fromMe()
method declaration is resident in the entityClass param.
-
fromMeOptimizedTypeMapWithConverter
public static void fromMeOptimizedTypeMapWithConverter(@NonNull org.modelmapper.ModelMapper mapper, @NonNull Class<?> sourceType, @NonNull Class<?> destinationType, @NonNull Function<org.modelmapper.spi.MappingContext<?, ?>, Object> converter, NearbyNestedMapping... nestedMappings) Create aTypeMap
instance that uses an explicit converter function (as a ModelMapper preConverter) for performing the mapping conversion. Implicit mappings are still compiled, but are all marked as skipped, except fornestedMappings
. The implicit mappings remain to serve their function as reference forChangeDetail
compilation, but are not used during actual mapping. Converting in this way should provide a performance benefit over standard implicit mapping conversion. This represents the mapping from the persistence state to the projection state.- Parameters:
mapper
- A pre-instantiatedModelMapper
instance. Should use strict property matching.sourceType
- The type of the source domaindestinationType
- The type of the destination domainconverter
- The converter function to use to convert from the source property type to the destination property typenestedMappings
- The paths for property mappings to keep that require additional mapping. These include field references to complex types or collections/maps containing complex types.- See Also:
-
toMeOptimizedTypeMapWithConverter
public static void toMeOptimizedTypeMapWithConverter(@NonNull org.modelmapper.ModelMapper mapper, @NonNull Class<?> sourceType, @NonNull Class<?> destinationType, @NonNull Function<org.modelmapper.spi.MappingContext<?, ?>, ?> converter, NearbyNestedMapping... nestedMappings) Create aTypeMap
instance that uses an explicit converter function (as a ModelMapper preConverter) for performing the mapping conversion. Implicit mappings are still compiled, but are all marked as skipped, except fornestedMappings
. The implicit mappings remain to serve their function as reference forChangeDetail
compilation, but are not used during actual mapping. Converting in this way should provide a performance benefit over standard implicit mapping conversion. This represents the mapping from the projection state to the persistence state.- Parameters:
mapper
- A pre-instantiatedModelMapper
instance. Should use strict property matching.sourceType
- The type of the source domaindestinationType
- The type of the destination domainconverter
- The converter function to use to convert from the source property type to the destination property typenestedMappings
- The paths for property mappings to keep that require additional mapping. These include field references to complex types or collections/maps containing complex types.- See Also:
-
map
public static <T,G> void map(@NonNull Supplier<T> from, @NonNull Consumer<G> to, @NonNull Function<T, G> converter, Supplier... conditions) Perform mapping for a particular field using getter/setter from a POJO to another POJO. Generally used inside aConverter
supplied viafromMeOptimizedTypeMapWithConverter(ModelMapper, Class, Class, Function, NearbyNestedMapping...)
andtoMeOptimizedTypeMapWithConverter(ModelMapper, Class, Class, Function, NearbyNestedMapping...)
.- Type Parameters:
T
- The value suppliedG
- The value applied to the destination- Parameters:
from
- The supplier for the field value from a POJOto
- The consumer for applying the field value to the target POJO.converter
- A function that can perform type conversion on the transfer valueconditions
- Optional array of boolean suppliers. If one or more return false, the mapping will be skipped.
-
map
public static <T> void map(@NonNull Supplier<T> from, @NonNull Consumer<T> to, Supplier... conditions) Perform mapping for a particular field using getter/setter from a POJO to another POJO. Generally used inside aConverter
supplied viafromMeOptimizedTypeMapWithConverter(ModelMapper, Class, Class, Function, NearbyNestedMapping...)
andtoMeOptimizedTypeMapWithConverter(ModelMapper, Class, Class, Function, NearbyNestedMapping...)
- Type Parameters:
T
- The value supplied- Parameters:
from
- The supplier for the field value from a POJOto
- The consumer for applying the field value to the target POJO.conditions
- Optional array of boolean suppliers. If one or more return false, the mapping will be skipped.
-
getTrackingType
Get the data tracking type for the repository domain type- Type Parameters:
D
- The repository domain type- Parameters:
domainType
- The repository domain type- Returns:
- The data tracking type
-
getTrackingType
Get the data tracking type for the repository domain instance- Parameters:
domainInstance
- The repository domain instance- Returns:
- The data tracking type
-
includeBaseConverters
public static void includeBaseConverters(org.modelmapper.ModelMapper mapper, org.modelmapper.TypeMap<?, ?> typeMap, Class<?> parentSource, Class<?> parentDestination, @Nullable org.modelmapper.Converter<?, ?> preOverride, @Nullable org.modelmapper.Converter<?, ?> postOverride, boolean skipParentConverters) TypeMap.includeBase(Class, Class)
does not automatically include pre and post converters. They must be handled separately. This utility method makes sure those converters are registered on the passedTypeMap
instance. Optionally, pre and post converter overrides may be passed, in which case, any parent converters will be called first, before calling the override. Note, if the parent converters should be skipped, then theskipParentConverters
can be set to true.- Parameters:
mapper
- The ModelMapper mapper instancetypeMap
- The inheriting (child) type map instanceparentSource
- The source type of the parent for the mappingparentDestination
- The destination type of the parent for the mappingpreOverride
- A converter to run prior to mapping. Will also call any parent pre converters, unlessskipParentConverters
is true.postOverride
- A converter to run after mapping. Will also call any parent post converters, unlessskipParentConverters
is true.skipParentConverters
- Whether or not to skip parent converters
-
setupExtensions
public static void setupExtensions(org.modelmapper.ModelMapper mapper, Class<?> extendedSource, Class<?> extendedDestination, Class<?> parentSource, Class<?> parentDestination, @Nullable org.modelmapper.Converter<?, ?> preOverride, @Nullable org.modelmapper.Converter<?, ?> postOverride, boolean skipParentConverters) Creates aTypeMap
for the extended source and destination. Also, includes mappings from the super classes. Finally, callsincludeBaseConverters(ModelMapper, TypeMap, Class, Class, Converter, Converter, boolean)
to make sure any converters from the superclass are also included, while also registering the optional pre and post converters passed in.- Parameters:
mapper
- The ModelMapper mapper instanceextendedSource
- The source type of the extension for the mappingextendedDestination
- The destination type of the extension for the mappingparentSource
- The source type of the parent for the mappingparentDestination
- The destination type of the parent for the mappingpreOverride
- A converter to run prior to mapping. Will also call any parent pre converters, unlessskipParentConverters
is true.postOverride
- A converter to run after mapping. Will also call any parent post converters, unlessskipParentConverters
is true.skipParentConverters
- Whether or not to skip parent converters
-
setupExtensions
public static void setupExtensions(org.modelmapper.ModelMapper mapper, Class<?> extendedSource, Class<?> extendedDestination, Class<?> parentSource, Class<?> parentDestination) Creates aTypeMap
for the extended source and destination. Also, includes mappings from the super classes. Finally, callsincludeBaseConverters(ModelMapper, TypeMap, Class, Class)
to make sure any converters from the superclass are also included.- Parameters:
mapper
- The ModelMapper mapper instanceextendedSource
- The source type of the extension for the mappingextendedDestination
- The destination type of the extension for the mappingparentSource
- The source type of the parent for the mappingparentDestination
- The destination type of the parent for the mapping
-
includeBaseConverters
public static void includeBaseConverters(org.modelmapper.ModelMapper mapper, org.modelmapper.TypeMap<?, ?> typeMap, Class<?> parentSource, Class<?> parentDestination) TypeMap.includeBase(Class, Class)
does not automatically include pre and post converters. They must be handled separately. This utility method makes sure those converters are registered on the passedTypeMap
instance.- Parameters:
mapper
- The ModelMapper mapper instancetypeMap
- The inheriting (child) type map instanceparentSource
- The source type of the parent for the mappingparentDestination
- The destination type of the parent for the mapping- See Also:
-