]> git.basschouten.com Git - openhab-addons.git/commitdiff
Minor value cache tweaks (#12493)
authorJacob Laursen <jacob-github@vindvejr.dk>
Sat, 19 Mar 2022 07:43:12 +0000 (08:43 +0100)
committerGitHub <noreply@github.com>
Sat, 19 Mar 2022 07:43:12 +0000 (08:43 +0100)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.binding.danfossairunit/src/main/java/org/openhab/binding/danfossairunit/internal/ValueCache.java

index 5d4722e24e8d90daa58cf0f54cf0acff749fac18..fda3b5432a0d3dcba4adb1c23dc25713e1124376 100644 (file)
@@ -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<String, StateWithTimestamp> stateByValue = new HashMap<>();
+    private final Map<String, StateWithTimestamp> 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;
         }