public Dimension getDimension() { return getUnit().getDimension(); }
@Override public int hashCode() { final int prime = 31; int tmp = prime * getUnit().hashCode(); tmp += prime * (quantity.getValue() == null ? 0 : quantity.getValue().hashCode()); return tmp; }
@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()); }
/** * 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; }
@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; }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public State calculate(Set<Item> items) { if (items == null || items.size() <= 0) { return UnDefType.UNDEF; } QuantityType<?> min = null; for (Item item : items) { if (isSameDimension(item)) { QuantityType itemState = item.getStateAs(QuantityType.class); if (itemState != null) { if (min == null || (min.getUnit().isCompatible(itemState.getUnit()) && min.compareTo(itemState) > 0)) { min = itemState; } } } } return min != null ? min : 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 @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; }
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) { new PercentType(this.toBigDecimal().multiply(HUNDRED)))); } else if (target == PercentType.class) { if (SmartHomeUnits.PERCENT.equals(getUnit())) { return target.cast(new PercentType(toBigDecimal()));
Unit<?> stateUnit = ((QuantityType<?>) state).getUnit(); if (itemUnit != null && (!stateUnit.getSystemUnit().equals(itemUnit.getSystemUnit()) || UnitUtils.isDifferentMeasurementSystem(itemUnit, stateUnit))) {
private static boolean isAbstractUnitOne(QuantityType<?> left) { return left.getUnit().equals(SmartHomeUnits.ONE); }
public Dimension getDimension() { return getUnit().getDimension(); }
@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; }
@Override public int hashCode() { final int prime = 31; int tmp = prime * getUnit().hashCode(); tmp += prime * (quantity.getValue() == null ? 0 : quantity.getValue().hashCode()); return tmp; }
/** * 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<?> min = null; for (Item item : items) { if (isSameDimension(item)) { QuantityType itemState = item.getStateAs(QuantityType.class); if (itemState != null) { if (min == null || (min.getUnit().isCompatible(itemState.getUnit()) && min.compareTo(itemState) > 0)) { min = itemState; } } } } return min != null ? min : 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."); } }
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; }