Introduce binding.EntryObject
[yangtools.git] / data / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / YIDv1.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableList;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18
19 /**
20  * Externalizable proxy for {@link YangInstanceIdentifier}.
21  */
22 final class YIDv1 implements Externalizable {
23     @java.io.Serial
24     private static final long serialVersionUID = 1L;
25
26     private YangInstanceIdentifier yid;
27
28     @SuppressWarnings("checkstyle:redundantModifier")
29     public YIDv1() {
30         // For Externalizable
31     }
32
33     YIDv1(final YangInstanceIdentifier yid) {
34         this.yid = requireNonNull(yid);
35     }
36
37     @Override
38     public void writeExternal(final ObjectOutput out) throws IOException {
39         final var args = yid.getPathArguments();
40         out.writeInt(args.size());
41         for (PathArgument arg : args) {
42             // Unfortunately PathArgument is an interface and we do not have control over all its implementations,
43             // hence we did not bother with making them WritableObjects. This works reasonably well, though.
44             out.writeObject(arg);
45         }
46     }
47
48     @Override
49     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
50         final int size = in.readInt();
51         final var builder = ImmutableList.<PathArgument>builderWithExpectedSize(size);
52         for (int i = 0; i < size; ++i) {
53             builder.add((PathArgument) in.readObject());
54         }
55         yid = YangInstanceIdentifier.of(builder.build());
56     }
57
58     @java.io.Serial
59     private Object readResolve() {
60         return yid;
61     }
62 }