Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / fraction_digits / FractionDigitsStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.fraction_digits;
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.AbstractStatementSupport;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
17 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
18
19 public final class FractionDigitsStatementSupport extends AbstractStatementSupport<Integer, FractionDigitsStatement,
20         EffectiveStatement<Integer, FractionDigitsStatement>> {
21     private static final Range<Integer> FRAC_DIGITS_ALLOWED = Range.closed(1, 18);
22     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
23         YangStmtMapping.FRACTION_DIGITS)
24         .build();
25     private static final FractionDigitsStatementSupport INSTANCE = new FractionDigitsStatementSupport();
26
27     private FractionDigitsStatementSupport() {
28         super(YangStmtMapping.FRACTION_DIGITS);
29     }
30
31     public static FractionDigitsStatementSupport getInstance() {
32         return INSTANCE;
33     }
34
35     @Override
36     public Integer parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
37         final Integer fractionDigits;
38         try {
39             fractionDigits = Integer.valueOf(value);
40         } catch (NumberFormatException e) {
41             throw new SourceException(ctx.getStatementSourceReference(), e,
42                 "%s is not valid fraction-digits integer argument", value);
43         }
44
45         SourceException.throwIf(!FRAC_DIGITS_ALLOWED.contains(fractionDigits), ctx.getStatementSourceReference(),
46             "fraction-digits argument should be integer within %s", FRAC_DIGITS_ALLOWED);
47         return fractionDigits;
48     }
49
50     @Override
51     public FractionDigitsStatement createDeclared(final StmtContext<Integer, FractionDigitsStatement, ?> ctx) {
52         return new FractionDigitsStatementImpl(ctx);
53     }
54
55     @Override
56     public EffectiveStatement<Integer, FractionDigitsStatement> createEffective(
57             final StmtContext<Integer, FractionDigitsStatement,
58             EffectiveStatement<Integer, FractionDigitsStatement>> ctx) {
59         return new FractionDigitsEffectiveStatementImpl(ctx);
60     }
61
62     @Override
63     protected SubstatementValidator getSubstatementValidator() {
64         return SUBSTATEMENT_VALIDATOR;
65     }
66 }