Skip to content

Minor improvements to Map.merge usage of @PolyNull and @NonNull. - #41

Merged
wmdietl merged 3 commits into
eisop:masterfrom
cpovirk:mapmerge
Jun 11, 2023
Merged

wmdietl merged 3 commits into
eisop:masterfrom
cpovirk:mapmerge

Conversation

@cpovirk

@cpovirk cpovirk commented Jun 8, 2023 •

Copy link
Copy Markdown
Contributor
  • The new TreeMap method wasn't annotated with @PolyNull or @NonNull.
  • The Map method had a stale comment from before the method began
    using
    @PolyNull
    .
  • The inputs of remappingFunction weren't annotated as @NonNull.

- The new `TreeMap` method wasn't annotated with `@PolyNull`.
- The `Map` method had a stale comment from before [the method began
  using
  `@PolyNull`](eisop@20c1642#diff-26e5bec525317a8d6f1c03687b5e99822ffa62956d2904d8c400981a62990b34R1303).
@wmdietl wmdietl changed the title Minor improvements to Map.merge @PolyNull usage: Minor improvements to Map.merge @PolyNull usage. Jun 9, 2023

@wmdietl wmdietl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! I have one question about a mismatch with Map.

*/
@Override
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
public @PolyNull V merge(K key, V value, BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
public @PolyNull V merge(K key, V value, BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
public @PolyNull V merge(K key, @NonNull V value, BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {

Would adding @NonNull to value make sense, to match the signature in Map?
(We would also need to add the import.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I should really look at this method (and probably some other Java 8 additions in Map) more carefully.

I agree that @NonNull makes sense on value. (And the implementations in Map, HashMap, TreeMap, and presumably others perform a null check, as the doc's first sentence suggests but the "implementation requirements" do not.)

In fact, I think we should go a step further to make the remappingFunction be a BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V>

  • The first argument is the old value, which is passed only if it's non-null. (In the null case, merge inserts value and returns.)
  • The second argument is value, which, as we just discussed, is non-null.

I've updated accordingly, and I'll tentatively do the same in jspecify/jdk#14.

- Use `@NonNull` on the `value` parameter of `TreeMap.merge`.
- Use `@NonNull` on the types of the inputs of `remappingFunction`.
@cpovirk
cpovirk requested a review from wmdietl June 9, 2023 15:39
@cpovirk cpovirk changed the title Minor improvements to Map.merge @PolyNull usage. Minor improvements to Map.merge usage of @PolyNull and @NonNull. Jun 9, 2023

@wmdietl wmdietl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the further improvements!
eisop-plume-lib/plume-util#93 fixes the downstream failure.

@wmdietl
wmdietl merged commit 7e9c413 into eisop:master Jun 11, 2023
cpovirk added a commit to cpovirk/jdk that referenced this pull request Feb 1, 2024
See typetools/jdk#177

(We already have the annotation on `ofNullable`, so we don't need to
carry that over.)

(eisop#41)
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