ac4c2c9451fdcf9e363a9125fdc9873f7c2fca32
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / BitEffectiveStatementImpl.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.type;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Objects;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.BitStatement;
20 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AbstractEffectiveDocumentedNode;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.PositionEffectiveStatementImpl;
24
25 public class BitEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, BitStatement> {
26
27     private final QName qname;
28     private final SchemaPath schemaPath;
29     private final Long declaredPosition;
30     private final List<UnknownSchemaNode> unknownSchemaNodes;
31
32     public BitEffectiveStatementImpl(final StmtContext<QName, BitStatement, ?> ctx) {
33         super(ctx);
34
35         qname = ctx.getStatementArgument();
36         schemaPath = ctx.getSchemaPath().get();
37
38         final List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
39         Long declaredPositionInit = null;
40         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
41             if (effectiveStatement instanceof PositionEffectiveStatementImpl) {
42                 declaredPositionInit = ((PositionEffectiveStatementImpl) effectiveStatement).argument();
43             }
44             if (effectiveStatement instanceof UnknownSchemaNode) {
45                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
46             }
47         }
48
49         declaredPosition = declaredPositionInit;
50         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
51     }
52
53     public Long getDeclaredPosition() {
54         return declaredPosition;
55     }
56
57     public String getName() {
58         return qname.getLocalName();
59     }
60
61     public QName getQName() {
62         return qname;
63     }
64
65     public SchemaPath getPath() {
66         return schemaPath;
67     }
68
69     @Nonnull
70     @Override
71     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
72         return unknownSchemaNodes;
73     }
74
75     @Override
76     public int hashCode() {
77         final int prime = 31;
78         int result = 1;
79         result = prime * result + qname.hashCode();
80         result = prime * result + schemaPath.hashCode();
81         result = prime * result + Objects.hashCode(declaredPosition);
82         result = prime * result + Objects.hashCode(unknownSchemaNodes);
83         return result;
84     }
85
86     @Override
87     public boolean equals(final Object obj) {
88         if (this == obj) {
89             return true;
90         }
91         if (obj == null) {
92             return false;
93         }
94         if (getClass() != obj.getClass()) {
95             return false;
96         }
97         final BitsTypeDefinition.Bit other = (BitsTypeDefinition.Bit) obj;
98         return Objects.equals(qname, other.getQName()) && Objects.equals(schemaPath, other.getPath());
99     }
100
101     @Override
102     public String toString() {
103         return BitEffectiveStatementImpl.class.getSimpleName() + "[name=" + qname.getLocalName() + ", position="
104                 + declaredPosition + "]";
105     }
106 }