]> git.basschouten.com Git - openhab-addons.git/blob
7131fc425398eeaa6c075d3c29a0afb14a6f65eb
[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         return super.put(key, value);
53     }
54
55     @Override
56     public void putAll(@Nullable Map<? extends K, ? extends V> m) {
57         if (m == null) {
58             throw new IllegalArgumentException();
59         }
60         for (K key : m.keySet()) {
61             V value = m.get(key);
62             if (value != null) { // don't allow null values
63                 put(key, value);
64             }
65         }
66     }
67
68     @Override
69     public @Nullable V putIfAbsent(K key, V value) {
70         if (!containsKey(key)) {
71             return put(key, value);
72         } else {
73             return get(key);
74         }
75     }
76
77     class CleanerThread extends Thread {
78         @Override
79         public void run() {
80             while (true) {
81                 cleanMap();
82                 try {
83                     Thread.sleep(expiryInMillis / 2);
84                 } catch (InterruptedException e) {
85                 }
86             }
87         }
88
89         @SuppressWarnings("null")
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 }