Bug 8675: Fix a design flaw of the new XML parser
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / AbstractNodeDataWithSchema.java
1 /*
2  * Copyright (c) 2016 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.util;
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.NormalizedNodeStreamWriter;
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  * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
21  */
22 @Beta
23 public abstract class AbstractNodeDataWithSchema {
24     private final DataSchemaNode schema;
25
26     public AbstractNodeDataWithSchema(final DataSchemaNode schema) {
27         this.schema = Preconditions.checkNotNull(schema);
28     }
29
30     /**
31      * Return the associated schema node.
32      *
33      * @return Associated schema node.
34      */
35     public final DataSchemaNode getSchema() {
36         return schema;
37     }
38
39     /**
40      * Emit this node's events into the specified writer.
41      *
42      * @param writer Target writer
43      * @throws IOException reported when thrown by the writer.
44      */
45     public abstract void write(NormalizedNodeStreamWriter writer) throws IOException;
46
47     protected final NodeIdentifier provideNodeIdentifier() {
48         return NodeIdentifier.create(schema.getQName());
49     }
50
51     @Override
52     public int hashCode() {
53         final int prime = 31;
54         int result = 1;
55         result = prime * result + Objects.hashCode(schema);
56         return result;
57     }
58
59     @Override
60     public boolean equals(final Object obj) {
61         if (this == obj) {
62             return true;
63         }
64         if (obj == null) {
65             return false;
66         }
67         if (getClass() != obj.getClass()) {
68             return false;
69         }
70         final AbstractNodeDataWithSchema other = (AbstractNodeDataWithSchema) obj;
71         return schema.equals(other.schema);
72     }
73
74 }