@Override public double convert(double x) throws ConversionException { Double refAmount = TO_REFERENCE.get(_code); if (refAmount == null) throw new ConversionException("Exchange rate not set for " + _code); return _invert ? x / refAmount.doubleValue() : x * refAmount.doubleValue(); }
/** * Workaround for RFE #4093999 ("Relax constraint on placement of this()/super() * call in constructors"). */ private static UnitConverter getConverter(final Unit<?> source) throws ConversionException { if (source == null) { throw new ConversionException(Errors.format(ErrorKeys.NO_UNIT)); } return source.getConverterTo(MILLISECOND); }
/** * Returns the exchange rate for this {@link Currency}. * * @return the amount stated in the {@link #getReferenceCurrency} * equals to one unit of this {@link Currency}. * @throws ConversionException if the exchange rate has not be set for * this {@link Currency}. */ public double getExchangeRate() { Double refAmount = TO_REFERENCE.get(this.getCode()); if (refAmount == null) throw new ConversionException("Exchange rate not set for " + this.getCode()); return refAmount.doubleValue(); }
@Override public UnitConverter toStandardUnit() { if (hasOnlyStandardUnit()) return UnitConverter.IDENTITY; UnitConverter converter = UnitConverter.IDENTITY; for (int i = 0; i < _elements.length; i++) { UnitConverter cvtr = _elements[i]._unit.toStandardUnit(); if (!cvtr.isLinear()) throw new ConversionException(_elements[i]._unit + " is non-linear, cannot convert"); if (_elements[i]._root != 1) throw new ConversionException(_elements[i]._unit + " holds a base unit with fractional exponent"); int pow = _elements[i]._pow; if (pow < 0) { // Negative power. pow = -pow; cvtr = cvtr.inverse(); } for (int j = 0; j < pow; j++) { converter = converter.concatenate(cvtr); } } return converter; }
@Override public UnitConverter toStandardUnit() { if (hasOnlyStandardUnit()) return UnitConverter.IDENTITY; UnitConverter converter = UnitConverter.IDENTITY; for (int i = 0; i < _elements.length; i++) { UnitConverter cvtr = _elements[i]._unit.toStandardUnit(); if (!cvtr.isLinear()) throw new ConversionException(_elements[i]._unit + " is non-linear, cannot convert"); if (_elements[i]._root != 1) throw new ConversionException(_elements[i]._unit + " holds a base unit with fractional exponent"); int pow = _elements[i]._pow; if (pow < 0) { // Negative power. pow = -pow; cvtr = cvtr.inverse(); } for (int j = 0; j < pow; j++) { converter = converter.concatenate(cvtr); } } return converter; }
@Override public UnitConverter toStandardUnit() { if (hasOnlyStandardUnit()) return UnitConverter.IDENTITY; UnitConverter converter = UnitConverter.IDENTITY; for (int i = 0; i < _elements.length; i++) { UnitConverter cvtr = _elements[i]._unit.toStandardUnit(); if (!cvtr.isLinear()) throw new ConversionException(_elements[i]._unit + " is non-linear, cannot convert"); if (_elements[i]._root != 1) throw new ConversionException(_elements[i]._unit + " holds a base unit with fractional exponent"); int pow = _elements[i]._pow; if (pow < 0) { // Negative power. pow = -pow; cvtr = cvtr.inverse(); } for (int j = 0; j < pow; j++) { converter = converter.concatenate(cvtr); } } return converter; }
private static UnitConverter transformOf(Unit<?> baseUnits) { if (baseUnits instanceof BaseUnit) return Dimension.getModel().getTransform((BaseUnit<?>) baseUnits); // Product of units. ProductUnit<?> productUnit = (ProductUnit<?>) baseUnits; UnitConverter converter = UnitConverter.IDENTITY; for (int i = 0; i < productUnit.getUnitCount(); i++) { Unit<?> unit = productUnit.getUnit(i); UnitConverter cvtr = transformOf(unit); if (!cvtr.isLinear()) throw new ConversionException(baseUnits + " is non-linear, cannot convert"); if (productUnit.getUnitRoot(i) != 1) throw new ConversionException(productUnit + " holds a base unit with fractional exponent"); int pow = productUnit.getUnitPow(i); if (pow < 0) { // Negative power. pow = -pow; cvtr = cvtr.inverse(); } for (int j = 0; j < pow; j++) { converter = converter.concatenate(cvtr); } } return converter; }
private static UnitConverter transformOf(Unit<?> baseUnits) { if (baseUnits instanceof BaseUnit) return Dimension.getModel().getTransform((BaseUnit<?>) baseUnits); // Product of units. ProductUnit<?> productUnit = (ProductUnit<?>) baseUnits; UnitConverter converter = UnitConverter.IDENTITY; for (int i = 0; i < productUnit.getUnitCount(); i++) { Unit<?> unit = productUnit.getUnit(i); UnitConverter cvtr = transformOf(unit); if (!cvtr.isLinear()) throw new ConversionException(baseUnits + " is non-linear, cannot convert"); if (productUnit.getUnitRoot(i) != 1) throw new ConversionException(productUnit + " holds a base unit with fractional exponent"); int pow = productUnit.getUnitPow(i); if (pow < 0) { // Negative power. pow = -pow; cvtr = cvtr.inverse(); } for (int j = 0; j < pow; j++) { converter = converter.concatenate(cvtr); } } return converter; }
private static UnitConverter transformOf(Unit<?> baseUnits) { if (baseUnits instanceof BaseUnit) return Dimension.getModel().getTransform((BaseUnit<?>) baseUnits); // Product of units. ProductUnit<?> productUnit = (ProductUnit<?>) baseUnits; UnitConverter converter = UnitConverter.IDENTITY; for (int i = 0; i < productUnit.getUnitCount(); i++) { Unit<?> unit = productUnit.getUnit(i); UnitConverter cvtr = transformOf(unit); if (!cvtr.isLinear()) throw new ConversionException(baseUnits + " is non-linear, cannot convert"); if (productUnit.getUnitRoot(i) != 1) throw new ConversionException(productUnit + " holds a base unit with fractional exponent"); int pow = productUnit.getUnitPow(i); if (pow < 0) { // Negative power. pow = -pow; cvtr = cvtr.inverse(); } for (int j = 0; j < pow; j++) { converter = converter.concatenate(cvtr); } } return converter; }
/** * Returns a converter of numeric values from this unit to another unit. * * @param that the unit to which to convert the numeric values. * @return the converter from this unit to <code>that</code> unit. * @throws ConversionException if the conveter cannot be constructed * (e.g. <code>!this.isCompatible(that)</code>). */ public final UnitConverter getConverterTo(Unit<?> that) throws ConversionException { if (this.equals(that)) return UnitConverter.IDENTITY; Unit<?> thisSystemUnit = this.getStandardUnit(); Unit<?> thatSystemUnit = that.getStandardUnit(); if (thisSystemUnit.equals(thatSystemUnit)) return that.toStandardUnit().inverse().concatenate( this.toStandardUnit()); // Use dimensional transforms. if (!thisSystemUnit.getDimension() .equals(thatSystemUnit.getDimension())) throw new ConversionException(this + " is not compatible with " + that); // Transform between SystemUnit and BaseUnits is Identity. UnitConverter thisTransform = this.toStandardUnit().concatenate( transformOf(this.getBaseUnits())); UnitConverter thatTransform = that.toStandardUnit().concatenate( transformOf(that.getBaseUnits())); return thatTransform.inverse().concatenate(thisTransform); }
/** * Returns a converter of numeric values from this unit to another unit. * * @param that the unit to which to convert the numeric values. * @return the converter from this unit to <code>that</code> unit. * @throws ConversionException if the conveter cannot be constructed * (e.g. <code>!this.isCompatible(that)</code>). */ public final UnitConverter getConverterTo(Unit<?> that) throws ConversionException { if (this.equals(that)) return UnitConverter.IDENTITY; Unit<?> thisSystemUnit = this.getStandardUnit(); Unit<?> thatSystemUnit = that.getStandardUnit(); if (thisSystemUnit.equals(thatSystemUnit)) return that.toStandardUnit().inverse().concatenate( this.toStandardUnit()); // Use dimensional transforms. if (!thisSystemUnit.getDimension() .equals(thatSystemUnit.getDimension())) throw new ConversionException(this + " is not compatible with " + that); // Transform between SystemUnit and BaseUnits is Identity. UnitConverter thisTransform = this.toStandardUnit().concatenate( transformOf(this.getBaseUnits())); UnitConverter thatTransform = that.toStandardUnit().concatenate( transformOf(that.getBaseUnits())); return thatTransform.inverse().concatenate(thisTransform); }
/** * Returns a converter of numeric values from this unit to another unit. * * @param that the unit to which to convert the numeric values. * @return the converter from this unit to <code>that</code> unit. * @throws ConversionException if the conveter cannot be constructed * (e.g. <code>!this.isCompatible(that)</code>). */ public final UnitConverter getConverterTo(Unit<?> that) throws ConversionException { if (this.equals(that)) return UnitConverter.IDENTITY; Unit<?> thisSystemUnit = this.getStandardUnit(); Unit<?> thatSystemUnit = that.getStandardUnit(); if (thisSystemUnit.equals(thatSystemUnit)) return that.toStandardUnit().inverse().concatenate( this.toStandardUnit()); // Use dimensional transforms. if (!thisSystemUnit.getDimension() .equals(thatSystemUnit.getDimension())) throw new ConversionException(this + " is not compatible with " + that); // Transform between SystemUnit and BaseUnits is Identity. UnitConverter thisTransform = this.toStandardUnit().concatenate( transformOf(this.getBaseUnits())); UnitConverter thatTransform = that.toStandardUnit().concatenate( transformOf(that.getBaseUnits())); return thatTransform.inverse().concatenate(thisTransform); }