Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Replace DefaultValues-class with Provider #179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/io/beanmapper/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public interface Configuration {
* Allows the user to set a default value for a given type.
*
* <p>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.</p>
*
* @param target The target class.
Expand All @@ -439,7 +439,7 @@ public interface Configuration {
*
* <p>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}.</p>
* defaults, or simply refer to the defaults in {@link io.beanmapper.utils.provider.Provider}.</p>
*
* @param targetClass The target class
* @return The value associated with the given target class, based off of the values set in the custom default
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/io/beanmapper/config/CoreConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -430,9 +430,12 @@ public void setFlushAfterClear(Trinary flushAfterClear) {
*/
@Override
public <T, V> V getDefaultValueForClass(Class<T> 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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/beanmapper/core/BeanProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,7 +36,7 @@ public <T> T instantiate(Class<T> 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();
}
}

Expand Down
38 changes: 0 additions & 38 deletions src/main/java/io/beanmapper/utils/DefaultValues.java

This file was deleted.

13 changes: 13 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/BooleanProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.beanmapper.utils.provider;

public class BooleanProvider implements Provider<Boolean>{
@Override
public Boolean getDefault() {
return false;
}

@Override
public Boolean getMaximum() {
return true;
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/ByteProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.beanmapper.utils.provider;

public class ByteProvider implements Provider<Byte> {
@Override
public Byte getDefault() {
return (byte) 0;
}

@Override
public Byte getMaximum() {
return Byte.MAX_VALUE;
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/CharacterProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.beanmapper.utils.provider;

public class CharacterProvider implements Provider<Character> {
@Override
public Character getDefault() {
return '\0';
}

@Override
public Character getMaximum() {
return '~';
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/DoubleProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.beanmapper.utils.provider;

public class DoubleProvider implements Provider<Double> {
@Override
public Double getDefault() {
return 0d;
}

@Override
public Double getMaximum() {
return Double.MAX_VALUE;
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/FloatProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.beanmapper.utils.provider;

public class FloatProvider implements Provider<Float> {
@Override
public Float getDefault() {
return 0f;
}

@Override
public Float getMaximum() {
return Float.MAX_VALUE;
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/IntegerProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.beanmapper.utils.provider;

public class IntegerProvider implements Provider<Integer> {

@Override
public Integer getDefault() {
return 0;
}

@Override
public Integer getMaximum() {
return Integer.MAX_VALUE;
}

}
16 changes: 16 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/ListProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.beanmapper.utils.provider;

import java.util.Collections;
import java.util.List;

public class ListProvider implements Provider<List>{
@Override
public List getDefault() {
return Collections.emptyList();
}

@Override
public List getMaximum() {
return Collections.emptyList();
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/LongProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.beanmapper.utils.provider;

public class LongProvider implements Provider<Long>{
@Override
public Long getDefault() {
return 0L;
}

@Override
public Long getMaximum() {
return Long.MAX_VALUE;
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/MapProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.beanmapper.utils.provider;

import java.util.Collections;
import java.util.Map;

public class MapProvider implements Provider<Map>{
@Override
public Map getDefault() {
return Collections.emptyMap();
}

@Override
public Map getMaximum() {
return Collections.emptyMap();
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/OprtionalProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.beanmapper.utils.provider;

import java.util.Optional;

public class OprtionalProvider implements Provider<Optional>{
@Override
public Optional getDefault() {
return Optional.empty();
}

@Override
public Optional getMaximum() {
return Optional.empty();
}
}
35 changes: 35 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/Provider.java
Original file line number Diff line number Diff line change
@@ -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<T> {


Map<Class<?>, 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 <T> Provider<T> of(Class<T> clazz) {
return (Provider<T>) PROVIDERS.get(clazz);
}

T getDefault();

T getMaximum();
}
17 changes: 17 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/SetProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.beanmapper.utils.provider;

import java.util.Collections;
import java.util.Set;

public class SetProvider implements Provider<Set> {

@Override
public Set getDefault() {
return Collections.emptySet();
}

@Override
public Set getMaximum() {
return Collections.emptySet();
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/beanmapper/utils/provider/ShortProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.beanmapper.utils.provider;

public class ShortProvider implements Provider<Short> {
@Override
public Short getDefault() {
return (short) 0;
}

@Override
public Short getMaximum() {
return Short.MAX_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down