/** * Static access to {@link QuantityType#QuantityType(double, Unit)}. * * @param value the non null measurement value. * @param unit the non null measurement unit. * @return a new {@link QuantityType} */ public static <T extends Quantity<T>> QuantityType<T> valueOf(double value, Unit<T> unit) { return new QuantityType<T>(value, unit); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public State calculate(Set<Item> items) { if (items == null || items.size() <= 0) { return UnDefType.UNDEF; } QuantityType<?> sum = null; int count = 0; for (Item item : items) { if (isSameDimension(item)) { QuantityType itemState = item.getStateAs(QuantityType.class); if (itemState != null) { if (sum == null) { sum = itemState; // initialise the sum from the first item count++; } else { sum = sum.add(itemState); count++; } } } } if (sum != null && count > 0) { BigDecimal result = sum.toBigDecimal().divide(BigDecimal.valueOf(count), RoundingMode.HALF_UP); return new QuantityType(result, sum.getUnit()); } 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."); } }
@Override public String format(@Nullable String pattern) { String formatPattern = pattern; if (formatPattern != null && formatPattern.contains(UnitUtils.UNIT_PLACEHOLDER)) { String unitSymbol = getUnit().equals(SmartHomeUnits.PERCENT) ? "%%" : getUnit().toString(); formatPattern = formatPattern.replace(UnitUtils.UNIT_PLACEHOLDER, unitSymbol); } // The value could be an integer value. Try to convert to BigInteger in // order to have access to more conversion formats. try { return String.format(formatPattern, toBigDecimal().toBigIntegerExact()); } catch (ArithmeticException ae) { // Could not convert to integer value without loss of // information. Fall through to default behavior. } catch (IllegalFormatConversionException ifce) { // The conversion is not valid for the type BigInteger. This // happens, if the format is like "%.1f" but the value is an // integer. Fall through to default behavior. } return String.format(formatPattern, toBigDecimal()); }
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<Dimensionless>("0"); finalOffset = finalOffset.negate(); QuantityType qtState = (QuantityType) state; try { if (finalOffset.getUnit() == SmartHomeUnits.ONE) { 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.", if (qtState.getUnit().getSystemUnit().equals(SmartHomeUnits.KELVIN)) { QuantityType<Temperature> tmp = handleTemperature(qtState, finalOffset); if (tmp != null) { result = qtState.add(finalOffset); logger.warn("Cannot apply offset '{}' to state '{}' because types do not match.", finalOffset, qtState); } else if (state instanceof DecimalType && finalOffset.getUnit().equals(SmartHomeUnits.ONE)) { DecimalType decState = (DecimalType) state; result = new DecimalType(decState.doubleValue() + finalOffset.doubleValue()); } else { logger.warn(
Unit<?> unit = getUnit(); if (unit != null) { super.setState(new QuantityType<>(((DecimalType) state).doubleValue(), unit)); return; Unit<?> stateUnit = ((QuantityType<?>) state).getUnit(); if (itemUnit != null && (!stateUnit.getSystemUnit().equals(itemUnit.getSystemUnit()) || UnitUtils.isDifferentMeasurementSystem(itemUnit, stateUnit))) { QuantityType<?> convertedState = ((QuantityType<?>) state).toUnit(itemUnit); if (convertedState != null) { super.setState(convertedState);
@Override public <U extends State> @Nullable U as(@Nullable Class<U> target) { if (target == OnOffType.class) { if (intValue() == 0) { return target.cast(OnOffType.OFF); } else if (SmartHomeUnits.PERCENT.equals(getUnit())) { return target.cast(toBigDecimal().compareTo(BigDecimal.ZERO) > 0 ? OnOffType.ON : OnOffType.OFF); } else if (toBigDecimal().compareTo(BigDecimal.ONE) == 0) { return target.cast(OnOffType.ON); } else { if (doubleValue() == 0) { return target.cast(UpDownType.UP); } else if (toBigDecimal().compareTo(BigDecimal.ONE) == 0) { return target.cast(UpDownType.DOWN); } else { if (doubleValue() == 0) { return target.cast(OpenClosedType.CLOSED); } else if (toBigDecimal().compareTo(BigDecimal.ONE) == 0) { return target.cast(OpenClosedType.OPEN); } else { new PercentType(this.toBigDecimal().multiply(HUNDRED)))); } else if (target == PercentType.class) { if (SmartHomeUnits.PERCENT.equals(getUnit())) { return target.cast(new PercentType(toBigDecimal())); return target.cast(new PercentType(toBigDecimal().multiply(HUNDRED))); } else if (target == DecimalType.class) {
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 @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; }
/** * 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; }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public State calculate(Set<Item> items) { if (items == null || items.size() <= 0) { return UnDefType.UNDEF; } QuantityType<?> max = null; for (Item item : items) { if (isSameDimension(item)) { QuantityType itemState = item.getStateAs(QuantityType.class); if (itemState != null) { if (max == null || (max.getUnit().isCompatible(itemState.getUnit()) && max.compareTo(itemState) < 0)) { max = itemState; } } } } return max != null ? max : UnDefType.UNDEF; }
public Dimension getDimension() { return getUnit().getDimension(); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public State calculate(Set<Item> items) { if (items == null || items.size() <= 0) { return UnDefType.UNDEF; } QuantityType<?> sum = null; for (Item item : items) { if (isSameDimension(item)) { QuantityType itemState = item.getStateAs(QuantityType.class); if (itemState != null) { if (sum == null) { sum = itemState; // initialise the sum from the first item } else if (sum.getUnit().isCompatible(itemState.getUnit())) { sum = sum.add(itemState); } } } } return sum != null ? sum : UnDefType.UNDEF; }
@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; }
@SuppressWarnings("unchecked") @Override public boolean equals(@Nullable Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof QuantityType)) { return false; } QuantityType<?> other = (QuantityType<?>) obj; if (!quantity.getUnit().getDimension().equals(other.quantity.getUnit().getDimension())) { return false; } else if (compareTo((QuantityType<T>) other) != 0) { return false; } return true; }
result = new org.openhab.core.library.types.DecimalType(type.toString()); } else if (typeClass.equals(QuantityType.class)) { result = new org.openhab.core.library.types.DecimalType(((QuantityType<?>) type).toBigDecimal()); } else if (typeClass.equals(HSBType.class)) { result = new org.openhab.core.library.types.HSBType(type.toString());
@SuppressWarnings({ "unchecked", "rawtypes" }) public static QuantityType<?> operator_plus(QuantityType<?> x, QuantityType<?> y) { return x == null ? y : y == null ? x : x.add((QuantityType) y); }
stateDoubleValue = ((DecimalType) state).doubleValue(); } else { stateDoubleValue = ((QuantityType<?>) state).doubleValue();
Unit<?> unit = getUnit(); if (unit != null) { super.setState(new QuantityType<>(((DecimalType) state).doubleValue(), unit)); return; Unit<?> stateUnit = ((QuantityType<?>) state).getUnit(); if (itemUnit != null && (!stateUnit.getSystemUnit().equals(itemUnit.getSystemUnit()) || UnitUtils.isDifferentMeasurementSystem(itemUnit, stateUnit))) { QuantityType<?> convertedState = ((QuantityType<?>) state).toUnit(itemUnit); if (convertedState != null) { super.setState(convertedState);