-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#182 Restrict NumberLeafValueHandler to only export values of types i…
…t supports
- Loading branch information
Showing
1 changed file
with
23 additions
and
39 deletions.
There are no files selected for viewing
62 changes: 23 additions & 39 deletions
62
src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/NumberLeafValueHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |