PercentType extends DecimalType { @Override public <T extends State> @Nullable T as(@Nullable Class<T> target) { if (target == OnOffType.class) { return target.cast(equals(ZERO) ? OnOffType.OFF : OnOffType.ON); } else if (target == DecimalType.class) { return target.cast(new DecimalType(toBigDecimal().divide(BigDecimal.valueOf(100), 8, RoundingMode.UP))); } else if (target == UpDownType.class) { if (equals(ZERO)) { return target.cast(UpDownType.UP); } else if (equals(HUNDRED)) { return target.cast(UpDownType.DOWN); } else { return null; } } else if (target == OpenClosedType.class) { if (equals(ZERO)) { return target.cast(OpenClosedType.CLOSED); } else if (equals(HUNDRED)) { return target.cast(OpenClosedType.OPEN); } else { return null; } } else if (target == HSBType.class) { return target.cast(new HSBType(DecimalType.ZERO, PercentType.ZERO, this)); } else if (target == QuantityType.class) { return target.cast(new QuantityType<>(toBigDecimal().doubleValue(), SmartHomeUnits.PERCENT)); } else { return defaultConversion(target); } } PercentType();  PercentType(int value);  PercentType(String value);  PercentType(BigDecimal value); static PercentType valueOf(String value); @Override T as(@Nullable Class<T> target); static final PercentType ZERO; static final PercentType HUNDRED; }
@Test public void testConversionToOnOffType() { assertEquals(OnOffType.ON, new PercentType("100.0").as(OnOffType.class)); assertEquals(OnOffType.ON, new PercentType("1.0").as(OnOffType.class)); assertEquals(OnOffType.OFF, new PercentType("0.0").as(OnOffType.class)); }
@Test public void testConversionToDecimalType() { assertEquals(new DecimalType("1.0"), new PercentType("100.0").as(DecimalType.class)); assertEquals(new DecimalType("0.01"), new PercentType("1.0").as(DecimalType.class)); assertEquals(DecimalType.ZERO, new PercentType("0.0").as(DecimalType.class)); }
@Test public void testConversionToQuantityType() { assertEquals(new QuantityType<>("100 %"), PercentType.HUNDRED.as(QuantityType.class)); assertEquals(new QuantityType<>("1 one"), ((QuantityType<?>) PercentType.HUNDRED.as(QuantityType.class)).toUnit(SmartHomeUnits.ONE)); }
@Test public void testConversionToOpenCloseType() { assertEquals(OpenClosedType.OPEN, new PercentType("100.0").as(OpenClosedType.class)); assertEquals(OpenClosedType.CLOSED, new PercentType("0.0").as(OpenClosedType.class)); assertNull(new PercentType("50.0").as(OpenClosedType.class)); }
@Test public void testConversionToUpDownType() { assertEquals(UpDownType.UP, new PercentType("0.0").as(UpDownType.class)); assertEquals(UpDownType.DOWN, new PercentType("100.0").as(UpDownType.class)); assertNull(new PercentType("50.0").as(OpenClosedType.class)); }
@Test public void testConversionToHSBType() { assertEquals(new HSBType("0,0,0"), new PercentType("0.0").as(HSBType.class)); assertEquals(new HSBType("0,0,100"), new PercentType("100.0").as(HSBType.class)); assertEquals(new HSBType("0,0,50"), new PercentType("50.0").as(HSBType.class)); }