Introduce formatting methods for SourceException
[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 org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
11 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
13 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
15 import org.opendaylight.yangtools.yang.model.util.BitImpl;
16 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
22
23 public final class BitsSpecificationEffectiveStatementImpl extends
24         DeclaredEffectiveStatementBase<String, BitsSpecification> implements TypeEffectiveStatement<BitsSpecification> {
25
26     private final BitsTypeDefinition typeDefinition;
27
28     public BitsSpecificationEffectiveStatementImpl(final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
29         super(ctx);
30
31         final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
32         Long highestPosition = null;
33         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
34             if (stmt instanceof Bit) {
35                 Bit b = (Bit) stmt;
36
37                 if (b.getPosition() == null) {
38                     final Long newPos;
39                     if (highestPosition == null) {
40                         newPos = 0L;
41                     } else if (highestPosition != 4294967295L) {
42                         newPos = highestPosition + 1;
43                     } else {
44                         throw new SourceException(ctx.getStatementSourceReference(),
45                             "Bit %s must have a position statement", b);
46                     }
47
48                     b = new BitImpl(newPos, b.getQName(), b.getPath(), b.getDescription(), b.getReference(),
49                         b.getStatus(), b.getUnknownSchemaNodes());
50                 }
51
52                 SourceException.throwIf(b.getPosition() < 0L && b.getPosition() > 4294967295L,
53                         ctx.getStatementSourceReference(), "Bit %s has illegal position", b);
54
55                 if (highestPosition == null || highestPosition < b.getPosition()) {
56                     highestPosition = b.getPosition();
57                 }
58
59                 builder.addBit(b);
60             }
61             if (stmt instanceof UnknownEffectiveStatementImpl) {
62                 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt);
63             }
64         }
65
66         typeDefinition = builder.build();
67     }
68
69     @Override
70     public BitsTypeDefinition getTypeDefinition() {
71         return typeDefinition;
72     }
73 }