Beispiel #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());
   }
 }
Beispiel #2
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());
   }
 }
Beispiel #3
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());
   }
 }
Beispiel #4
0
 /**
  * Returns the unit derived from this unit using the specified converter. The converter does not
  * need to be linear. For example:[code] Unit<Dimensionless> DECIBEL = Unit.ONE.transform( new
  * LogConverter(10).inverse().concatenate( new RationalConverter(1, 10))); [/code]
  *
  * @param operation the converter from the transformed unit to this unit.
  * @return the unit after the specified transformation.
  */
 @SuppressWarnings("unchecked")
 public final Unit<IMoney> transform(UnitConverter operation) {
   if (this instanceof Unit<?>) {
     Unit<IMoney> tf = this;
     Unit<?> parent = (Unit<?>) ((TransformedUnit<?>) tf).getParentUnit();
     UnitConverter toParent = ((TransformedUnit<?>) tf).toParentUnit();
     if (toParent == null) return (Unit<IMoney>) parent;
     UnitConverter toParentConcat = toParent.concatenate(operation);
     if (toParentConcat == AbstractConverter.IDENTITY) return (Unit<IMoney>) parent;
     return new TransformedUnit<IMoney>((Unit<IMoney>) parent, (AbstractConverter) toParentConcat);
   }
   if (operation == AbstractConverter.IDENTITY) return this;
   return new TransformedUnit<IMoney>(this, (AbstractConverter) operation);
 }