Merge "Bug 2997: Fixed instanceof checks to use interfaces"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ExtensionEffectiveStatementImpl.java
1 /**
2  * Copyright (c) 2015 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.parser.stmt.rfc6020.effective;
9
10 import java.util.Collection;
11 import java.util.LinkedList;
12 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
16 import com.google.common.collect.ImmutableList;
17 import java.util.List;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
22
23 public class ExtensionEffectiveStatementImpl extends
24         AbstractEffectiveDocumentedNode<QName, ExtensionStatement> implements
25         ExtensionDefinition {
26     private final QName qname;
27     String argument;
28     private final SchemaPath schemaPath;
29
30     ImmutableList<UnknownSchemaNode> unknownNodes;
31     boolean yin;
32
33     public ExtensionEffectiveStatementImpl(
34             StmtContext<QName, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>> ctx) {
35         super(ctx);
36
37         this.qname = ctx.getStatementArgument();
38         this.schemaPath = Utils.getSchemaPath(ctx);
39
40         initSubstatementCollections();
41         initFields();
42
43     }
44
45     private void initFields() {
46
47         ArgumentEffectiveStatementImpl argumentSubstatement = firstEffective(ArgumentEffectiveStatementImpl.class);
48
49         if (argumentSubstatement != null) {
50             this.argument = argumentSubstatement.argument().getLocalName();
51         }
52
53         YinElementEffectiveStatementImpl yinElement = firstEffective(YinElementEffectiveStatementImpl.class);
54
55         if (yinElement != null) {
56             this.yin = yinElement.argument();
57         } else
58             yin = false;
59
60     }
61
62     private void initSubstatementCollections() {
63         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
64
65         LinkedList<UnknownSchemaNode> unknownNodes = new LinkedList<UnknownSchemaNode>();
66
67         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
68             if (effectiveStatement instanceof UnknownSchemaNode) {
69                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
70                 unknownNodes.add(unknownNode);
71             }
72         }
73
74         this.unknownNodes = ImmutableList.copyOf(unknownNodes);
75     }
76
77     @Override
78     public QName getQName() {
79         return qname;
80     }
81
82     @Override
83     public SchemaPath getPath() {
84         return schemaPath;
85     }
86
87     @Override
88     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
89         return unknownNodes;
90     }
91
92     @Override
93     public String getArgument() {
94         return argument;
95     }
96
97     @Override
98     public boolean isYinElement() {
99         return yin;
100     }
101
102     @Override
103     public int hashCode() {
104         final int prime = 31;
105         int result = 1;
106         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
107         result = prime * result
108                 + ((schemaPath == null) ? 0 : schemaPath.hashCode());
109         return result;
110     }
111
112     @Override
113     public boolean equals(final Object obj) {
114         if (this == obj) {
115             return true;
116         }
117         if (obj == null) {
118             return false;
119         }
120         if (getClass() != obj.getClass()) {
121             return false;
122         }
123         ExtensionEffectiveStatementImpl other = (ExtensionEffectiveStatementImpl) obj;
124         if (qname == null) {
125             if (other.qname != null) {
126                 return false;
127             }
128         } else if (!qname.equals(other.qname)) {
129             return false;
130         }
131         if (schemaPath == null) {
132             if (other.schemaPath != null) {
133                 return false;
134             }
135         } else if (!schemaPath.equals(other.schemaPath)) {
136             return false;
137         }
138         return true;
139     }
140
141     @Override
142     public String toString() {
143         StringBuilder sb = new StringBuilder(
144                 ExtensionEffectiveStatementImpl.class.getSimpleName());
145         sb.append("[");
146         sb.append("argument=").append(argument);
147         sb.append(", qname=").append(qname);
148         sb.append(", schemaPath=").append(schemaPath);
149         sb.append(", extensionSchemaNodes=").append(unknownNodes);
150         sb.append(", yin=").append(yin);
151         sb.append("]");
152         return sb.toString();
153     }
154 }