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