reswqa commented on code in PR #24845: URL: https://github.com/apache/flink/pull/24845#discussion_r1630969295
########## flink-core/src/main/java/org/apache/flink/api/java/typeutils/SetTypeInfo.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.flink.api.java.typeutils; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.serialization.SerializerConfig; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.common.typeutils.base.SetSerializer; + +import java.util.Set; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * A {@link TypeInformation} for the set types of the Java API. + * + * @param <T> The type of the elements in the set. + */ +@PublicEvolving +public final class SetTypeInfo<T> extends TypeInformation<Set<T>> { + + private static final long serialVersionUID = 1L; + + private final TypeInformation<T> elementTypeInfo; + + public SetTypeInfo(Class<T> elementTypeClass) { + this.elementTypeInfo = of(checkNotNull(elementTypeClass, "elementTypeClass")); + } + + public SetTypeInfo(TypeInformation<T> elementTypeInfo) { + this.elementTypeInfo = checkNotNull(elementTypeInfo, "elementTypeInfo"); Review Comment: ```suggestion this.elementTypeInfo = checkNotNull(elementTypeInfo, "The element type information cannot be null"); ``` ########## flink-core/src/main/java/org/apache/flink/api/common/typeutils/base/SetSerializer.java: ########## @@ -0,0 +1,168 @@ +/* + * 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.flink.api.common.typeutils.base; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot; +import org.apache.flink.core.memory.DataInputView; +import org.apache.flink.core.memory.DataOutputView; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * A serializer for {@link java.util.Set}. The serializer relies on an element serializer for the + * serialization of the set's elements. + * + * <p>The serialization format for the set is as follows: four bytes for the length of the set, + * followed by the serialized representation of each element. + * + * @param <T> The type of element in the set. + */ +@Internal +public final class SetSerializer<T> extends TypeSerializer<Set<T>> { + + private static final long serialVersionUID = 1L; + + /** The serializer for the elements of the set. */ + private final TypeSerializer<T> elementSerializer; + + /** + * Creates a set serializer that uses the given serializer to serialize the set's elements. + * + * @param elementSerializer The serializer for the elements of the set + */ + public SetSerializer(TypeSerializer<T> elementSerializer) { + this.elementSerializer = checkNotNull(elementSerializer); + } + + // ------------------------------------------------------------------------ + // SetSerializer specific properties + // ------------------------------------------------------------------------ + + /** + * Gets the serializer for the elements of the set. + * + * @return The serializer for the elements of the set + */ + public TypeSerializer<T> getElementSerializer() { + return elementSerializer; + } + + // ------------------------------------------------------------------------ + // Type Serializer implementation + // ------------------------------------------------------------------------ + + @Override + public boolean isImmutableType() { + return false; + } + + @Override + public TypeSerializer<Set<T>> duplicate() { + TypeSerializer<T> duplicateElement = elementSerializer.duplicate(); + return duplicateElement == elementSerializer ? this : new SetSerializer<>(duplicateElement); + } + + @Override + public Set<T> createInstance() { + return new HashSet<>(0); + } + + @Override + public Set<T> copy(Set<T> from) { + Set<T> newSet = new HashSet<>(from.size()); + for (T element : from) { + newSet.add(elementSerializer.copy(element)); + } + return newSet; + } + + @Override + public Set<T> copy(Set<T> from, Set<T> reuse) { + return copy(from); + } + + @Override + public int getLength() { + return -1; // var length + } + + @Override + public void serialize(Set<T> set, DataOutputView target) throws IOException { + final int size = set.size(); + target.writeInt(size); + for (T element : set) { + elementSerializer.serialize(element, target); Review Comment: What would happened if `element == null`? ########## flink-core/src/main/java/org/apache/flink/api/java/typeutils/SetTypeInfo.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.flink.api.java.typeutils; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.serialization.SerializerConfig; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.common.typeutils.base.SetSerializer; + +import java.util.Set; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * A {@link TypeInformation} for the set types of the Java API. + * + * @param <T> The type of the elements in the set. + */ +@PublicEvolving +public final class SetTypeInfo<T> extends TypeInformation<Set<T>> { + + private static final long serialVersionUID = 1L; + + private final TypeInformation<T> elementTypeInfo; + + public SetTypeInfo(Class<T> elementTypeClass) { + this.elementTypeInfo = of(checkNotNull(elementTypeClass, "elementTypeClass")); Review Comment: ```suggestion this.elementTypeInfo = of(checkNotNull(elementTypeClass, "The element type class cannot be null")); `` -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
