Skip to content

Commit 21245ae

Browse files
committed
refactor: apply sonarlint suggestions
1 parent d261417 commit 21245ae

32 files changed

+192
-166
lines changed

redis-om-spring/src/main/java/com/redis/om/spring/RedisEnhancedKeyValueAdapter.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public Object put(Object id, Object item, String keyspace) {
147147
Map<byte[], byte[]> rawMap = rdo.getBucket().rawMap();
148148
connection.hashCommands().hMSet(objectKey, rawMap);
149149

150-
if (expires(rdo)) {
150+
if (willExpire(rdo)) {
151151
connection.keyCommands().expire(objectKey, rdo.getTimeToLive());
152152
}
153153

@@ -168,8 +168,8 @@ public Object put(Object id, Object item, String keyspace) {
168168
@Override
169169
public <T> T get(Object id, String keyspace, Class<T> type) {
170170

171-
String stringId = asString(id);
172-
String stringKeyspace = asString(keyspace);
171+
String stringId = asStringValue(id);
172+
String stringKeyspace = asStringValue(keyspace);
173173

174174
byte[] binId = createKey(stringKeyspace, stringId);
175175

@@ -184,7 +184,7 @@ public <T> T get(Object id, String keyspace, Class<T> type) {
184184
data.setId(stringId);
185185
data.setKeyspace(stringKeyspace);
186186

187-
return readBackTimeToLiveIfSet(binId, converter.read(type, data));
187+
return readTimeToLiveIfSet(binId, converter.read(type, data));
188188
}
189189

190190
/*
@@ -200,7 +200,7 @@ public <T> T delete(Object id, String keyspace, Class<T> type) {
200200

201201
if (o != null) {
202202

203-
byte[] keyToDelete = createKey(asString(keyspace), asString(id));
203+
byte[] keyToDelete = createKey(asStringValue(keyspace), asStringValue(id));
204204

205205
redisOperations.execute((RedisCallback<Void>) connection -> {
206206
connection.keyCommands().del(keyToDelete);
@@ -331,7 +331,7 @@ public void update(PartialUpdate<?> update) {
331331

332332
if (update.isRefreshTtl()) {
333333

334-
if (expires(rdo)) {
334+
if (willExpire(rdo)) {
335335
connection.keyCommands().expire(redisKey, rdo.getTimeToLive());
336336
} else {
337337
connection.keyCommands().persist(redisKey);
@@ -398,7 +398,7 @@ private RedisUpdateObject fetchDeletePathsFromHash(RedisUpdateObject redisUpdate
398398

399399
for (byte[] field : existingFields) {
400400

401-
if (asString(field).startsWith(path + ".")) {
401+
if (asStringValue(field).startsWith(path + ".")) {
402402
redisUpdateObject.addFieldToRemove(field);
403403
connection.hashCommands().hGet(redisUpdateObject.targetKey, toBytes(field));
404404
}
@@ -407,7 +407,7 @@ private RedisUpdateObject fetchDeletePathsFromHash(RedisUpdateObject redisUpdate
407407
return redisUpdateObject;
408408
}
409409

410-
private String asString(Object value) {
410+
private String asStringValue(Object value) {
411411
if (value instanceof String valueAsString) {
412412
return valueAsString;
413413
} else {
@@ -423,7 +423,7 @@ private String asString(Object value) {
423423
* @return
424424
*/
425425
@Nullable
426-
private <T> T readBackTimeToLiveIfSet(@Nullable byte[] key, @Nullable T target) {
426+
private <T> T readTimeToLiveIfSet(@Nullable byte[] key, @Nullable T target) {
427427

428428
if (target == null || key == null) {
429429
return target;
@@ -469,7 +469,7 @@ private <T> T readBackTimeToLiveIfSet(@Nullable byte[] key, @Nullable T target)
469469
* @param data must not be {@literal null}.
470470
* @since 2.3.7
471471
*/
472-
private boolean expires(RedisData data) {
472+
private boolean willExpire(RedisData data) {
473473
return data.getTimeToLive() != null && data.getTimeToLive() > 0;
474474
}
475475

redis-om-spring/src/main/java/com/redis/om/spring/RedisJSONKeyValueAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ private Optional<Long> getTTLForEntity(Object entity) {
303303
} catch (SecurityException | IllegalArgumentException e) {
304304
return Optional.empty();
305305
}
306-
} else if (settings != null && settings.getTimeToLive() != null && settings.getTimeToLive() > 0) {
306+
} else if (settings.getTimeToLive() != null && settings.getTimeToLive() > 0) {
307307
return Optional.of(settings.getTimeToLive());
308308
}
309309
}

redis-om-spring/src/main/java/com/redis/om/spring/RedisOMSpringProperties.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ public void setLimit(int limit) {
3636
}
3737
}
3838

39-
// DJL properies
39+
// DJL properties
4040
@Data
4141
public static class Djl {
42+
private static final String DEFAULT_ENGINE = "PyTorch";
4243
// image embedding settings
4344
@NotNull
44-
private String imageEmbeddingModelEngine = "PyTorch";
45+
private String imageEmbeddingModelEngine = DEFAULT_ENGINE;
4546
@NotNull
4647
private String imageEmbeddingModelModelUrls = "djl://ai.djl.pytorch/resnet18_embedding";
4748
@NotNull
@@ -61,15 +62,15 @@ public static class Djl {
6162

6263
// face detection
6364
@NotNull
64-
private String faceDetectionModelEngine = "PyTorch";
65+
private String faceDetectionModelEngine = DEFAULT_ENGINE;
6566
@NotNull
6667
private String faceDetectionModelName = "retinaface";
6768
@NotNull
6869
private String faceDetectionModelModelUrls = "https://resources.djl.ai/test-models/pytorch/retinaface.zip";
6970

7071
// face embeddings
7172
@NotNull
72-
private String faceEmbeddingModelEngine = "PyTorch";
73+
private String faceEmbeddingModelEngine = DEFAULT_ENGINE;
7374
@NotNull
7475
private String faceEmbeddingModelName = "face_feature";
7576
@NotNull
Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,79 @@
11
package com.redis.om.spring.annotations;
22

33
public enum ReducerFunction {
4-
// REDUCE COUNT 0
5-
// Count the number of records in each group
4+
/**
5+
* REDUCE COUNT 0
6+
* Count the number of records in each group
7+
*/
68
COUNT,
79

8-
// REDUCE COUNT_DISTINCT 1 {property}
9-
// Count the number of distinct values for property.
10+
/**
11+
* REDUCE COUNT_DISTINCT 1 {property}
12+
* Count the number of distinct values for property.
13+
*/
1014
COUNT_DISTINCT,
1115

12-
// REDUCE COUNT_DISTINCTISH 1 {property}
13-
// Same as COUNT_DISTINCT - but provide an approximation instead of an exact count
16+
/**
17+
* REDUCE COUNT_DISTINCTISH 1 {property}
18+
* Same as COUNT_DISTINCT - but provide an approximation instead of an exact count
19+
*/
1420
COUNT_DISTINCTISH,
1521

16-
// REDUCE SUM 1 {property}
17-
// Return the sum of all numeric values of a given property in a group.
18-
// Non-numeric values if the group are counted as 0.
22+
/**
23+
* REDUCE SUM 1 {property}
24+
* Return the sum of all numeric values of a given property in a group.
25+
* Non-numeric values if the group are counted as 0.
26+
*/
1927
SUM,
2028

21-
// REDUCE MIN 1 {property}
22-
// Return the minimal value of a property, whether it is a string, number or NULL.
29+
/**
30+
* REDUCE MIN 1 {property}
31+
* Return the minimal value of a property, whether it is a string, number or NULL.
32+
*/
2333
MIN,
2434

25-
// REDUCE MAX 1 {property}
26-
// Return the maximal value of a property, whether it is a string, number or NULL.
35+
/**
36+
* REDUCE MAX 1 {property}
37+
* Return the maximal value of a property, whether it is a string, number or NULL.
38+
*/
2739
MAX,
2840

29-
// REDUCE AVG 1 {property}
30-
// Return the average value of a numeric property.
41+
/**
42+
* REDUCE AVG 1 {property}
43+
* Return the average value of a numeric property.
44+
*/
3145
AVG,
3246

33-
// REDUCE STDDEV 1 {property}
34-
// Return the standard deviation of a numeric property in the group.
47+
/**
48+
* REDUCE STDDEV 1 {property}
49+
* Return the standard deviation of a numeric property in the group.
50+
*/
3551
STDDEV,
3652

37-
// REDUCE QUANTILE 2 {property} {quantile}
38-
// Return the value of a numeric property at a given quantile of the results.
39-
// Quantile is expressed as a number between 0 and 1.
53+
/**
54+
* REDUCE QUANTILE 2 {property} {quantile}
55+
* Return the value of a numeric property at a given quantile of the results.
56+
* Quantile is expressed as a number between 0 and 1.
57+
*/
4058
QUANTILE,
4159

42-
// REDUCE TOLIST 1 {property}
43-
// Merge all distinct values of a given property into a single array.
60+
/**
61+
* REDUCE TOLIST 1 {property}
62+
* Merge all distinct values of a given property into a single array.
63+
*/
4464
TOLIST,
4565

46-
// REDUCE FIRST_VALUE {nargs} {property} [BY {property} [ASC|DESC]]
47-
// Return the first or top value of a given property in the group,
48-
// optionally by comparing that or another property.
66+
/**
67+
* REDUCE FIRST_VALUE {nargs} {property} [BY {property} [ASC|DESC]]
68+
* Return the first or top value of a given property in the group,
69+
* optionally by comparing that or another property
70+
*/
4971
FIRST_VALUE,
5072

51-
// REDUCE RANDOM_SAMPLE {nargs} {property} {sample_size}
52-
// Perform a reservoir sampling of the group elements with a given size,
53-
// and return an array of the sampled items with an even distribution.
73+
/**
74+
* REDUCE RANDOM_SAMPLE {nargs} {property} {sample_size}
75+
* Perform a reservoir sampling of the group elements with a given size,
76+
* and return an array of the sampled items with an even distribution.
77+
*/
5478
RANDOM_SAMPLE
5579
}

redis-om-spring/src/main/java/com/redis/om/spring/audit/EntityAuditor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public EntityAuditor(RedisOperations<?, ?> redisOperations) {
2323
public void processEntity(byte[] redisKey, Object item) {
2424
boolean isNew = (boolean) redisOperations
2525
.execute((RedisCallback<Object>) connection -> !connection.keyCommands().exists(redisKey));
26-
processEntity(redisKey, item, isNew);
26+
processEntity(item, isNew);
2727
}
2828

29-
public void processEntity(byte[] redisKey, Object item, boolean isNew) {
29+
public void processEntity(Object item, boolean isNew) {
3030
var auditClass = isNew ? CreatedDate.class : LastModifiedDate.class;
3131

3232
List<Field> fields = com.redis.om.spring.util.ObjectUtils.getFieldsWithAnnotation(item.getClass(), auditClass);

redis-om-spring/src/main/java/com/redis/om/spring/client/RedisModulesClient.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,28 @@ public GsonBuilder gsonBuilder() {
8989

9090
private JedisClientConfig createClientConfig(int database, @Nullable String username, RedisPassword password, JedisClientConfiguration clientConfiguration) {
9191

92-
DefaultJedisClientConfig.Builder builder = DefaultJedisClientConfig.builder();
92+
DefaultJedisClientConfig.Builder jedisConfigBuilder = DefaultJedisClientConfig.builder();
9393

94-
clientConfiguration.getClientName().ifPresent(builder::clientName);
95-
builder.connectionTimeoutMillis(Math.toIntExact(clientConfiguration.getConnectTimeout().toMillis()));
96-
builder.socketTimeoutMillis(Math.toIntExact(clientConfiguration.getReadTimeout().toMillis()));
94+
clientConfiguration.getClientName().ifPresent(jedisConfigBuilder::clientName);
95+
jedisConfigBuilder.connectionTimeoutMillis(Math.toIntExact(clientConfiguration.getConnectTimeout().toMillis()));
96+
jedisConfigBuilder.socketTimeoutMillis(Math.toIntExact(clientConfiguration.getReadTimeout().toMillis()));
9797

98-
builder.database(database);
98+
jedisConfigBuilder.database(database);
9999

100100
if (!ObjectUtils.isEmpty(username)) {
101-
builder.user(username);
101+
jedisConfigBuilder.user(username);
102102
}
103-
password.toOptional().map(String::new).ifPresent(builder::password);
103+
password.toOptional().map(String::new).ifPresent(jedisConfigBuilder::password);
104104

105105
if (clientConfiguration.isUseSsl()) {
106106

107-
builder.ssl(true);
107+
jedisConfigBuilder.ssl(true);
108108

109-
clientConfiguration.getSslSocketFactory().ifPresent(builder::sslSocketFactory);
110-
clientConfiguration.getHostnameVerifier().ifPresent(builder::hostnameVerifier);
111-
clientConfiguration.getSslParameters().ifPresent(builder::sslParameters);
109+
clientConfiguration.getSslSocketFactory().ifPresent(jedisConfigBuilder::sslSocketFactory);
110+
clientConfiguration.getHostnameVerifier().ifPresent(jedisConfigBuilder::hostnameVerifier);
111+
clientConfiguration.getSslParameters().ifPresent(jedisConfigBuilder::sslParameters);
112112
}
113113

114-
return builder.build();
114+
return jedisConfigBuilder.build();
115115
}
116116
}

redis-om-spring/src/main/java/com/redis/om/spring/convert/MappingRedisOMConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,9 +1308,9 @@ public boolean isPhantomKey() {
13081308
*/
13091309
public static class BinaryKeyspaceIdentifier {
13101310

1311-
public static final byte[] PHANTOM = KeyspaceIdentifier.PHANTOM.getBytes();
1311+
protected static final byte[] PHANTOM = KeyspaceIdentifier.PHANTOM.getBytes();
13121312
public static final byte DELIMITER = ':';
1313-
public static final byte[] PHANTOM_SUFFIX = ByteUtils.concat(new byte[] { DELIMITER }, PHANTOM);
1313+
protected static final byte[] PHANTOM_SUFFIX = ByteUtils.concat(new byte[] { DELIMITER }, PHANTOM);
13141314

13151315
private final byte[] keyspace;
13161316
private final byte[] id;

redis-om-spring/src/main/java/com/redis/om/spring/metamodel/Alias.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public Alias(String alias) {
66
}
77

88
public static Alias of(String alias) {
9-
return new Alias(alias);
9+
return new Alias<>(alias);
1010
}
1111
}

redis-om-spring/src/main/java/com/redis/om/spring/metamodel/MetamodelField.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ public MetamodelField(SearchFieldAccessor searchFieldAccessor, boolean indexed)
1818
this.alias = null;
1919
}
2020

21-
public MetamodelField(String alias, Class targetClass, boolean indexed) {
21+
public MetamodelField(String alias, Class<?> targetClass, boolean indexed) {
2222
this.searchFieldAccessor = null;
2323
this.indexed = indexed;
2424
this.alias = alias;
2525
this.targetClass = targetClass;
2626
}
2727

28-
public MetamodelField(String alias, Class targetClass) {
28+
public MetamodelField(String alias, Class<?> targetClass) {
2929
this.searchFieldAccessor = null;
3030
this.indexed = false;
3131
this.alias = alias;

redis-om-spring/src/main/java/com/redis/om/spring/metamodel/MetamodelGenerator.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,18 @@ void generateMetaModelClass(final Element annotatedElement) throws IOException {
140140

141141
blockBuilder.beginControlFlow("try");
142142
for (ObjectGraphFieldSpec ogfs : fields) {
143-
String sb = "$T.class";
143+
StringBuilder sb = new StringBuilder("$T.class");
144144
for (int i = 0; i < ogfs.chain().size(); i++) {
145145
Element element = ogfs.chain().get(i);
146146
if (i != 0) {
147-
sb = sb + ".getType()";
147+
sb.append(".getType()");
148148
}
149-
sb = String.format("com.redis.om.spring.util.ObjectUtils.getDeclaredFieldTransitively(%s, \"%s\")", sb, element.getSimpleName());
149+
String formattedString = String.format("com.redis.om.spring.util.ObjectUtils.getDeclaredFieldTransitively(%s, \"%s\")", sb.toString(), element.getSimpleName());
150+
sb.setLength(0); // clear the builder
151+
sb.append(formattedString);
150152
}
151153
FieldSpec fieldSpec = ogfs.fieldSpec();
152-
blockBuilder.addStatement("$L = " + sb, fieldSpec.name, entity);
154+
blockBuilder.addStatement("$L = " + sb.toString(), fieldSpec.name, entity);
153155
}
154156

155157
for (CodeBlock initCodeBlock : initCodeBlocks) {
@@ -389,16 +391,18 @@ private Triple<ObjectGraphFieldSpec, FieldSpec, CodeBlock> generateCollectionFie
389391

390392
blockBuilder.beginControlFlow("try");
391393
for (ObjectGraphFieldSpec ogfs : fields) {
392-
String sb = "$T.class";
394+
StringBuilder sb = new StringBuilder("$T.class");
393395
for (int i = 0; i < ogfs.chain().size(); i++) {
394396
Element element = ogfs.chain().get(i);
395397
if (i != 0) {
396-
sb = sb + ".getType()";
398+
sb.append(".getType()");
397399
}
398-
sb = String.format("com.redis.om.spring.util.ObjectUtils.getDeclaredFieldTransitively(%s, \"%s\")", sb, element.getSimpleName());
400+
String formattedString = String.format("com.redis.om.spring.util.ObjectUtils.getDeclaredFieldTransitively(%s, \"%s\")", sb.toString(), element.getSimpleName());
401+
sb.setLength(0); // clear the buffer
402+
sb.append(formattedString);
399403
}
400404
FieldSpec fieldSpec = ogfs.fieldSpec();
401-
blockBuilder.addStatement("$L = " + sb, fieldSpec.name, entity);
405+
blockBuilder.addStatement("$L = " + sb.toString(), fieldSpec.name, entity);
402406
}
403407

404408
for (CodeBlock initCodeBlock : initCodeBlocks) {
@@ -710,7 +714,7 @@ private Triple<ObjectGraphFieldSpec, FieldSpec, CodeBlock> generateFieldMetamode
710714
}
711715

712716

713-
private Pair<FieldSpec, CodeBlock> generateUnboundMetamodelField(TypeName entity, String name, String alias, Class type) {
717+
private Pair<FieldSpec, CodeBlock> generateUnboundMetamodelField(TypeName entity, String name, String alias, Class<?> type) {
714718
TypeName interceptor = ParameterizedTypeName.get(ClassName.get(MetamodelField.class), entity, TypeName.get(type));
715719

716720
FieldSpec aField = FieldSpec.builder(interceptor, name).addModifiers(Modifier.PUBLIC, Modifier.STATIC)

0 commit comments

Comments
 (0)