Java Code Examples for javax.measure.converter.UnitConverter#IDENTITY

The following examples show how to use javax.measure.converter.UnitConverter#IDENTITY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProductUnit.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: Unit.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: Unit.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: Unit.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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<>(parent, toParent);
    }
    if (operation == UnitConverter.IDENTITY) 
        return this;
    return new TransformedUnit<>(this, operation);
}
 
Example 5
Source File: Dimension.java    From microMathematics with GNU General Public License v3.0 4 votes vote down vote up
public UnitConverter getTransform(BaseUnit<?> unit) {
    if (unit.equals(SI.CANDELA)) return new RationalConverter(1, 683);
    return UnitConverter.IDENTITY;
}
 
Example 6
Source File: AlternateUnit.java    From microMathematics with GNU General Public License v3.0 4 votes vote down vote up
@Override
public final UnitConverter toStandardUnit() {
    return UnitConverter.IDENTITY;
}
 
Example 7
Source File: BaseUnit.java    From microMathematics with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UnitConverter toStandardUnit() {
    return UnitConverter.IDENTITY;
}
 
Example 8
Source File: TransformedUnit.java    From microMathematics with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Creates a transformed unit from the specified parent unit.
 *
 * @param parentUnit the untransformed unit from which this unit is 
 *        derived.
 * @param  toParentUnit the converter to the parent units.
 * @throws IllegalArgumentException if <code>toParentUnit == 
 *         {@link UnitConverter#IDENTITY UnitConverter.IDENTITY}</code>
 */
TransformedUnit(Unit<Q> parentUnit, UnitConverter toParentUnit) {
    if (toParentUnit == UnitConverter.IDENTITY)
        throw new IllegalArgumentException("Identity not allowed");
    _parentUnit = parentUnit;
    _toParentUnit = toParentUnit;
}