diff --git a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java index b80159b7..67aefd5e 100644 --- a/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java +++ b/src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java @@ -1,66 +1,50 @@ package ch.jalu.configme.beanmapper.leafvaluehandler; -import java.math.BigDecimal; -import java.math.BigInteger; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.function.Function; /** * Number handler for types without arbitrary precision. See also {@link BigNumberLeafValueHandler}. */ public class NumberLeafValueHandler extends AbstractLeafValueHandler { - private static final Map, Class> PRIMITIVE_NUMBERS_MAP = buildPrimitiveNumberMap(); + private static final Map, Function> NUMBER_CLASSES_TO_CONVERSION = + createMapOfTypeToTransformFunction(); @Override public Object convert(Class clazz, Object value) { - if (!(value instanceof Number)) { - return null; - } - Number number = (Number) value; - clazz = asReferenceClass(clazz); - - if (clazz.isInstance(value)) { - return number; - } else if (Integer.class == clazz) { - return number.intValue(); - } else if (Double.class == clazz) { - return number.doubleValue(); - } else if (Float.class == clazz) { - return number.floatValue(); - } else if (Byte.class == clazz) { - return number.byteValue(); - } else if (Long.class == clazz) { - return number.longValue(); - } else if (Short.class == clazz) { - return number.shortValue(); + if (value instanceof Number) { + Function numberFunction = NUMBER_CLASSES_TO_CONVERSION.get(clazz); + return numberFunction == null ? null : numberFunction.apply((Number) value); } return null; } @Override public Object toExportValue(Object value) { - if (value instanceof Number) { - // TODO #182: Turn check around so no values are ever exported that are not supported by the handler. - return (value instanceof BigInteger || value instanceof BigDecimal) ? null : value; + Class clazz = value == null ? null : value.getClass(); + if (NUMBER_CLASSES_TO_CONVERSION.containsKey(clazz)) { + return value; } return null; } - protected Class asReferenceClass(Class clazz) { - Class referenceClass = PRIMITIVE_NUMBERS_MAP.get(clazz); - return referenceClass == null ? clazz : referenceClass; - } - - private static Map, Class> buildPrimitiveNumberMap() { - Map, Class> map = new HashMap<>(); - map.put(byte.class, Byte.class); - map.put(short.class, Short.class); - map.put(int.class, Integer.class); - map.put(long.class, Long.class); - map.put(float.class, Float.class); - map.put(double.class, Double.class); + private static Map, Function> createMapOfTypeToTransformFunction() { + Map, Function> map = new HashMap<>(); + map.put(byte.class, Number::byteValue); + map.put(Byte.class, Number::byteValue); + map.put(short.class, Number::shortValue); + map.put(Short.class, Number::shortValue); + map.put(int.class, Number::intValue); + map.put(Integer.class, Number::intValue); + map.put(long.class, Number::longValue); + map.put(Long.class, Number::longValue); + map.put(float.class, Number::floatValue); + map.put(Float.class, Number::floatValue); + map.put(double.class, Number::doubleValue); + map.put(Double.class, Number::doubleValue); return Collections.unmodifiableMap(map); } }