Move SchemaNodeIdentifier to yang-common
[yangtools.git] / codec / yang-data-codec-binfmt / src / main / java / org / opendaylight / yangtools / yang / data / codec / binfmt / AbstractNormalizedNodeDataInput.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.codec.binfmt;
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.DataInput;
15 import java.io.IOException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.SchemaNodeIdentifier;
19 import org.opendaylight.yangtools.yang.common.SchemaNodeIdentifier.Absolute;
20 import org.opendaylight.yangtools.yang.common.SchemaNodeIdentifier.Descendant;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22
23 abstract class AbstractNormalizedNodeDataInput extends ForwardingDataInput implements NormalizedNodeDataInput {
24     // Visible for subclasses
25     final @NonNull DataInput input;
26
27     AbstractNormalizedNodeDataInput(final DataInput input) {
28         this.input = requireNonNull(input);
29     }
30
31     @Override
32     final DataInput delegate() {
33         return input;
34     }
35
36     @Override
37     @Deprecated
38     public final SchemaPath readSchemaPath() throws IOException {
39         final boolean absolute = input.readBoolean();
40         return SchemaPath.create(readQNames(), absolute);
41     }
42
43     @Override
44     public final SchemaNodeIdentifier readSchemaNodeIdentifier() throws IOException {
45         final boolean absolute = input.readBoolean();
46         final ImmutableList<QName> qnames = readQNames();
47         return absolute ? Absolute.of(qnames) : Descendant.of(qnames);
48     }
49
50     private ImmutableList<QName> readQNames() throws IOException {
51         final int size = input.readInt();
52         final Builder<QName> qnames = ImmutableList.builderWithExpectedSize(size);
53         for (int i = 0; i < size; ++i) {
54             qnames.add(readQName());
55         }
56         return qnames.build();
57     }
58 }