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