2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.api;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.util.Map.Entry;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 * Externalizable proxy for {@link NodeIdentifierWithPredicates}.
24 final class NIPv2 implements Externalizable {
25 private static final long serialVersionUID = 1L;
27 private NodeIdentifierWithPredicates nip;
29 @SuppressWarnings("checkstyle:redundantModifier")
34 NIPv2(final NodeIdentifierWithPredicates nip) {
35 this.nip = requireNonNull(nip);
39 public void writeExternal(final ObjectOutput out) throws IOException {
40 nip.getNodeType().writeTo(out);
42 out.writeInt(nip.size());
43 for (Entry<QName, Object> entry : nip.entrySet()) {
44 entry.getKey().writeTo(out);
45 out.writeObject(entry.getValue());
50 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
51 final QName qname = QName.readFrom(in);
52 final int size = in.readInt();
54 case 0 -> NodeIdentifierWithPredicates.of(qname);
55 case 1 -> NodeIdentifierWithPredicates.of(qname, QName.readFrom(in), in.readObject());
57 final var keys = ImmutableMap.<QName, Object>builderWithExpectedSize(size);
58 for (int i = 0; i < size; ++i) {
59 keys.put(QName.readFrom(in), in.readObject());
61 yield NodeIdentifierWithPredicates.of(qname, keys.build());
66 private Object readResolve() {