Skip to content

Commit 42f2184

Browse files
committed
Polish for RedisSerializationContext.
Closes #2651 Original pull request: #2652
1 parent 675c617 commit 42f2184

File tree

3 files changed

+97
-75
lines changed

3 files changed

+97
-75
lines changed

src/main/java/org/springframework/data/redis/serializer/DefaultRedisSerializationContext.java

+21-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @author Mark Paluch
2525
* @author Christoph Strobl
2626
* @author Shyngys Sapraliyev
27-
* @author Zhou KQ
27+
* @author John Blum
2828
* @since 2.0
2929
*/
3030
class DefaultRedisSerializationContext<K, V> implements RedisSerializationContext<K, V> {
@@ -36,7 +36,8 @@ class DefaultRedisSerializationContext<K, V> implements RedisSerializationContex
3636
private final SerializationPair<String> stringTuple;
3737

3838
private DefaultRedisSerializationContext(SerializationPair<K> keyTuple, SerializationPair<V> valueTuple,
39-
SerializationPair<?> hashKeyTuple, SerializationPair<?> hashValueTuple, SerializationPair<String> stringTuple) {
39+
SerializationPair<?> hashKeyTuple, SerializationPair<?> hashValueTuple,
40+
SerializationPair<String> stringTuple) {
4041

4142
this.keyTuple = keyTuple;
4243
this.valueTuple = valueTuple;
@@ -47,36 +48,37 @@ private DefaultRedisSerializationContext(SerializationPair<K> keyTuple, Serializ
4748

4849
@Override
4950
public SerializationPair<K> getKeySerializationPair() {
50-
return keyTuple;
51+
return this.keyTuple;
5152
}
5253

5354
@Override
5455
public SerializationPair<V> getValueSerializationPair() {
55-
return valueTuple;
56+
return this.valueTuple;
5657
}
5758

5859
@Override
5960
@SuppressWarnings("unchecked")
6061
public <HK> SerializationPair<HK> getHashKeySerializationPair() {
61-
return (SerializationPair) hashKeyTuple;
62+
return (SerializationPair<HK>) this.hashKeyTuple;
6263
}
6364

6465
@Override
6566
@SuppressWarnings("unchecked")
6667
public <HV> SerializationPair<HV> getHashValueSerializationPair() {
67-
return (SerializationPair) hashValueTuple;
68+
return (SerializationPair<HV>) this.hashValueTuple;
6869
}
6970

7071
@Override
7172
public SerializationPair<String> getStringSerializationPair() {
72-
return stringTuple;
73+
return this.stringTuple;
7374
}
7475

7576
/**
7677
* Default implementation of {@link RedisSerializationContextBuilder}.
7778
*
7879
* @author Mark Paluch
7980
* @author Christoph Strobl
81+
* @author Zhou KQ
8082
* @since 2.0
8183
*/
8284
static class DefaultRedisSerializationContextBuilder<K, V> implements RedisSerializationContextBuilder<K, V> {
@@ -85,6 +87,7 @@ static class DefaultRedisSerializationContextBuilder<K, V> implements RedisSeria
8587
private @Nullable SerializationPair<V> valueTuple;
8688
private @Nullable SerializationPair<?> hashKeyTuple;
8789
private @Nullable SerializationPair<?> hashValueTuple;
90+
8891
private SerializationPair<String> stringTuple = SerializationPair.fromSerializer(RedisSerializer.string());
8992

9093
@Override
@@ -93,6 +96,7 @@ public RedisSerializationContextBuilder<K, V> key(SerializationPair<K> tuple) {
9396
Assert.notNull(tuple, "SerializationPair must not be null");
9497

9598
this.keyTuple = tuple;
99+
96100
return this;
97101
}
98102

@@ -102,6 +106,7 @@ public RedisSerializationContextBuilder<K, V> value(SerializationPair<V> tuple)
102106
Assert.notNull(tuple, "SerializationPair must not be null");
103107

104108
this.valueTuple = tuple;
109+
105110
return this;
106111
}
107112

@@ -111,6 +116,7 @@ public RedisSerializationContextBuilder<K, V> hashKey(SerializationPair<?> tuple
111116
Assert.notNull(tuple, "SerializationPair must not be null");
112117

113118
this.hashKeyTuple = tuple;
119+
114120
return this;
115121
}
116122

@@ -120,6 +126,7 @@ public RedisSerializationContextBuilder<K, V> hashValue(SerializationPair<?> tup
120126
Assert.notNull(tuple, "SerializationPair must not be null");
121127

122128
this.hashValueTuple = tuple;
129+
123130
return this;
124131
}
125132

@@ -129,19 +136,20 @@ public RedisSerializationContextBuilder<K, V> string(SerializationPair<String> t
129136
Assert.notNull(tuple, "SerializationPair must not be null");
130137

131138
this.stringTuple = tuple;
139+
132140
return this;
133141
}
134142

135143
@Override
136144
public RedisSerializationContext<K, V> build() {
137145

138-
Assert.notNull(keyTuple, "Key SerializationPair must not be null!");
139-
Assert.notNull(valueTuple, "Value SerializationPair must not be null!");
140-
Assert.notNull(hashKeyTuple, "HashKey SerializationPair must not be null!");
141-
Assert.notNull(hashValueTuple, "HashValue SerializationPair must not be null!");
146+
Assert.notNull(this.keyTuple, "Key SerializationPair must not be null");
147+
Assert.notNull(this.valueTuple, "Value SerializationPair must not be null");
148+
Assert.notNull(this.hashKeyTuple, "HashKey SerializationPair must not be null");
149+
Assert.notNull(this.hashValueTuple, "HashValue SerializationPair must not be null");
142150

143-
return new DefaultRedisSerializationContext<>(keyTuple, valueTuple, hashKeyTuple, hashValueTuple,
144-
stringTuple);
151+
return new DefaultRedisSerializationContext<>(this.keyTuple, this.valueTuple,
152+
this.hashKeyTuple, this.hashValueTuple, this.stringTuple);
145153
}
146154
}
147155
}

src/main/java/org/springframework/data/redis/serializer/RedisSerializationContext.java

+37-23
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*
2828
* @author Mark Paluch
2929
* @author Christoph Strobl
30+
* @author John Blum
3031
* @since 2.0
3132
* @see RedisElementWriter
3233
* @see RedisElementReader
@@ -40,9 +41,8 @@ public interface RedisSerializationContext<K, V> {
4041
* @param <V> expected value type.
4142
* @return a new {@link RedisSerializationContextBuilder}.
4243
*/
43-
@SuppressWarnings("unchecked")
4444
static <K, V> RedisSerializationContextBuilder<K, V> newSerializationContext() {
45-
return new DefaultRedisSerializationContext.DefaultRedisSerializationContextBuilder();
45+
return new DefaultRedisSerializationContext.DefaultRedisSerializationContextBuilder<>();
4646
}
4747

4848
/**
@@ -68,7 +68,7 @@ static <K, V> RedisSerializationContextBuilder<K, V> newSerializationContext(Red
6868
* @param <V> expected value type.
6969
* @return a new {@link RedisSerializationContextBuilder}.
7070
*/
71-
@SuppressWarnings("unchecked")
71+
@SuppressWarnings({ "rawtypes", "unchecked" })
7272
static <K, V> RedisSerializationContextBuilder<K, V> newSerializationContext(SerializationPair<?> serializationPair) {
7373

7474
Assert.notNull(serializationPair, "SerializationPair must not be null");
@@ -114,7 +114,7 @@ static RedisSerializationContext<ByteBuffer, ByteBuffer> byteBuffer() {
114114
/**
115115
* Creates a new {@link RedisSerializationContext} using a {@link JdkSerializationRedisSerializer}.
116116
*
117-
* @return new instance of {@link RedisSerializationContext}.
117+
* @return a new {@link RedisSerializationContext} using JDK Serializaton.
118118
* @since 2.1
119119
*/
120120
static RedisSerializationContext<Object, Object> java() {
@@ -123,10 +123,11 @@ static RedisSerializationContext<Object, Object> java() {
123123

124124
/**
125125
* Creates a new {@link RedisSerializationContext} using a {@link JdkSerializationRedisSerializer} with given
126-
* {@link ClassLoader}.
126+
* {@link ClassLoader} to resolves {@link Class type} of the keys and values stored in Redis.
127127
*
128-
* @param classLoader the {@link ClassLoader} to use for deserialization. Can be {@literal null}.
129-
* @return new instance of {@link RedisSerializationContext}.
128+
* @param classLoader {@link ClassLoader} used to resolve {@link Class types} of keys and value stored in Redis
129+
* during deserialization; can be {@literal null}.
130+
* @return a new {@link RedisSerializationContext} using JDK Serializaton.
130131
* @since 2.1
131132
*/
132133
static RedisSerializationContext<Object, Object> java(ClassLoader classLoader) {
@@ -136,7 +137,7 @@ static RedisSerializationContext<Object, Object> java(ClassLoader classLoader) {
136137
/**
137138
* Creates a new {@link RedisSerializationContext} using a {@link StringRedisSerializer}.
138139
*
139-
* @return
140+
* @return a new {@link RedisSerializationContext} using a {@link StringRedisSerializer}.
140141
*/
141142
static RedisSerializationContext<String, String> string() {
142143
return fromSerializer(RedisSerializer.string());
@@ -145,9 +146,10 @@ static RedisSerializationContext<String, String> string() {
145146
/**
146147
* Creates a new {@link RedisSerializationContext} using the given {@link RedisSerializer}.
147148
*
148-
* @param serializer must not be {@literal null}.
149-
* @param <T>
150-
* @return
149+
* @param <T> {@link Class Type} of {@link Object} being de/serialized by the {@link RedisSerializer}.
150+
* @param serializer {@link RedisSerializer} used to de/serialize keys and value stored in Redis;
151+
* must not be {@literal null}.
152+
* @return a new {@link RedisSerializationContext} using the given {@link RedisSerializer}.
151153
*/
152154
static <T> RedisSerializationContext<T, T> fromSerializer(RedisSerializer<T> serializer) {
153155
return just(SerializationPair.fromSerializer(serializer));
@@ -156,9 +158,10 @@ static <T> RedisSerializationContext<T, T> fromSerializer(RedisSerializer<T> ser
156158
/**
157159
* Creates a new {@link RedisSerializationContext} using the given {@link SerializationPair}.
158160
*
159-
* @param serializationPair
160-
* @param <T>
161-
* @return
161+
* @param <T> {@link Class Type} of {@link Object} de/serialized by the {@link SerializationPair}.
162+
* @param serializationPair {@link SerializationPair} used to de/serialize keys and values stored in Redis;
163+
* must not be {@literal null}.
164+
* @return a new {@link RedisSerializationContext} using the given {@link SerializationPair}.
162165
*/
163166
static <T> RedisSerializationContext<T, T> just(SerializationPair<T> serializationPair) {
164167
return RedisSerializationContext.<T, T> newSerializationContext(serializationPair).build();
@@ -278,10 +281,10 @@ default T read(ByteBuffer buffer) {
278281
RedisElementWriter<T> getWriter();
279282

280283
/**
281-
* Serialize a {@code element} to its {@link ByteBuffer} representation.
284+
* Serialize the given {@code element} to its {@link ByteBuffer} representation.
282285
*
283-
* @param element
284-
* @return the {@link ByteBuffer} representing {@code element} in its binary form.
286+
* @param element {@link Object} to write (serialize) as a stream of bytes.
287+
* @return the {@link ByteBuffer} representing the given {@code element} in binary form.
285288
*/
286289
default ByteBuffer write(T element) {
287290
return getWriter().write(element);
@@ -314,6 +317,7 @@ interface RedisSerializationContextBuilder<K, V> {
314317
default RedisSerializationContextBuilder<K, V> key(RedisElementReader<K> reader, RedisElementWriter<K> writer) {
315318

316319
key(SerializationPair.just(reader, writer));
320+
317321
return this;
318322
}
319323

@@ -326,6 +330,7 @@ default RedisSerializationContextBuilder<K, V> key(RedisElementReader<K> reader,
326330
default RedisSerializationContextBuilder<K, V> key(RedisSerializer<K> serializer) {
327331

328332
key(SerializationPair.fromSerializer(serializer));
333+
329334
return this;
330335
}
331336

@@ -347,6 +352,7 @@ default RedisSerializationContextBuilder<K, V> key(RedisSerializer<K> serializer
347352
default RedisSerializationContextBuilder<K, V> value(RedisElementReader<V> reader, RedisElementWriter<V> writer) {
348353

349354
value(SerializationPair.just(reader, writer));
355+
350356
return this;
351357
}
352358

@@ -359,6 +365,7 @@ default RedisSerializationContextBuilder<K, V> value(RedisElementReader<V> reade
359365
default RedisSerializationContextBuilder<K, V> value(RedisSerializer<V> serializer) {
360366

361367
value(SerializationPair.fromSerializer(serializer));
368+
362369
return this;
363370
}
364371

@@ -377,10 +384,11 @@ default RedisSerializationContextBuilder<K, V> value(RedisSerializer<V> serializ
377384
* @param writer must not be {@literal null}.
378385
* @return {@literal this} builder.
379386
*/
380-
default RedisSerializationContextBuilder<K, V> hashKey(RedisElementReader<? extends Object> reader,
381-
RedisElementWriter<? extends Object> writer) {
387+
default RedisSerializationContextBuilder<K, V> hashKey(RedisElementReader<?> reader,
388+
RedisElementWriter<?> writer) {
382389

383390
hashKey(SerializationPair.just(reader, writer));
391+
384392
return this;
385393
}
386394

@@ -390,9 +398,10 @@ default RedisSerializationContextBuilder<K, V> hashKey(RedisElementReader<? exte
390398
* @param serializer must not be {@literal null}.
391399
* @return {@literal this} builder.
392400
*/
393-
default RedisSerializationContextBuilder<K, V> hashKey(RedisSerializer<? extends Object> serializer) {
401+
default RedisSerializationContextBuilder<K, V> hashKey(RedisSerializer<?> serializer) {
394402

395403
hashKey(SerializationPair.fromSerializer(serializer));
404+
396405
return this;
397406
}
398407

@@ -411,10 +420,11 @@ default RedisSerializationContextBuilder<K, V> hashKey(RedisSerializer<? extends
411420
* @param writer must not be {@literal null}.
412421
* @return {@literal this} builder.
413422
*/
414-
default RedisSerializationContextBuilder<K, V> hashValue(RedisElementReader<? extends Object> reader,
415-
RedisElementWriter<? extends Object> writer) {
423+
default RedisSerializationContextBuilder<K, V> hashValue(RedisElementReader<?> reader,
424+
RedisElementWriter<?> writer) {
416425

417426
hashValue(SerializationPair.just(reader, writer));
427+
418428
return this;
419429
}
420430

@@ -424,9 +434,10 @@ default RedisSerializationContextBuilder<K, V> hashValue(RedisElementReader<? ex
424434
* @param serializer must not be {@literal null}.
425435
* @return {@literal this} builder.
426436
*/
427-
default RedisSerializationContextBuilder<K, V> hashValue(RedisSerializer<? extends Object> serializer) {
437+
default RedisSerializationContextBuilder<K, V> hashValue(RedisSerializer<?> serializer) {
428438

429439
hashValue(SerializationPair.fromSerializer(serializer));
440+
430441
return this;
431442
}
432443

@@ -449,6 +460,7 @@ default RedisSerializationContextBuilder<K, V> string(RedisElementReader<String>
449460
RedisElementWriter<String> writer) {
450461

451462
string(SerializationPair.just(reader, writer));
463+
452464
return this;
453465
}
454466

@@ -461,6 +473,7 @@ default RedisSerializationContextBuilder<K, V> string(RedisElementReader<String>
461473
default RedisSerializationContextBuilder<K, V> string(RedisSerializer<String> serializer) {
462474

463475
string(SerializationPair.fromSerializer(serializer));
476+
464477
return this;
465478
}
466479

@@ -470,5 +483,6 @@ default RedisSerializationContextBuilder<K, V> string(RedisSerializer<String> se
470483
* @return the {@link RedisSerializationContext}.
471484
*/
472485
RedisSerializationContext<K, V> build();
486+
473487
}
474488
}

0 commit comments

Comments
 (0)