16
16
17
17
package org .springframework .kafka .support .serializer ;
18
18
19
- import java .util .LinkedHashMap ;
20
- import java .util .Map ;
19
+ import java .util .*;
21
20
import java .util .Map .Entry ;
22
21
23
22
import org .apache .kafka .common .errors .SerializationException ;
32
31
* @author Gary Russell
33
32
* @author Artem Bilan
34
33
* @author Wang Zhiyang
34
+ * @author Mahesh Aravind V
35
35
*
36
36
* @since 2.7.9
37
37
*
38
38
*/
39
39
public class DelegatingByTypeSerializer implements Serializer <Object > {
40
40
41
- private final Map <Class <?>, Serializer <?>> delegates = new LinkedHashMap <>();
41
+ private static final Comparator <Class <?>> DELEGATES_ASSIGNABILITY_COMPARATOR =
42
+ (type1 , type2 ) -> {
43
+
44
+ if (type1 .isAssignableFrom (type2 )) {
45
+ return 1 ;
46
+ }
47
+ if (type2 .isAssignableFrom (type1 )) {
48
+ return -1 ;
49
+ }
50
+
51
+ return 0 ;
52
+ };
53
+
54
+ private final Map <Class <?>, Serializer <?>> delegates = new TreeMap <>(DELEGATES_ASSIGNABILITY_COMPARATOR );
42
55
43
56
private final boolean assignable ;
44
57
@@ -51,17 +64,23 @@ public DelegatingByTypeSerializer(Map<Class<?>, Serializer<?>> delegates) {
51
64
}
52
65
53
66
/**
54
- * Construct an instance with the map of delegates; keys matched exactly or if the
55
- * target object is assignable to the key, depending on the assignable argument.
56
- * If assignable, entries are checked in the natural entry order so an ordered map
57
- * such as a {@link LinkedHashMap} is recommended.
58
- * @param delegates the delegates.
59
- * @param assignable whether the target is assignable to the key.
67
+ * Construct an instance with the map of delegates.
68
+ * If {@code assignable} is {@code false}, only exact key matches are considered.
69
+ * If {@code assignable} is {@code true}, a delegate is selected if its key class
70
+ * is assignable from the target object's class. When multiple matches are possible,
71
+ * the most specific matching class is selected — that is, the closest match in the
72
+ * class hierarchy.
73
+ *
74
+ * @param delegates the delegates
75
+ * @param assignable true if {@link #findDelegate(Object, Map)} should consider assignability to
76
+ * the key rather than an exact match.
77
+ *
60
78
* @since 2.8.3
61
79
*/
62
80
public DelegatingByTypeSerializer (Map <Class <?>, Serializer <?>> delegates , boolean assignable ) {
63
81
Assert .notNull (delegates , "'delegates' cannot be null" );
64
82
Assert .noNullElements (delegates .values (), "Serializers in delegates map cannot be null" );
83
+
65
84
this .delegates .putAll (delegates );
66
85
this .assignable = assignable ;
67
86
}
@@ -99,7 +118,7 @@ public byte[] serialize(String topic, Headers headers, Object data) {
99
118
return delegate .serialize (topic , headers , data );
100
119
}
101
120
102
- private <T > Serializer <T > findDelegate (T data ) {
121
+ protected <T > Serializer <T > findDelegate (T data ) {
103
122
return findDelegate (data , this .delegates );
104
123
}
105
124
0 commit comments