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

Iterate using Map entries #4003

Merged
merged 2 commits into from
Jan 3, 2024
Merged

Iterate using Map entries #4003

merged 2 commits into from
Jan 3, 2024

Conversation

wborn
Copy link
Member

@wborn wborn commented Jan 2, 2024

Iteration using Map entries is preferred because it is more efficient and helps preventing NPEs.

@wborn wborn requested a review from a team as a code owner January 2, 2024 21:00
for (String stateVariable : values.keySet()) {
StateVariableValue value = values.get(stateVariable);
for (Entry<String, StateVariableValue> entry : values.entrySet()) {
StateVariableValue value = entry.getValue();
if (value.getValue() != null) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this null-check can be omitted if we use entry.getValue() because the map has no null values.

Choose a reason for hiding this comment

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

I'm getting NPE's I believe due to the removal of the null check here. I've opened #4037 to track that.

ConfigDescriptionParameter configDescriptionParameter = map.get(key);
for (Entry<String, ConfigDescriptionParameter> entry : map.entrySet()) {
String key = entry.getKey();
ConfigDescriptionParameter configDescriptionParameter = entry.getValue();
if (configDescriptionParameter != null) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this null-check can be omitted if we use entry.getValue() because the map has no null values.

Comment on lines 154 to 155
List<String> vendorPortfolio = entry.getValue();
return vendorPortfolio == null ? List.of() : vendorPortfolio;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
List<String> vendorPortfolio = entry.getValue();
return vendorPortfolio == null ? List.of() : vendorPortfolio;
return entry.getValue();

The map does not contain null values

Iteration using Map entries is preferred because it is more efficient and helps preventing NPEs.

Signed-off-by: Wouter Born <github@maindrain.net>
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 added the enhancement An enhancement or new feature of the Core label Jan 3, 2024
@J-N-K J-N-K added this to the 4.2 milestone Jan 3, 2024
@J-N-K J-N-K merged commit afd1d47 into openhab:main Jan 3, 2024
3 checks passed
@wborn wborn deleted the entryset-iteration branch January 3, 2024 13:52
cipianpascu pushed a commit to cipianpascu/openhab-core that referenced this pull request Jan 17, 2024
* Iterate using Map entries

Iteration using Map entries is preferred because it is more efficient and helps preventing NPEs.

Signed-off-by: Wouter Born <github@maindrain.net>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
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.

3 participants