diff --git a/bundles/org.openhab.binding.danfossairunit/src/main/java/org/openhab/binding/danfossairunit/internal/ValueCache.java b/bundles/org.openhab.binding.danfossairunit/src/main/java/org/openhab/binding/danfossairunit/internal/ValueCache.java index 5d4722e24..fda3b5432 100644 --- a/bundles/org.openhab.binding.danfossairunit/src/main/java/org/openhab/binding/danfossairunit/internal/ValueCache.java +++ b/bundles/org.openhab.binding.danfossairunit/src/main/java/org/openhab/binding/danfossairunit/internal/ValueCache.java @@ -12,7 +12,8 @@ */ package org.openhab.binding.danfossairunit.internal; -import java.util.Calendar; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.HashMap; import java.util.Map; @@ -21,7 +22,7 @@ import org.openhab.core.types.State; /** * The {@link ValueCache} is responsible for holding the last value of the channels for a - * certain amount of time {@link ValueCache#durationMs} to prevent unnecessary event bus updates if the value didn't + * certain amount of time {@link ValueCache#durationMillis} to prevent unnecessary event bus updates if the value didn't * change. * * @author Robert Bach - Initial contribution @@ -29,39 +30,38 @@ import org.openhab.core.types.State; @NonNullByDefault public class ValueCache { - private Map stateByValue = new HashMap<>(); + private final Map stateByValue = new HashMap<>(); - private final long durationMs; + private final long durationMillis; - public ValueCache(long durationMs) { - this.durationMs = durationMs; + public ValueCache(long durationMillis) { + this.durationMillis = durationMillis; } /** * Updates or inserts the given value into the value cache. Returns true if there was no value in the cache * for the given channelId or if the value has updated to a different value or if the value is older than - * the cache duration + * the cache duration. + * + * @param channelId the channel's id + * @param state new state */ - public boolean updateValue(String channelId, State newState) { - long currentTimeMs = Calendar.getInstance().getTimeInMillis(); - StateWithTimestamp oldState = stateByValue.get(channelId); - boolean writeToCache; - if (oldState == null) { - writeToCache = true; - } else { - writeToCache = !oldState.state.equals(newState) || oldState.timestamp < (currentTimeMs - durationMs); + public boolean updateValue(String channelId, State state) { + Instant now = Instant.now(); + StateWithTimestamp cachedValue = stateByValue.get(channelId); + if (cachedValue == null || !state.equals(cachedValue.state) + || cachedValue.timestamp.isBefore(now.minus(durationMillis, ChronoUnit.MILLIS))) { + stateByValue.put(channelId, new StateWithTimestamp(state, now)); + return true; } - if (writeToCache) { - stateByValue.put(channelId, new StateWithTimestamp(newState, currentTimeMs)); - } - return writeToCache; + return false; } private static class StateWithTimestamp { State state; - long timestamp; + Instant timestamp; - public StateWithTimestamp(State state, long timestamp) { + public StateWithTimestamp(State state, Instant timestamp) { this.state = state; this.timestamp = timestamp; }