Skip to content

Commit

Permalink
#182 Restrict NumberLeafValueHandler to only export values of types i…
Browse files Browse the repository at this point in the history
…t supports
  • Loading branch information
ljacqu committed Apr 19, 2021
1 parent 4725ac0 commit af7eb4d
Showing 1 changed file with 23 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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<?>, Class<?>> PRIMITIVE_NUMBERS_MAP = buildPrimitiveNumberMap();
private static final Map<Class<?>, Function<Number, Number>> 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<Number, Number> 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<?>, Class<?>> buildPrimitiveNumberMap() {
Map<Class<?>, 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<Class<?>, Function<Number, Number>> createMapOfTypeToTransformFunction() {
Map<Class<?>, Function<Number, Number>> 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);
}
}

0 comments on commit af7eb4d

Please sign in to comment.