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