Skip to content

Commit 4252895

Browse files
authored
Merge pull request #217 from pdambrauskas/add-value-nullability-for-cache
2 parents 9413127 + f6a6255 commit 4252895

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/main/java/org/dataloader/ValueCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.dataloader.annotations.PublicSpi;
44
import org.dataloader.impl.CompletableFutureKit;
55
import org.dataloader.impl.NoOpValueCache;
6+
import org.jspecify.annotations.Nullable;
67
import org.jspecify.annotations.NullMarked;
78

89
import java.util.ArrayList;
@@ -40,7 +41,7 @@
4041
*/
4142
@PublicSpi
4243
@NullMarked
43-
public interface ValueCache<K, V> {
44+
public interface ValueCache<K, V extends @Nullable Object> {
4445

4546
/**
4647
* Creates a new value cache, using the default no-op implementation.

src/test/kotlin/org/dataloader/KotlinExamples.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.dataloader
22

3+
import java.util.concurrent.CompletableFuture
34
import org.junit.jupiter.api.Test
45
import reactor.core.publisher.Flux
56
import java.util.concurrent.CompletableFuture.completedFuture
7+
import org.dataloader.impl.NoOpValueCache
68

79
/**
810
* Some Kotlin code to prove that are JSpecify annotations work here
@@ -81,6 +83,19 @@ class KotlinExamples {
8183
standardNullableAsserts(dataLoader)
8284
}
8385

86+
@Test
87+
fun `basic kotlin test of nullable value types in value cache`() {
88+
val valueCache = object : ValueCache<String, String?> by NoOpValueCache() {
89+
override fun get(key: String): CompletableFuture<String?> = if (key == "null")
90+
completedFuture(null)
91+
else
92+
completedFuture(key)
93+
}
94+
95+
assert(valueCache["key"].get() == "key")
96+
assert(valueCache["null"].get() == null)
97+
}
98+
8499
private fun standardNullableAsserts(dataLoader: DataLoader<String, String?>) {
85100
val cfA = dataLoader.load("A")
86101
val cfB = dataLoader.load("B")

0 commit comments

Comments
 (0)