In my code we have to convert euros into euro cents: taken a BigDecimal
as input we have to multiply it by 100.
We need to apply this conversion a lot of times, so we decided to use an UnaryOperator
from java.util.function
:
private static final UnaryOperator<BigDecimal> CONVERT_EURO_TO_CENTS =
input -> input.multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.DOWN);
Then we used the CONVERT_EURO_TO_CENTS
as follows:
[.....]
CONVERT_EURO_TO_CENT.apply(<aBigDecimal>)
[.....]
Could be dangerous declaring the UnaryOperator
as a constant (static final
), avoiding data inconsistency in a multi-threaded environment (thread safety)?