Package org.kiwiproject.base
Class KiwiBigDecimals
- java.lang.Object
-
- org.kiwiproject.base.KiwiBigDecimals
-
public class KiwiBigDecimals extends Object
Utilities for working withBigDecimal.Neither Guava (e.g.
DoubleMath) nor Apache Commons (e.g.NumberUtils) contained these exact conversions when this was originally implemented. The Apache CommonsNumberUtilsdoes contain conversion methods that accept a default value for null input, or if zero is OK as the default.
-
-
Constructor Summary
Constructors Constructor Description KiwiBigDecimals()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doublerequireDouble(BigDecimal value)Converts the givenBigDecimalto a primitive double.static DoubletoDoubleOrNull(BigDecimal value)Converts a nullableBigDecimalto aDoubleor returns null if the given value is null.static OptionalDoubletoOptionalDouble(BigDecimal value)Converts a nullableBigDecimalto anOptionalDouble.static Optional<Double>toOptionalDoubleObject(BigDecimal value)
-
-
-
Method Detail
-
toOptionalDouble
public static OptionalDouble toOptionalDouble(@Nullable BigDecimal value)
Converts a nullableBigDecimalto anOptionalDouble. Use this if you need a primitive double and want to avoid boxing.- Parameters:
value- the BigDecimal or null- Returns:
- an
OptionalDouble
-
toOptionalDoubleObject
public static Optional<Double> toOptionalDoubleObject(@Nullable BigDecimal value)
Converts a nullableBigDecimalto anOptionalcontaining aDouble. Use this if you need a Double or if you need to perform further map operations on the value.Optionalhas map, filter, etc. whileOptionalDoubledoes not.
-
toDoubleOrNull
public static Double toDoubleOrNull(@Nullable BigDecimal value)
Converts a nullableBigDecimalto aDoubleor returns null if the given value is null.- Parameters:
value- the BigDecimal or null- Returns:
- converted
Doubleor null
-
requireDouble
public static double requireDouble(@Nonnull BigDecimal value)Converts the givenBigDecimalto a primitive double.- Parameters:
value- the non-null BigDecimal- Returns:
- a primitive double
- Throws:
IllegalArgumentException- if the given value is null
-
-