Skip to content

Commit

Permalink
Extend OnOffType so it can be used as drop-in-replacement for Number …
Browse files Browse the repository at this point in the history
…types in rules

Signed-off-by: Roland Tapken <[email protected]>
  • Loading branch information
Cybso committed Dec 14, 2023
1 parent 4b92db3 commit eff454f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ public String toFullString() {
return super.toString();
}

public boolean booleanValue() {
return this == ON;
}

public byte byteValue() {
return (byte) (this == ON ? 1 : 0);
}

public double doubleValue() {
return this == ON ? 1.0 : 0.0;
}

public float floatValue() {
return this == ON ? 1.0f : 0.0f;
}

public int intValue() {
return this == ON ? 1 : 0;
}

public long longValue() {
return this == ON ? 1L : 0L;
}

public short shortValue() {
return (short) (this == ON ? 1 : 0);
}

@Override
public <T extends State> @Nullable T as(@Nullable Class<T> target) {
if (target == DecimalType.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,46 @@ public void testConversionToPointType() {
assertNull(OnOffType.ON.as(PointType.class));
assertNull(OnOffType.OFF.as(PointType.class));
}

@Test
public void testBooleanValue() {
assertEquals(true, OnOffType.ON.booleanValue());
assertEquals(false, OnOffType.OFF.booleanValue());
}

@Test
public void testByteValue() {
assertEquals((byte) 1, OnOffType.ON.byteValue());
assertEquals((byte) 0, OnOffType.OFF.byteValue());
}

@Test
public void testDoubleValue() {
assertEquals(1.0, OnOffType.ON.doubleValue());
assertEquals(0.0, OnOffType.OFF.doubleValue());
}

@Test
public void testFloatValue() {
assertEquals(1.0f, OnOffType.ON.floatValue());
assertEquals(0.0f, OnOffType.OFF.floatValue());
}

@Test
public void testIntValue() {
assertEquals(1, OnOffType.ON.intValue());
assertEquals(0, OnOffType.OFF.intValue());
}

@Test
public void testLongValue() {
assertEquals((long) 1, OnOffType.ON.longValue());
assertEquals((long) 0, OnOffType.OFF.longValue());
}

@Test
public void testShortValue() {
assertEquals((short) 1, OnOffType.ON.shortValue());
assertEquals((short) 0, OnOffType.OFF.shortValue());
}
}

0 comments on commit eff454f

Please sign in to comment.