Bug 6897: Adding getSubstatementValidator() method to AbstractStatementSupport
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / PositionStatementImpl.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;
9
10 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
11 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
12 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.PositionStatement;
14 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.PositionEffectiveStatementImpl;
19
20 public class PositionStatementImpl extends AbstractDeclaredStatement<Long> implements PositionStatement {
21     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
22         YangStmtMapping.POSITION).build();
23
24     protected PositionStatementImpl(final StmtContext<Long, PositionStatement, ?> context) {
25         super(context);
26     }
27
28     public static class Definition extends AbstractStatementSupport<Long, PositionStatement,
29             EffectiveStatement<Long, PositionStatement>> {
30
31         public Definition() {
32             super(YangStmtMapping.POSITION);
33         }
34
35         @Override
36         public Long parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
37             try {
38                 return Long.parseLong(value);
39             } catch (NumberFormatException e) {
40                 throw new SourceException(String.format("Bit position value %s is not valid integer", value),
41                         ctx.getStatementSourceReference(), e);
42             }
43         }
44
45         @Override
46         public PositionStatement createDeclared(final StmtContext<Long, PositionStatement, ?> ctx) {
47             return new PositionStatementImpl(ctx);
48         }
49
50         @Override
51         public EffectiveStatement<Long, PositionStatement> createEffective(
52                 final StmtContext<Long, PositionStatement, EffectiveStatement<Long, PositionStatement>> ctx) {
53             return new PositionEffectiveStatementImpl(ctx);
54         }
55
56         @Override
57         public void onFullDefinitionDeclared(
58                 final StmtContext.Mutable<Long, PositionStatement, EffectiveStatement<Long, PositionStatement>> stmt) {
59             super.onFullDefinitionDeclared(stmt);
60             getSubstatementValidator().validate(stmt);
61         }
62
63         @Override
64         protected SubstatementValidator getSubstatementValidator() {
65             return SUBSTATEMENT_VALIDATOR;
66         }
67     }
68
69     @Override
70     public long getValue() {
71         return argument();
72     }
73 }