Skip to content

Commit 2031498

Browse files
dfa1claude
andauthored
chore: extract repeated requireNonNull message literals (S1192) (#23)
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 <noreply@anthropic.com>
1 parent 7dbad8e commit 2031498

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
/// state allocation. Not thread-safe: confine an instance to one thread or pool it.
1111
public final class ZstdDecompressCtx extends NativeObject {
1212

13+
private static final String COMPRESSED = "compressed";
14+
1315
/// Creates a new decompression context.
1416
public ZstdDecompressCtx() {
1517
super(create());
@@ -53,7 +55,7 @@ public ZstdDecompressCtx windowLogMax(int windowLogMax) {
5355
/// @param maxSize upper bound on the decompressed length
5456
/// @return the original bytes
5557
public byte[] decompress(byte[] compressed, int maxSize) {
56-
Objects.requireNonNull(compressed, "compressed");
58+
Objects.requireNonNull(compressed, COMPRESSED);
5759
try (Arena arena = Arena.ofConfined()) {
5860
MemorySegment in = Zstd.copyIn(arena, compressed);
5961
MemorySegment out = arena.allocate(Math.max(maxSize, 1));
@@ -74,7 +76,7 @@ public byte[] decompress(byte[] compressed, int maxSize) {
7476
/// @param dict the dictionary the frame was compressed against
7577
/// @return the original bytes
7678
public byte[] decompress(byte[] compressed, int maxSize, ZstdDictionary dict) {
77-
Objects.requireNonNull(compressed, "compressed");
79+
Objects.requireNonNull(compressed, COMPRESSED);
7880
Objects.requireNonNull(dict, "dict");
7981
try (Arena arena = Arena.ofConfined()) {
8082
MemorySegment in = Zstd.copyIn(arena, compressed);
@@ -94,7 +96,7 @@ public byte[] decompress(byte[] compressed, int maxSize, ZstdDictionary dict) {
9496
/// @param dict the pre-digested decompression dictionary
9597
/// @return the original bytes
9698
public byte[] decompress(byte[] compressed, int maxSize, ZstdDecompressDict dict) {
97-
Objects.requireNonNull(compressed, "compressed");
99+
Objects.requireNonNull(compressed, COMPRESSED);
98100
Objects.requireNonNull(dict, "dict");
99101
try (Arena arena = Arena.ofConfined()) {
100102
MemorySegment in = Zstd.copyIn(arena, compressed);

zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class ZstdDictionary {
3434

3535
private static final String FIELD_NB_THREADS = "nbThreads";
3636
private static final String FIELD_COMPRESSION_LEVEL = "compressionLevel";
37+
private static final String SAMPLES = "samples";
3738

3839
// ZDICT_cover_params_t fields: unsigned k, d, steps, nbThreads; double
3940
// splitPoint; unsigned shrinkDict, shrinkDictMaxRegression; then a nested
@@ -94,7 +95,7 @@ public static ZstdDictionary of(byte[] raw) {
9495
/// @return the trained dictionary
9596
/// @throws ZstdException if training fails (commonly: not enough sample data)
9697
public static ZstdDictionary train(List<byte[]> samples, int maxDictBytes) {
97-
Objects.requireNonNull(samples, "samples");
98+
Objects.requireNonNull(samples, SAMPLES);
9899
if (samples.isEmpty()) {
99100
throw new ZstdException("cannot train a dictionary from zero samples");
100101
}
@@ -178,7 +179,7 @@ public static ZstdDictionary trainFastCover(List<byte[]> samples, int maxDictByt
178179

179180
private static ZstdDictionary optimize(List<byte[]> samples, int maxDictBytes,
180181
int compressionLevel, boolean fast) {
181-
Objects.requireNonNull(samples, "samples");
182+
Objects.requireNonNull(samples, SAMPLES);
182183
if (samples.isEmpty()) {
183184
throw new ZstdException("cannot train a dictionary from zero samples");
184185
}
@@ -233,7 +234,7 @@ private static ZstdDictionary optimize(List<byte[]> samples, int maxDictBytes,
233234
public static ZstdDictionary finalizeFrom(byte[] content, List<byte[]> samples,
234235
int maxDictBytes, int compressionLevel) {
235236
Objects.requireNonNull(content, "content");
236-
Objects.requireNonNull(samples, "samples");
237+
Objects.requireNonNull(samples, SAMPLES);
237238
if (samples.isEmpty()) {
238239
throw new ZstdException("cannot finalise a dictionary from zero samples");
239240
}

0 commit comments

Comments
 (0)