Use serialization proxy for ImmutableOffsetMap 46/102046/11
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Aug 2022 22:15:54 +0000 (00:15 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 17 Aug 2022 06:28:00 +0000 (08:28 +0200)
This patch switches to using an Externalizable proxy, allowing us to
properly inject fields.

Change-Id: I2a556713f2ab47a3ea1fabff7a5e45da02494c81
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/main/java/org/opendaylight/yangtools/util/ImmutableOffsetMap.java

index 5df5bd010cfef8dabfa9ba2b3f97d892630141b6..a04166477c0c3601f2847c1b036cf144eeba119e 100644 (file)
@@ -16,7 +16,6 @@ import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.UnmodifiableIterator;
 import java.io.IOException;
 import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 import java.io.Serial;
 import java.io.Serializable;
 import java.lang.reflect.Field;
@@ -64,6 +63,11 @@ public abstract sealed class ImmutableOffsetMap<K, V> implements UnmodifiableMap
             setField(this, OFFSETS_FIELD, OffsetMapCache.orderedOffsets(keys));
             setField(this, ARRAY_FIELD, values);
         }
+
+        @Override
+        Object writeReplace() {
+            return new OIOMv1(this);
+        }
     }
 
     static final class Unordered<K, V> extends ImmutableOffsetMap<K, V> {
@@ -86,6 +90,11 @@ public abstract sealed class ImmutableOffsetMap<K, V> implements UnmodifiableMap
             setField(this, OFFSETS_FIELD, newOffsets);
             setField(this, ARRAY_FIELD, OffsetMapCache.adjustedArray(newOffsets, keys, values));
         }
+
+        @Override
+        Object writeReplace() {
+            return new UIOMv1(this);
+        }
     }
 
     @Serial
@@ -372,13 +381,7 @@ public abstract sealed class ImmutableOffsetMap<K, V> implements UnmodifiableMap
     }
 
     @Serial
-    private void writeObject(final ObjectOutputStream out) throws IOException {
-        out.writeInt(offsets.size());
-        for (Entry<K, V> e : entrySet()) {
-            out.writeObject(e.getKey());
-            out.writeObject(e.getValue());
-        }
-    }
+    abstract Object writeReplace();
 
     // FIXME: this is ugly, use an Externalizable proxy
     private static final Field OFFSETS_FIELD = fieldFor("offsets");