Convert AbstractEnumStatementSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / EnumSpecificationEffectiveStatement.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.type;
9
10 import java.util.Optional;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
20 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
21 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25
26 final class EnumSpecificationEffectiveStatement extends DeclaredEffectiveStatementBase<String, EnumSpecification>
27         implements TypeEffectiveStatement<EnumSpecification> {
28
29     private final @NonNull EnumTypeDefinition typeDefinition;
30
31     EnumSpecificationEffectiveStatement(
32             final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
33         super(ctx);
34
35         final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
36         Integer highestValue = null;
37         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
38             if (stmt instanceof EnumEffectiveStatement) {
39                 final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) stmt;
40
41                 final Optional<Integer> declaredValue =
42                         enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
43                 final int effectiveValue;
44                 if (declaredValue.isEmpty()) {
45                     if (highestValue != null) {
46                         SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
47                                 "Enum '%s' must have a value statement", enumSubStmt);
48                         effectiveValue = highestValue + 1;
49                     } else {
50                         effectiveValue = 0;
51                     }
52                 } else {
53                     effectiveValue = declaredValue.orElseThrow();
54                 }
55
56                 final EnumPair pair = EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue);
57                 if (highestValue == null || highestValue < pair.getValue()) {
58                     highestValue = pair.getValue();
59                 }
60
61                 builder.addEnum(pair);
62             }
63             if (stmt instanceof UnknownSchemaNode) {
64                 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
65             }
66         }
67
68         typeDefinition = builder.build();
69     }
70
71     @Override
72     public EnumTypeDefinition getTypeDefinition() {
73         return typeDefinition;
74     }
75 }