From 34c658c777b74092b6dde3c23e62162825c354c2 Mon Sep 17 00:00:00 2001 From: Federico Herrera Date: Mon, 20 Nov 2023 16:26:03 -0300 Subject: [PATCH] Add Provider interface and implementation, cover with unit test --- .../providers/DefaultValuesProvider.java | 33 ++++++++++++ .../io/beanmapper/providers/Provider.java | 12 +++++ .../providers/DefaultValuesProviderTest.java | 53 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/main/java/io/beanmapper/providers/DefaultValuesProvider.java create mode 100644 src/main/java/io/beanmapper/providers/Provider.java create mode 100644 src/test/java/io/beanmapper/providers/DefaultValuesProviderTest.java diff --git a/src/main/java/io/beanmapper/providers/DefaultValuesProvider.java b/src/main/java/io/beanmapper/providers/DefaultValuesProvider.java new file mode 100644 index 0000000..6e4770d --- /dev/null +++ b/src/main/java/io/beanmapper/providers/DefaultValuesProvider.java @@ -0,0 +1,33 @@ +package io.beanmapper.providers; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +import static java.util.Map.entry; +import static java.util.Objects.isNull; + +public class DefaultValuesProvider implements Provider { + private static final Map, Object> DEFAULT_VALUES = 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()) + ); + + @Override + public T get(Class source) { + if (isNull(source)) return null; + return (T) DEFAULT_VALUES.get(source); + } +} \ No newline at end of file diff --git a/src/main/java/io/beanmapper/providers/Provider.java b/src/main/java/io/beanmapper/providers/Provider.java new file mode 100644 index 0000000..fd0e842 --- /dev/null +++ b/src/main/java/io/beanmapper/providers/Provider.java @@ -0,0 +1,12 @@ +package io.beanmapper.providers; + +public interface Provider { + + /** + * Get the default value given some S type. + * + * @param source {@link Class} to get the default value of. + * @return the default value given the type. + */ + T get(Class source); +} \ No newline at end of file diff --git a/src/test/java/io/beanmapper/providers/DefaultValuesProviderTest.java b/src/test/java/io/beanmapper/providers/DefaultValuesProviderTest.java new file mode 100644 index 0000000..d132d21 --- /dev/null +++ b/src/test/java/io/beanmapper/providers/DefaultValuesProviderTest.java @@ -0,0 +1,53 @@ +package io.beanmapper.providers; + +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.emptySet; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class DefaultValuesProviderTest { + private final DefaultValuesProvider defaultValuesProvider = new DefaultValuesProvider(); + + @Test + void shouldGetNull() { + Object actualValue = defaultValuesProvider.get(null); + UndefinedClassInProvider actualValue2 = defaultValuesProvider.get(UndefinedClassInProvider.class); + + assertNull(actualValue); + assertNull(actualValue2); + } + + @Test + void shouldGetDefault() { + assertEquals(false, defaultValuesProvider.get(boolean.class)); + byte actualDefaultByte = defaultValuesProvider.get(byte.class); + assertEquals((byte) 0, actualDefaultByte); + short actualDefaultShort = defaultValuesProvider.get(short.class); + assertEquals((short) 0, actualDefaultShort); + int actualDefaultInt = defaultValuesProvider.get(int.class); + assertEquals(0, actualDefaultInt); + long actualDefaultLong = defaultValuesProvider.get(long.class); + assertEquals(0L, actualDefaultLong); + char actualDefaultChar = defaultValuesProvider.get(char.class); + assertEquals('\0', actualDefaultChar); + float actualDefaultFloat = defaultValuesProvider.get(float.class); + assertEquals(0.0F, actualDefaultFloat); + double actualDefaultDouble = defaultValuesProvider.get(double.class); + assertEquals(0.0, actualDefaultDouble); + assertEquals(Optional.empty(), defaultValuesProvider.get(Optional.class)); + assertEquals(emptyList(), defaultValuesProvider.get(List.class)); + assertEquals(emptySet(), defaultValuesProvider.get(Set.class)); + assertEquals(emptyMap(), defaultValuesProvider.get(Map.class)); + } + + private static final class UndefinedClassInProvider { + } +} \ No newline at end of file