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