Java Code Examples for javax.measure.UnitConverter#inverse()

The following examples show how to use javax.measure.UnitConverter#inverse() . 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: Verifier.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the information about an "value out of range" error. The range in the error message will be formatted
 * in the unit given by the user, which is not necessarily the same than the unit of the parameter descriptor.
 *
 * @param converter  the conversion from user unit to descriptor unit, or {@code null} if none.
 *        This method uses the inverse of that conversion for converting the given minimum and maximum values.
 */
private void convertRange(UnitConverter converter) {
    if (converter != null && !internal && errorKey == Errors.Keys.ValueOutOfRange_4) {
        converter = converter.inverse();
        Object minimumValue = arguments[1];
        Object maximumValue = arguments[2];
        minimumValue = (minimumValue != null) ? converter.convert(((Number) minimumValue).doubleValue()) : "−∞";
        maximumValue = (maximumValue != null) ? converter.convert(((Number) maximumValue).doubleValue()) :  "∞";
        arguments[1] = minimumValue;
        arguments[2] = maximumValue;
    }
}
 
Example 2
Source File: DerivedScalar.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new scalar for the given value.
 *
 * @param toSystem  converter from {@code unit} to the system unit.
 */
DerivedScalar(final double value, final Unit<Q> unit, final Unit<Q> systemUnit, final UnitConverter toSystem) {
    super(toSystem.convert(value), systemUnit);
    derivedValue = value;
    derivedUnit  = unit;
    fromSystem   = toSystem.inverse();
}
 
Example 3
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 4
Source File: SexagesimalConverterTest.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given value to an other unit, compares with the expected value, and verify
 * the inverse conversion. Then tries again with the negative of the given values.
 */
private static <Q extends Quantity<Q>> void checkConversion(
        final double expected, final Unit<Q> unitExpected,
        final double actual,   final Unit<Q> unitActual)
{
    final UnitConverter converter = unitActual.getConverterTo(unitExpected);
    final UnitConverter inverse   = converter.inverse();
    assertEquals( expected, converter.convert( actual), TOLERANCE);
    assertEquals( actual,   inverse.convert( expected), TOLERANCE);
    assertEquals(-expected, converter.convert(-actual), TOLERANCE);
    assertEquals(-actual,   inverse.convert(-expected), TOLERANCE);
}