diff --git a/src/main/java/io/beanmapper/config/Configuration.java b/src/main/java/io/beanmapper/config/Configuration.java index 24b1561..dae61c5 100644 --- a/src/main/java/io/beanmapper/config/Configuration.java +++ b/src/main/java/io/beanmapper/config/Configuration.java @@ -424,7 +424,7 @@ public interface Configuration { * Allows the user to set a default value for a given type. * *

While the standard implementation of BeanMapper contains a utility-class - * {@link io.beanmapper.utils.DefaultValues}, the map within is far from exhaustive. Furthermore, custom default + * {@link io.beanmapper.utils.provider.Provider}, the map within is far from exhaustive. Furthermore, custom default * values can serve as a useful compatibility feature for projects that would prefer different defaults.

* * @param target The target class. @@ -439,7 +439,7 @@ public interface Configuration { * *

Any implementation of this method must first check the registered custom default values for a suitable value, * if a container for custom defaults exists. Afterwards, the method may check a parent-configuration's custom - * defaults, or simply refer to the defaults in {@link io.beanmapper.utils.DefaultValues}.

+ * defaults, or simply refer to the defaults in {@link io.beanmapper.utils.provider.Provider}.

* * @param targetClass The target class * @return The value associated with the given target class, based off of the values set in the custom default diff --git a/src/main/java/io/beanmapper/config/CoreConfiguration.java b/src/main/java/io/beanmapper/config/CoreConfiguration.java index 45b1440..cd2def0 100644 --- a/src/main/java/io/beanmapper/config/CoreConfiguration.java +++ b/src/main/java/io/beanmapper/config/CoreConfiguration.java @@ -18,7 +18,7 @@ import io.beanmapper.core.unproxy.SkippingBeanUnproxy; import io.beanmapper.dynclass.ClassStore; import io.beanmapper.exceptions.BeanConfigurationOperationNotAllowedException; -import io.beanmapper.utils.DefaultValues; +import io.beanmapper.utils.provider.Provider; import io.beanmapper.utils.Trinary; public class CoreConfiguration implements Configuration { @@ -430,9 +430,12 @@ public void setFlushAfterClear(Trinary flushAfterClear) { */ @Override public V getDefaultValueForClass(Class targetClass) { - return this.customDefaultValueMap.containsKey(targetClass) - ? (V) this.customDefaultValueMap.get(targetClass) - : DefaultValues.defaultValueFor(targetClass); + if (this.customDefaultValueMap.containsKey(targetClass)) { + return (V) this.customDefaultValueMap.get(targetClass); + } + return (V) Provider.of(targetClass) != null + ? (V) Provider.of(targetClass).getDefault() + : null; } /** diff --git a/src/main/java/io/beanmapper/core/BeanProperty.java b/src/main/java/io/beanmapper/core/BeanProperty.java index 6e8c5bc..4d221f2 100644 --- a/src/main/java/io/beanmapper/core/BeanProperty.java +++ b/src/main/java/io/beanmapper/core/BeanProperty.java @@ -8,8 +8,8 @@ import io.beanmapper.core.inspector.PropertyAccessor; import io.beanmapper.exceptions.BeanMappingException; import io.beanmapper.strategy.ConstructorArguments; -import io.beanmapper.utils.DefaultValues; import io.beanmapper.utils.Records; +import io.beanmapper.utils.provider.Provider; public class BeanProperty { @@ -117,7 +117,7 @@ public Object writeObject(Object value, Object parent, Object source, BeanMatch } else { if (value == null && getCurrentAccessor().getType().isPrimitive()) { // Primitives types can't be null. - value = DefaultValues.defaultValueFor(getCurrentAccessor().getType()); + value = Provider.of(getCurrentAccessor().getType()).getDefault(); } getCurrentAccessor().setValue(parent, value); } diff --git a/src/main/java/io/beanmapper/core/constructor/DefaultBeanInitializer.java b/src/main/java/io/beanmapper/core/constructor/DefaultBeanInitializer.java index b03f8ba..0259f69 100644 --- a/src/main/java/io/beanmapper/core/constructor/DefaultBeanInitializer.java +++ b/src/main/java/io/beanmapper/core/constructor/DefaultBeanInitializer.java @@ -12,7 +12,7 @@ import io.beanmapper.BeanMapper; import io.beanmapper.config.BeanMapperBuilder; import io.beanmapper.strategy.ConstructorArguments; -import io.beanmapper.utils.DefaultValues; +import io.beanmapper.utils.provider.Provider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,7 +36,7 @@ public T instantiate(Class beanClass, ConstructorArguments arguments) { } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { logger.error("Could not instantiate bean of class %s. Returning the default value associated with the given type. %s".formatted(beanClass.getName(), e.getMessage())); - return DefaultValues.defaultValueFor(beanClass); + return Provider.of(beanClass).getDefault(); } } diff --git a/src/main/java/io/beanmapper/utils/DefaultValues.java b/src/main/java/io/beanmapper/utils/DefaultValues.java deleted file mode 100644 index 2607a22..0000000 --- a/src/main/java/io/beanmapper/utils/DefaultValues.java +++ /dev/null @@ -1,38 +0,0 @@ -package io.beanmapper.utils; - -import static java.util.Map.entry; - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; - -public class DefaultValues { - - private static final Map, Object> defaultValues = Map.ofEntries( - entry(boolean.class, false), - entry(byte.class, (byte) 0), - entry(short.class, (short) 0), - entry(int.class, 0), - entry(long.class, 0L), - entry(char.class, '\0'), - entry(float.class, 0.0F), - entry(double.class, 0.0), - entry(Optional.class, Optional.empty()), - entry(List.class, Collections.emptyList()), - entry(Set.class, Collections.emptySet()), - entry(Map.class, Collections.emptyMap()) - ); - - /** - * Private constructor to hide implicit public constructor of utility-class. - */ - private DefaultValues() { - } - - @SuppressWarnings("unchecked") - public static V defaultValueFor(Class clazz) { - return (V) defaultValues.get(clazz); - } -} \ No newline at end of file diff --git a/src/main/java/io/beanmapper/utils/provider/BooleanProvider.java b/src/main/java/io/beanmapper/utils/provider/BooleanProvider.java new file mode 100644 index 0000000..667b38b --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/BooleanProvider.java @@ -0,0 +1,13 @@ +package io.beanmapper.utils.provider; + +public class BooleanProvider implements Provider{ + @Override + public Boolean getDefault() { + return false; + } + + @Override + public Boolean getMaximum() { + return true; + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/ByteProvider.java b/src/main/java/io/beanmapper/utils/provider/ByteProvider.java new file mode 100644 index 0000000..2e02022 --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/ByteProvider.java @@ -0,0 +1,13 @@ +package io.beanmapper.utils.provider; + +public class ByteProvider implements Provider { + @Override + public Byte getDefault() { + return (byte) 0; + } + + @Override + public Byte getMaximum() { + return Byte.MAX_VALUE; + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/CharacterProvider.java b/src/main/java/io/beanmapper/utils/provider/CharacterProvider.java new file mode 100644 index 0000000..2878b17 --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/CharacterProvider.java @@ -0,0 +1,13 @@ +package io.beanmapper.utils.provider; + +public class CharacterProvider implements Provider { + @Override + public Character getDefault() { + return '\0'; + } + + @Override + public Character getMaximum() { + return '~'; + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/DoubleProvider.java b/src/main/java/io/beanmapper/utils/provider/DoubleProvider.java new file mode 100644 index 0000000..7648ffe --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/DoubleProvider.java @@ -0,0 +1,13 @@ +package io.beanmapper.utils.provider; + +public class DoubleProvider implements Provider { + @Override + public Double getDefault() { + return 0d; + } + + @Override + public Double getMaximum() { + return Double.MAX_VALUE; + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/FloatProvider.java b/src/main/java/io/beanmapper/utils/provider/FloatProvider.java new file mode 100644 index 0000000..2a60eca --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/FloatProvider.java @@ -0,0 +1,13 @@ +package io.beanmapper.utils.provider; + +public class FloatProvider implements Provider { + @Override + public Float getDefault() { + return 0f; + } + + @Override + public Float getMaximum() { + return Float.MAX_VALUE; + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/IntegerProvider.java b/src/main/java/io/beanmapper/utils/provider/IntegerProvider.java new file mode 100644 index 0000000..9aef9fa --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/IntegerProvider.java @@ -0,0 +1,15 @@ +package io.beanmapper.utils.provider; + +public class IntegerProvider implements Provider { + + @Override + public Integer getDefault() { + return 0; + } + + @Override + public Integer getMaximum() { + return Integer.MAX_VALUE; + } + +} diff --git a/src/main/java/io/beanmapper/utils/provider/ListProvider.java b/src/main/java/io/beanmapper/utils/provider/ListProvider.java new file mode 100644 index 0000000..ad1d976 --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/ListProvider.java @@ -0,0 +1,16 @@ +package io.beanmapper.utils.provider; + +import java.util.Collections; +import java.util.List; + +public class ListProvider implements Provider{ + @Override + public List getDefault() { + return Collections.emptyList(); + } + + @Override + public List getMaximum() { + return Collections.emptyList(); + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/LongProvider.java b/src/main/java/io/beanmapper/utils/provider/LongProvider.java new file mode 100644 index 0000000..584f4d6 --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/LongProvider.java @@ -0,0 +1,13 @@ +package io.beanmapper.utils.provider; + +public class LongProvider implements Provider{ + @Override + public Long getDefault() { + return 0L; + } + + @Override + public Long getMaximum() { + return Long.MAX_VALUE; + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/MapProvider.java b/src/main/java/io/beanmapper/utils/provider/MapProvider.java new file mode 100644 index 0000000..29ad63a --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/MapProvider.java @@ -0,0 +1,16 @@ +package io.beanmapper.utils.provider; + +import java.util.Collections; +import java.util.Map; + +public class MapProvider implements Provider{ + @Override + public Map getDefault() { + return Collections.emptyMap(); + } + + @Override + public Map getMaximum() { + return Collections.emptyMap(); + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/OprtionalProvider.java b/src/main/java/io/beanmapper/utils/provider/OprtionalProvider.java new file mode 100644 index 0000000..f79ed5c --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/OprtionalProvider.java @@ -0,0 +1,15 @@ +package io.beanmapper.utils.provider; + +import java.util.Optional; + +public class OprtionalProvider implements Provider{ + @Override + public Optional getDefault() { + return Optional.empty(); + } + + @Override + public Optional getMaximum() { + return Optional.empty(); + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/Provider.java b/src/main/java/io/beanmapper/utils/provider/Provider.java new file mode 100644 index 0000000..b076a38 --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/Provider.java @@ -0,0 +1,35 @@ +package io.beanmapper.utils.provider; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +import static java.util.Map.entry; + +public interface Provider { + + + Map, Provider> PROVIDERS = Map.ofEntries( + entry(boolean.class, new BooleanProvider()), + entry(byte.class, new ByteProvider()), + entry(short.class, new ShortProvider()), + entry(int.class, new IntegerProvider()), + entry(long.class, new LongProvider()), + entry(char.class, new CharacterProvider()), + entry(float.class, new FloatProvider()), + entry(double.class, new DoubleProvider()), + entry(List.class, new ListProvider()), + entry(Set.class, new SetProvider()), + entry(Map.class, new MapProvider()), + entry(Optional.class, new OprtionalProvider()) + ); + + static Provider of(Class clazz) { + return (Provider) PROVIDERS.get(clazz); + } + + T getDefault(); + + T getMaximum(); +} diff --git a/src/main/java/io/beanmapper/utils/provider/SetProvider.java b/src/main/java/io/beanmapper/utils/provider/SetProvider.java new file mode 100644 index 0000000..65cf139 --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/SetProvider.java @@ -0,0 +1,17 @@ +package io.beanmapper.utils.provider; + +import java.util.Collections; +import java.util.Set; + +public class SetProvider implements Provider { + + @Override + public Set getDefault() { + return Collections.emptySet(); + } + + @Override + public Set getMaximum() { + return Collections.emptySet(); + } +} diff --git a/src/main/java/io/beanmapper/utils/provider/ShortProvider.java b/src/main/java/io/beanmapper/utils/provider/ShortProvider.java new file mode 100644 index 0000000..44de2dc --- /dev/null +++ b/src/main/java/io/beanmapper/utils/provider/ShortProvider.java @@ -0,0 +1,13 @@ +package io.beanmapper.utils.provider; + +public class ShortProvider implements Provider { + @Override + public Short getDefault() { + return (short) 0; + } + + @Override + public Short getMaximum() { + return Short.MAX_VALUE; + } +} diff --git a/src/test/java/io/beanmapper/strategy/MapToRecordStrategyTest.java b/src/test/java/io/beanmapper/strategy/MapToRecordStrategyTest.java index e3c9737..8a51464 100644 --- a/src/test/java/io/beanmapper/strategy/MapToRecordStrategyTest.java +++ b/src/test/java/io/beanmapper/strategy/MapToRecordStrategyTest.java @@ -7,6 +7,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import io.beanmapper.utils.provider.Provider; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.List; @@ -48,7 +49,6 @@ import io.beanmapper.strategy.record.model.collection.result.ResultRecordWithSet; import io.beanmapper.strategy.record.model.inheritance.Layer3; import io.beanmapper.testmodel.person.Person; -import io.beanmapper.utils.DefaultValues; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -85,7 +85,7 @@ void testMapObjectToRecordWithMoreFields() { var form = new FormWithName(); var result = this.beanMapper.map(form, ResultRecordWithIdAndName.class); - assertEquals((int) DefaultValues.defaultValueFor(int.class), result.id()); + assertEquals((int) Provider.of(int.class).getDefault(), result.id()); assertEquals(form.name, result.name()); }