Skip to content

Commit 1924925

Browse files
Sebastian Hoßsebhoss
authored andcommitted
add jspecify/spotbugs annotations
1 parent e6c8605 commit 1924925

File tree

16 files changed

+79
-15
lines changed

16 files changed

+79
-15
lines changed

memoization-core/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,19 @@
3131
<!-- https://maven.apache.org/pom.html#More_Project_Information -->
3232
<name>memoization.java :: Core</name>
3333

34+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
35+
<!-- DEPENDENCIES -->
36+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
37+
<!-- https://maven.apache.org/pom.html#Dependencies -->
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.github.spotbugs</groupId>
41+
<artifactId>spotbugs-annotations</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.jspecify</groupId>
45+
<artifactId>jspecify</artifactId>
46+
</dependency>
47+
</dependencies>
48+
3449
</project>

memoization-core/src/main/java/module-info.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
/**
66
* The core module contains the core interfaces and utilities of memoization.java
77
*/
8+
@org.jspecify.nullness.NullMarked
89
module wtf.metio.memoization.core {
910

11+
requires com.github.spotbugs.annotations;
12+
requires org.jspecify;
13+
1014
exports wtf.metio.memoization.core;
1115

1216
}

memoization-core/src/main/java/wtf/metio/memoization/core/AbstractMemoizer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package wtf.metio.memoization.core;
66

7+
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
8+
79
import java.util.concurrent.ConcurrentMap;
810
import java.util.function.Function;
911

@@ -12,9 +14,10 @@ public abstract class AbstractMemoizer<KEY, VALUE> {
1214
private final ConcurrentMap<KEY, VALUE> cache;
1315

1416
protected AbstractMemoizer(final ConcurrentMap<KEY, VALUE> cache) {
15-
this.cache = ConcurrentMaps.nullsafe(cache);
17+
this.cache = cache;
1618
}
1719

20+
@CheckReturnValue
1821
protected final VALUE computeIfAbsent(final KEY key, final Function<KEY, VALUE> mappingFunction) {
1922
return cache.computeIfAbsent(key, mappingFunction);
2023
}

memoization-core/src/main/java/wtf/metio/memoization/core/ConcurrentMaps.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,37 @@
44
*/
55
package wtf.metio.memoization.core;
66

7+
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
8+
79
import java.util.Map;
10+
import java.util.Objects;
811
import java.util.concurrent.ConcurrentHashMap;
912
import java.util.concurrent.ConcurrentMap;
1013

11-
import static java.util.Objects.requireNonNull;
12-
14+
/**
15+
* Utility class that helps with handling {@link ConcurrentMap}s.
16+
*/
1317
public final class ConcurrentMaps {
1418

1519
private ConcurrentMaps() {
1620
// utility class
1721
}
1822

23+
/**
24+
* Converts a given {@link Map} to a {@link ConcurrentMap}.
25+
*
26+
* @param map The given map.
27+
* @return The converted map which may be the given one if it is already a {@link ConcurrentMap}.
28+
* @param <KEY> The key type of the map.
29+
* @param <VALUE> The value type of the map.
30+
*/
31+
@CheckReturnValue
1932
public static <KEY, VALUE> ConcurrentMap<KEY, VALUE> asConcurrentMap(final Map<KEY, VALUE> map) {
33+
Objects.requireNonNull(map, "Provide a non-null map or remove your custom cache entirely");
2034
if (ConcurrentMap.class.isAssignableFrom(map.getClass())) {
21-
return (ConcurrentMap<KEY, VALUE>) nullsafe(map);
35+
return (ConcurrentMap<KEY, VALUE>) map;
2236
}
23-
return new ConcurrentHashMap<>(nullsafe(map));
24-
}
25-
26-
public static <MAP extends Map<?, ?>> MAP nullsafe(final MAP map) {
27-
return requireNonNull(map, "Provide an empty map instead of NULL.");
37+
return new ConcurrentHashMap<>(map);
2838
}
2939

3040
}

memoization-core/src/main/java/wtf/metio/memoization/core/MemoizationDefaults.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package wtf.metio.memoization.core;
66

7+
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
8+
79
import java.util.Arrays;
810
import java.util.function.Supplier;
911

@@ -21,6 +23,7 @@ private MemoizationDefaults() {
2123
*
2224
* @return The default key supplier used throughout the library.
2325
*/
26+
@CheckReturnValue
2427
public static Supplier<Integer> staticKey() {
2528
return () -> 1;
2629
}
@@ -31,6 +34,7 @@ public static Supplier<Integer> staticKey() {
3134
* @param values The values to use.
3235
* @return The constructed cache key.
3336
*/
37+
@CheckReturnValue
3438
public static int hashCodes(final Object... values) {
3539
return Arrays.hashCode(values);
3640
}

memoization-core/src/main/java/wtf/metio/memoization/core/WrappedException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package wtf.metio.memoization.core;
77

8+
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
9+
810
import java.io.Serial;
911

1012
public final class WrappedException extends RuntimeException {
@@ -18,6 +20,7 @@ public WrappedException(final Exception wrappedException) {
1820
this.wrappedException = wrappedException;
1921
}
2022

23+
@CheckReturnValue
2124
public Exception wrappedException() {
2225
return wrappedException;
2326
}

memoization-core/src/main/java/wtf/metio/memoization/core/WrappedThrowable.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package wtf.metio.memoization.core;
77

8+
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
9+
810
import java.io.Serial;
911

1012
public final class WrappedThrowable extends RuntimeException {
@@ -18,6 +20,7 @@ public WrappedThrowable(final Throwable wrappedThrowable) {
1820
this.wrappedThrowable = wrappedThrowable;
1921
}
2022

23+
@CheckReturnValue
2124
public Throwable wrappedThrowable() {
2225
return wrappedThrowable;
2326
}

memoization-core/src/main/java/wtf/metio/memoization/core/package-info.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@
44
*/
55
/**
66
* Core interfaces and utilities.
7-
*
8-
* @see wtf.metio.memoization.core.MemoizationDefaults
97
*/
108
package wtf.metio.memoization.core;

memoization-jdk/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
<groupId>com.github.spotbugs</groupId>
5050
<artifactId>spotbugs-annotations</artifactId>
5151
</dependency>
52+
<dependency>
53+
<groupId>org.jspecify</groupId>
54+
<artifactId>jspecify</artifactId>
55+
</dependency>
5256
<dependency>
5357
<groupId>org.mockito</groupId>
5458
<artifactId>mockito-core</artifactId>

memoization-jdk/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
/**
66
* The JDK module contains the implementation covering JDK interfaces.
77
*/
8+
@org.jspecify.nullness.NullMarked
89
module wtf.metio.memoization.jdk {
910

1011
requires wtf.metio.memoization.core;
1112
requires com.github.spotbugs.annotations;
13+
requires org.jspecify;
1214

1315
exports wtf.metio.memoization.jdk;
1416

0 commit comments

Comments
 (0)