@Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof AddConverter) { double offset = _offset + ((AddConverter)converter)._offset; return valueOf(offset); } else { return super.concatenate(converter); } }
/** * 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 concatenate(UnitConverter converter) { if (converter instanceof AddConverter) { double offset = _offset + ((AddConverter)converter)._offset; return valueOf(offset); } else { return super.concatenate(converter); } }
@Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof AddConverter) { double offset = _offset + ((AddConverter)converter)._offset; return valueOf(offset); } else { return super.concatenate(converter); } }
/** * 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; }
public UnitConverter toStandardUnit() { return _parentUnit.toStandardUnit().concatenate(_toParentUnit); }
/** * 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; }
public UnitConverter toStandardUnit() { return _parentUnit.toStandardUnit().concatenate(_toParentUnit); }
public UnitConverter toStandardUnit() { return _parentUnit.toStandardUnit().concatenate(_toParentUnit); }
@Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof RationalConverter) { RationalConverter that = (RationalConverter) converter; long dividendLong = this._dividend * that._dividend; long divisorLong = this._divisor * that._divisor; double dividendDouble = ((double)this._dividend) * that._dividend; double divisorDouble = ((double)this._divisor) * that._divisor; if ((dividendLong != dividendDouble) || (divisorLong != divisorDouble)) { // Long overflows. return new MultiplyConverter(dividendDouble / divisorDouble); } long gcd = gcd(dividendLong, divisorLong); return RationalConverter.valueOf(dividendLong / gcd, divisorLong / gcd); } else if (converter instanceof MultiplyConverter) { return converter.concatenate(this); } else { return super.concatenate(converter); } }
@Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof RationalConverter) { RationalConverter that = (RationalConverter) converter; long dividendLong = this._dividend * that._dividend; long divisorLong = this._divisor * that._divisor; double dividendDouble = ((double)this._dividend) * that._dividend; double divisorDouble = ((double)this._divisor) * that._divisor; if ((dividendLong != dividendDouble) || (divisorLong != divisorDouble)) { // Long overflows. return new MultiplyConverter(dividendDouble / divisorDouble); } long gcd = gcd(dividendLong, divisorLong); return RationalConverter.valueOf(dividendLong / gcd, divisorLong / gcd); } else if (converter instanceof MultiplyConverter) { return converter.concatenate(this); } else { return super.concatenate(converter); } }
@Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof RationalConverter) { RationalConverter that = (RationalConverter) converter; long dividendLong = this._dividend * that._dividend; long divisorLong = this._divisor * that._divisor; double dividendDouble = ((double)this._dividend) * that._dividend; double divisorDouble = ((double)this._divisor) * that._divisor; if ((dividendLong != dividendDouble) || (divisorLong != divisorDouble)) { // Long overflows. return new MultiplyConverter(dividendDouble / divisorDouble); } long gcd = gcd(dividendLong, divisorLong); return RationalConverter.valueOf(dividendLong / gcd, divisorLong / gcd); } else if (converter instanceof MultiplyConverter) { return converter.concatenate(this); } else { return super.concatenate(converter); } }
@Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof MultiplyConverter) { double factor = _factor * ((MultiplyConverter) converter)._factor; return valueOf(factor); } else if (converter instanceof RationalConverter) { double factor = _factor * ((RationalConverter) converter).getDividend() / ((RationalConverter) converter).getDivisor(); return valueOf(factor); } else { return super.concatenate(converter); } }
@Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof MultiplyConverter) { double factor = _factor * ((MultiplyConverter) converter)._factor; return valueOf(factor); } else if (converter instanceof RationalConverter) { double factor = _factor * ((RationalConverter) converter).getDividend() / ((RationalConverter) converter).getDivisor(); return valueOf(factor); } else { return super.concatenate(converter); } }
@Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof MultiplyConverter) { double factor = _factor * ((MultiplyConverter) converter)._factor; return valueOf(factor); } else if (converter instanceof RationalConverter) { double factor = _factor * ((RationalConverter) converter).getDividend() / ((RationalConverter) converter).getDivisor(); return valueOf(factor); } else { return super.concatenate(converter); } }
/** * 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. */ public final Unit<Q> transform(UnitConverter operation) { if (this instanceof TransformedUnit) { TransformedUnit<Q> tf = (TransformedUnit<Q>) this; Unit<Q> parent = tf.getParentUnit(); UnitConverter toParent = tf.toParentUnit().concatenate(operation); if (toParent == UnitConverter.IDENTITY) return parent; return new TransformedUnit<Q>(parent, toParent); } if (operation == UnitConverter.IDENTITY) return this; return new TransformedUnit<Q>(this, operation); }
/** * 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. */ public final Unit<Q> transform(UnitConverter operation) { if (this instanceof TransformedUnit) { TransformedUnit<Q> tf = (TransformedUnit<Q>) this; Unit<Q> parent = tf.getParentUnit(); UnitConverter toParent = tf.toParentUnit().concatenate(operation); if (toParent == UnitConverter.IDENTITY) return parent; return new TransformedUnit<Q>(parent, toParent); } if (operation == UnitConverter.IDENTITY) return this; return new TransformedUnit<Q>(this, operation); }
@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; }