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 CommonsNumberUtils
does 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 double
requireDouble(BigDecimal value)
Converts the givenBigDecimal
to a primitive double.static Double
toDoubleOrNull(BigDecimal value)
Converts a nullableBigDecimal
to aDouble
or returns null if the given value is null.static OptionalDouble
toOptionalDouble(BigDecimal value)
Converts a nullableBigDecimal
to anOptionalDouble
.static Optional<Double>
toOptionalDoubleObject(BigDecimal value)
-
-
-
Method Detail
-
toOptionalDouble
public static OptionalDouble toOptionalDouble(@Nullable BigDecimal value)
Converts a nullableBigDecimal
to 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 nullableBigDecimal
to anOptional
containing aDouble
. Use this if you need a Double or if you need to perform further map operations on the value.Optional
has map, filter, etc. whileOptionalDouble
does not.
-
toDoubleOrNull
public static Double toDoubleOrNull(@Nullable BigDecimal value)
Converts a nullableBigDecimal
to aDouble
or returns null if the given value is null.- Parameters:
value
- the BigDecimal or null- Returns:
- converted
Double
or null
-
requireDouble
public static double requireDouble(@Nonnull BigDecimal value)
Converts the givenBigDecimal
to a primitive double.- Parameters:
value
- the non-null BigDecimal- Returns:
- a primitive double
- Throws:
IllegalArgumentException
- if the given value is null
-
-