/** * Indicates whether this converter is considered the same as the * converter specified. To be considered equal this converter * concatenated with the one specified must returns the {@link #IDENTITY}. * * @param cvtr the converter with which to compare. * @return <code>true</code> if the specified object is a converter * considered equals to this converter;<code>false</code> otherwise. */ public boolean equals(Object cvtr) { if (!(cvtr instanceof UnitConverter)) return false; return this.concatenate(((UnitConverter)cvtr).inverse()) == IDENTITY; }
/** * Indicates whether this converter is considered the same as the * converter specified. To be considered equal this converter * concatenated with the one specified must returns the {@link #IDENTITY}. * * @param cvtr the converter with which to compare. * @return <code>true</code> if the specified object is a converter * considered equals to this converter;<code>false</code> otherwise. */ public boolean equals(Object cvtr) { if (!(cvtr instanceof UnitConverter)) return false; return this.concatenate(((UnitConverter)cvtr).inverse()) == IDENTITY; }
/** * Indicates whether this converter is considered the same as the * converter specified. To be considered equal this converter * concatenated with the one specified must returns the {@link #IDENTITY}. * * @param cvtr the converter with which to compare. * @return <code>true</code> if the specified object is a converter * considered equals to this converter;<code>false</code> otherwise. */ public boolean equals(Object cvtr) { if (!(cvtr instanceof UnitConverter)) return false; return this.concatenate(((UnitConverter)cvtr).inverse()) == IDENTITY; }
@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; }
/** * 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); }
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; }
return (getConverter(fromParent, to)).concatenate((transform.inverse()).concatenate(multiplier));