Skip to content

Commit 409d2a8

Browse files
committed
Polishing.
Tweak naming. Simplify converters by removing unused methods. See #2754
1 parent 5dd43c1 commit 409d2a8

11 files changed

+35
-103
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java

+4-23
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,7 @@ public static Map<byte[], Double> toTupleMap(Set<Tuple> tuples) {
160160
return args;
161161
}
162162

163-
public static byte[] toBytes(Integer source) {
164-
return String.valueOf(source).getBytes();
165-
}
166-
167-
public static byte[] toBytes(Long source) {
168-
return String.valueOf(source).getBytes();
169-
}
170-
171-
/**
172-
* @since 1.6
173-
*/
174-
public static byte[] toBytes(Double source) {
163+
public static byte[] toBytes(Number source) {
175164
return toBytes(String.valueOf(source));
176165
}
177166

@@ -185,10 +174,6 @@ public static String toString(@Nullable byte[] source) {
185174
return source == null ? null : SafeEncoder.encode(source);
186175
}
187176

188-
public static Long toLong(byte[] source) {
189-
return Long.valueOf(toString(source));
190-
}
191-
192177
/**
193178
* Convert the given {@code source} value to the corresponding {@link ValueEncoding}.
194179
*
@@ -451,16 +436,12 @@ private static byte[] boundaryToBytes(org.springframework.data.domain.Range.Boun
451436
byte[] exclPrefix) {
452437

453438
byte[] prefix = boundary.isInclusive() ? inclPrefix : exclPrefix;
454-
byte[] value = null;
439+
byte[] value;
455440
Object theValue = boundary.getValue().get();
456441
if (theValue instanceof byte[] bytes) {
457442
value = bytes;
458-
} else if (theValue instanceof Double doubleValue) {
459-
value = toBytes(doubleValue);
460-
} else if (theValue instanceof Long longValue) {
461-
value = toBytes(longValue);
462-
} else if (theValue instanceof Integer integer) {
463-
value = toBytes(integer);
443+
} else if (theValue instanceof Number number) {
444+
value = toBytes(number);
464445
} else if (theValue instanceof String string) {
465446
value = toBytes(string);
466447
} else {

src/main/java/org/springframework/data/redis/connection/jedis/JedisExceptionConverter.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,25 @@ public class JedisExceptionConverter implements Converter<Exception, DataAccessE
4545

4646
public DataAccessException convert(Exception ex) {
4747

48-
if (ex instanceof DataAccessException dataAccessException) {
49-
return dataAccessException;
50-
}
51-
52-
if (ex instanceof UnsupportedOperationException) {
53-
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
48+
if (ex instanceof DataAccessException dae) {
49+
return dae;
5450
}
5551

5652
if (ex instanceof JedisClusterOperationException && "No more cluster attempts left".equals(ex.getMessage())) {
5753
return new TooManyClusterRedirectionsException(ex.getMessage(), ex);
5854
}
5955

60-
if (ex instanceof JedisRedirectionException redirectionException) {
56+
if (ex instanceof JedisRedirectionException rex) {
6157

62-
return new ClusterRedirectException(redirectionException.getSlot(),
63-
redirectionException.getTargetNode().getHost(), redirectionException.getTargetNode().getPort(), ex);
58+
return new ClusterRedirectException(rex.getSlot(), rex.getTargetNode().getHost(), rex.getTargetNode().getPort(),
59+
ex);
6460
}
6561

6662
if (ex instanceof JedisConnectionException) {
6763
return new RedisConnectionFailureException(ex.getMessage(), ex);
6864
}
6965

70-
if (ex instanceof JedisException) {
66+
if (ex instanceof JedisException || ex instanceof UnsupportedOperationException) {
7167
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
7268
}
7369

src/main/java/org/springframework/data/redis/connection/jedis/JedisScriptReturnConverter.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.redis.connection.jedis;
1717

18-
import redis.clients.jedis.util.SafeEncoder;
19-
2018
import java.util.ArrayList;
2119
import java.util.List;
2220

@@ -41,7 +39,7 @@ public JedisScriptReturnConverter(ReturnType returnType) {
4139
public Object convert(@Nullable Object result) {
4240
if (result instanceof String stringResult) {
4341
// evalsha converts byte[] to String. Convert back for consistency
44-
return SafeEncoder.encode(stringResult);
42+
return JedisConverters.toBytes(stringResult);
4543
}
4644
if (returnType == ReturnType.STATUS) {
4745
return JedisConverters.toString((byte[]) result);
@@ -60,7 +58,7 @@ public Object convert(@Nullable Object result) {
6058
if (res instanceof String stringResult) {
6159
// evalsha converts byte[] to String. Convert back for
6260
// consistency
63-
convertedResults.add(SafeEncoder.encode(stringResult));
61+
convertedResults.add(JedisConverters.toBytes(stringResult));
6462
} else {
6563
convertedResults.add(res);
6664
}

src/main/java/org/springframework/data/redis/connection/jedis/StreamConverters.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import redis.clients.jedis.params.XReadParams;
2525
import redis.clients.jedis.resps.StreamEntry;
2626
import redis.clients.jedis.resps.StreamPendingEntry;
27-
import redis.clients.jedis.util.SafeEncoder;
2827

2928
import java.time.Duration;
3029
import java.util.ArrayList;
@@ -132,7 +131,7 @@ static List<ByteRecord> convertToByteRecord(byte[] key, Object source) {
132131
continue;
133132
}
134133

135-
String entryIdString = SafeEncoder.encode((byte[]) res.get(0));
134+
String entryIdString = JedisConverters.toString((byte[]) res.get(0));
136135
List<byte[]> hash = (List<byte[]>) res.get(1);
137136

138137
Iterator<byte[]> hashIterator = hash.iterator();
@@ -163,19 +162,19 @@ static PendingMessagesSummary toPendingMessagesSummary(String groupName, Object
163162
List<Object> objectList = (List<Object>) source;
164163
long total = BuilderFactory.LONG.build(objectList.get(0));
165164
Range.Bound<String> lower = objectList.get(1) != null
166-
? Range.Bound.inclusive(SafeEncoder.encode((byte[]) objectList.get(1)))
165+
? Range.Bound.inclusive(JedisConverters.toString((byte[]) objectList.get(1)))
167166
: Range.Bound.unbounded();
168167
Range.Bound<String> upper = objectList.get(2) != null
169-
? Range.Bound.inclusive(SafeEncoder.encode((byte[]) objectList.get(2)))
168+
? Range.Bound.inclusive(JedisConverters.toString((byte[]) objectList.get(2)))
170169
: Range.Bound.unbounded();
171170
List<List<Object>> consumerObjList = (List<List<Object>>) objectList.get(3);
172171
Map<String, Long> map;
173172

174173
if (consumerObjList != null) {
175174
map = new HashMap<>(consumerObjList.size());
176175
for (List<Object> consumerObj : consumerObjList) {
177-
map.put(SafeEncoder.encode((byte[]) consumerObj.get(0)),
178-
Long.parseLong(SafeEncoder.encode((byte[]) consumerObj.get(1))));
176+
map.put(JedisConverters.toString((byte[]) consumerObj.get(0)),
177+
Long.parseLong(JedisConverters.toString((byte[]) consumerObj.get(1))));
179178
}
180179
} else {
181180
map = Collections.emptyMap();
@@ -185,7 +184,7 @@ static PendingMessagesSummary toPendingMessagesSummary(String groupName, Object
185184
}
186185

187186
/**
188-
* Convert the raw Jedis xpending result to {@link PendingMessages}.
187+
* Convert the raw Jedis {@code xpending} result to {@link PendingMessages}.
189188
*
190189
* @param groupName the group name
191190
* @param range the range of messages requested

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceExceptionConverter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public DataAccessException convert(Exception ex) {
5252
return new RedisSystemException("Error in execution", ex);
5353
}
5454

55-
if (ex instanceof DataAccessException dataAccessException) {
56-
return dataAccessException;
55+
if (ex instanceof DataAccessException dae) {
56+
return dae;
5757
}
5858

5959
if (ex instanceof RedisCommandInterruptedException) {

src/main/java/org/springframework/data/redis/connection/lettuce/StreamConverters.java

+1-42
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import io.lettuce.core.models.stream.PendingMessage;
2222
import io.lettuce.core.models.stream.PendingMessages;
2323

24-
import java.nio.ByteBuffer;
2524
import java.time.Duration;
26-
import java.util.ArrayList;
2725
import java.util.List;
2826

2927
import org.springframework.core.convert.converter.Converter;
@@ -34,9 +32,6 @@
3432
import org.springframework.data.redis.connection.stream.RecordId;
3533
import org.springframework.data.redis.connection.stream.StreamReadOptions;
3634
import org.springframework.data.redis.connection.stream.StreamRecords;
37-
import org.springframework.data.redis.util.ByteUtils;
38-
import org.springframework.lang.Nullable;
39-
import org.springframework.util.NumberUtils;
4035

4136
/**
4237
* Converters for Redis Stream-specific types.
@@ -76,10 +71,6 @@ static Converter<StreamMessage<byte[], byte[]>, ByteRecord> byteRecordConverter(
7671
return (it) -> StreamRecords.newRecord().in(it.getStream()).withId(it.getId()).ofBytes(it.getBody());
7772
}
7873

79-
static Converter<StreamMessage<byte[], byte[]>, RecordId> messageToIdConverter() {
80-
return (it) -> RecordId.of(it.getId());
81-
}
82-
8374
/**
8475
* Convert the raw Lettuce xpending result to {@link PendingMessages}.
8576
*
@@ -106,7 +97,7 @@ static org.springframework.data.redis.connection.stream.PendingMessages toPendin
10697
}
10798

10899
/**
109-
* Convert the raw Lettuce xpending result to {@link PendingMessagesSummary}.
100+
* Convert the raw Lettuce {@code xpending} result to {@link PendingMessagesSummary}.
110101
*
111102
* @param groupName
112103
* @param source the raw lettuce response.
@@ -123,38 +114,6 @@ static PendingMessagesSummary toPendingMessagesInfo(String groupName, PendingMes
123114
return new PendingMessagesSummary(groupName, source.getCount(), range, source.getConsumerMessageCount());
124115
}
125116

126-
/**
127-
* We need to convert values into the correct target type since lettuce will give us {@link ByteBuffer} or arrays but
128-
* the parser requires us to have them as {@link String} or numeric values. Oh and {@literal null} values aren't real
129-
* good citizens as well, so we make them empty strings instead - see it works - somehow ;P
130-
*
131-
* @param value dont't get me started om this.
132-
* @return preconverted values that Lettuce parsers are able to understand \ö/.
133-
*/
134-
private static Object preConvertNativeValues(@Nullable Object value) {
135-
136-
if (value instanceof ByteBuffer || value instanceof byte[]) {
137-
138-
byte[] targetArray = value instanceof ByteBuffer byteBuffer ? ByteUtils.getBytes(byteBuffer) : (byte[]) value;
139-
String tmp = LettuceConverters.toString(targetArray);
140-
141-
try {
142-
return NumberUtils.parseNumber(tmp, Long.class);
143-
} catch (NumberFormatException ex) {
144-
return tmp;
145-
}
146-
}
147-
if (value instanceof List listValue) {
148-
List<Object> targetList = new ArrayList<>();
149-
for (Object it : listValue) {
150-
targetList.add(preConvertNativeValues(it));
151-
}
152-
return targetList;
153-
}
154-
155-
return value != null ? value : "";
156-
}
157-
158117
/**
159118
* {@link Converter} to convert {@link StreamReadOptions} to Lettuce's {@link XReadArgs}.
160119
*/

src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public ByteArrayWrapper(byte[] array) {
4141
}
4242

4343
public boolean equals(@Nullable Object obj) {
44-
if (obj instanceof ByteArrayWrapper byteArrayWrapper) {
45-
return Arrays.equals(array, byteArrayWrapper.array);
44+
if (obj instanceof ByteArrayWrapper other) {
45+
return Arrays.equals(array, other.array);
4646
}
4747

4848
return false;

src/main/java/org/springframework/data/redis/core/IndexWriter.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ protected void addKeyToIndex(byte[] key, IndexedData indexedData) {
208208
return;
209209
}
210210

211-
if (indexedData instanceof SimpleIndexedPropertyValue simpleIndexedData) {
211+
if (indexedData instanceof SimpleIndexedPropertyValue propertyValue) {
212212

213-
Object value = simpleIndexedData.getValue();
213+
Object value = propertyValue.getValue();
214214

215215
if (value == null) {
216216
return;
@@ -222,15 +222,15 @@ protected void addKeyToIndex(byte[] key, IndexedData indexedData) {
222222

223223
// keep track of indexes used for the object
224224
connection.sAdd(ByteUtils.concatAll(toBytes(indexedData.getKeyspace() + ":"), key, toBytes(":idx")), indexKey);
225-
} else if (indexedData instanceof GeoIndexedPropertyValue geoIndexedData) {
225+
} else if (indexedData instanceof GeoIndexedPropertyValue propertyValue) {
226226

227-
Object value = geoIndexedData.getValue();
227+
Object value = propertyValue.getValue();
228228
if (value == null) {
229229
return;
230230
}
231231

232232
byte[] indexKey = toBytes(indexedData.getKeyspace() + ":" + indexedData.getIndexName());
233-
connection.geoAdd(indexKey, geoIndexedData.getPoint(), key);
233+
connection.geoAdd(indexKey, propertyValue.getPoint(), key);
234234

235235
// keep track of indexes used for the object
236236
connection.sAdd(ByteUtils.concatAll(toBytes(indexedData.getKeyspace() + ":"), key, toBytes(":idx")), indexKey);
@@ -264,7 +264,7 @@ private byte[] toBytes(@Nullable Object source) {
264264
* @author Christoph Strobl
265265
* @since 1.8
266266
*/
267-
private static enum IndexWriteMode {
267+
private enum IndexWriteMode {
268268

269269
CREATE, UPDATE, PARTIAL_UPDATE
270270
}

src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.aopalliance.intercept.MethodInvocation;
2323
import org.apache.commons.logging.Log;
2424
import org.apache.commons.logging.LogFactory;
25-
2625
import org.springframework.aop.RawTargetAccess;
2726
import org.springframework.aop.framework.ProxyFactory;
2827
import org.springframework.dao.DataAccessException;
@@ -305,8 +304,8 @@ private static RedisConnection getTargetConnection(RedisConnection connection) {
305304

306305
RedisConnection connectionToUse = connection;
307306

308-
while (connectionToUse instanceof RedisConnectionProxy redisConnectionProxy) {
309-
connectionToUse = redisConnectionProxy.getTargetConnection();
307+
while (connectionToUse instanceof RedisConnectionProxy proxy) {
308+
connectionToUse = proxy.getTargetConnection();
310309
}
311310

312311
return connectionToUse;

src/main/java/org/springframework/data/redis/core/convert/SpelIndexResolver.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ public Set<IndexedData> resolveIndexesFor(TypeInformation<?> typeInformation, @N
9393

9494
for (IndexDefinition setting : settings.getIndexDefinitionsFor(keyspace)) {
9595

96-
if (setting instanceof SpelIndexDefinition spelIndexDefinition) {
96+
if (setting instanceof SpelIndexDefinition spel) {
9797

98-
Expression expression = getAndCacheIfAbsent(spelIndexDefinition);
98+
Expression expression = getAndCacheIfAbsent(spel);
9999

100100
StandardEvaluationContext context = new StandardEvaluationContext();
101101
context.setRootObject(value);

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ public E moveLastTo(RedisList<E> destination, RedisListCommands.Direction destin
166166
}
167167

168168
private void potentiallyCap(RedisList<E> destination) {
169-
if (destination instanceof DefaultRedisList<?> defaultRedisList) {
170-
defaultRedisList.cap();
169+
if (destination instanceof DefaultRedisList<?> redisList) {
170+
redisList.cap();
171171
}
172172
}
173173

0 commit comments

Comments
 (0)