]> git.basschouten.com Git - openhab-addons.git/blob
5e375b45219ec308fbfb43504a5d2be13f8d1175
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.pentair.internal.utils;
14
15 import java.lang.ref.SoftReference;
16 import java.time.Duration;
17 import java.util.function.Supplier;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * This is a modified version of the ExpiryCache which adds functions such as getLastKnownValue. It also allows an
24  * interface via Supplier which will return the value, or through a function which calls putValue.
25  *
26  * There must be provided an action in order to retrieve/calculate the value. This action will be called only if the
27  * answer from the last calculation is not valid anymore, i.e. if it is expired.
28  *
29  * @author Christoph Weitkamp - Initial contribution
30  * @author Martin van Wingerden - Add Duration constructor
31  * @author Jeff James - Added added getLastKnownValue
32  *
33  * @param <V> the type of the value
34  */
35 @NonNullByDefault
36 public class ExpiringCache<V> {
37     private final long expiry;
38
39     private SoftReference<@Nullable V> value = new SoftReference<>(null);
40     private long expiresAt;
41
42     public interface RefreshAction {
43         void refresh();
44     }
45
46     public ExpiringCache() {
47         this.expiry = 0;
48     }
49
50     /**
51      * Create a new instance.
52      *
53      * @param expiry the duration for how long the value stays valid
54      * @param action the action to retrieve/calculate the value
55      * @throws IllegalArgumentException For an expire value <=0.
56      */
57     public ExpiringCache(Duration expiry) {
58         if (expiry.isNegative() || expiry.isZero()) {
59             throw new IllegalArgumentException("Cache expire time must be greater than 0");
60         }
61         this.expiry = expiry.toNanos();
62     }
63
64     /**
65      * Create a new instance.
66      *
67      * @param expiry the duration in milliseconds for how long the value stays valid
68      * @param action the action to retrieve/calculate the value
69      */
70     public ExpiringCache(long expiry) {
71         this(Duration.ofMillis(expiry));
72     }
73
74     /**
75      * Returns the value - possibly from the cache, if it is still valid.
76      */
77     public synchronized @Nullable V getValue(Supplier<@Nullable V> action) {
78         @Nullable
79         V cachedValue = value.get();
80         if (cachedValue == null || isExpired()) {
81             return refreshValue(action);
82         }
83         return cachedValue;
84     }
85
86     /**
87      * Returns the value - either from the cache or will call the action function which is responsible for calling
88      * putValue.
89      */
90     public synchronized @Nullable V getValue(RefreshAction action) {
91         @Nullable
92         V cachedValue = value.get();
93         if (cachedValue == null || isExpired()) {
94             action.refresh();
95             cachedValue = value.get();
96         }
97
98         return cachedValue;
99     }
100
101     /**
102      * Returns the last known value
103      */
104     public synchronized @Nullable V getLastKnownValue() {
105         return value.get();
106     }
107
108     /**
109      * Puts a new value into the cache.
110      *
111      * @param value the new value
112      */
113     public final synchronized void putValue(@Nullable V value) {
114         this.value = new SoftReference<>(value);
115         expiresAt = calcExpiresAt();
116     }
117
118     /**
119      * Invalidates the value in the cache.
120      */
121     public final synchronized void invalidateValue() {
122         value = new SoftReference<>(null);
123         expiresAt = 0;
124     }
125
126     /**
127      * Refreshes and returns the value in the cache.
128      * If null returned from action.get, the get action should have sued putValue to update the item
129      *
130      * @return the new value
131      */
132     public synchronized @Nullable V refreshValue(Supplier<@Nullable V> action) {
133         @Nullable
134         V freshValue = action.get();
135         if (freshValue == null) {
136             return null;
137         }
138         value = new SoftReference<>(freshValue);
139         expiresAt = calcExpiresAt();
140         return freshValue;
141     }
142
143     /**
144      * Checks if the value is expired.
145      *
146      * @return true if the value is expired
147      */
148     public boolean isExpired() {
149         return expiresAt < System.nanoTime();
150     }
151
152     private long calcExpiresAt() {
153         return System.nanoTime() + expiry;
154     }
155 }