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

Add Provider interface and implementation, cover with unit test #192

Open
wants to merge 1 commit 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
33 changes: 33 additions & 0 deletions src/main/java/io/beanmapper/providers/DefaultValuesProvider.java
Original file line number Diff line number Diff line change
@@ -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<Class<?>, 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 extends S, S> T get(Class<S> source) {
if (isNull(source)) return null;
return (T) DEFAULT_VALUES.get(source);
}
}
12 changes: 12 additions & 0 deletions src/main/java/io/beanmapper/providers/Provider.java
Original file line number Diff line number Diff line change
@@ -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 extends S, S> T get(Class<S> source);
}
Original file line number Diff line number Diff line change
@@ -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 {
}
}