Skip to content

Commit

Permalink
[automation] Allow returning null from supplier function in ValueCache
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
florian-h05 committed Oct 26, 2024
1 parent 63788b0 commit d38e348
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public ValueCacheImpl(String scriptIdentifier) {

@Override
public Object get(String key, Supplier<Object> supplier) {

Check failure on line 175 in bundles/org.openhab.core.automation.module.script.rulesupport/src/main/java/org/openhab/core/automation/module/script/rulesupport/internal/CacheScriptExtension.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-24.04)

The default '@org.eclipse.jdt.annotation.NonNull' conflicts with the inherited '@org.eclipse.jdt.annotation.Nullable' annotation in the overridden method from org.openhab.core.automation.module.script.rulesupport.shared.ValueCache
return Objects.requireNonNull(cache.computeIfAbsent(key, k -> supplier.get()));
return cache.computeIfAbsent(key, k -> supplier.get());
}

private Collection<Object> values() {
Expand Down Expand Up @@ -235,7 +235,7 @@ public Object get(String key, Supplier<Object> supplier) {
cacheLock.lock();
try {
rememberAccessToKey(key);
Object value = Objects.requireNonNull(sharedCache.computeIfAbsent(key, k -> supplier.get()));
Object value = sharedCache.computeIfAbsent(key, k -> supplier.get());

logger.trace("GET with supplier to cache from '{}': '{}' -> '{}'", scriptIdentifier, key, value);
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public interface ValueCache {
* Get a value from the cache or create a new key-value-pair from the given supplier
*
* @param key the key of the requested value
* @param supplier a supplier that returns a non-null value to be used if the key was not present
* @param supplier a supplier that returns a value to be used if the key was not present
* @return the value associated with the key
*/
@Nullable
Object get(String key, Supplier<Object> supplier);
}

0 comments on commit d38e348

Please sign in to comment.