javax.measure.UnconvertibleException Java Examples

The following examples show how to use javax.measure.UnconvertibleException. 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: QuantityType.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Convert this QuantityType to a new {@link QuantityType} using the given target unit.
 *
 * @param targetUnit the unit to which this {@link QuantityType} will be converted to.
 * @return the new {@link QuantityType} in the given {@link Unit} or {@code null} in case of a
 */
@SuppressWarnings("unchecked")
public @Nullable QuantityType<T> toUnit(Unit<?> targetUnit) {
    if (!targetUnit.equals(getUnit())) {
        try {
            UnitConverter uc = getUnit().getConverterToAny(targetUnit);
            Quantity<?> result = Quantities.getQuantity(uc.convert(quantity.getValue()), targetUnit);

            return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit);
        } catch (UnconvertibleException | IncommensurableException e) {
            logger.debug("Unable to convert unit from {} to {}", getUnit(), targetUnit);
            return null;
        }
    }
    return this;
}
 
Example #2
Source File: QuantityType.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Convert this QuantityType to a new {@link QuantityType} using the given target unit.
 *
 * @param targetUnit the unit to which this {@link QuantityType} will be converted to.
 * @return the new {@link QuantityType} in the given {@link Unit} or {@code null} in case of a
 */
@SuppressWarnings("unchecked")
public @Nullable QuantityType<T> toUnit(Unit<?> targetUnit) {
    if (!targetUnit.equals(getUnit())) {
        try {
            UnitConverter uc = getUnit().getConverterToAny(targetUnit);
            Quantity<?> result = Quantities.getQuantity(uc.convert(quantity.getValue()), targetUnit);

            return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit);
        } catch (UnconvertibleException | IncommensurableException e) {
            logger.debug("Unable to convert unit from {} to {}", getUnit(), targetUnit);
            return null;
        }
    }
    return this;
}
 
Example #3
Source File: SystemUnit.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a converter of numeric values from this unit to another unit of same type.
 *
 * @param  unit  the unit of same type to which to convert the numeric values.
 * @return the converter from this unit to {@code that} unit.
 * @throws UnconvertibleException if the converter can not be constructed.
 */
@Override
public UnitConverter getConverterTo(final Unit<Q> unit) throws UnconvertibleException {
    ArgumentChecks.ensureNonNull("unit", unit);
    final Unit<Q> step = unit.getSystemUnit();
    if (step != this && !equalsIgnoreMetadata(step)) {
        // Should never occur unless parameterized type has been compromised.
        throw new UnconvertibleException(incompatible(unit));
    }
    if (step == unit) {
        return IdentityConverter.INSTANCE;
    }
    /*
     * At this point we know that the given units is not a system unit. Ask the conversion
     * FROM the given units (before to inverse it) instead than TO the given units because
     * in Apache SIS implementation, the former returns directly ConventionalUnit.toTarget
     * while the later implies a recursive call to this method.
     */
    return unit.getConverterTo(step).inverse();
}
 
Example #4
Source File: ConventionalUnit.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a converter of numeric values from this unit to another unit of same type.
 *
 * @param  that  the unit of same type to which to convert the numeric values.
 * @return the converter from this unit to {@code that} unit.
 * @throws UnconvertibleException if the converter can not be constructed.
 */
@Override
public UnitConverter getConverterTo(final Unit<Q> that) throws UnconvertibleException {
    if (that == this) {
        return IdentityConverter.INSTANCE;
    }
    ArgumentChecks.ensureNonNull("that", that);
    UnitConverter c = toTarget;
    if (target != that) {                           // Optimization for a common case.
        final Unit<Q> step = that.getSystemUnit();
        if (target != step && !target.isCompatible(step)) {
            // Should never occur unless parameterized type has been compromised.
            throw new UnconvertibleException(incompatible(that));
        }
        c = target.getConverterTo(step).concatenate(c);         // Usually leave 'c' unchanged.
        c =   step.getConverterTo(that).concatenate(c);
    }
    return c;
}
 
Example #5
Source File: MetadataFactoryTest.java    From january with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public UnitConverter getConverterTo(Unit<Time> that) throws UnconvertibleException {
	return null;
}
 
Example #6
Source File: MetadataFactoryTest.java    From january with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public UnitConverter getConverterToAny(Unit<?> that) throws IncommensurableException, UnconvertibleException {
	return null;
}
 
Example #7
Source File: SystemOffsetProfile.java    From openhab-core with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private Type applyOffset(Type state, boolean towardsItem) {
    if (state instanceof UnDefType) {
        // we cannot adjust undef or null values, thus we simply return them without reporting an error or warning
        return state;
    }

    QuantityType finalOffset = offset;
    if (finalOffset == null) {
        logger.warn(
                "Offset not configured correctly, please make sure it is of type QuantityType, e.g. \"3\", \"-1.4\", \"3.2°C\". Using offset 0 now.");
        finalOffset = new QuantityType<>("0");
    }

    if (!towardsItem) {
        finalOffset = finalOffset.negate();
    }

    Type result = UnDefType.UNDEF;

    if (state instanceof QuantityType) {
        QuantityType qtState = (QuantityType) state;
        try {
            if (finalOffset.getUnit() == SmartHomeUnits.ONE) {
                // allow offsets without unit -> implicitly assume its the same as the one from the state, but warn
                // the user
                finalOffset = new QuantityType<>(finalOffset.toBigDecimal(), qtState.getUnit());
                logger.warn(
                        "Received a QuantityType state '{}' with unit, but the offset is defined as a plain number without unit ({}), please consider adding a unit to the profile offset.",
                        state, offset);
            }
            // take care of temperatures because they start at offset -273°C = 0K
            if (SmartHomeUnits.KELVIN.equals(qtState.getUnit().getSystemUnit())) {
                QuantityType<Temperature> tmp = handleTemperature(qtState, finalOffset);
                if (tmp != null) {
                    result = tmp;
                }
            } else {
                result = qtState.add(finalOffset);
            }

        } catch (UnconvertibleException e) {
            logger.warn("Cannot apply offset '{}' to state '{}' because types do not match.", finalOffset, qtState);
        }
    } else if (state instanceof DecimalType && SmartHomeUnits.ONE.equals(finalOffset.getUnit())) {
        DecimalType decState = (DecimalType) state;
        result = new DecimalType(decState.doubleValue() + finalOffset.doubleValue());
    } else {
        logger.warn(
                "Offset '{}' cannot be applied to the incompatible state '{}' sent from the binding. Returning original state.",
                offset, state);
        result = state;
    }

    return result;
}