Skip to content

Commit e299d83

Browse files
authored
fix nullability issue (#46)
* fix nullability issue * update deps
1 parent 8735ec2 commit e299d83

3 files changed

Lines changed: 11 additions & 14 deletions

File tree

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

56
<parent>
@@ -17,8 +18,8 @@
1718

1819
<properties>
1920
<java.version>25</java.version>
20-
<errorprone.version>2.45.0</errorprone.version>
21-
<nullaway.version>0.12.14</nullaway.version>
21+
<errorprone.version>2.46.0</errorprone.version>
22+
<nullaway.version>0.12.15</nullaway.version>
2223
</properties>
2324

2425
<dependencyManagement>

src/main/java/it/aboutbits/springboot/toolbox/util/CollectUtil.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package it.aboutbits.springboot.toolbox.util;
22

33
import org.jspecify.annotations.NullMarked;
4-
import org.jspecify.annotations.Nullable;
54
import org.springframework.data.util.Streamable;
65

76
import java.util.Collection;
@@ -94,12 +93,11 @@ public static <T, R> Stream<R> collectToStream(Stream<T> items, NullableFunction
9493
}
9594

9695
public static <T, K, R> Map<K, R> collectToMap(
97-
Collection<@Nullable T> items,
96+
Collection<T> items,
9897
Function<T, K> keyMapper,
9998
Function<T, R> valueMapper
10099
) {
101100
return items.stream()
102-
.filter(Objects::nonNull)
103101
.collect(Collectors.toMap(keyMapper, valueMapper));
104102
}
105103

@@ -113,12 +111,11 @@ public static <T, K, R> Map<K, R> collectToMap(
113111
}
114112

115113
public static <T, K, R> Map<K, R> collectToMap(
116-
Stream<@Nullable T> items,
114+
Stream<T> items,
117115
Function<T, K> keyMapper,
118116
Function<T, R> valueMapper
119117
) {
120118
return items
121-
.filter(Objects::nonNull)
122119
.collect(Collectors.toMap(keyMapper, valueMapper));
123120
}
124121
}

src/test/java/it/aboutbits/springboot/toolbox/util/CollectUtilTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package it.aboutbits.springboot.toolbox.util;
22

33
import org.jspecify.annotations.NullMarked;
4-
import org.jspecify.annotations.Nullable;
54
import org.junit.jupiter.api.DisplayName;
65
import org.junit.jupiter.api.Nested;
76
import org.junit.jupiter.api.Test;
@@ -381,7 +380,7 @@ class CollectToMap {
381380
@DisplayName("Should convert Collection to Map using key and value mappers")
382381
void shouldConvertCollectionToMapUsingMappers() {
383382
// given
384-
var items = (Collection<@Nullable String>) Arrays.asList("a", "bb", "ccc");
383+
var items = (Collection<String>) Arrays.asList("a", "bb", "ccc");
385384

386385
// when
387386
var result = CollectUtil.collectToMap(items, String::length, Function.identity());
@@ -398,7 +397,7 @@ void shouldConvertCollectionToMapUsingMappers() {
398397
@DisplayName("Should throw on duplicate keys according to Collectors.toMap default behavior")
399398
void shouldThrowOnDuplicateKeys() {
400399
// given
401-
var items = (Collection<@Nullable String>) Arrays.asList("a", "b"); // both have length of 1
400+
var items = (Collection<String>) Arrays.asList("a", "b"); // both have length of 1
402401

403402
// when / then
404403
assertThatIllegalStateException().isThrownBy(
@@ -431,7 +430,7 @@ void shouldConvertStreamableToMapUsingMappers() {
431430
@DisplayName("Should convert Stream to Map using key and value mappers")
432431
void shouldConvertStreamToMapUsingMappers() {
433432
// given
434-
var items = Stream.<@Nullable String>of("m", "nn");
433+
var items = Stream.of("m", "nn");
435434

436435
// when
437436
var result = CollectUtil.collectToMap(items, String::length, Function.identity());
@@ -447,7 +446,7 @@ void shouldConvertStreamToMapUsingMappers() {
447446
@DisplayName("Should return empty map for empty collection")
448447
void shouldReturnEmptyMapForEmptyCollection() {
449448
// given
450-
var items = Collections.<@Nullable String>emptyList();
449+
var items = Collections.<String>emptyList();
451450

452451
// when
453452
var result = CollectUtil.collectToMap(items, String::length, Function.identity());
@@ -473,7 +472,7 @@ void shouldReturnEmptyMapForEmptyStreamable() {
473472
@DisplayName("Should return empty map for empty stream")
474473
void shouldReturnEmptyMapForEmptyStream() {
475474
// given
476-
var items = Stream.<@Nullable String>empty();
475+
var items = Stream.<String>empty();
477476

478477
// when
479478
var result = CollectUtil.collectToMap(items, String::length, Function.identity());

0 commit comments

Comments
 (0)