Skip to content

[Java] Ensure that Object2*HashMaps and ObjectHashSet always check equality using the value in the map and not vice versa.#253

Merged
mjpt777 merged 12 commits into
aeron-io:masterfrom
vyazelenko:object2primitive-maps-fixes
May 11, 2022
Merged

[Java] Ensure that Object2*HashMaps and ObjectHashSet always check equality using the value in the map and not vice versa.#253
mjpt777 merged 12 commits into
aeron-io:masterfrom
vyazelenko:object2primitive-maps-fixes

Conversation

@vyazelenko
Copy link
Copy Markdown
Contributor

@vyazelenko vyazelenko commented Apr 29, 2022

This PR contains the following changes:

  • Fix EntryIterator.equals/hashCode to handle properly null values.
  • Fix MapEntry.equals/hashCode to handle properly null values.
  • Fix Object2IntHashMap.MapEntry.hashCode to use the stored value instead of calling getIntValue from the EntryIterator.
  • Implement Object2IntHashMap.EntryIterator.equals/hashCode methods.
  • Fix MapEntry.getValue/setValue to return updated value after a successful set.
  • Use Objects.equals when comparing keys in the Object2ObjectHanshMap/Object2IntHashMap using the key from the map as the first parameter. This ensures that the asymmetric keys can be supported. For example see CharSequenceKey in the tests.
  • Use Objects.equals in the Object2ObjectHashMap.containsValue for symmetry with the key comparison.
  • Refactor maps to load field values only once when looping/iterating.

…uality using the value in the map and not vice versa. This allows implementing asymmetric keys/values which can match multiple other types. For an example see CharSequenceKey from the tests.
@vyazelenko vyazelenko marked this pull request as draft May 2, 2022 19:03
vyazelenko added 8 commits May 4, 2022 19:22
…ck on the value stored rather than the value passed as an argument. Ensure that the equality is always done the same, i.e. use LangUtil#exactEquals.
…heck using the key from the map as the source of the comparison.
…e. handle null values as keys/values and use `Objects.equals` for the equality checks.
@vyazelenko vyazelenko marked this pull request as ready for review May 6, 2022 14:12
@lgtm-com
Copy link
Copy Markdown
Contributor

lgtm-com Bot commented May 6, 2022

This pull request introduces 2 alerts when merging e357fbf into f5b2f90 - view on LGTM.com

new alerts:

  • 2 for Array index out of bounds

@lgtm-com
Copy link
Copy Markdown
Contributor

lgtm-com Bot commented May 6, 2022

This pull request introduces 2 alerts when merging 0ac6bb4 into f5b2f90 - view on LGTM.com

new alerts:

  • 2 for Array index out of bounds

@vyazelenko
Copy link
Copy Markdown
Contributor Author

vyazelenko commented May 7, 2022

Note, according to the https://lgtm.com/help/lgtm/alert-suppression:

If you've enabled automated code review of pull requests for your projects, it's important to note that alert suppression changes made in a pull request are only applied when that pull request is merged. So, if you suppress an alert in a pull request, LGTM will continue to report the related alert for as long as the pull request is open. For more information, see Automated code review FAQs.

LGTM warning can be ignored and will disappear once the PR is merged.

@lgtm-com
Copy link
Copy Markdown
Contributor

lgtm-com Bot commented May 7, 2022

This pull request introduces 2 alerts when merging e07b631 into f5b2f90 - view on LGTM.com

new alerts:

  • 2 for Array index out of bounds

@mjpt777 mjpt777 merged commit a320110 into aeron-io:master May 11, 2022
@vyazelenko vyazelenko deleted the object2primitive-maps-fixes branch May 11, 2022 10:23
vyazelenko added a commit to vyazelenko/agrona that referenced this pull request May 16, 2022
…uality using the value in the map and not vice versa. (aeron-io#253)

* [Java] Ensure that Object2*HashMaps and ObjectHashSet always check equality using the value in the map and not vice versa. This allows implementing asymmetric keys/values which can match multiple other types. For an example see CharSequenceKey from the tests.

* [Java] Refactor map equality tests.

* [Java] Fix Object2ObjectHashMap#containsValue to perform equality check on the value stored rather than the value passed as an argument. Ensure that the equality is always done the same, i.e. use LangUtil#exactEquals.

* [Java] Add sanity tests for Object2IntHashMap and missing value handling.

* [Java] Simplify Object2ObjectHashMap.

* [Java] Fix Object2IntHashMap put/containsKey/removeKey key equality check using the key from the map as the source of the comparison.

* [Java] Avoid loading keys/values/entries multiple times during lookup and bulk operations.

* [Java] Fix EntryIterator/MapEntry equals/hashCode implementations, i.e. handle null values as keys/values and use `Objects.equals` for the equality checks.

* [Java] Replace LangUtil.exactEquals with Objects.equals.

* [Java] Add tests for `Int2Int*Map.compact()`.

* [Java] Suppress lgtm IOOBE warnings.

* [Java] Update LGTM suppression syntax.

(cherry picked from commit a320110)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants