@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 @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", "rawtypes" }) public static QuantityType<?> operator_plus(QuantityType<?> x, QuantityType<?> y) { return x == null ? y : y == null ? x : x.add((QuantityType) y); }
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()); } }
@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 @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; }
result = qtState.add(finalOffset);