From 1c1146b3a6a4dffa1d5b2e679ab82755456d123e Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 11 Aug 2022 00:14:27 +0200 Subject: [PATCH] Add IOMv1 proxy ImmutableOffsetMap is using ugly reflection, which we are ditching. Add the serialization proxy for compatibility. Change-Id: I8687eee174462f62cdba927eb7c6d7386f7009ad Signed-off-by: Robert Varga --- .../opendaylight/yangtools/util/IOMv1.java | 65 +++++++++++++++++++ .../opendaylight/yangtools/util/OIOMv1.java | 35 ++++++++++ .../opendaylight/yangtools/util/UIOMv1.java | 36 ++++++++++ 3 files changed, 136 insertions(+) create mode 100644 common/util/src/main/java/org/opendaylight/yangtools/util/IOMv1.java create mode 100644 common/util/src/main/java/org/opendaylight/yangtools/util/OIOMv1.java create mode 100644 common/util/src/main/java/org/opendaylight/yangtools/util/UIOMv1.java 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 index 0000000000..ac7b00c878 --- /dev/null +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/IOMv1.java @@ -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> 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 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 index 0000000000..fd50413289 --- /dev/null +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/OIOMv1.java @@ -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> { + @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 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 index 0000000000..a2d40a8147 --- /dev/null +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/UIOMv1.java @@ -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> { + @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 keys, final Object[] values) { + final var newOffsets = OffsetMapCache.unorderedOffsets(keys); + return new Unordered<>(newOffsets, OffsetMapCache.adjustedArray(newOffsets, keys, values)); + } +} -- 2.36.6