Skip to content

Commit 810beef

Browse files
authored
MINOR: improve (De)Serializer JavaDocs (apache#19467)
Reviewers: Kirk True <[email protected]>, Lianet Magrans <[email protected]>
1 parent c199418 commit 810beef

File tree

2 files changed

+88
-36
lines changed

2 files changed

+88
-36
lines changed

clients/src/main/java/org/apache/kafka/common/serialization/Deserializer.java

+52-20
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,61 @@
2525

2626
/**
2727
* An interface for converting bytes to objects.
28-
*
2928
* A class that implements this interface is expected to have a constructor with no parameters.
30-
* <p>
31-
* Implement {@link org.apache.kafka.common.ClusterResourceListener} to receive cluster metadata once it's available. Please see the class documentation for ClusterResourceListener for more information.
32-
* Implement {@link org.apache.kafka.common.metrics.Monitorable} to enable the deserializer to register metrics. The following tags are automatically added to
33-
* all metrics registered: <code>config</code> set to either <code>key.deserializer</code> or <code>value.deserializer</code>, and <code>class</code> set to the Deserializer class name.
29+
*
30+
* <p>This interface can be combined with {@link org.apache.kafka.common.ClusterResourceListener ClusterResourceListener}
31+
* to receive cluster metadata once it's available, as well as {@link org.apache.kafka.common.metrics.Monitorable Monitorable}
32+
* to enable the deserializer to register metrics. For the latter, the following tags are automatically added to
33+
* all metrics registered: {@code config} set to either {@code key.deserializer} or {@code value.deserializer},
34+
* and {@code class} set to the deserializer class name.
35+
*
3436
* @param <T> Type to be deserialized into.
3537
*/
3638
public interface Deserializer<T> extends Closeable {
3739

3840
/**
3941
* Configure this class.
40-
* @param configs configs in key/value pairs
41-
* @param isKey whether is for key or value
42+
*
43+
* @param configs
44+
* configs in key/value pairs
45+
* @param isKey
46+
* whether the deserializer is used for the key or the value
4247
*/
4348
default void configure(Map<String, ?> configs, boolean isKey) {
4449
// intentionally left blank
4550
}
4651

4752
/**
4853
* Deserialize a record value from a byte array into a value or object.
49-
* @param topic topic associated with the data
50-
* @param data serialized bytes; may be null; implementations are recommended to handle null by returning a value or null rather than throwing an exception.
51-
* @return deserialized typed data; may be null
54+
*
55+
* <p>It is recommended to deserialize a {@code null} byte array to a {@code null} object.
56+
*
57+
* @param topic
58+
* topic associated with the data
59+
* @param data
60+
* serialized bytes; may be {@code null}
61+
*
62+
* @return deserialized typed data; may be {@code null}
5263
*/
5364
T deserialize(String topic, byte[] data);
5465

5566
/**
5667
* Deserialize a record value from a byte array into a value or object.
57-
* @param topic topic associated with the data
58-
* @param headers headers associated with the record; may be empty.
59-
* @param data serialized bytes; may be null; implementations are recommended to handle null by returning a value or null rather than throwing an exception.
60-
* @return deserialized typed data; may be null
68+
*
69+
* <p>It is recommended to deserialize a {@code null} byte array to a {@code null} object.
70+
*
71+
* <p>Note that the passed in {@link Headers} may be empty, but never {@code null}.
72+
* The implementation is allowed to modify the passed in headers, as a side effect of deserialization.
73+
* It is considered best practice to not delete or modify existing headers, but rather only add new ones.
74+
*
75+
* @param topic
76+
* topic associated with the data
77+
* @param headers
78+
* headers associated with the record
79+
* @param data
80+
* serialized bytes; may be {@code null}
81+
*
82+
* @return deserialized typed data; may be {@code null}
6183
*/
6284
default T deserialize(String topic, Headers headers, byte[] data) {
6385
return deserialize(topic, data);
@@ -73,19 +95,29 @@ default T deserialize(String topic, Headers headers, byte[] data) {
7395
* <p>Similarly, if this method is overridden, the implementation cannot make any assumptions about the
7496
* passed in {@link ByteBuffer} either.
7597
*
76-
* @param topic topic associated with the data
77-
* @param headers headers associated with the record; may be empty.
78-
* @param data serialized ByteBuffer; may be null; implementations are recommended to handle null by returning a value or null rather than throwing an exception.
79-
* @return deserialized typed data; may be null
98+
* <p>It is recommended to deserialize a {@code null} {@link ByteBuffer} to a {@code null} object.
99+
*
100+
* <p>Note that the passed in {@link Headers} may be empty, but never {@code null}.
101+
* The implementation is allowed to modify the passed in headers, as a side effect of deserialization.
102+
* It is considered best practice to not delete or modify existing headers, but rather only add new ones.
103+
*
104+
* @param topic
105+
* topic associated with the data
106+
* @param headers
107+
* headers associated with the record
108+
* @param data
109+
* serialized ByteBuffer; may be {@code null}
110+
*
111+
* @return deserialized typed data; may be {@code null}
80112
*/
81113
default T deserialize(String topic, Headers headers, ByteBuffer data) {
82114
return deserialize(topic, headers, Utils.toNullableArray(data));
83115
}
84116

85117
/**
86118
* Close this deserializer.
87-
* <p>
88-
* This method must be idempotent as it may be called multiple times.
119+
*
120+
* <p>This method must be idempotent as it may be called multiple times.
89121
*/
90122
@Override
91123
default void close() {

clients/src/main/java/org/apache/kafka/common/serialization/Serializer.java

+36-16
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,25 @@
2323

2424
/**
2525
* An interface for converting objects to bytes.
26-
*
2726
* A class that implements this interface is expected to have a constructor with no parameter.
28-
* <p>
29-
* Implement {@link org.apache.kafka.common.ClusterResourceListener} to receive cluster metadata once it's available. Please see the class documentation for ClusterResourceListener for more information.
30-
* Implement {@link org.apache.kafka.common.metrics.Monitorable} to enable the serializer to register metrics. The following tags ae automatically added to
31-
* all metrics registered: <code>config</code> set to either <code>key.serializer</code> or <code>value.serializer</code>, and <code>class</code> set to the Serializer class name.
27+
*
28+
* <p>This interface can be combined with {@link org.apache.kafka.common.ClusterResourceListener ClusterResourceListener}
29+
* to receive cluster metadata once it's available, as well as {@link org.apache.kafka.common.metrics.Monitorable Monitorable}
30+
* to enable the serializer to register metrics. For the latter, the following tags are automatically added to all
31+
* metrics registered: {@code config} set to either {@code key.serializer} or {@code value.serializer},
32+
* and {@code class} set to the serializer class name.
33+
*
3234
* @param <T> Type to be serialized from.
3335
*/
3436
public interface Serializer<T> extends Closeable {
3537

3638
/**
3739
* Configure this class.
38-
* @param configs configs in key/value pairs
39-
* @param isKey whether is for key or value
40+
*
41+
* @param configs
42+
* configs in key/value pairs
43+
* @param isKey
44+
* whether the serializer is used for the key or the value
4045
*/
4146
default void configure(Map<String, ?> configs, boolean isKey) {
4247
// intentionally left blank
@@ -45,28 +50,43 @@ default void configure(Map<String, ?> configs, boolean isKey) {
4550
/**
4651
* Convert {@code data} into a byte array.
4752
*
48-
* @param topic topic associated with data
49-
* @param data typed data
50-
* @return serialized bytes
53+
* <p>It is recommended to serialize {@code null} data to the {@code null} byte array.
54+
*
55+
* @param topic
56+
* topic associated with data
57+
* @param data
58+
* typed data; may be {@code null}
59+
*
60+
* @return serialized bytes; may be {@code null}
5161
*/
5262
byte[] serialize(String topic, T data);
5363

5464
/**
5565
* Convert {@code data} into a byte array.
5666
*
57-
* @param topic topic associated with data
58-
* @param headers headers associated with the record
59-
* @param data typed data
60-
* @return serialized bytes
67+
* <p>It is recommended to serialize {@code null} data to the {@code null} byte array.
68+
*
69+
* <p>Note that the passed in {@link Headers} may be empty, but never {@code null}.
70+
* The implementation is allowed to modify the passed in headers, as a side effect of serialization.
71+
* It is considered best practice to not delete or modify existing headers, but rather only add new ones.
72+
*
73+
* @param topic
74+
* topic associated with data
75+
* @param headers
76+
* headers associated with the record
77+
* @param data
78+
* typed data; may be {@code null}
79+
*
80+
* @return serialized bytes; may be {@code null}
6181
*/
6282
default byte[] serialize(String topic, Headers headers, T data) {
6383
return serialize(topic, data);
6484
}
6585

6686
/**
6787
* Close this serializer.
68-
* <p>
69-
* This method must be idempotent as it may be called multiple times.
88+
*
89+
* <p>This method must be idempotent as it may be called multiple times.
7090
*/
7191
@Override
7292
default void close() {

0 commit comments

Comments
 (0)