Use NodeIdentifier.create() in JSON codec
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / AbstractNodeDataWithSchema.java
1 /*
2  * Copyright (c) 2014 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.gson;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12
13 import java.io.IOException;
14
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18
19 /**
20  * Utility abstract class for tracking parser state, as needed by StAX-like parser.
21  */
22 @Beta
23 abstract class AbstractNodeDataWithSchema {
24     private final DataSchemaNode schema;
25
26     protected AbstractNodeDataWithSchema(final DataSchemaNode schema) {
27         this.schema = Preconditions.checkNotNull(schema);
28     }
29
30     /**
31      * Return the associated schema node.
32      * @return
33      */
34     public final DataSchemaNode getSchema() {
35         return schema;
36     }
37
38     /**
39      * Emit this node's events into the specified writer.
40      *
41      * @param writer Target writer
42      * @throws IOException reported when thrown by the writer.
43      */
44     public abstract void write(final NormalizedNodeStreamWriter writer) throws IOException;
45
46     protected final NodeIdentifier provideNodeIdentifier() {
47         return NodeIdentifier.create(schema.getQName());
48     }
49
50     @Override
51     public int hashCode() {
52         final int prime = 31;
53         int result = 1;
54         result = prime * result + ((schema == null) ? 0 : schema.hashCode());
55         return result;
56     }
57
58     @Override
59     public boolean equals(final Object obj) {
60         if (this == obj) {
61             return true;
62         }
63         if (obj == null) {
64             return false;
65         }
66         if (getClass() != obj.getClass()) {
67             return false;
68         }
69         final AbstractNodeDataWithSchema other = (AbstractNodeDataWithSchema) obj;
70         return schema.equals(other.schema);
71     }
72
73 }