]> git.basschouten.com Git - openhab-addons.git/commitdiff
[energidataservice] Fix IllegalStateException (#15693)
authorJacob Laursen <jacob-github@vindvejr.dk>
Thu, 5 Oct 2023 12:54:04 +0000 (14:54 +0200)
committerGitHub <noreply@github.com>
Thu, 5 Oct 2023 12:54:04 +0000 (14:54 +0200)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/CacheManager.java

index 3e95bf67e82f673e2c5ac8f9640aa0ac83b30355..64bae83d589d9e21fa4fadcac9ddc4d3447a91bf 100644 (file)
@@ -50,10 +50,9 @@ public class CacheManager {
     private final Clock clock;
     private final PriceListParser priceListParser = new PriceListParser();
 
-    private Map<DatahubTariff, Collection<DatahubPricelistRecord>> datahubRecordsMap = new HashMap<>();
-
     private Map<Instant, BigDecimal> spotPriceMap = new ConcurrentHashMap<>(SPOT_PRICE_MAX_CACHE_SIZE);
 
+    private Map<DatahubTariff, Collection<DatahubPricelistRecord>> datahubRecordsMap = new HashMap<>();
     private Map<DatahubTariff, Map<Instant, BigDecimal>> tariffsMap = new ConcurrentHashMap<>();
 
     public CacheManager() {
@@ -63,9 +62,9 @@ public class CacheManager {
     public CacheManager(Clock clock) {
         this.clock = clock.withZone(NORD_POOL_TIMEZONE);
 
-        for (DatahubTariff tariff : DatahubTariff.values()) {
-            datahubRecordsMap.put(tariff, new ArrayList<>());
-            tariffsMap.put(tariff, new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE));
+        for (DatahubTariff datahubTariff : DatahubTariff.values()) {
+            datahubRecordsMap.put(datahubTariff, new ArrayList<>());
+            tariffsMap.put(datahubTariff, new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE));
         }
     }
 
@@ -73,9 +72,19 @@ public class CacheManager {
      * Clear all cached data.
      */
     public void clear() {
-        datahubRecordsMap.clear();
         spotPriceMap.clear();
-        tariffsMap.clear();
+
+        for (DatahubTariff datahubTariff : DatahubTariff.values()) {
+            Collection<DatahubPricelistRecord> datahubRecords = datahubRecordsMap.get(datahubTariff);
+            if (datahubRecords != null) {
+                datahubRecords.clear();
+            }
+
+            Map<Instant, BigDecimal> tariffs = tariffsMap.get(datahubTariff);
+            if (tariffs != null) {
+                tariffs.clear();
+            }
+        }
     }
 
     /**