Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Map operations using computeIfAbsent #4020

Merged
merged 1 commit into from Feb 6, 2024

Conversation

wborn
Copy link
Member

@wborn wborn commented Jan 5, 2024

If the specified key is not already associated with a value (or is mapped to null), the given mapping function computes the value.

@wborn wborn requested a review from a team as a code owner January 5, 2024 20:41
objects = new HashMap<>();
objectCache.put(scriptIdentifier, objects);
}
Map<String, Object> objects = objectCache.computeIfAbsent(scriptIdentifier, k -> new HashMap<>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that this always creates a warning in the compiler because the EEA can't properly model "return value is non-null if second parameter is non-null" and assumes the return value is nullable.

Unfortunately the ECJ guys don't see the problem or the need to improve that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had the same situation recently here: openhab/openhab-addons#15828 (comment). Maybe a similar work-around could be used here as well.

If the specified key is not already associated with a value (or is mapped to null), the given mapping function computes the value.

Signed-off-by: Wouter Born <github@maindrain.net>
Copy link
Member

@J-N-K J-N-K left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@J-N-K J-N-K merged commit f4e8369 into openhab:main Feb 6, 2024
3 checks passed
@J-N-K J-N-K added the enhancement An enhancement or new feature of the Core label Feb 6, 2024
@J-N-K J-N-K added this to the 4.2 milestone Feb 6, 2024
@wborn wborn deleted the use-computeIfAbsent branch February 6, 2024 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants