Add IOMv1 proxy 45/102045/8
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Aug 2022 22:14:27 +0000 (00:14 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 16 Aug 2022 20:02:49 +0000 (22:02 +0200)
ImmutableOffsetMap is using ugly reflection, which we are ditching. Add
the serialization proxy for compatibility.

Change-Id: I8687eee174462f62cdba927eb7c6d7386f7009ad
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/main/java/org/opendaylight/yangtools/util/IOMv1.java [new file with mode: 0644]
common/util/src/main/java/org/opendaylight/yangtools/util/OIOMv1.java [new file with mode: 0644]
common/util/src/main/java/org/opendaylight/yangtools/util/UIOMv1.java [new file with mode: 0644]

diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/IOMv1.java b/common/util/src/main/java/org/opendaylight/yangtools/util/IOMv1.java
new file mode 100644 (file)
index 0000000..ac7b00c
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.util;
+
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.ImmutableList;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.Serial;
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * Base class for {@link ImmutableOffsetMap} serialization proxies. Implements most of the serialization form at logic.
+ */
+abstract sealed class IOMv1<T extends ImmutableOffsetMap<?, ?>> implements Externalizable permits OIOMv1, UIOMv1 {
+    private ImmutableOffsetMap<?, ?> map;
+
+    IOMv1() {
+        // For Externalizable
+    }
+
+    IOMv1(final @NonNull T map) {
+        this.map = requireNonNull(map);
+    }
+
+    @Override
+    public final void writeExternal(final ObjectOutput out) throws IOException {
+        final var local = verifyNotNull(map);
+        out.writeInt(local.size());
+        for (var e : local.entrySet()) {
+            out.writeObject(e.getKey());
+            out.writeObject(e.getValue());
+        }
+    }
+
+    @Override
+    public final void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+        // TODO: optimize for size == 1? what can we gain?
+        final int size = in.readInt();
+        final var keysBuilder = ImmutableList.builderWithExpectedSize(size);
+        final var values = new Object[size];
+        for (int i = 0; i < size; ++i) {
+            keysBuilder.add(in.readObject());
+            values[i] = in.readObject();
+        }
+
+        map = verifyNotNull(readReplace(keysBuilder.build(), values));
+    }
+
+    @Serial
+    final Object readReplace() {
+        return verifyNotNull(map);
+    }
+
+    abstract @NonNull T readReplace(@NonNull ImmutableList<Object> keys, @NonNull Object[] values);
+}
diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/OIOMv1.java b/common/util/src/main/java/org/opendaylight/yangtools/util/OIOMv1.java
new file mode 100644 (file)
index 0000000..fd50413
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.util;
+
+import com.google.common.collect.ImmutableList;
+import java.io.Serial;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.util.ImmutableOffsetMap.Ordered;
+
+/**
+ * Serialization proxy for {@link Ordered}.
+ */
+final class OIOMv1 extends IOMv1<Ordered<?, ?>> {
+    @Serial
+    private static final long serialVersionUID = 1;
+
+    @SuppressWarnings("checkstyle:RedundantModifier")
+    public OIOMv1() {
+        // For Externalizable
+    }
+
+    OIOMv1(final @NonNull Ordered<?, ?> map) {
+        super(map);
+    }
+
+    @Override
+    Ordered<?, ?> readReplace(final ImmutableList<Object> keys, final Object[] values) {
+        return new Ordered<>(OffsetMapCache.orderedOffsets(keys), values);
+    }
+}
diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/UIOMv1.java b/common/util/src/main/java/org/opendaylight/yangtools/util/UIOMv1.java
new file mode 100644 (file)
index 0000000..a2d40a8
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.util;
+
+import com.google.common.collect.ImmutableList;
+import java.io.Serial;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.util.ImmutableOffsetMap.Unordered;
+
+/**
+ * Serialization proxy for {@link Unordered}.
+ */
+final class UIOMv1 extends IOMv1<Unordered<?, ?>> {
+    @Serial
+    private static final long serialVersionUID = 1;
+
+    @SuppressWarnings("checkstyle:RedundantModifier")
+    public UIOMv1() {
+        // For Externalizable
+    }
+
+    UIOMv1(final @NonNull Unordered<?, ?> map) {
+        super(map);
+    }
+
+    @Override
+    Unordered<?, ?> readReplace(final ImmutableList<Object> keys, final Object[] values) {
+        final var newOffsets = OffsetMapCache.unorderedOffsets(keys);
+        return new Unordered<>(newOffsets, OffsetMapCache.adjustedArray(newOffsets, keys, values));
+    }
+}