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 / FractionDigitsStatementImpl.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 com.google.common.collect.Range;
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.FractionDigitsStatement;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.FractionDigitsEffectiveStatementImpl;
20
21 public class FractionDigitsStatementImpl extends AbstractDeclaredStatement<Integer> implements FractionDigitsStatement {
22     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
23             .FRACTION_DIGITS)
24             .build();
25
26     private static final Range<Integer> FRAC_DIGITS_ALLOWED = Range.closed(1, 18);
27
28     protected FractionDigitsStatementImpl(final StmtContext<Integer, FractionDigitsStatement, ?> context) {
29         super(context);
30     }
31
32     public static class Definition extends AbstractStatementSupport<Integer, FractionDigitsStatement,
33             EffectiveStatement<Integer, FractionDigitsStatement>> {
34
35         public Definition() {
36             super(YangStmtMapping.FRACTION_DIGITS);
37         }
38
39         @Override
40         public Integer parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
41             final Integer fractionDigits;
42             try {
43                 fractionDigits = Integer.valueOf(value);
44             } catch (NumberFormatException e) {
45                 throw new SourceException(ctx.getStatementSourceReference(), e,
46                     "%s is not valid fraction-digits integer argument", value);
47             }
48
49             SourceException.throwIf(!FRAC_DIGITS_ALLOWED.contains(fractionDigits), ctx.getStatementSourceReference(),
50                 "fraction-digits argument should be integer within %s", FRAC_DIGITS_ALLOWED);
51             return fractionDigits;
52         }
53
54         @Override
55         public FractionDigitsStatement createDeclared(final StmtContext<Integer, FractionDigitsStatement, ?> ctx) {
56             return new FractionDigitsStatementImpl(ctx);
57         }
58
59         @Override
60         public EffectiveStatement<Integer, FractionDigitsStatement> createEffective(
61                 final StmtContext<Integer, FractionDigitsStatement,
62                 EffectiveStatement<Integer, FractionDigitsStatement>> ctx) {
63             return new FractionDigitsEffectiveStatementImpl(ctx);
64         }
65
66         @Override
67         protected SubstatementValidator getSubstatementValidator() {
68             return SUBSTATEMENT_VALIDATOR;
69         }
70     }
71
72     @Override
73     public int getValue() {
74         return argument();
75     }
76 }