db7cb770650ad1f438d0387592124a0d2e5eac0a
[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.Map;
14 import java.util.Objects;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19
20 /**
21  * Utility abstract class for tracking parser state, as needed by StAX-like parser.
22  * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
23  */
24 @Beta
25 public abstract class AbstractNodeDataWithSchema {
26     private final DataSchemaNode schema;
27     private Map<QName, String> attributes;
28
29     public AbstractNodeDataWithSchema(final DataSchemaNode schema) {
30         this.schema = Preconditions.checkNotNull(schema);
31     }
32
33     /**
34      * Return the associated schema node.
35      *
36      * @return Associated schema node.
37      */
38     public final DataSchemaNode getSchema() {
39         return schema;
40     }
41
42     /**
43      * Set the associated attributes.
44      *
45      * @param attributes parsed attributes
46      */
47     public final void setAttributes(final Map<QName, String> attributes) {
48         Preconditions.checkState(this.attributes == null, "Node '%s' has already set its attributes to %s.",
49                 getSchema().getQName(), this.attributes);
50         this.attributes = attributes;
51     }
52
53     /**
54      * Return the associated attributes.
55      *
56      * @return associated attributes
57      */
58     public final Map<QName, String> getAttributes() {
59         return attributes;
60     }
61
62     /**
63      * Emit this node's events into the specified writer.
64      *
65      * @param writer Target writer
66      * @throws IOException reported when thrown by the writer.
67      */
68     public abstract void write(NormalizedNodeStreamWriter writer) throws IOException;
69
70     protected final NodeIdentifier provideNodeIdentifier() {
71         return NodeIdentifier.create(schema.getQName());
72     }
73
74     @Override
75     public int hashCode() {
76         final int prime = 31;
77         int result = 1;
78         result = prime * result + Objects.hashCode(schema);
79         return result;
80     }
81
82     @Override
83     public boolean equals(final Object obj) {
84         if (this == obj) {
85             return true;
86         }
87         if (obj == null) {
88             return false;
89         }
90         if (getClass() != obj.getClass()) {
91             return false;
92         }
93         final AbstractNodeDataWithSchema other = (AbstractNodeDataWithSchema) obj;
94         return schema.equals(other.schema);
95     }
96
97 }