Consolidate IntegralTypeEffectiveStatementImpl
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / BitsSpecificationEffectiveStatement.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.common.Uint32;
13 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
20 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
21 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
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 BitsSpecificationEffectiveStatement extends DeclaredEffectiveStatementBase<String, BitsSpecification>
27         implements TypeEffectiveStatement<BitsSpecification> {
28
29     private final @NonNull BitsTypeDefinition typeDefinition;
30
31     BitsSpecificationEffectiveStatement(
32             final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
33         super(ctx);
34
35         final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
36         Uint32 highestPosition = null;
37         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
38             if (stmt instanceof BitEffectiveStatement) {
39                 final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
40
41                 final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
42                 final Uint32 effectivePos;
43                 if (declaredPosition.isEmpty()) {
44                     if (highestPosition != null) {
45                         SourceException.throwIf(Uint32.MAX_VALUE.equals(highestPosition),
46                             ctx.getStatementSourceReference(), "Bit %s must have a position statement", bitSubStmt);
47                         effectivePos = Uint32.fromIntBits(highestPosition.intValue() + 1);
48                     } else {
49                         effectivePos = Uint32.ZERO;
50                     }
51                 } else {
52                     effectivePos = declaredPosition.get();
53                 }
54
55                 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
56                 if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
57                     highestPosition = bit.getPosition();
58                 }
59
60                 builder.addBit(bit);
61             }
62             if (stmt instanceof UnknownSchemaNode) {
63                 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
64             }
65         }
66
67         typeDefinition = builder.build();
68     }
69
70     @Override
71     public BitsTypeDefinition getTypeDefinition() {
72         return typeDefinition;
73     }
74 }