diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fceccb..c410ad9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Issue [#196](https://github.com/42BV/beanmapper/issues/196) **Calls to the logger should not contain a call to String.formatted(Object...)**; Fixed by removing any formatting from performance logging. Trace-logging should keep the level of detail provided by the formatting. - Issue [#197](https://github.com/42BV/beanmapper/issues/197) **Implement caching for Unproxy results.**; Created UnproxyResultStore, allowing for thread-safe caching and retrieval of unproxied classes. - Issue [#199](https://github.com/42BV/beanmapper/issues/199) **Use of OverrideField causes excessive resource consumption.**; Use of OverrideField was phased out. OverrideField itself is now deprecated for removal. +- Issue [#201](https://github.com/42BV/beanmapper/issues/201) **Calls from OverrideConfiguration to parentConfiguration cause unnecessary overhead.**; Introduced fields in the OverrideConfiguration, to limit the overhead of calls to the parent configuration. ## [4.1.3] - 2024-03-27 diff --git a/src/main/java/io/beanmapper/config/Configuration.java b/src/main/java/io/beanmapper/config/Configuration.java index 24b1561..ae449c7 100644 --- a/src/main/java/io/beanmapper/config/Configuration.java +++ b/src/main/java/io/beanmapper/config/Configuration.java @@ -149,6 +149,13 @@ public interface Configuration { */ List getBeanPairs(); + /** + * Retrieves the CollectionHandlerStore from the CoreConfiguration. + * + * @return The CollectionHandlerStore retrieved from the CoreConfiguration. + */ + CollectionHandlerStore getCollectionHandlerStore(); + boolean isConverterChoosable(); void setConverterChoosable(boolean converterChoosable); @@ -448,4 +455,11 @@ public interface Configuration { * @param The type of the associated value. */ V getDefaultValueForClass(Class targetClass); + + /** + * Retrieved the Map containing the custom values for classes registered to the current mapping. + * + * @return Map containing custom values per type. + */ + Map, Object> getCustomDefaultValuesMap(); } diff --git a/src/main/java/io/beanmapper/config/CoreConfiguration.java b/src/main/java/io/beanmapper/config/CoreConfiguration.java index 45b1440..c0f54d0 100644 --- a/src/main/java/io/beanmapper/config/CoreConfiguration.java +++ b/src/main/java/io/beanmapper/config/CoreConfiguration.java @@ -430,9 +430,8 @@ public void setFlushAfterClear(Trinary flushAfterClear) { */ @Override public V getDefaultValueForClass(Class targetClass) { - return this.customDefaultValueMap.containsKey(targetClass) - ? (V) this.customDefaultValueMap.get(targetClass) - : DefaultValues.defaultValueFor(targetClass); + Object defaultValue = customDefaultValueMap.get(targetClass); + return defaultValue != null ? (V) defaultValue : DefaultValues.defaultValueFor(targetClass); } /** @@ -442,4 +441,20 @@ public V getDefaultValueForClass(Class targetClass) { public void addCustomDefaultValueForClass(Class targetClass, V value) { this.customDefaultValueMap.put(targetClass, value); } + + /** + * {@inheritDoc} + */ + @Override + public CollectionHandlerStore getCollectionHandlerStore() { + return this.collectionHandlerStore; + } + + /** + * {@inheritDoc} + */ + @Override + public Map, Object> getCustomDefaultValuesMap() { + return customDefaultValueMap; + } } diff --git a/src/main/java/io/beanmapper/config/OverrideConfiguration.java b/src/main/java/io/beanmapper/config/OverrideConfiguration.java index ddac73b..1692394 100644 --- a/src/main/java/io/beanmapper/config/OverrideConfiguration.java +++ b/src/main/java/io/beanmapper/config/OverrideConfiguration.java @@ -1,7 +1,6 @@ package io.beanmapper.config; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -15,6 +14,7 @@ import io.beanmapper.core.unproxy.BeanUnproxy; import io.beanmapper.dynclass.ClassStore; import io.beanmapper.exceptions.BeanConfigurationOperationNotAllowedException; +import io.beanmapper.utils.DefaultValues; import io.beanmapper.utils.Trinary; public class OverrideConfiguration implements Configuration { @@ -23,14 +23,34 @@ public class OverrideConfiguration implements Configuration { private BeanInitializer beanInitializer; - private final List beanConverters = new ArrayList<>(); + private final List beanConverters; - private final List beanPairs = new ArrayList<>(); + private final List beanPairs; private final List downsizeSourceFields; private final List downsizeTargetFields; + private final Map, Object> customDefaultValuesMap; + + private final CollectionHandlerStore collectionHandlerStore; + + private final BeanUnproxy beanUnproxy; + + private final BeanMatchStore beanMatchStore; + + private final List packagePrefixes; + + private final Map, LogicSecuredCheck> logicSecuredChecks; + + private final List collectionHandlers; + + private final CollectionFlusher collectionFlusher; + + private final RoleSecuredCheck roleSecuredCheck; + + private final StrictMappingProperties strictMappingProperties; + private Class targetClass; private Object target; @@ -41,11 +61,9 @@ public class OverrideConfiguration implements Configuration { private boolean converterChoosable; - private final StrictMappingProperties strictMappingProperties; - - private BeanCollectionUsage collectionUsage = null; + private BeanCollectionUsage collectionUsage; - private Class preferredCollectionClass = null; + private Class preferredCollectionClass; private boolean enforcedSecuredProperties; @@ -55,8 +73,6 @@ public class OverrideConfiguration implements Configuration { private boolean flushEnabled; - private final Map, Object> customDefaultValues; - public OverrideConfiguration(Configuration configuration) { if (configuration == null) { throw new ParentConfigurationPossiblyNullException("Developer error: the parent configuration may not be null"); @@ -72,17 +88,29 @@ public OverrideConfiguration(Configuration configuration) { this.useNullValue = configuration.getUseNullValue(); this.strictMappingProperties = configuration.getStrictMappingProperties(); this.enforcedSecuredProperties = configuration.getEnforceSecuredProperties(); - this.customDefaultValues = new HashMap<>(); + this.customDefaultValuesMap = new HashMap<>(parentConfiguration.getCustomDefaultValuesMap()); + this.collectionHandlerStore = parentConfiguration.getCollectionHandlerStore(); + this.beanUnproxy = parentConfiguration.getBeanUnproxy(); + this.beanMatchStore = parentConfiguration.getBeanMatchStore(); + this.packagePrefixes = new ArrayList<>(parentConfiguration.getPackagePrefixes()); + this.beanConverters = new ArrayList<>(parentConfiguration.getBeanConverters()); + this.beanPairs = new ArrayList<>(parentConfiguration.getBeanPairs()); + this.collectionUsage = parentConfiguration.getCollectionUsage(); + this.logicSecuredChecks = parentConfiguration.getLogicSecuredChecks(); + this.collectionHandlers = parentConfiguration.getCollectionHandlers(); + this.collectionFlusher = parentConfiguration.getCollectionFlusher(); + this.roleSecuredCheck = parentConfiguration.getRoleSecuredCheck(); + this.preferredCollectionClass = parentConfiguration.getPreferredCollectionClass(); } @Override public List getDownsizeSource() { - return this.downsizeSourceFields; + return downsizeSourceFields; } @Override public List getDownsizeTarget() { - return this.downsizeTargetFields; + return downsizeTargetFields; } @Override @@ -97,17 +125,17 @@ public void setCollectionClass(Class collectionClass) { @Override public CollectionHandler getCollectionHandlerForCollectionClass() { - return getCollectionHandlerFor(getCollectionClass()); + return collectionHandlerStore.getCollectionHandlerFor(collectionClass, beanUnproxy); } @Override public CollectionHandler getCollectionHandlerFor(Class clazz) { - return parentConfiguration.getCollectionHandlerFor(clazz); + return getCollectionHandlerFor(clazz); } @Override public Class getTargetClass() { - return this.targetClass; + return targetClass; } @Override @@ -147,7 +175,7 @@ public void setBeanInitializer(BeanInitializer beanInitializer) { @Override public BeanUnproxy getBeanUnproxy() { - return parentConfiguration.getBeanUnproxy(); + return beanUnproxy; } @Override @@ -158,7 +186,7 @@ public void setBeanUnproxy(BeanUnproxy beanUnproxy) { @Override public BeanMatchStore getBeanMatchStore() { - return parentConfiguration.getBeanMatchStore(); + return beanMatchStore; } @Override @@ -168,35 +196,26 @@ public ClassStore getClassStore() { @Override public List getPackagePrefixes() { - var list = parentConfiguration.getPackagePrefixes(); - return list != null ? list : Collections.emptyList(); + return packagePrefixes; } @Override public List getBeanConverters() { - List converters = new ArrayList<>(); - converters.addAll(parentConfiguration.getBeanConverters()); - converters.addAll(beanConverters); - return converters; + return beanConverters; } @Override public Map, LogicSecuredCheck> getLogicSecuredChecks() { - var map = this.parentConfiguration.getLogicSecuredChecks(); - return map != null ? map : Collections.emptyMap(); + return logicSecuredChecks; } @Override public List getCollectionHandlers() { - var list = this.parentConfiguration.getCollectionHandlers(); - return list != null ? list : Collections.emptyList(); + return collectionHandlers; } @Override public List getBeanPairs() { - List beanPairs = new ArrayList<>(); - beanPairs.addAll(parentConfiguration.getBeanPairs()); - beanPairs.addAll(this.beanPairs); return beanPairs; } @@ -218,12 +237,12 @@ public void withoutDefaultConverters() { @Override public String getStrictSourceSuffix() { - return this.strictMappingProperties.getStrictSourceSuffix(); + return strictMappingProperties.getStrictSourceSuffix(); } @Override public void setStrictSourceSuffix(String strictSourceSuffix) { - this.strictMappingProperties.setStrictSourceSuffix(strictSourceSuffix); + strictMappingProperties.setStrictSourceSuffix(strictSourceSuffix); } @Override @@ -233,7 +252,7 @@ public String getStrictTargetSuffix() { @Override public void setStrictTargetSuffix(String strictTargetSuffix) { - this.strictMappingProperties.setStrictTargetSuffix(strictTargetSuffix); + strictMappingProperties.setStrictTargetSuffix(strictTargetSuffix); } @Override @@ -243,7 +262,7 @@ public boolean isApplyStrictMappingConvention() { @Override public void setApplyStrictMappingConvention(boolean applyStrictMappingConvention) { - this.strictMappingProperties.setApplyStrictMappingConvention(applyStrictMappingConvention); + strictMappingProperties.setApplyStrictMappingConvention(applyStrictMappingConvention); } @Override @@ -268,14 +287,12 @@ public void setPreferredCollectionClass(Class preferredCollectionClass) { @Override public CollectionFlusher getCollectionFlusher() { - return parentConfiguration.getCollectionFlusher(); + return collectionFlusher; } @Override public BeanCollectionUsage getCollectionUsage() { - return this.collectionUsage == null ? - parentConfiguration.getCollectionUsage() : - this.collectionUsage; + return collectionUsage; } @Override @@ -290,7 +307,7 @@ public Trinary isFlushAfterClear() { @Override public boolean isFlushEnabled() { - return this.flushEnabled; + return flushEnabled; } @Override @@ -315,7 +332,7 @@ public void setUseNullValue(boolean useNullValue) { @Override public RoleSecuredCheck getRoleSecuredCheck() { - return parentConfiguration.getRoleSecuredCheck(); + return roleSecuredCheck; } @Override @@ -326,12 +343,12 @@ public void setRoleSecuredCheck(RoleSecuredCheck roleSecuredCheck) { @Override public boolean getEnforceSecuredProperties() { - return this.enforcedSecuredProperties; + return enforcedSecuredProperties; } @Override public void setEnforceSecuredProperties(boolean enforceSecuredProperties) { - this.enforcedSecuredProperties = enforceSecuredProperties; + enforcedSecuredProperties = enforceSecuredProperties; } @Override @@ -353,12 +370,12 @@ public void addCollectionHandler(CollectionHandler collectionHandler) { @Override public void addBeanPairWithStrictSource(Class source, Class target) { - this.beanPairs.add(new BeanPair(source, target).withStrictSource()); + beanPairs.add(new BeanPair(source, target).withStrictSource()); } @Override public void addBeanPairWithStrictTarget(Class source, Class target) { - this.beanPairs.add(new BeanPair(source, target).withStrictTarget()); + beanPairs.add(new BeanPair(source, target).withStrictTarget()); } @Override @@ -392,14 +409,14 @@ public boolean isAddDefaultConverters() { @Override public void downsizeSource(List includeFields) { - this.downsizeSourceFields.clear(); - this.downsizeSourceFields.addAll(includeFields); + downsizeSourceFields.clear(); + downsizeSourceFields.addAll(includeFields); } @Override public void downsizeTarget(List includeFields) { - this.downsizeTargetFields.clear(); - this.downsizeTargetFields.addAll(includeFields); + downsizeTargetFields.clear(); + downsizeTargetFields.addAll(includeFields); } @Override @@ -417,7 +434,7 @@ public void setFlushAfterClear(Trinary flushAfterClear) { */ @Override public void addCustomDefaultValueForClass(Class target, V value) { - this.customDefaultValues.put(target, value); + customDefaultValuesMap.put(target, value); } /** @@ -425,9 +442,23 @@ public void addCustomDefaultValueForClass(Class target, V value) { */ @Override public V getDefaultValueForClass(Class targetClass) { - return this.customDefaultValues.containsKey(targetClass) - ? (V) this.customDefaultValues.get(targetClass) - : this.parentConfiguration.getDefaultValueForClass(targetClass); + V value = (V) customDefaultValuesMap.get(targetClass); + return value != null ? value : DefaultValues.defaultValueFor(targetClass); + } + + /** + * {@inheritDoc} + */ + @Override + public CollectionHandlerStore getCollectionHandlerStore() { + return collectionHandlerStore; } + /** + * {@inheritDoc} + */ + @Override + public Map, Object> getCustomDefaultValuesMap() { + return customDefaultValuesMap; + } }