DecimalType extends Number implements PrimitiveType, State, Command, Comparable<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 == PercentType.class) { return target.cast(new PercentType(toBigDecimal().multiply(BigDecimal.valueOf(100)))); } else if (target == UpDownType.class) { if (equals(ZERO)) { return target.cast(UpDownType.UP); } else if (toBigDecimal().compareTo(BigDecimal.valueOf(1)) == 0) { return target.cast(UpDownType.DOWN); } else { return null; } } else if (target == OpenClosedType.class) { if (equals(ZERO)) { return target.cast(OpenClosedType.CLOSED); } else if (toBigDecimal().compareTo(BigDecimal.valueOf(1)) == 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(this.toBigDecimal().multiply(BigDecimal.valueOf(100))))); } else if (target == DateTimeType.class) { return target.cast(new DateTimeType(value.toString())); } else { return defaultConversion(target); } } DecimalType();  DecimalType(BigDecimal value);  DecimalType(long value);  DecimalType(double value);  DecimalType(String value); @Override String toString(); @Override String toFullString(); static DecimalType valueOf(String value); @Override String format(String pattern); BigDecimal toBigDecimal(); @Override int hashCode(); @Override boolean equals(@Nullable Object obj); @Override int compareTo(DecimalType o); @Override double doubleValue(); @Override float floatValue(); @Override int intValue(); @Override long longValue(); @Override T as(@Nullable Class<T> target); static final DecimalType ZERO; }
@Test public void testConversionToOnOffType() { assertEquals(OnOffType.ON, new DecimalType("100.0").as(OnOffType.class)); assertEquals(OnOffType.ON, new DecimalType("1.0").as(OnOffType.class)); assertEquals(OnOffType.OFF, new DecimalType("0.0").as(OnOffType.class)); }
@Test public void testConversionToOpenCloseType() { assertEquals(OpenClosedType.OPEN, new DecimalType("1.0").as(OpenClosedType.class)); assertEquals(OpenClosedType.CLOSED, new DecimalType("0.0").as(OpenClosedType.class)); assertNull(new DecimalType("0.5").as(OpenClosedType.class)); }
@Test public void testConversionToUpDownType() { assertEquals(UpDownType.UP, new DecimalType("0.0").as(UpDownType.class)); assertEquals(UpDownType.DOWN, new DecimalType("1.0").as(UpDownType.class)); assertNull(new DecimalType("0.5").as(OpenClosedType.class)); }
@Test public void testConversionToHSBType() { assertEquals(new HSBType("0,0,0"), new DecimalType("0.0").as(HSBType.class)); assertEquals(new HSBType("0,0,100"), new DecimalType("1.0").as(HSBType.class)); assertEquals(new HSBType("0,0,50"), new DecimalType("0.5").as(HSBType.class)); }
@Test public void testConversionToDateTimeType() { assertEquals(new DateTimeType("2014-03-30T10:58:47+0000"), new DecimalType("1396177127").as(DateTimeType.class)); assertEquals(new DateTimeType("1969-12-31T23:59:59+0000"), new DecimalType("-1").as(DateTimeType.class)); assertEquals(new DateTimeType("1970-01-01T00:00:00+0000"), DecimalType.ZERO.as(DateTimeType.class)); assertEquals(new DateTimeType("1970-01-01T00:00:01+0000"), new DecimalType("1").as(DateTimeType.class)); assertEquals(new DateTimeType("1970-01-01T00:00:01+0000"), new DecimalType("1.0").as(DateTimeType.class)); assertEquals(new DateTimeType("1970-01-01T00:00:01+0000"), new DecimalType("1.5").as(DateTimeType.class)); assertEquals(new DateTimeType("1969-12-31T23:59:59+0000"), new DecimalType("-1.0").as(DateTimeType.class)); }
@Test public void testConversionToPercentType() { assertEquals(new PercentType(70), new DecimalType("0.7").as(PercentType.class)); }
@Test public void testConversionToPointType() { assertNull(new DecimalType("0.23").as(PointType.class)); }