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

Stream Nullity #1

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/src/main/java/net/sridharan/sample/Scratch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Canva Inc. All Rights Reserved.

package net.sridharan.sample;

import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

class Scratch {
public record Foo(@Nullable String bar, String baz){}
record Qux(String a, String b){}
record Transformer(){
public static Qux transform(String bar) {
var split = bar.split("-");
return new Qux(split[0], split[1]);
}
}

// Not fine here, but fine in my environment
void streams() {
List<Foo> foos = List.of(new Foo("a-b", "c"));
foos.stream()
.filter(f -> f.bar != null)
// NullAway complains about nullability of bar even though bar is null checked
.collect(Collectors.toMap(f -> Transformer.transform(f.bar), f -> f.baz));
}

// Fine in this environment but not in mine
// In my env, Nullaway complains about the nullability of bar in collect as above
void streams2() {
List<Foo> foos = List.of(new Foo("a-b", "c"));
foos.stream()
.filter(f -> f.bar != null)
.collect(Collectors.toMap(f -> f.bar, f -> f.baz));
}
}