Clean up public entrypoints
[yangtools.git] / yang / yang-data-codec-binfmt / src / main / java / org / opendaylight / yangtools / yang / data / codec / binfmt / NormalizedNodeDataInput.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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 com.google.common.annotations.Beta;
11 import java.io.DataInput;
12 import java.io.IOException;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23
24 /**
25  * Interface for reading {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
26  * and {@link SchemaPath}s.
27  */
28 @Beta
29 public interface NormalizedNodeDataInput extends DataInput {
30     /**
31      * Interpret current stream position as a NormalizedNode, stream its events into a NormalizedNodeStreamWriter.
32      *
33      * @param writer Writer to emit events to
34      * @throws IOException if an error occurs
35      * @throws IllegalStateException if the dictionary has been detached
36      * @throws NullPointerException if {@code writer} is null
37      */
38     void streamNormalizedNode(NormalizedNodeStreamWriter writer) throws IOException;
39
40     /**
41      * Read a normalized node from the reader.
42      *
43      * @return Next node from the stream, or null if end of stream has been reached.
44      * @throws IOException if an error occurs
45      * @throws IllegalStateException if the dictionary has been detached
46      */
47     default NormalizedNode<?, ?> readNormalizedNode() throws IOException {
48         return readNormalizedNode(ReusableImmutableNormalizedNodeStreamWriter.create());
49     }
50
51     /**
52      * Read a normalized node from the reader, using specified writer to construct the result.
53      *
54      * @param receiver Reusable receiver to, expected to be reset
55      * @return Next node from the stream, or null if end of stream has been reached.
56      * @throws IOException if an error occurs
57      * @throws IllegalStateException if the dictionary has been detached
58      */
59     default NormalizedNode<?, ?> readNormalizedNode(final ReusableStreamReceiver receiver) throws IOException {
60         try {
61             streamNormalizedNode(receiver);
62             return receiver.getResult();
63         } finally {
64             receiver.reset();
65         }
66     }
67
68     YangInstanceIdentifier readYangInstanceIdentifier() throws IOException;
69
70     @NonNull QName readQName() throws IOException;
71
72     PathArgument readPathArgument() throws IOException;
73
74     SchemaPath readSchemaPath() throws IOException;
75
76     /**
77      * Return the version of the underlying input stream.
78      *
79      * @return Stream version
80      * @throws IOException if the version cannot be ascertained
81      */
82     NormalizedNodeStreamVersion getVersion() throws IOException;
83
84     default Optional<NormalizedNode<?, ?>> readOptionalNormalizedNode() throws IOException {
85         return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
86     }
87
88     /**
89      * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method first reads
90      * and validates that the input contains a valid NormalizedNode stream.
91      *
92      * @param input the DataInput to read from
93      * @return a new {@link NormalizedNodeDataInput} instance
94      * @throws InvalidNormalizedNodeStreamException if the stream version is not supported
95      * @throws IOException if an error occurs reading from the input
96      */
97     static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
98         return new VersionedNormalizedNodeDataInput(input).delegate();
99     }
100
101     /**
102      * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method does not
103      * perform any initial validation of the input stream.
104      *
105      * @param input the DataInput to read from
106      * @return a new {@link NormalizedNodeDataInput} instance
107      * @deprecated Use {@link #newDataInput(DataInput)} instead.
108      */
109     // FIXME: 5.0.0: deprecate for removal
110     @Deprecated
111     static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
112         return new VersionedNormalizedNodeDataInput(input);
113     }
114 }