Merge "Direct schema node lookup in SchemaUtils"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / LeafListEffectiveStatementImpl.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.parser.spi.meta.StmtContext;
16 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
17 import com.google.common.base.Optional;
18 import com.google.common.collect.ImmutableList;
19 import java.util.List;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
27
28 public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, LeafListStatement> implements LeafListSchemaNode, DerivableSchemaNode {
29     private final QName qname;
30     private final SchemaPath path;
31
32     boolean augmenting;
33     boolean addedByUses;
34     LeafListSchemaNode original;
35     boolean configuration;
36     ConstraintDefinition constraintsDef;
37     TypeDefinition<?> type;
38     boolean userOrdered;
39
40     private ImmutableList<UnknownSchemaNode> unknownNodes;
41
42     public LeafListEffectiveStatementImpl(StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
43         super(ctx);
44         this.qname = ctx.getStatementArgument();
45         this.path = Utils.getSchemaPath(ctx);
46         //:TODO init other fields
47
48         initSubstatementCollections();
49     }
50
51     private void initSubstatementCollections() {
52         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
53
54         LinkedList<UnknownSchemaNode> unknownNodes = new LinkedList<UnknownSchemaNode>();
55
56         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
57             if (effectiveStatement instanceof UnknownSchemaNode) {
58                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
59                 unknownNodes.add(unknownNode);
60             }
61         }
62
63         this.unknownNodes = ImmutableList.copyOf(unknownNodes);
64     }
65
66     @Override
67     public QName getQName() {
68         return qname;
69     }
70
71     @Override
72     public SchemaPath getPath() {
73         return path;
74     }
75
76     @Override
77     public boolean isAugmenting() {
78         return augmenting;
79     }
80
81     @Override
82     public boolean isAddedByUses() {
83         return addedByUses;
84     }
85
86     @Override
87     public Optional<LeafListSchemaNode> getOriginal() {
88         return Optional.fromNullable(original);
89     }
90
91     @Override
92     public boolean isConfiguration() {
93         return configuration;
94     }
95
96     @Override
97     public ConstraintDefinition getConstraints() {
98         return constraintsDef;
99     }
100
101     @Override
102     public TypeDefinition<?> getType() {
103         return type;
104     }
105
106     @Override
107     public boolean isUserOrdered() {
108         return userOrdered;
109     }
110
111     @Override
112     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
113         return unknownNodes;
114     }
115
116     @Override
117     public int hashCode() {
118         final int prime = 31;
119         int result = 1;
120         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
121         result = prime * result + ((path == null) ? 0 : path.hashCode());
122         return result;
123     }
124
125     @Override
126     public boolean equals(final Object obj) {
127         if (this == obj) {
128             return true;
129         }
130         if (obj == null) {
131             return false;
132         }
133         if (getClass() != obj.getClass()) {
134             return false;
135         }
136         LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
137         if (qname == null) {
138             if (other.qname != null) {
139                 return false;
140             }
141         } else if (!qname.equals(other.qname)) {
142             return false;
143         }
144         if (path == null) {
145             if (other.path != null) {
146                 return false;
147             }
148         } else if (!path.equals(other.path)) {
149             return false;
150         }
151         return true;
152     }
153
154     @Override
155     public String toString() {
156         StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
157         sb.append("[");
158         sb.append(qname);
159         sb.append("]");
160         return sb.toString();
161     }
162 }