@SuppressWarnings("unchecked") public @Nullable QuantityType<T> toUnit(String targetUnit) { Unit<T> unit = (Unit<T>) AbstractUnit.parse(targetUnit); if (unit != null) { return toUnit(unit); } return null; }
private @NonNull State convertOrUndef(QuantityType<?> quantityState, Unit<?> targetUnit) { QuantityType<?> converted = quantityState.toUnit(targetUnit); if (converted != null) { return converted; } return UnDefType.UNDEF; }
@Override public int compareTo(QuantityType<T> o) { if (quantity.getUnit().isCompatible(o.quantity.getUnit())) { QuantityType<T> v1 = this.toUnit(getUnit().getSystemUnit()); QuantityType<?> v2 = o.toUnit(o.getUnit().getSystemUnit()); if (v1 != null && v2 != null) { return Double.compare(v1.doubleValue(), v2.doubleValue()); } else { throw new IllegalArgumentException("Unable to convert to system unit during compare."); } } else { throw new IllegalArgumentException("Can not compare incompatible units."); } }
if (itemUnit != null && (!stateUnit.getSystemUnit().equals(itemUnit.getSystemUnit()) || UnitUtils.isDifferentMeasurementSystem(itemUnit, stateUnit))) { QuantityType<?> convertedState = ((QuantityType<?>) state).toUnit(itemUnit); if (convertedState != null) { super.setState(convertedState);
@SuppressWarnings("unchecked") public @Nullable QuantityType<T> toUnit(String targetUnit) { Unit<T> unit = (Unit<T>) AbstractUnit.parse(targetUnit); if (unit != null) { return toUnit(unit); } return null; }
private @NonNull State convertOrUndef(QuantityType<?> quantityState, Unit<?> targetUnit) { QuantityType<?> converted = quantityState.toUnit(targetUnit); if (converted != null) { return converted; } return UnDefType.UNDEF; }
@Override public @Nullable State convertStateToLabelUnit(QuantityType<?> state, String label) { String labelUnit = label.lastIndexOf(" ") > 0 ? label.substring(label.lastIndexOf(" ")) : null; if (labelUnit != null && !state.getUnit().toString().equals(labelUnit)) { return state.toUnit(labelUnit); } return state; }
private @Nullable QuantityType<Temperature> handleTemperature(QuantityType<Temperature> qtState, QuantityType<Temperature> offset) { QuantityType<Temperature> finalOffset; // do the math in kelvin and afterwards convert it back to the unit of the state QuantityType<Temperature> kelvinState = qtState.toUnit(SmartHomeUnits.KELVIN); QuantityType<Temperature> kelvinOffset = offset.toUnit(SmartHomeUnits.KELVIN); if (offset.getUnit().equals(SIUnits.CELSIUS)) { finalOffset = kelvinOffset.add(ZERO_CELSIUS_IN_KELVIN.negate()); } else if (offset.getUnit().equals(ImperialUnits.FAHRENHEIT)) { finalOffset = kelvinOffset.add(ZERO_FAHRENHEIT_IN_KELVIN.negate()); } else { // offset is already in kelvin finalOffset = offset; } kelvinState = kelvinState.add(finalOffset); return kelvinState.toUnit(qtState.getUnit()); } }
/** * Convert the given number into a BigDecimal * * @param number * the number to convert * @return the given number as BigDecimal or null if number is null */ public static BigDecimal numberToBigDecimal(Number number) { if (number instanceof QuantityType) { QuantityType<?> state = ((QuantityType<?>) number) .toUnit(((QuantityType<?>) number).getUnit().getSystemUnit()); if (state != null) { return state.toBigDecimal(); } return null; } if (number != null) { return new BigDecimal(number.toString()); } else { return null; } }
@Override public int compareTo(QuantityType<T> o) { if (quantity.getUnit().isCompatible(o.quantity.getUnit())) { QuantityType<T> v1 = this.toUnit(getUnit().getSystemUnit()); QuantityType<?> v2 = o.toUnit(o.getUnit().getSystemUnit()); if (v1 != null && v2 != null) { return Double.compare(v1.doubleValue(), v2.doubleValue()); } else { throw new IllegalArgumentException("Unable to convert to system unit during compare."); } } else { throw new IllegalArgumentException("Can not compare incompatible units."); } }
private QuantityType<?> convertStateToWidgetUnit(QuantityType<?> quantityState, @NonNull Widget w) { Unit<?> widgetUnit = UnitUtils.parseUnit(getFormatPattern(w.getLabel())); if (widgetUnit != null && !widgetUnit.equals(quantityState.getUnit())) { return quantityState.toUnit(widgetUnit); } return quantityState; }
quantityState = quantityState.toUnit(patternUnit);
if (itemUnit != null && (!stateUnit.getSystemUnit().equals(itemUnit.getSystemUnit()) || UnitUtils.isDifferentMeasurementSystem(itemUnit, stateUnit))) { QuantityType<?> convertedState = ((QuantityType<?>) state).toUnit(itemUnit); if (convertedState != null) { super.setState(convertedState);