YANGTOOLS-706: Retrofit EffectiveStatement interfaces into parser
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / EnumEffectiveStatementImpl.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 javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.EnumStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AbstractEffectiveDocumentedNode;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ValueEffectiveStatementImpl;
21
22 public class EnumEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<String, EnumStatement>
23         implements EnumEffectiveStatement {
24     private final List<UnknownSchemaNode> unknownSchemaNodes;
25     private final String name;
26     private final Integer declaredValue;
27
28     public EnumEffectiveStatementImpl(final StmtContext<String, EnumStatement, ?> ctx) {
29         super(ctx);
30
31         name = ctx.rawStatementArgument();
32
33         final List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
34         Integer declaredValueInit = null;
35         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
36             if (effectiveStatement instanceof ValueEffectiveStatementImpl) {
37                 declaredValueInit = ((ValueEffectiveStatementImpl) effectiveStatement).argument();
38             }
39             if (effectiveStatement instanceof UnknownSchemaNode) {
40                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
41             }
42         }
43
44         declaredValue = declaredValueInit;
45         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
46     }
47
48     public String getName() {
49         return name;
50     }
51
52     public Integer getDeclaredValue() {
53         return declaredValue;
54     }
55
56     @Nonnull
57     @Override
58     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
59         return unknownSchemaNodes;
60     }
61 }