80fcf144417f3978987822482ff1b5b6e7cdfac5
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / InstanceIdentifierV3.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.binding;
9
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.Iterables;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.io.ObjectStreamException;
17 import java.io.Serial;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
22
23 class InstanceIdentifierV3<T extends DataObject> implements Externalizable {
24     @Serial
25     private static final long serialVersionUID = 3L;
26
27     private @Nullable Iterable<PathArgument> pathArguments;
28     private @Nullable Class<T> targetType;
29     private boolean wildcarded;
30     private int hash;
31
32     @SuppressWarnings("redundantModifier")
33     public InstanceIdentifierV3() {
34         // For Externalizable
35     }
36
37     InstanceIdentifierV3(final InstanceIdentifier<T> source) {
38         pathArguments = source.pathArguments;
39         targetType = source.getTargetType();
40         wildcarded = source.isWildcarded();
41         hash = source.hashCode();
42     }
43
44     final int getHash() {
45         return hash;
46     }
47
48     final Iterable<PathArgument> getPathArguments() {
49         return pathArguments;
50     }
51
52     final Class<T> getTargetType() {
53         return targetType;
54     }
55
56     final boolean isWildcarded() {
57         return wildcarded;
58     }
59
60     @Override
61     public void writeExternal(final ObjectOutput out) throws IOException {
62         out.writeObject(targetType);
63         out.writeBoolean(wildcarded);
64         out.writeInt(hash);
65         out.writeInt(Iterables.size(pathArguments));
66         for (Object o : pathArguments) {
67             out.writeObject(o);
68         }
69     }
70
71     @Override
72     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
73         targetType = (Class<T>) in.readObject();
74         wildcarded = in.readBoolean();
75         hash = in.readInt();
76
77         final int size = in.readInt();
78         final List<PathArgument> args = new ArrayList<>();
79         for (int i = 0; i < size; ++i) {
80             args.add((PathArgument) in.readObject());
81         }
82         pathArguments = ImmutableList.copyOf(args);
83     }
84
85     @Serial
86     Object readResolve() throws ObjectStreamException {
87         return new InstanceIdentifier<>(targetType, pathArguments, wildcarded, hash);
88     }
89 }