Skip to content

Commit cf711d0

Browse files
committed
[automation] Allow returning null from supplier function in ValueCache
In some cases, it is useful to be able to return null from the supplier function using null as an "unspecified" value. Signed-off-by: Florian Hotze <[email protected]>
1 parent 63788b0 commit cf711d0

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/CacheScriptExtension.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ public ValueCacheImpl(String scriptIdentifier) {
172172
}
173173

174174
@Override
175-
public Object get(String key, Supplier<Object> supplier) {
176-
return Objects.requireNonNull(cache.computeIfAbsent(key, k -> supplier.get()));
175+
public @Nullable Object get(String key, Supplier<Object> supplier) {
176+
return cache.computeIfAbsent(key, k -> supplier.get());
177177
}
178178

179179
private Collection<Object> values() {
@@ -231,11 +231,11 @@ public TrackingValueCacheImpl(String scriptIdentifier) {
231231
}
232232

233233
@Override
234-
public Object get(String key, Supplier<Object> supplier) {
234+
public @Nullable Object get(String key, Supplier<Object> supplier) {
235235
cacheLock.lock();
236236
try {
237237
rememberAccessToKey(key);
238-
Object value = Objects.requireNonNull(sharedCache.computeIfAbsent(key, k -> supplier.get()));
238+
Object value = sharedCache.computeIfAbsent(key, k -> supplier.get());
239239

240240
logger.trace("GET with supplier to cache from '{}': '{}' -> '{}'", scriptIdentifier, key, value);
241241
return value;

bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/shared/ValueCache.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ public interface ValueCache {
5858
* Get a value from the cache or create a new key-value-pair from the given supplier
5959
*
6060
* @param key the key of the requested value
61-
* @param supplier a supplier that returns a non-null value to be used if the key was not present
61+
* @param supplier a supplier that returns a value to be used if the key was not present
6262
* @return the value associated with the key
6363
*/
64+
@Nullable
6465
Object get(String key, Supplier<Object> supplier);
6566
}

0 commit comments

Comments
 (0)