Exemplo n.º 1
0
 // Try to convert the specified value.
 private static Number convert(Number value, UnitConverter cvtr, MathContext ctx) {
   if (cvtr instanceof RationalConverter) { // Try converting through Field
     // methods.
     RationalConverter rCvtr = (RationalConverter) cvtr;
     BigInteger dividend = rCvtr.getDividend();
     BigInteger divisor = rCvtr.getDivisor();
     if (dividend.abs().compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0)
       throw new ArithmeticException("Multiplier overflow");
     if (divisor.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0)
       throw new ArithmeticException("Divisor overflow");
     return (value.longValue() * dividend.longValue()) / (divisor.longValue());
   } else if (cvtr instanceof AbstractConverter.Compound
       && cvtr.isLinear()) { // Do it in two parts.
     AbstractConverter.Compound compound = (AbstractConverter.Compound) cvtr;
     Number firstConversion = convert(value, compound.getRight(), ctx);
     Number secondConversion = convert(firstConversion, compound.getLeft(), ctx);
     return secondConversion;
   } else { // Try using BigDecimal as intermediate.
     BigDecimal decimalValue = BigDecimal.valueOf(value.doubleValue());
     Number newValue = cvtr.convert(decimalValue.toBigDecimal(), ctx);
     return newValue;
     // if (((FieldNumber)value) instanceof Decimal)
     // return (N)((FieldNumber)Decimal.valueOf(newValue));
     // if (((FieldNumber)value) instanceof Float64)
     // return (N)((FieldNumber)Float64.valueOf(newValue.doubleValue()));
     // throw new ArithmeticException(
     // "Generic amount conversion not implemented for amount of type " +
     // value.getClass());
   }
 }
Exemplo n.º 2
0
 public long longValue(Unit<IMoney> unit) throws ArithmeticException {
   Unit<IMoney> myUnit = unit();
   try {
     UnitConverter converter = unit.getConverterToAny(myUnit);
     return (converter.convert(BigDecimal.valueOf(super.getNumber().longValue())).longValue());
   } catch (UnconvertibleException e) {
     throw e;
   } catch (IncommensurableException e) {
     throw new IllegalArgumentException(e.getMessage());
   }
 }
Exemplo n.º 3
0
 public double doubleValue(Unit<IMoney> unit) {
   Unit<IMoney> myUnit = unit();
   try {
     UnitConverter converter = unit.getConverterToAny(myUnit);
     return converter.convert(getNumber().doubleValue());
   } catch (UnconvertibleException e) {
     throw e;
   } catch (IncommensurableException e) {
     throw new IllegalArgumentException(e.getMessage());
   }
 }