Clean up more Sonar warnings
[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.NormalizedNodeStreamWriter.MetadataExtension;
23 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30
31 /**
32  * Utility abstract class for tracking parser state, as needed by StAX-like parser.
33  * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
34  */
35 @Beta
36 public abstract sealed class AbstractNodeDataWithSchema<T extends DataSchemaNode>
37         permits SimpleNodeDataWithSchema, CompositeNodeDataWithSchema {
38     private final T schema;
39     private ImmutableMap<QName, Object> attributes;
40
41     AbstractNodeDataWithSchema(final T schema) {
42         this.schema = requireNonNull(schema);
43     }
44
45     public static @NonNull AbstractNodeDataWithSchema<?> of(final DataSchemaNode schema) {
46         if (schema instanceof ContainerLike containerLike) {
47             return new ContainerNodeDataWithSchema(containerLike);
48         } else if (schema instanceof ListSchemaNode list) {
49             return new ListNodeDataWithSchema(list);
50         } else if (schema instanceof AnyxmlSchemaNode anyxml) {
51             return new AnyXmlNodeDataWithSchema(anyxml);
52         } else if (schema instanceof LeafSchemaNode leaf) {
53             return new LeafNodeDataWithSchema(leaf);
54         } else if (schema instanceof LeafListSchemaNode leafList) {
55             return new LeafListNodeDataWithSchema(leafList);
56         } else if (schema instanceof AnydataSchemaNode anydata) {
57             return new AnydataNodeDataWithSchema(anydata);
58         } else {
59             throw new IllegalStateException("Unsupported schema " + schema);
60         }
61     }
62
63     /**
64      * Return the associated schema node.
65      *
66      * @return Associated schema node.
67      */
68     public final @NonNull T getSchema() {
69         return schema;
70     }
71
72     /**
73      * Set the associated attributes.
74      *
75      * @param attributes parsed attributes
76      */
77     public final void setAttributes(final ImmutableMap<QName, Object> attributes) {
78         checkState(this.attributes == null, "Node '%s' has already set its attributes to %s.", getSchema().getQName(),
79                 this.attributes);
80         this.attributes = attributes;
81     }
82
83     /**
84      * Return the associated attributes.
85      *
86      * @return associated attributes
87      */
88     public final ImmutableMap<QName, Object> getAttributes() {
89         return attributes;
90     }
91
92     /**
93      * Emit this node's events into the specified writer.
94      *
95      * @param writer Target writer
96      * @throws IOException reported when thrown by the writer.
97      */
98     public final void write(final NormalizedNodeStreamWriter writer) throws IOException {
99         write(writer, writer.extension(MetadataExtension.class));
100     }
101
102     protected abstract void write(NormalizedNodeStreamWriter writer, @Nullable MetadataExtension metaWriter)
103         throws IOException;
104
105     protected final NodeIdentifier provideNodeIdentifier() {
106         return NodeIdentifier.create(schema.getQName());
107     }
108
109     protected final void writeMetadata(final MetadataExtension metaWriter) throws IOException {
110         if (metaWriter != null && attributes != null && !attributes.isEmpty()) {
111             metaWriter.metadata(attributes);
112         }
113     }
114
115     @Override
116     public int hashCode() {
117         final int prime = 31;
118         int result = 1;
119         result = prime * result + Objects.hashCode(schema);
120         return result;
121     }
122
123     @Override
124     public boolean equals(final Object obj) {
125         if (this == obj) {
126             return true;
127         }
128         if (obj == null) {
129             return false;
130         }
131         if (getClass() != obj.getClass()) {
132             return false;
133         }
134         final AbstractNodeDataWithSchema<?> other = (AbstractNodeDataWithSchema<?>) obj;
135         return schema.equals(other.schema);
136     }
137
138 }