igalshilman commented on a change in pull request #7696: [FLINK-11588][core] 
Migrating the CopyableValueSerializer
URL: https://github.com/apache/flink/pull/7696#discussion_r257635909
 
 

 ##########
 File path: 
flink-core/src/main/java/org/apache/flink/api/common/typeutils/GenericTypeSerializerSnapshot.java
 ##########
 @@ -0,0 +1,100 @@
+/*
+ * 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;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.util.InstantiationUtil;
+
+import java.io.IOException;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * Base {@link TypeSerializerSnapshot} for serializers for generic types.
+ *
+ * @param <T> The type to be instantiated.
+ */
+@Internal
+public abstract class GenericTypeSerializerSnapshot<T, S extends 
TypeSerializer> implements TypeSerializerSnapshot<T> {
+
+       private static final int VERSION = 2;
+       private Class<T> typeClass;
+
+       protected GenericTypeSerializerSnapshot() {
+       }
+
+       protected GenericTypeSerializerSnapshot(Class<T> typeClass) {
+               this.typeClass = checkNotNull(typeClass, "type class can not be 
NULL");
+       }
+
+       /**
+        * Create a serializer that is able to serialize the generic type 
{@code typeClass}.
+        */
+       protected abstract TypeSerializer<T> createSerializer(Class<T> 
typeClass);
+
+       /**
+        * Gets the type class from the corresponding serializer.
+        */
+       protected abstract Class<T> getTypeClass(S serializer);
+
+       /**
+        * Gets the serializer's class.
+        */
+       protected abstract Class<?> serializerClass();
+
+       @Override
+       public int getCurrentVersion() {
+               return VERSION;
+       }
+
+       @Override
+       public void writeSnapshot(DataOutputView out) throws IOException {
+               checkState(typeClass != null, "type class can not be NULL");
+               out.writeUTF(typeClass.getName());
+       }
+
+       @Override
+       public void readSnapshot(int readVersion, DataInputView in, ClassLoader 
userCodeClassLoader) throws IOException {
+               typeClass = InstantiationUtil.resolveClassByName(in, 
userCodeClassLoader);
+       }
+
+       @Override
+       @SuppressWarnings("unchecked")
+       public TypeSerializer<T> restoreSerializer() {
+               checkState(typeClass != null, "type class can not be NULL");
+               return createSerializer(typeClass);
+       }
+
+       @Override
+       public TypeSerializerSchemaCompatibility<T> 
resolveSchemaCompatibility(TypeSerializer<T> newSerializer) {
+               if (!serializerClass().isInstance(newSerializer)) {
+                       return TypeSerializerSchemaCompatibility.incompatible();
+               }
+               @SuppressWarnings("unchecked") S casted = (S) newSerializer;
+               if (typeClass == getTypeClass(casted)) {
+                       return 
TypeSerializerSchemaCompatibility.compatibleAsIs();
+               }
+               else {
 
 Review comment:
   I adopted this style from Stephan, but I can revert it if you want.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to