From e004ea9f856c73c1a2528770d00b41b4f63fbcfe Mon Sep 17 00:00:00 2001 From: Davide Angelocola Date: Fri, 26 Jun 2026 22:45:15 +0200 Subject: [PATCH] chore: extract repeated requireNonNull message literals (S1192) The null checks added the parameter-name strings "compressed" and "samples" three times each, tripping S1192. Hoist them to constants (COMPRESSED, SAMPLES). The short names (src/dst/dict) are below Sonar's literal-length threshold and stay inline. Co-Authored-By: Claude Opus 4.8 --- .../main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java | 8 +++++--- .../src/main/java/io/github/dfa1/zstd/ZstdDictionary.java | 7 ++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java index bfa42d1..f864695 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java @@ -10,6 +10,8 @@ /// state allocation. Not thread-safe: confine an instance to one thread or pool it. public final class ZstdDecompressCtx extends NativeObject { + private static final String COMPRESSED = "compressed"; + /// Creates a new decompression context. public ZstdDecompressCtx() { super(create()); @@ -53,7 +55,7 @@ public ZstdDecompressCtx windowLogMax(int windowLogMax) { /// @param maxSize upper bound on the decompressed length /// @return the original bytes public byte[] decompress(byte[] compressed, int maxSize) { - Objects.requireNonNull(compressed, "compressed"); + Objects.requireNonNull(compressed, COMPRESSED); try (Arena arena = Arena.ofConfined()) { MemorySegment in = Zstd.copyIn(arena, compressed); MemorySegment out = arena.allocate(Math.max(maxSize, 1)); @@ -74,7 +76,7 @@ public byte[] decompress(byte[] compressed, int maxSize) { /// @param dict the dictionary the frame was compressed against /// @return the original bytes public byte[] decompress(byte[] compressed, int maxSize, ZstdDictionary dict) { - Objects.requireNonNull(compressed, "compressed"); + Objects.requireNonNull(compressed, COMPRESSED); Objects.requireNonNull(dict, "dict"); try (Arena arena = Arena.ofConfined()) { MemorySegment in = Zstd.copyIn(arena, compressed); @@ -94,7 +96,7 @@ public byte[] decompress(byte[] compressed, int maxSize, ZstdDictionary dict) { /// @param dict the pre-digested decompression dictionary /// @return the original bytes public byte[] decompress(byte[] compressed, int maxSize, ZstdDecompressDict dict) { - Objects.requireNonNull(compressed, "compressed"); + Objects.requireNonNull(compressed, COMPRESSED); Objects.requireNonNull(dict, "dict"); try (Arena arena = Arena.ofConfined()) { MemorySegment in = Zstd.copyIn(arena, compressed); diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java index 1eae3c5..42cbc72 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java @@ -34,6 +34,7 @@ public final class ZstdDictionary { private static final String FIELD_NB_THREADS = "nbThreads"; private static final String FIELD_COMPRESSION_LEVEL = "compressionLevel"; + private static final String SAMPLES = "samples"; // ZDICT_cover_params_t fields: unsigned k, d, steps, nbThreads; double // splitPoint; unsigned shrinkDict, shrinkDictMaxRegression; then a nested @@ -94,7 +95,7 @@ public static ZstdDictionary of(byte[] raw) { /// @return the trained dictionary /// @throws ZstdException if training fails (commonly: not enough sample data) public static ZstdDictionary train(List samples, int maxDictBytes) { - Objects.requireNonNull(samples, "samples"); + Objects.requireNonNull(samples, SAMPLES); if (samples.isEmpty()) { throw new ZstdException("cannot train a dictionary from zero samples"); } @@ -178,7 +179,7 @@ public static ZstdDictionary trainFastCover(List samples, int maxDictByt private static ZstdDictionary optimize(List samples, int maxDictBytes, int compressionLevel, boolean fast) { - Objects.requireNonNull(samples, "samples"); + Objects.requireNonNull(samples, SAMPLES); if (samples.isEmpty()) { throw new ZstdException("cannot train a dictionary from zero samples"); } @@ -233,7 +234,7 @@ private static ZstdDictionary optimize(List samples, int maxDictBytes, public static ZstdDictionary finalizeFrom(byte[] content, List samples, int maxDictBytes, int compressionLevel) { Objects.requireNonNull(content, "content"); - Objects.requireNonNull(samples, "samples"); + Objects.requireNonNull(samples, SAMPLES); if (samples.isEmpty()) { throw new ZstdException("cannot finalise a dictionary from zero samples"); }