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