Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ private Humidity(byte value) {
}

/**
* Creates {@link Humidity} object with correctness check.
* Creates {@link Humidity} object with safeguard to have min 0 and max 100 as value
* @param value humidity
* @return created {@link Humidity} object
*/
public static Humidity withValue(byte value) {
if (value < 0 || value > 100) {
throw new IllegalArgumentException("Humidity value must be in [0, 100] range.");
}
return new Humidity(value);
int v = Math.max(0, Math.min(100, value));
return new Humidity((byte) v);
}

/**
Expand All @@ -68,13 +66,10 @@ public int getValue() {
* Sets humidity percentage value.
*
* @param value new humidity value.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
* Rounds to 0 or 100 if provided value isn't in allowed range.
*/
public void setValue(int value) {
if (value < 0 || value > 100) {
throw new IllegalArgumentException("Humidity value must be in [0, 100] range.");
}
this.value = value;
this.value = Math.max(0, Math.min(100, value));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ public void whenCreateHumidityWithArgs_thenValueIsSet() {
}

@Test
public void whenCreateHumidityByConstructorWithInvalidDataAboveHundred_thenThrowAnException() {
assertThrows(IllegalArgumentException.class, () -> Humidity.withValue((byte) 112));
public void whenCreateHumidityByConstructorWithInvalidDataAboveHundred_thenValueIsMaxHundred() {
Humidity h = Humidity.withValue((byte) 112);
assertEquals(100, h.getValue());
}

@Test
public void whenCreateHumidityByConstructorWithInvalidDataNegative_thenThrowAnException() {
assertThrows(IllegalArgumentException.class, () -> Humidity.withValue((byte) -33));
public void whenCreateHumidityByConstructorWithInvalidDataNegative_thenValueIsMinZero() {
Humidity h = Humidity.withValue((byte) -33);
assertEquals(0, h.getValue());
}

@Test
Expand All @@ -59,15 +61,17 @@ public void whenSetValidValues_thenAllIsFine() {
}

@Test
public void whenCreateHumidityAndSetInvalidDataAboveHundred_thenThrowAnException() {
public void whenCreateHumidityAndSetInvalidDataAboveHundred_thenValueIsMaxHundred() {
Humidity humidity = Humidity.withValue((byte) 12);
assertThrows(IllegalArgumentException.class, () -> humidity.setValue((byte) 112));
humidity.setValue(112);
assertEquals(100, humidity.getValue());
}

@Test
public void whenCreateHumidityAndSetInvalidDataNegative_thenThrowAnException() {
public void whenCreateHumidityAndSetInvalidDataNegative_thenValueIsMinZero() {
Humidity humidity = Humidity.withValue((byte) 88);
assertThrows(IllegalArgumentException.class, () -> humidity.setValue((byte) -89));
humidity.setValue(-89);
assertEquals(0, humidity.getValue());
}

@Test
Expand Down