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