QuantityType extends Number implements PrimitiveType, State, Command, Comparable<QuantityType<T>> { @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 { return null; } } else if (target == UpDownType.class) { if (doubleValue() == 0) { return target.cast(UpDownType.UP); } else if (toBigDecimal().compareTo(BigDecimal.ONE) == 0) { return target.cast(UpDownType.DOWN); } else { return null; } } else if (target == OpenClosedType.class) { if (doubleValue() == 0) { return target.cast(OpenClosedType.CLOSED); } else if (toBigDecimal().compareTo(BigDecimal.ONE) == 0) { return target.cast(OpenClosedType.OPEN); } else { return null; } } else if (target == HSBType.class) { return target.cast( new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(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) { return target.cast(new DecimalType(toBigDecimal())); } else { return State.super.as(target); } } @SuppressWarnings("unchecked")  QuantityType(); @SuppressWarnings("unchecked")  QuantityType(String value);  QuantityType(Number value, Unit<T> unit); private  QuantityType(Quantity<T> quantity); static QuantityType<T> valueOf(double value, Unit<T> unit); @Override String toString(); static QuantityType<? extends Quantity<?>> valueOf(String value); @SuppressWarnings("unchecked") @Override boolean equals(@Nullable Object obj); @Override int compareTo(QuantityType<T> o); Unit<T> getUnit(); Dimension getDimension(); @SuppressWarnings("unchecked") @Nullable QuantityType<T> toUnit(Unit<?> targetUnit); @SuppressWarnings("unchecked") @Nullable QuantityType<T> toUnit(String targetUnit); BigDecimal toBigDecimal(); @Override int hashCode(); @Override String format(String pattern); @Override int intValue(); @Override long longValue(); @Override float floatValue(); @Override double doubleValue(); @Override String toFullString(); @Override U as(@Nullable Class<U> target); QuantityType<T> add(QuantityType<T> state); QuantityType<T> negate(); QuantityType<T> subtract(QuantityType<T> state); QuantityType<?> multiply(BigDecimal value); QuantityType<?> multiply(QuantityType<?> state); QuantityType<?> divide(BigDecimal value); QuantityType<?> divide(QuantityType<?> state); QuantityType<T> offset(QuantityType<T> offset, Unit<T> unit); static final QuantityType<Dimensionless> ZERO; static final QuantityType<Dimensionless> ONE; }
@Test public void testConversionToOnOffType() { assertEquals(OnOffType.ON, new QuantityType<>("1").as(OnOffType.class)); assertEquals(OnOffType.OFF, new QuantityType<>("0").as(OnOffType.class)); assertEquals(OnOffType.ON, new QuantityType<>("1 %").as(OnOffType.class)); assertEquals(OnOffType.ON, new QuantityType<>("100 %").as(OnOffType.class)); assertEquals(OnOffType.OFF, new QuantityType<>("0 %").as(OnOffType.class)); }
@Test public void testConversionToOpenCloseType() { assertEquals(OpenClosedType.OPEN, new QuantityType<>("1.0").as(OpenClosedType.class)); assertEquals(OpenClosedType.CLOSED, new QuantityType<>("0.0").as(OpenClosedType.class)); assertNull(new QuantityType<>("0.5").as(OpenClosedType.class)); }
@Test public void testConversionToUpDownType() { assertEquals(UpDownType.UP, new QuantityType<>("0.0").as(UpDownType.class)); assertEquals(UpDownType.DOWN, new QuantityType<>("1.0").as(UpDownType.class)); assertNull(new QuantityType<>("0.5").as(OpenClosedType.class)); }
@Test public void testConversionToHSBType() { assertEquals(new HSBType("0,0,0"), new QuantityType<>("0.0").as(HSBType.class)); assertEquals(new HSBType("0,0,100"), new QuantityType<>("1.0").as(HSBType.class)); assertEquals(new HSBType("0,0,50"), new QuantityType<>("0.5").as(HSBType.class)); }
@Test public void testConversionToPercentType() { assertEquals(PercentType.HUNDRED, new QuantityType<>("100 %").as(PercentType.class)); assertEquals(PercentType.ZERO, new QuantityType<>("0 %").as(PercentType.class)); }