Skip to content

Commit 4c88fe1

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-659 - Polishing.
Replace anonymous inner classes with lambdas. Reformat. Original Pull Request: #253
1 parent 1f2610b commit 4c88fe1

File tree

1 file changed

+94
-142
lines changed

1 file changed

+94
-142
lines changed

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

Lines changed: 94 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -106,126 +106,101 @@ abstract public class LettuceConverters extends Converters {
106106
public static final byte[] NEGATIVE_INFINITY_BYTES;
107107

108108
static {
109-
DATE_TO_LONG = new Converter<Date, Long>() {
110-
public Long convert(Date source) {
111-
return source != null ? source.getTime() : null;
112-
}
113-
};
114-
BYTES_LIST_TO_BYTES_SET = new Converter<List<byte[]>, Set<byte[]>>() {
115-
public Set<byte[]> convert(List<byte[]> results) {
116-
return results != null ? new LinkedHashSet<>(results) : null;
117-
}
118-
};
119-
BYTES_TO_STRING = new Converter<byte[], String>() {
120109

121-
@Override
122-
public String convert(byte[] source) {
123-
if (source == null || Arrays.equals(source, new byte[0])) {
124-
return null;
125-
}
126-
return new String(source);
110+
DATE_TO_LONG = source -> source != null ? source.getTime() : null;
111+
112+
BYTES_LIST_TO_BYTES_SET = results -> results != null ? new LinkedHashSet<>(results) : null;
113+
114+
BYTES_TO_STRING = source -> {
115+
if (source == null || Arrays.equals(source, new byte[0])) {
116+
return null;
127117
}
118+
return new String(source);
128119
};
129-
STRING_TO_BYTES = new Converter<String, byte[]>() {
130120

131-
@Override
132-
public byte[] convert(String source) {
133-
if (source == null) {
134-
return null;
135-
}
136-
return source.getBytes();
121+
STRING_TO_BYTES = source -> {
122+
if (source == null) {
123+
return null;
137124
}
125+
return source.getBytes();
138126
};
139-
BYTES_SET_TO_BYTES_LIST = new Converter<Set<byte[]>, List<byte[]>>() {
140-
public List<byte[]> convert(Set<byte[]> results) {
141-
return results != null ? new ArrayList<>(results) : null;
127+
128+
BYTES_SET_TO_BYTES_LIST = results -> results != null ? new ArrayList<>(results) : null;
129+
130+
BYTES_COLLECTION_TO_BYTES_LIST = results -> {
131+
132+
if (results instanceof List) {
133+
return (List<byte[]>) results;
142134
}
135+
return results != null ? new ArrayList<>(results) : null;
143136
};
144-
BYTES_COLLECTION_TO_BYTES_LIST = new Converter<Collection<byte[]>, List<byte[]>>() {
145-
public List<byte[]> convert(Collection<byte[]> results) {
146-
if (results instanceof List) {
147-
return (List<byte[]>) results;
148-
}
149-
return results != null ? new ArrayList<>(results) : null;
137+
138+
KEY_VALUE_TO_BYTES_LIST = source -> {
139+
140+
if (source == null) {
141+
return null;
150142
}
143+
List<byte[]> list = new ArrayList<>(2);
144+
list.add(source.getKey());
145+
list.add(source.getValue());
146+
147+
return list;
151148
};
152-
KEY_VALUE_TO_BYTES_LIST = new Converter<KeyValue<byte[], byte[]>, List<byte[]>>() {
153-
public List<byte[]> convert(KeyValue<byte[], byte[]> source) {
154-
if (source == null) {
155-
return null;
156-
}
157-
List<byte[]> list = new ArrayList<>(2);
158-
list.add(source.getKey());
159-
list.add(source.getValue());
160-
return list;
149+
BYTES_LIST_TO_MAP = source -> {
150+
151+
if (CollectionUtils.isEmpty(source)) {
152+
return Collections.emptyMap();
161153
}
162-
};
163-
BYTES_LIST_TO_MAP = new Converter<List<byte[]>, Map<byte[], byte[]>>() {
164154

165-
@Override
166-
public Map<byte[], byte[]> convert(final List<byte[]> source) {
155+
Map<byte[], byte[]> target = new LinkedHashMap<>();
167156

168-
if (CollectionUtils.isEmpty(source)) {
169-
return Collections.emptyMap();
170-
}
157+
Iterator<byte[]> kv = source.iterator();
158+
while (kv.hasNext()) {
159+
target.put(kv.next(), kv.hasNext() ? kv.next() : null);
160+
}
171161

172-
Map<byte[], byte[]> target = new LinkedHashMap<>();
162+
return target;
163+
};
173164

174-
Iterator<byte[]> kv = source.iterator();
175-
while (kv.hasNext()) {
176-
target.put(kv.next(), kv.hasNext() ? kv.next() : null);
177-
}
165+
SCORED_VALUES_TO_TUPLE_SET = source -> {
178166

179-
return target;
167+
if (source == null) {
168+
return null;
180169
}
181-
};
182-
SCORED_VALUES_TO_TUPLE_SET = new Converter<List<ScoredValue<byte[]>>, Set<Tuple>>() {
183-
public Set<Tuple> convert(List<ScoredValue<byte[]>> source) {
184-
if (source == null) {
185-
return null;
186-
}
187-
Set<Tuple> tuples = new LinkedHashSet<>(source.size());
188-
for (ScoredValue<byte[]> value : source) {
189-
tuples.add(LettuceConverters.toTuple(value));
190-
}
191-
return tuples;
170+
Set<Tuple> tuples = new LinkedHashSet<>(source.size());
171+
for (ScoredValue<byte[]> value : source) {
172+
tuples.add(LettuceConverters.toTuple(value));
192173
}
174+
return tuples;
193175
};
194176

195-
SCORED_VALUES_TO_TUPLE_LIST = new Converter<List<ScoredValue<byte[]>>, List<Tuple>>() {
196-
public List<Tuple> convert(List<ScoredValue<byte[]>> source) {
197-
if (source == null) {
198-
return null;
199-
}
200-
List<Tuple> tuples = new ArrayList<>(source.size());
201-
for (ScoredValue<byte[]> value : source) {
202-
tuples.add(LettuceConverters.toTuple(value));
203-
}
204-
return tuples;
177+
SCORED_VALUES_TO_TUPLE_LIST = source -> {
178+
179+
if (source == null) {
180+
return null;
205181
}
206-
};
207-
SCORED_VALUE_TO_TUPLE = new Converter<ScoredValue<byte[]>, Tuple>() {
208-
public Tuple convert(ScoredValue<byte[]> source) {
209-
return source != null ? new DefaultTuple(source.getValue(), Double.valueOf(source.getScore())) : null;
182+
List<Tuple> tuples = new ArrayList<>(source.size());
183+
for (ScoredValue<byte[]> value : source) {
184+
tuples.add(LettuceConverters.toTuple(value));
210185
}
186+
return tuples;
211187
};
212-
BYTES_LIST_TO_TUPLE_LIST_CONVERTER = new Converter<List<byte[]>, List<Tuple>>() {
213188

214-
@Override
215-
public List<Tuple> convert(List<byte[]> source) {
189+
SCORED_VALUE_TO_TUPLE = source -> source != null
190+
? new DefaultTuple(source.getValue(), Double.valueOf(source.getScore())) : null;
216191

217-
if (CollectionUtils.isEmpty(source)) {
218-
return Collections.emptyList();
219-
}
192+
BYTES_LIST_TO_TUPLE_LIST_CONVERTER = source -> {
220193

221-
List<Tuple> tuples = new ArrayList<>();
222-
Iterator<byte[]> it = source.iterator();
223-
while (it.hasNext()) {
224-
tuples.add(
225-
new DefaultTuple(it.next(), it.hasNext() ? Double.valueOf(LettuceConverters.toString(it.next())) : null));
226-
}
227-
return tuples;
194+
if (CollectionUtils.isEmpty(source)) {
195+
return Collections.emptyList();
196+
}
197+
198+
List<Tuple> tuples = new ArrayList<>();
199+
Iterator<byte[]> it = source.iterator();
200+
while (it.hasNext()) {
201+
tuples.add(new DefaultTuple(it.next(), it.hasNext() ? Double.valueOf(toString(it.next())) : null));
228202
}
203+
return tuples;
229204
};
230205

231206
PARTITIONS_TO_CLUSTER_NODES = new Converter<Partitions, List<RedisClusterNode>>() {
@@ -301,45 +276,24 @@ private Set<Flag> parseFlags(Set<NodeFlag> source) {
301276
POSITIVE_INFINITY_BYTES = toBytes("+inf");
302277
NEGATIVE_INFINITY_BYTES = toBytes("-inf");
303278

304-
BYTES_LIST_TO_TIME_CONVERTER = new Converter<List<byte[]>, Long>() {
279+
BYTES_LIST_TO_TIME_CONVERTER = source -> {
305280

306-
@Override
307-
public Long convert(List<byte[]> source) {
281+
Assert.notEmpty(source, "Received invalid result from server. Expected 2 items in collection.");
282+
Assert.isTrue(source.size() == 2,
283+
"Received invalid nr of arguments from redis server. Expected 2 received " + source.size());
308284

309-
Assert.notEmpty(source, "Received invalid result from server. Expected 2 items in collection.");
310-
Assert.isTrue(source.size() == 2,
311-
"Received invalid nr of arguments from redis server. Expected 2 received " + source.size());
312-
313-
return toTimeMillis(LettuceConverters.toString(source.get(0)), LettuceConverters.toString(source.get(1)));
314-
}
285+
return toTimeMillis(toString(source.get(0)), toString(source.get(1)));
315286
};
316287

317-
GEO_COORDINATE_TO_POINT_CONVERTER = new Converter<io.lettuce.core.GeoCoordinates, Point>() {
318-
@Override
319-
public Point convert(io.lettuce.core.GeoCoordinates geoCoordinate) {
320-
return geoCoordinate != null ? new Point(geoCoordinate.getX().doubleValue(), geoCoordinate.getY().doubleValue())
321-
: null;
322-
}
323-
};
288+
GEO_COORDINATE_TO_POINT_CONVERTER = geoCoordinate -> geoCoordinate != null
289+
? new Point(geoCoordinate.getX().doubleValue(), geoCoordinate.getY().doubleValue()) : null;
324290
GEO_COORDINATE_LIST_TO_POINT_LIST_CONVERTER = new ListConverter<>(GEO_COORDINATE_TO_POINT_CONVERTER);
325291

326-
KEY_VALUE_UNWRAPPER = new Converter<KeyValue<Object, Object>, Object>() {
327-
328-
@Override
329-
public Object convert(KeyValue<Object, Object> source) {
330-
return source.getValueOrElse(null);
331-
}
332-
};
292+
KEY_VALUE_UNWRAPPER = source -> source.getValueOrElse(null);
333293

334294
KEY_VALUE_LIST_UNWRAPPER = new ListConverter<>(KEY_VALUE_UNWRAPPER);
335295

336-
TRANSACTION_RESULT_UNWRAPPER = new Converter<TransactionResult, List<Object>>() {
337-
338-
@Override
339-
public List<Object> convert(TransactionResult transactionResult) {
340-
return transactionResult.stream().collect(Collectors.toList());
341-
}
342-
};
296+
TRANSACTION_RESULT_UNWRAPPER = transactionResult -> transactionResult.stream().collect(Collectors.toList());
343297
}
344298

345299
public static List<Tuple> toTuple(List<byte[]> list) {
@@ -355,16 +309,14 @@ public static Point geoCoordinatesToPoint(GeoCoordinates geoCoordinates) {
355309
}
356310

357311
public static Converter<String, List<RedisClientInfo>> stringToRedisClientListConverter() {
358-
return new Converter<String, List<RedisClientInfo>>() {
359312

360-
@Override
361-
public List<RedisClientInfo> convert(String source) {
362-
if (!StringUtils.hasText(source)) {
363-
return Collections.emptyList();
364-
}
313+
return source -> {
365314

366-
return STRING_TO_LIST_OF_CLIENT_INFO.convert(source.split("\\r?\\n"));
315+
if (!StringUtils.hasText(source)) {
316+
return Collections.emptyList();
367317
}
318+
319+
return STRING_TO_LIST_OF_CLIENT_INFO.convert(source.split("\\r?\\n"));
368320
};
369321
}
370322

@@ -445,6 +397,7 @@ public static String toString(byte[] source) {
445397
}
446398

447399
public static ScriptOutputType toScriptOutputType(ReturnType returnType) {
400+
448401
switch (returnType) {
449402
case BOOLEAN:
450403
return ScriptOutputType.BOOLEAN;
@@ -479,6 +432,7 @@ public static Converter<List<byte[]>, Map<byte[], byte[]>> bytesListToMapConvert
479432
}
480433

481434
public static SortArgs toSortArgs(SortParameters params) {
435+
482436
SortArgs args = new SortArgs();
483437
if (params == null) {
484438
return args;
@@ -650,6 +604,7 @@ public static List<RedisServer> toListOfRedisServer(List<Map<String, String>> so
650604
* @since 1.5
651605
*/
652606
public static RedisURI sentinelConfigurationToRedisURI(RedisSentinelConfiguration sentinelConfiguration) {
607+
653608
Assert.notNull(sentinelConfiguration, "RedisSentinelConfiguration is required");
654609

655610
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
@@ -884,22 +839,19 @@ public static Properties toProperties(List<String> input) {
884839
* @since 1.8
885840
*/
886841
public static Converter<Set<byte[]>, GeoResults<GeoLocation<byte[]>>> bytesSetToGeoResultsConverter() {
887-
return new Converter<Set<byte[]>, GeoResults<GeoLocation<byte[]>>>() {
888842

889-
@Override
890-
public GeoResults<GeoLocation<byte[]>> convert(Set<byte[]> source) {
843+
return source -> {
891844

892-
if (CollectionUtils.isEmpty(source)) {
893-
return new GeoResults<>(Collections.<GeoResult<GeoLocation<byte[]>>> emptyList());
894-
}
845+
if (CollectionUtils.isEmpty(source)) {
846+
return new GeoResults<>(Collections.<GeoResult<GeoLocation<byte[]>>> emptyList());
847+
}
895848

896-
List<GeoResult<GeoLocation<byte[]>>> results = new ArrayList<>(source.size());
897-
Iterator<byte[]> it = source.iterator();
898-
while (it.hasNext()) {
899-
results.add(new GeoResult<>(new GeoLocation<>(it.next(), null), new Distance(0D)));
900-
}
901-
return new GeoResults<>(results);
849+
List<GeoResult<GeoLocation<byte[]>>> results = new ArrayList<>(source.size());
850+
Iterator<byte[]> it = source.iterator();
851+
while (it.hasNext()) {
852+
results.add(new GeoResult<>(new GeoLocation<>(it.next(), null), new Distance(0D)));
902853
}
854+
return new GeoResults<>(results);
903855
};
904856
}
905857

0 commit comments

Comments
 (0)