30ba7d425bfefd941e7b0eacc05ed9b536446be1
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ListEffectiveStatementImpl.java
1 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
2
3 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
4
5 import java.util.Collection;
6 import java.util.HashSet;
7 import java.util.LinkedList;
8 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
9 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
10 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
11 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.List;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
20 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
24
25 public class ListEffectiveStatementImpl extends
26         AbstractEffectiveDocumentedDataNodeContainer<QName, ListStatement>
27         implements ListSchemaNode, DerivableSchemaNode {
28     private final QName qname;
29     private final SchemaPath path;
30
31     boolean augmenting;
32     boolean addedByUses;
33     ListSchemaNode original;
34     boolean configuration;
35     ConstraintDefinition constraints;
36     boolean userOrdered;
37
38     ImmutableList<QName> keyDefinition;
39     ImmutableSet<AugmentationSchema> augmentations;
40     ImmutableList<UnknownSchemaNode> unknownNodes;
41
42     public ListEffectiveStatementImpl(
43             StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
44         super(ctx);
45         this.qname = ctx.getStatementArgument();
46         this.path = Utils.getSchemaPath(ctx);
47         // :TODO init other fields
48
49         initKeyDefinition();
50         initSubstatementCollections();
51     }
52
53     /**
54      *
55      */
56     private void initKeyDefinition() {
57         List<QName> keyDefinition = new LinkedList<QName>();
58         KeyEffectiveStatementImpl key = firstEffective(KeyEffectiveStatementImpl.class);
59
60         if (key != null) {
61             Collection<SchemaNodeIdentifier> keyParts = key.argument();
62             for (SchemaNodeIdentifier keyPart : keyParts) {
63                 keyDefinition.add(keyPart.getLastComponent());
64             }
65         }
66
67         this.keyDefinition = ImmutableList.copyOf(keyDefinition);
68     }
69
70     private void initSubstatementCollections() {
71         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
72
73         LinkedList<UnknownSchemaNode> unknownNodes = new LinkedList<UnknownSchemaNode>();
74         HashSet<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
75
76         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
77             if (effectiveStatement instanceof UnknownSchemaNode) {
78                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
79                 unknownNodes.add(unknownNode);
80             }
81             if (effectiveStatement instanceof AugmentationSchema) {
82                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
83                 augmentations.add(augmentationSchema);
84             }
85         }
86
87         this.unknownNodes = ImmutableList.copyOf(unknownNodes);
88         this.augmentations = ImmutableSet.copyOf(augmentations);
89     }
90
91     @Override
92     public QName getQName() {
93         return qname;
94     }
95
96     @Override
97     public SchemaPath getPath() {
98         return path;
99     }
100
101     @Override
102     public List<QName> getKeyDefinition() {
103         return keyDefinition;
104     }
105
106     @Override
107     public boolean isAugmenting() {
108         return augmenting;
109     }
110
111     @Override
112     public boolean isAddedByUses() {
113         return addedByUses;
114     }
115
116     @Override
117     public Optional<ListSchemaNode> getOriginal() {
118         return Optional.fromNullable(original);
119     }
120
121     @Override
122     public boolean isConfiguration() {
123         return configuration;
124     }
125
126     @Override
127     public ConstraintDefinition getConstraints() {
128         return constraints;
129     }
130
131     @Override
132     public Set<AugmentationSchema> getAvailableAugmentations() {
133         return augmentations;
134     }
135
136     @Override
137     public boolean isUserOrdered() {
138         return userOrdered;
139     }
140
141     @Override
142     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
143         return unknownNodes;
144     }
145
146     @Override
147     public int hashCode() {
148         final int prime = 31;
149         int result = 1;
150         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
151         result = prime * result + ((path == null) ? 0 : path.hashCode());
152         return result;
153     }
154
155     @Override
156     public boolean equals(final Object obj) {
157         if (this == obj) {
158             return true;
159         }
160         if (obj == null) {
161             return false;
162         }
163         if (getClass() != obj.getClass()) {
164             return false;
165         }
166         final ListEffectiveStatementImpl other = (ListEffectiveStatementImpl) obj;
167         if (qname == null) {
168             if (other.qname != null) {
169                 return false;
170             }
171         } else if (!qname.equals(other.qname)) {
172             return false;
173         }
174         if (path == null) {
175             if (other.path != null) {
176                 return false;
177             }
178         } else if (!path.equals(other.path)) {
179             return false;
180         }
181         return true;
182     }
183
184     @Override
185     public String toString() {
186         return "list " + qname.getLocalName();
187     }
188 }