Remove implements NormalizedNodeStreamWriter declaration
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeOutputStreamWriter.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
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10
11 import com.google.common.base.Preconditions;
12 import java.io.DataOutput;
13 import java.io.DataOutputStream;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.util.HashMap;
17 import java.util.Map;
18 import org.opendaylight.yangtools.yang.common.QName;
19
20 /**
21  * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
22  * a stream.
23  * A stream writer wrapper around this class will write node objects to stream in recursive manner.
24  * for example - If you have a ContainerNode which has a two LeafNode as children, then
25  * you will first call
26  * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)},
27  * then will call
28  * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice
29  * and then, {@link #endNode()} to end container node.
30  *
31  * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
32  * while reading.
33  */
34 public class NormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput {
35     private final Map<String, Integer> stringCodeMap = new HashMap<>();
36
37     /**
38      * @deprecated Use {@link #NormalizedNodeOutputStreamWriter(DataOutput)} instead.
39      */
40     @Deprecated
41     public NormalizedNodeOutputStreamWriter(final OutputStream stream) throws IOException {
42         this((DataOutput) new DataOutputStream(Preconditions.checkNotNull(stream)));
43     }
44
45     /**
46      * @deprecated Use {@link NormalizedNodeInputOutput#newDataOutput(DataOutput)} instead.
47      */
48     @Deprecated
49     public NormalizedNodeOutputStreamWriter(final DataOutput output) {
50         super(output);
51     }
52
53     @Override
54     protected final short streamVersion() {
55         return TokenTypes.LITHIUM_VERSION;
56     }
57
58     @Override
59     protected void writeQName(final QName qname) throws IOException {
60         writeString(qname.getLocalName());
61         writeString(qname.getNamespace().toString());
62         writeString(qname.getFormattedRevision());
63     }
64
65     @Override
66     protected void writeString(final String string) throws IOException {
67         if (string != null) {
68             final Integer value = stringCodeMap.get(string);
69             if (value == null) {
70                 stringCodeMap.put(string, stringCodeMap.size());
71                 writeByte(TokenTypes.IS_STRING_VALUE);
72                 writeUTF(string);
73             } else {
74                 writeByte(TokenTypes.IS_CODE_VALUE);
75                 writeInt(value);
76             }
77         } else {
78             writeByte(TokenTypes.IS_NULL_VALUE);
79         }
80     }
81 }