Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / EnumSpecificationEffectiveStatementImpl.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 javax.annotation.Nonnull;
11 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
14 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
16 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 import org.opendaylight.yangtools.yang.model.util.type.EnumPairBuilder;
18 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
23
24 public final class EnumSpecificationEffectiveStatementImpl extends
25         DeclaredEffectiveStatementBase<String, EnumSpecification> implements
26         TypeEffectiveStatement<EnumSpecification> {
27
28     private final EnumTypeDefinition typeDefinition;
29
30     public EnumSpecificationEffectiveStatementImpl(
31             final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
32         super(ctx);
33
34         final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
35         Integer highestValue = null;
36         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
37             if (stmt instanceof EnumEffectiveStatementImpl) {
38                 final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
39
40                 final int effectiveValue;
41                 if (enumSubStmt.getDeclaredValue() == null) {
42                     if (highestValue != null) {
43                         SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
44                                 "Enum '%s' must have a value statement", enumSubStmt);
45                         effectiveValue = highestValue + 1;
46                     } else {
47                         effectiveValue = 0;
48                     }
49                 } else {
50                     effectiveValue = enumSubStmt.getDeclaredValue();
51                 }
52
53                 final EnumPair p = EnumPairBuilder.create(enumSubStmt.getName(), effectiveValue)
54                         .setDescription(enumSubStmt.getDescription()).setReference(enumSubStmt.getReference())
55                         .setStatus(enumSubStmt.getStatus()).setUnknownSchemaNodes(enumSubStmt.getUnknownSchemaNodes())
56                         .build();
57
58                 if (highestValue == null || highestValue < p.getValue()) {
59                     highestValue = p.getValue();
60                 }
61
62                 builder.addEnum(p);
63             }
64             if (stmt instanceof UnknownEffectiveStatementImpl) {
65                 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
66             }
67         }
68
69         typeDefinition = builder.build();
70     }
71
72     @Nonnull
73     @Override
74     public EnumTypeDefinition getTypeDefinition() {
75         return typeDefinition;
76     }
77 }