]> git.basschouten.com Git - openhab-addons.git/blob
68b2f9106edad2adb84e15be107cbf3b686e2c5f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.shelly.internal.manager;
14
15 import java.util.Date;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * {@link ShellyManagerCache} implements a cache with expiring times of the entries
24  *
25  * @author Markus Michels - Initial contribution
26  */
27 @NonNullByDefault
28 public class ShellyManagerCache<K, V> extends ConcurrentHashMap<K, V> {
29
30     private static final long serialVersionUID = 1L;
31
32     private Map<K, Long> timeMap = new ConcurrentHashMap<K, Long>();
33     private long expiryInMillis = ShellyManagerConstants.CACHE_TIMEOUT_DEF_MIN * 60 * 1000; // Default 1h
34
35     public ShellyManagerCache() {
36         initialize();
37     }
38
39     public ShellyManagerCache(long expiryInMillis) {
40         this.expiryInMillis = expiryInMillis;
41         initialize();
42     }
43
44     void initialize() {
45         new CleanerThread().start();
46     }
47
48     @Override
49     public @Nullable V put(K key, V value) {
50         Date date = new Date();
51         timeMap.put(key, date.getTime());
52         V returnVal = super.put(key, value);
53         return returnVal;
54     }
55
56     @Override
57     public void putAll(@Nullable Map<? extends K, ? extends V> m) {
58         if (m == null) {
59             throw new IllegalArgumentException();
60         }
61         for (K key : m.keySet()) {
62             V value = m.get(key);
63             if (value != null) { // don't allow null values
64                 put(key, value);
65             }
66         }
67     }
68
69     @Override
70     public @Nullable V putIfAbsent(K key, V value) {
71         if (!containsKey(key)) {
72             return put(key, value);
73         } else {
74             return get(key);
75         }
76     }
77
78     class CleanerThread extends Thread {
79         @Override
80         public void run() {
81             while (true) {
82                 cleanMap();
83                 try {
84                     Thread.sleep(expiryInMillis / 2);
85                 } catch (InterruptedException e) {
86                 }
87             }
88         }
89
90         private void cleanMap() {
91             long currentTime = new Date().getTime();
92             for (K key : timeMap.keySet()) {
93                 if (currentTime > (timeMap.get(key) + expiryInMillis)) {
94                     remove(key);
95                     timeMap.remove(key);
96                 }
97             }
98         }
99     }
100 }