Skip to content

Commit

Permalink
[persistence] Handle null value for unit field of filters (#3716)
Browse files Browse the repository at this point in the history
Setting up a treshold filter with the UI, it did not work because the unit field was blank.
I got an NPE from PersistenceTresholdFilter, and the PersistenceIncludeFilter would also throw a NPE in that case.
For PersistenceTimeFilter, defaulting to "s" is just cosmetic.

Picks-up PR #3681 and should be merged for the 4.0 release, because the UI does not prevent the unit field from being null.

Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 authored Jul 20, 2023
1 parent 5a00bfd commit 62a8355
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.math.BigDecimal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.items.Item;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
Expand All @@ -38,11 +39,12 @@ public class PersistenceIncludeFilter extends PersistenceFilter {
private final String unit;
private final boolean inverted;

public PersistenceIncludeFilter(String name, BigDecimal lower, BigDecimal upper, String unit, boolean inverted) {
public PersistenceIncludeFilter(String name, BigDecimal lower, BigDecimal upper, @Nullable String unit,
boolean inverted) {
super(name);
this.lower = lower;
this.upper = upper;
this.unit = unit;
this.unit = (unit == null) ? "" : unit;
this.inverted = inverted;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.measure.UnconvertibleException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.items.Item;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
Expand Down Expand Up @@ -47,10 +48,10 @@ public class PersistenceThresholdFilter extends PersistenceFilter {

private final transient Map<String, State> valueCache = new HashMap<>();

public PersistenceThresholdFilter(String name, BigDecimal value, String unit, boolean relative) {
public PersistenceThresholdFilter(String name, BigDecimal value, @Nullable String unit, boolean relative) {
super(name);
this.value = value;
this.unit = unit;
this.unit = (unit == null) ? "" : unit;
this.relative = relative;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public class PersistenceTimeFilter extends PersistenceFilter {
private transient @Nullable Duration duration;
private final transient Map<String, ZonedDateTime> nextPersistenceTimes = new HashMap<>();

public PersistenceTimeFilter(String name, int value, String unit) {
public PersistenceTimeFilter(String name, int value, @Nullable String unit) {
super(name);
this.value = value;
this.unit = unit;
this.unit = (unit == null) ? "s" : unit;
}

public int getValue() {
Expand Down

0 comments on commit 62a8355

Please sign in to comment.