Java Code Examples for javax.measure.Unit#transform()

The following examples show how to use javax.measure.Unit#transform() . 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: UCUMFormatParser.java    From uom-systems with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
final public Unit SimpleUnit() throws TokenException {
    Token token = null;
    token = jj_consume_token(ATOM);
    Unit unit = symbols.getUnit(token.image);
    if (unit == null) {
        Prefix prefix = symbols.getPrefix(token.image);
        if (prefix != null) {
            String prefixSymbol = symbols.getSymbol(prefix);
            unit = symbols.getUnit(token.image.substring(prefixSymbol.length()));
            if (unit != null) {
                {
                    return unit.transform(MultiplyConverter.ofPrefix(prefix));
                }
            }
        }
        {
            throw new TokenException();
        }
    } else {
        {
            return unit;
        }
    }
}
 
Example 2
Source File: SystemUnit.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Implementation of {@link #multiply(Unit)} and {@link #divide(Unit)} methods.
 *
 * @param  inverse  wether to use the inverse of {@code other}.
 */
private <T extends Quantity<T>> Unit<?> product(final Unit<T> other, final boolean inverse) {
    final Unit<T> intermediate = other.getSystemUnit();
    final Dimension dim = intermediate.getDimension();
    final UnitDimension newDimension;
    final char operation;
    if (inverse) {
        operation = DIVIDE;
        newDimension = dimension.divide(dim);
    } else {
        operation = MULTIPLY;
        newDimension = dimension.multiply(dim);
    }
    final boolean transformed = (intermediate != other);
    Unit<?> result = create(newDimension, operation, transformed ? null : other);
    if (transformed) {
        UnitConverter c = other.getConverterTo(intermediate);
        if (!c.isLinear()) {
            throw new IllegalArgumentException(Errors.format(Errors.Keys.NonRatioUnit_1, other));
        }
        if (!c.isIdentity()) {
            if (inverse) c = c.inverse();
            result = result.transform(c);
            /*
             * If the system unit product is an Apache SIS implementation, try to infer a unit symbol
             * to be given to our customized 'transform' method. Otherwise fallback on standard API.
             */
            result = inferSymbol(result, operation, other);
        }
    }
    return result;
}
 
Example 3
Source File: ConventionalUnit.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Casts this unit to a parameterized unit of specified nature or throw a {@code ClassCastException}
 * if the dimension of the specified quantity and this unit's dimension do not match.
 *
 * @param  <T>   the type of the quantity measured by the unit.
 * @param  type  the quantity class identifying the nature of the unit.
 * @return this unit parameterized with the specified type.
 * @throws ClassCastException if the dimension of this unit is different from the specified quantity dimension.
 */
@Override
@SuppressWarnings("unchecked")
public <T extends Quantity<T>> Unit<T> asType(final Class<T> type) throws ClassCastException {
    final Unit<T> alternate = target.asType(type);
    if (target.equals(alternate)) {
        return (Unit<T>) this;
    }
    return alternate.transform(toTarget);
}
 
Example 4
Source File: ConventionalUnit.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Applies the {@link #toTarget} conversion factor on the result of raising the system unit to the given power.
 * This method shall be invoked only if {@link #ensureRatioScale()} succeed (this is not verified).
 * This method tries to build a unit symbol made from the current unit raised to the given power.
 * This is not needed for SI units since {@link #create(AbstractUnit, UnitConverter)} can infer
 * the symbol automatically (including its prefix), but this is useful for non SI units like "miĀ²"
 *
 * @param  result  the result of {@link SystemUnit#pow(int)} or {@link SystemUnit#root(int)}.
 * @param  n       the power by which the {@link #target} has been raised for producing {@code result}.
 * @param  root    {@code true} if the power is 1/n instead of n.
 */
private Unit<?> applyConversion(final Unit<?> result, final int n, final boolean root) {
    if (result == target) return this;
    final LinearConverter operation = LinearConverter.pow(toTarget, n, root);
    if (result instanceof SystemUnit<?>) {
        final String symbol = pow(getSymbol(), n, root);
        if (symbol != null) {
            return new ConventionalUnit<>((SystemUnit<?>) result, operation, symbol, (byte) 0, (short) 0).unique(symbol);
        }
    }
    return result.transform(operation);
}
 
Example 5
Source File: UnitFormatParser.java    From uom-systems with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
final public Unit AtomicExpr() throws TokenException {
    Unit result = ONE;
    Number n = null;
    Token token = null;
    switch ((nextTokenIndex == -1) ? jj_ntk() : nextTokenIndex) {
        case INTEGER:
        case FLOATING_POINT:
            n = NumberExpr();
            if (n instanceof Integer) {
                {
                    return result.multiply(n.intValue());
                }
            } else {
                {
                    return result.multiply(n.doubleValue());
                }
            }
        case UNIT_IDENTIFIER:
            token = consumeToken(UNIT_IDENTIFIER);
            Unit unit = symbols.getUnit(token.image);
            if (unit == null) {
                Prefix prefix = symbols.getPrefix(token.image);
                if (prefix != null) {
                    String prefixSymbol = symbols.getSymbol(prefix);
                    unit = symbols.getUnit(token.image.substring(prefixSymbol.length()));
                    if (unit != null) {
                        {
                            return unit.transform(MultiplyConverter.ofPrefix(prefix));
                        }
                    }
                }
                {
                    throw new TokenException();
                }
            } else {
                {
                    return unit;
                }
            }
        case OPEN_PAREN:
            consumeToken(OPEN_PAREN);
            result = AddExpr();
            consumeToken(CLOSE_PAREN); {
            return result;
        }
        default:
            laA[10] = genInt;
            consumeToken(-1);
            throw new TokenException();
    }
}
 
Example 6
Source File: Units.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Multiplies the given unit by the given ratio. For example multiplying {@link #CENTIMETRE} by 254/100 gives
 * {@link #INCH}. Invoking this method is equivalent to invoking <code>{@linkplain Unit#multiply(double)
 * Unit.multiply}(numerator / denominator)</code> except that the use of a ration of integer values help
 * Apache SIS to improve accuracy when more than one arithmetic operation are chained.
 *
 * @param  <Q>          the quantity measured by the unit.
 * @param  unit         the unit to multiply.
 * @param  numerator    the numerator of the multiplication factor.
 * @param  denominator  the denominator of the multiplication factor.
 * @return the unit multiplied by the given factor.
 *
 * @since 0.8
 */
public static <Q extends Quantity<Q>> Unit<Q> multiply(Unit<Q> unit, double numerator, double denominator) {
    return unit.transform(LinearConverter.scale(numerator, denominator));
}