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