Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / bit / 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.rfc7950.stmt.bit;
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.eclipse.jdt.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.BitEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.BitStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.PositionEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDocumentedNode;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25
26 // FIXME: hide this class
27 public final class BitEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, BitStatement>
28         implements BitEffectiveStatement {
29
30     private final QName qname;
31     private final SchemaPath schemaPath;
32     private final Long declaredPosition;
33     private final @NonNull ImmutableList<UnknownSchemaNode> unknownSchemaNodes;
34
35     BitEffectiveStatementImpl(final StmtContext<QName, BitStatement, ?> ctx) {
36         super(ctx);
37
38         qname = ctx.getStatementArgument();
39         schemaPath = ctx.getSchemaPath().get();
40
41         final List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
42         Long declaredPositionInit = null;
43         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
44             if (effectiveStatement instanceof PositionEffectiveStatement) {
45                 declaredPositionInit = ((PositionEffectiveStatement) effectiveStatement).argument();
46             }
47             if (effectiveStatement instanceof UnknownSchemaNode) {
48                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
49             }
50         }
51
52         declaredPosition = declaredPositionInit;
53         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
54     }
55
56     public Long getDeclaredPosition() {
57         return declaredPosition;
58     }
59
60     public String getName() {
61         return qname.getLocalName();
62     }
63
64     public QName getQName() {
65         return qname;
66     }
67
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(declaredPosition);
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         final 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                 + declaredPosition + "]";
107     }
108 }