Bug 2366 - Effective statements impl for new yang parser.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / IdentityEffectiveStatementImpl.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
13 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
17 import com.google.common.collect.ImmutableList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Set;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25
26 public class IdentityEffectiveStatementImpl extends
27         AbstractEffectiveDocumentedNode<QName, IdentityStatement> implements
28         IdentitySchemaNode {
29     private final QName qname;
30     private final SchemaPath path;
31     IdentitySchemaNode baseIdentity;
32     private final Set<IdentitySchemaNode> derivedIdentities;
33
34     ImmutableList<UnknownSchemaNode> unknownNodes;
35
36     public IdentityEffectiveStatementImpl(
37             StmtContext<QName, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> ctx) {
38         super(ctx);
39
40         this.qname = ctx.getStatementArgument();
41         this.path = Utils.getSchemaPath(ctx);
42
43         initSubstatementCollections();
44
45         // :TODO init other fields
46         this.derivedIdentities = null;
47     }
48
49     private void initSubstatementCollections() {
50         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
51
52         LinkedList<UnknownSchemaNode> unknownNodes = new LinkedList<UnknownSchemaNode>();
53
54         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
55             if (effectiveStatement instanceof UnknownSchemaNode) {
56                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
57                 unknownNodes.add(unknownNode);
58             }
59         }
60
61         this.unknownNodes = ImmutableList.copyOf(unknownNodes);
62     }
63
64     @Override
65     public QName getQName() {
66         return qname;
67     }
68
69     @Override
70     public IdentitySchemaNode getBaseIdentity() {
71         return baseIdentity;
72     }
73
74     @Override
75     public Set<IdentitySchemaNode> getDerivedIdentities() {
76         return Collections.unmodifiableSet(derivedIdentities);
77     }
78
79     @Override
80     public SchemaPath getPath() {
81         return path;
82     }
83
84     @Override
85     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
86         return unknownNodes;
87     }
88
89     @Override
90     public int hashCode() {
91         final int prime = 31;
92         int result = 1;
93         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
94         result = prime * result + ((path == null) ? 0 : path.hashCode());
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         IdentityEffectiveStatementImpl other = (IdentityEffectiveStatementImpl) obj;
110         if (qname == null) {
111             if (other.qname != null) {
112                 return false;
113             }
114         } else if (!qname.equals(other.qname)) {
115             return false;
116         }
117         if (path == null) {
118             if (other.path != null) {
119                 return false;
120             }
121         } else if (!path.equals(other.path)) {
122             return false;
123         }
124         return true;
125     }
126
127     @Override
128     public String toString() {
129         StringBuilder sb = new StringBuilder(
130                 IdentityEffectiveStatementImpl.class.getSimpleName());
131         sb.append("[");
132         sb.append("base=").append(baseIdentity);
133         sb.append(", qname=").append(qname);
134         sb.append("]");
135         return sb.toString();
136     }
137 }