yeralin commented on a change in pull request #6592:
URL: https://github.com/apache/kafka/pull/6592#discussion_r630539285



##########
File path: 
clients/src/main/java/org/apache/kafka/common/serialization/ListSerializer.java
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.serialization;
+
+import org.apache.kafka.clients.CommonClientConfigs;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.config.ConfigException;
+import org.apache.kafka.common.utils.Utils;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static 
org.apache.kafka.common.serialization.Serdes.ListSerde.SerializationStrategy;
+
+public class ListSerializer<Inner> implements Serializer<List<Inner>> {
+
+    private static final List<Class<? extends Serializer<?>>> 
FIXED_LENGTH_SERIALIZERS = Arrays.asList(
+        ShortSerializer.class,
+        IntegerSerializer.class,
+        FloatSerializer.class,
+        LongSerializer.class,
+        DoubleSerializer.class,
+        UUIDSerializer.class);
+
+    private Serializer<Inner> inner;
+    private SerializationStrategy serStrategy;
+    private boolean isFixedLength;
+
+    public ListSerializer() {}
+
+    public ListSerializer(Serializer<Inner> inner) {
+        if (inner == null) {
+            throw new IllegalArgumentException("ListSerializer requires 
\"serializer\" parameter to be provided during initialization");
+        }
+        this.inner = inner;
+        this.isFixedLength = 
FIXED_LENGTH_SERIALIZERS.contains(inner.getClass());
+        this.serStrategy = this.isFixedLength ? 
SerializationStrategy.CONSTANT_SIZE : SerializationStrategy.VARIABLE_SIZE;
+    }
+
+    public Serializer<Inner> getInnerSerializer() {
+        return inner;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public void configure(Map<String, ?> configs, boolean isKey) {
+        if (inner != null) {
+            throw new ConfigException("List serializer was already initialized 
using a non-default constructor");
+        }
+        final String innerSerdePropertyName = isKey ? 
CommonClientConfigs.DEFAULT_LIST_KEY_SERDE_INNER_CLASS : 
CommonClientConfigs.DEFAULT_LIST_VALUE_SERDE_INNER_CLASS;
+        final Object innerSerdeClassOrName = 
configs.get(innerSerdePropertyName);
+        if (innerSerdeClassOrName == null) {
+            throw new ConfigException("Not able to determine the serializer 
class because it was neither passed via the constructor nor set in the 
config.");
+        }
+        try {
+            if (innerSerdeClassOrName instanceof String) {
+                inner = Utils.newInstance((String) innerSerdeClassOrName, 
Serde.class).serializer();
+            } else if (innerSerdeClassOrName instanceof Class) {
+                inner = (Serializer<Inner>) ((Serde) Utils.newInstance((Class) 
innerSerdeClassOrName)).serializer();
+            } else {
+                throw new KafkaException("Could not create a serializer class 
instance using \"" + innerSerdePropertyName + "\" property.");
+            }
+            inner.configure(configs, isKey);
+            isFixedLength = 
FIXED_LENGTH_SERIALIZERS.contains(inner.getClass());
+            serStrategy = this.isFixedLength ? 
SerializationStrategy.CONSTANT_SIZE : SerializationStrategy.VARIABLE_SIZE;
+        } catch (final ClassNotFoundException e) {
+            throw new ConfigException(innerSerdePropertyName, 
innerSerdeClassOrName, "Serializer class " + innerSerdeClassOrName + " could 
not be found.");
+        }
+    }
+
+    private void serializeNullIndexList(final DataOutputStream out, 
List<Inner> data) throws IOException {
+        List<Integer> nullIndexList = IntStream.range(0, data.size())
+                .filter(i -> data.get(i) == null)

Review comment:
       Would something like this work?
   ```
   int i = 0;
   List<Integer> nullIndexList = new ArrayList<>();
   for (Iterator<Inner> it = data.listIterator(); it.hasNext(); i++) {
       if (it.next() == null) {
           nullIndexList.add(i); 
       }
   }
   out.writeInt(nullIndexList.size());
   for (int nullIndex : nullIndexList) {
       out.writeInt(nullIndex);
   }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to