/** * Returns the <tt>BigDecimal</tt> value for the wrapped value or <tt>defaultValue</tt> if the wrapped value * isn't a BigDecimal and cannot be converted to one. * <p> * If the wrapped value is a <tt>BigDecimal</tt>, <tt>Double</tt>, <tt>Long</tt> or <tt>Integer</tt>, * it is either directly returned or converted by calling <tt>java.math.BigDecimal#valueOf</tt>. * <p> * Otherwise {@link BigDecimal#BigDecimal(String, java.math.MathContext)} is called on the string representation * of the wrapped value (with "," replaced to ".") and <tt>MathContext.UNLIMITED</tt>. If parsing fails, or if * the wrapped value was <tt>null</tt>, the <tt>defaultValue</tt> will be returned. * * @param defaultValue the value to be used, if no conversion to <tt>BigDecimal</tt> is possible. * @return the wrapped value casted or converted to <tt>BigDecimal</tt> or <tt>defaultValue</tt> * if no conversion is possible. */ @Nonnull public BigDecimal getBigDecimal(@Nonnull BigDecimal defaultValue) { BigDecimal result = getBigDecimal(); if (result == null) { return defaultValue; } return result; }
/** * Returns the <tt>Amount</tt> for the wrapped value. * <p> * If the wrapped value can be converted to a BigDecimal ({@link #getBigDecimal(java.math.BigDecimal)}, * an <tt>Amount</tt> for the result is returned. Otherwise an empty <tt>Amount</tt> is returned. * * @return the wrapped value converted to <tt>Amount</tt>. The result might be an empty amount, if the wrapped * value is <tt>null</tt> or if no conversion was possible. * @see #getBigDecimal(java.math.BigDecimal) */ public Amount getAmount() { return Amount.of(getBigDecimal()); }
@SuppressWarnings("unchecked") private <T> T continueCoerceWithBasicTypes(Class<T> targetClazz, T defaultValue) { if (Integer.class.equals(targetClazz) || int.class.equals(targetClazz)) { if (data instanceof Double) { return (T) (Integer) ((Long) Math.round((Double) data)).intValue(); } return (T) getInteger(); } if (Long.class.equals(targetClazz) || long.class.equals(targetClazz)) { if (data instanceof Double) { return (T) (Long) Math.round((Double) data); } return (T) getLong(); } if (String.class.equals(targetClazz)) { return (T) NLS.toMachineString(data); } if (BigDecimal.class.equals(targetClazz)) { return (T) getBigDecimal(); } if (Amount.class.equals(targetClazz)) { return (T) getAmount(); } return continueCoerceWithDateTypes(targetClazz, defaultValue); }