YANGTOOLS-706: Split up base utility classes into rfc6020.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / BitsSpecificationEffectiveStatementImpl.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.UnknownSchemaNode;
12 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
15 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
17 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
18 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.rfc6020.util.DeclaredEffectiveStatementBase;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22
23 public final class BitsSpecificationEffectiveStatementImpl extends
24         DeclaredEffectiveStatementBase<String, BitsSpecification> implements TypeEffectiveStatement<BitsSpecification> {
25
26     private final BitsTypeDefinition typeDefinition;
27
28     public BitsSpecificationEffectiveStatementImpl(
29             final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
30         super(ctx);
31
32         final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
33         Long highestPosition = null;
34         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
35             if (stmt instanceof BitEffectiveStatementImpl) {
36                 final BitEffectiveStatementImpl bitSubStmt = (BitEffectiveStatementImpl) stmt;
37
38                 final long effectivePos;
39                 if (bitSubStmt.getDeclaredPosition() == null) {
40                     if (highestPosition != null) {
41                         SourceException.throwIf(highestPosition == 4294967295L, ctx.getStatementSourceReference(),
42                                 "Bit %s must have a position statement", bitSubStmt);
43                         effectivePos = highestPosition + 1;
44                     } else {
45                         effectivePos = 0L;
46                     }
47                 } else {
48                     effectivePos = bitSubStmt.getDeclaredPosition();
49                 }
50
51                 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
52                 SourceException.throwIf(bit.getPosition() < 0L && bit.getPosition() > 4294967295L,
53                         ctx.getStatementSourceReference(), "Bit %s has illegal position", bit);
54
55                 if (highestPosition == null || highestPosition < bit.getPosition()) {
56                     highestPosition = bit.getPosition();
57                 }
58
59                 builder.addBit(bit);
60             }
61             if (stmt instanceof UnknownSchemaNode) {
62                 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
63             }
64         }
65
66         typeDefinition = builder.build();
67     }
68
69     @Nonnull
70     @Override
71     public BitsTypeDefinition getTypeDefinition() {
72         return typeDefinition;
73     }
74 }