BUG-4556: Introduce StmtContext.getSchemaPath()
[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.Status;
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.DescriptionEffectiveStatementImpl;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.PositionEffectiveStatementImpl;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ReferenceEffectiveStatementImpl;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.StatusEffectiveStatementImpl;
27
28 public class BitEffectiveStatementImpl extends EffectiveStatementBase<QName, BitStatement> implements
29         BitsTypeDefinition.Bit {
30
31     private final QName qName;
32     private final SchemaPath schemaPath;
33     private Long position;
34     private String description;
35     private String reference;
36     private Status status;
37     private final List<UnknownSchemaNode> unknownSchemaNodes;
38
39     public BitEffectiveStatementImpl(final StmtContext<QName, BitStatement, ?> ctx) {
40         super(ctx);
41
42         List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
43
44         qName = ctx.getStatementArgument();
45         schemaPath = ctx.getSchemaPath().get();
46
47         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
48             if (effectiveStatement instanceof DescriptionEffectiveStatementImpl) {
49                 description = ((DescriptionEffectiveStatementImpl) effectiveStatement).argument();
50             }
51             if (effectiveStatement instanceof ReferenceEffectiveStatementImpl) {
52                 reference = ((ReferenceEffectiveStatementImpl) effectiveStatement).argument();
53             }
54             if (effectiveStatement instanceof StatusEffectiveStatementImpl) {
55                 status = ((StatusEffectiveStatementImpl) effectiveStatement).argument();
56             }
57             if (effectiveStatement instanceof PositionEffectiveStatementImpl) {
58                 position = ((PositionEffectiveStatementImpl) effectiveStatement).argument();
59             }
60
61             if (effectiveStatement instanceof UnknownSchemaNode) {
62                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
63             }
64         }
65
66         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
67     }
68
69     @Override
70     public Long getPosition() {
71         return position;
72     }
73
74     @Override
75     public String getName() {
76         return qName.getLocalName();
77     }
78
79     @Override
80     public QName getQName() {
81         return qName;
82     }
83
84     @Override
85     public SchemaPath getPath() {
86         return schemaPath;
87     }
88
89     @Override
90     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
91         return unknownSchemaNodes;
92     }
93
94     @Override
95     public String getDescription() {
96         return description;
97     }
98
99     @Override
100     public String getReference() {
101         return reference;
102     }
103
104     @Override
105     public Status getStatus() {
106         return status;
107     }
108
109     @Override
110     public int hashCode() {
111         final int prime = 31;
112         int result = 1;
113         result = prime * result + qName.hashCode();
114         result = prime * result + schemaPath.hashCode();
115         result = prime * result + Objects.hashCode(position);
116         result = prime * result + Objects.hashCode(unknownSchemaNodes);
117         return result;
118     }
119
120     @Override
121     public boolean equals(final Object obj) {
122         if (this == obj) {
123             return true;
124         }
125         if (obj == null) {
126             return false;
127         }
128         if (getClass() != obj.getClass()) {
129             return false;
130         }
131         BitsTypeDefinition.Bit other = (BitsTypeDefinition.Bit) obj;
132         return Objects.equals(qName, other.getQName()) && Objects.equals(schemaPath, other.getPath());
133     }
134
135     @Override
136     public String toString() {
137         return BitEffectiveStatementImpl.class.getSimpleName() + "[name=" + qName.getLocalName() + ", position="
138                 + position + "]";
139     }
140 }