[Java] Ensure that Object2*HashMaps and ObjectHashSet always check equality using the value in the map and not vice versa.#253
Conversation
…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.
…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.
… and bulk operations.
…e. handle null values as keys/values and use `Objects.equals` for the equality checks.
|
This pull request introduces 2 alerts when merging e357fbf into f5b2f90 - view on LGTM.com new alerts:
|
|
This pull request introduces 2 alerts when merging 0ac6bb4 into f5b2f90 - view on LGTM.com new alerts:
|
|
Note, according to the https://lgtm.com/help/lgtm/alert-suppression:
LGTM warning can be ignored and will disappear once the PR is merged. |
|
This pull request introduces 2 alerts when merging e07b631 into f5b2f90 - view on LGTM.com new alerts:
|
…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)
This PR contains the following changes:
EntryIterator.equals/hashCodeto handle properly null values.MapEntry.equals/hashCodeto handle properly null values.Object2IntHashMap.MapEntry.hashCodeto use the stored value instead of callinggetIntValuefrom theEntryIterator.Object2IntHashMap.EntryIterator.equals/hashCodemethods.MapEntry.getValue/setValueto return updated value after a successful set.Objects.equalswhen comparing keys in theObject2ObjectHanshMap/Object2IntHashMapusing the key from the map as the first parameter. This ensures that the asymmetric keys can be supported. For example seeCharSequenceKeyin the tests.Objects.equalsin theObject2ObjectHashMap.containsValuefor symmetry with the key comparison.