com.softicar.platform.common.core.number.BigDecimalMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.core.number;
import java.math.BigDecimal;
import java.util.function.Function;
/**
* A class to map between {@link BigDecimal} and a {@link Number} based type.
*
* @author Oliver Richers
*/
public class BigDecimalMapper {
public static final BigDecimalMapper BIG_DECIMAL = new BigDecimalMapper<>(Function.identity(), Function.identity());
public static final BigDecimalMapper DOUBLE = new BigDecimalMapper<>(BigDecimal::valueOf, BigDecimal::doubleValue);
public static final BigDecimalMapper FLOAT = new BigDecimalMapper<>(BigDecimal::valueOf, BigDecimal::floatValue);
private final Function toMapper;
private final Function fromMapper;
public BigDecimalMapper(Function toMapper, Function fromMapper) {
this.toMapper = toMapper;
this.fromMapper = fromMapper;
}
public BigDecimal toBigDecimal(T value) {
return toMapper.apply(value);
}
public T fromBigDecimal(BigDecimal value) {
return fromMapper.apply(value);
}
}