2121import java .util .HashMap ;
2222import java .util .Map ;
2323import java .util .Map .Entry ;
24+ import java .util .Set ;
2425import java .util .concurrent .ConcurrentHashMap ;
2526import java .util .regex .Pattern ;
2627
@@ -74,6 +75,8 @@ public abstract class DelegatingByTopicSerialization<T extends Closeable> implem
7475
7576 private final Map <Pattern , T > delegates = new ConcurrentHashMap <>();
7677
78+ private final Set <String > patterns = ConcurrentHashMap .newKeySet ();
79+
7780 private T defaultDelegate ;
7881
7982 private boolean forKeys ;
@@ -84,7 +87,11 @@ public DelegatingByTopicSerialization() {
8487 }
8588
8689 public DelegatingByTopicSerialization (Map <Pattern , T > delegates , T defaultDelegate ) {
90+ Assert .notNull (delegates , "'delegates' cannot be null" );
91+ Assert .notNull (defaultDelegate , "'defaultDelegate' cannot be null" );
8792 this .delegates .putAll (delegates );
93+ delegates .keySet ().forEach (pattern -> Assert .isTrue (this .patterns .add (pattern .pattern ()),
94+ "Duplicate pattern: " + pattern .pattern ()));
8895 this .defaultDelegate = defaultDelegate ;
8996 }
9097
@@ -98,6 +105,9 @@ public void setCaseSensitive(boolean caseSensitive) {
98105
99106 @ SuppressWarnings (UNCHECKED )
100107 protected void configure (Map <String , ?> configs , boolean isKey ) {
108+ if (this .delegates .size () > 0 ) {
109+ this .delegates .values ().forEach (delegate -> configureDelegate (configs , isKey , delegate ));
110+ }
101111 this .forKeys = isKey ;
102112 Object insensitive = configs .get (CASE_SENSITIVE );
103113 if (insensitive instanceof String ) {
@@ -107,7 +117,7 @@ else if (insensitive instanceof Boolean) {
107117 this .cased = (Boolean ) insensitive ;
108118 }
109119 String configKey = defaultKey ();
110- if (configKey != null ) {
120+ if (configKey != null && configs . containsKey ( configKey ) ) {
111121 buildDefault (configs , configKey , isKey , configs .get (configKey ));
112122 }
113123 configKey = configKey ();
@@ -146,6 +156,10 @@ private void processMap(Map<String, ?> configs, boolean isKey, String configKey,
146156 protected void build (Map <String , ?> configs , boolean isKey , String configKey , Object delegate , Pattern pattern ) {
147157
148158 if (isInstance (delegate )) {
159+ if (pattern != null && !this .patterns .add (pattern .pattern ())) {
160+ LOGGER .debug (() -> "Delegate already configured for " + pattern .pattern ());
161+ return ;
162+ }
149163 this .delegates .put (pattern , (T ) delegate );
150164 configureDelegate (configs , isKey , (T ) delegate );
151165 }
@@ -208,8 +222,9 @@ private Map<Pattern, T> createDelegates(String mappings, Map<String, ?> configs,
208222 return delegateMap ;
209223 }
210224
225+ @ Nullable
211226 private T createInstanceAndConfigure (Map <String , ?> configs , boolean isKey ,
212- Map <Pattern , T > delegates2 , Pattern pattern , String className ) {
227+ Map <Pattern , T > delegates2 , @ Nullable Pattern pattern , String className ) {
213228
214229 try {
215230 Class <?> clazz = ClassUtils .forName (className .trim (), ClassUtils .getDefaultClassLoader ());
@@ -240,6 +255,10 @@ else if (key instanceof String) {
240255 protected T instantiateAndConfigure (Map <String , ?> configs , boolean isKey , Map <Pattern , T > delegates2 ,
241256 @ Nullable Pattern pattern , Class <?> clazz ) {
242257
258+ if (pattern != null && !this .patterns .add (pattern .pattern ())) {
259+ LOGGER .debug (() -> "Delegate already configured for " + pattern .pattern ());
260+ return null ;
261+ }
243262 try {
244263 @ SuppressWarnings (UNCHECKED )
245264 T delegate = (T ) clazz .getDeclaredConstructor ().newInstance ();
@@ -282,7 +301,7 @@ protected T findDelegate(String topic) {
282301 }
283302 if (delegate == null ) {
284303 throw new IllegalStateException (
285- "No serializer found for topic '" + topic + "'" );
304+ "No (de) serializer found for topic '" + topic + "'" );
286305 }
287306 return delegate ;
288307 }
0 commit comments