BUG-4861: Make FractionDigitsStatement#getValue() return an int
[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.base.Preconditions;
11 import com.google.common.collect.Range;
12 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
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.FractionDigitsEffectiveStatementImpl;
21
22 public class FractionDigitsStatementImpl extends AbstractDeclaredStatement<Integer> implements FractionDigitsStatement {
23     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
24             .FRACTION_DIGITS)
25             .build();
26
27     private static final Range<Integer> FRAC_DIGITS_ALLOWED = Range.closed(1, 18);
28
29     protected FractionDigitsStatementImpl(final StmtContext<Integer, FractionDigitsStatement, ?> context) {
30         super(context);
31     }
32
33     public static class Definition
34             extends
35             AbstractStatementSupport<Integer, FractionDigitsStatement, EffectiveStatement<Integer, FractionDigitsStatement>> {
36
37         public Definition() {
38             super(Rfc6020Mapping.FRACTION_DIGITS);
39         }
40
41         @Override
42         public Integer parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
43
44             int fractionDigits;
45
46             try {
47                 fractionDigits = Integer.parseInt(value);
48             } catch (NumberFormatException e) {
49                 throw new IllegalArgumentException(String.format("%s is not valid fraction-digits integer argument",
50                         value), e);
51             }
52
53             Preconditions.checkArgument(FRAC_DIGITS_ALLOWED.contains(fractionDigits),
54                 "fraction-digits argument should be integer within %s", FRAC_DIGITS_ALLOWED);
55
56             return fractionDigits;
57         }
58
59         @Override
60         public FractionDigitsStatement createDeclared(final StmtContext<Integer, FractionDigitsStatement, ?> ctx) {
61             return new FractionDigitsStatementImpl(ctx);
62         }
63
64         @Override
65         public EffectiveStatement<Integer, FractionDigitsStatement> createEffective(
66                 final StmtContext<Integer, FractionDigitsStatement, EffectiveStatement<Integer, FractionDigitsStatement>> ctx) {
67             return new FractionDigitsEffectiveStatementImpl(ctx);
68         }
69
70         @Override
71         public void onFullDefinitionDeclared(final StmtContext.Mutable<Integer, FractionDigitsStatement,
72                 EffectiveStatement<Integer, FractionDigitsStatement>> stmt) throws SourceException {
73             super.onFullDefinitionDeclared(stmt);
74             SUBSTATEMENT_VALIDATOR.validate(stmt);
75         }
76     }
77
78     @Override
79     public int getValue() {
80         return argument();
81     }
82 }