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