Bug 2366 - Effective statments impl merge, retest & bugfix
[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 org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
11 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsStatement;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
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.stmt.rfc6020.effective.FractionDigitsEffectiveStatementImpl;
17
18 import com.google.common.collect.Range;
19
20 public class FractionDigitsStatementImpl extends AbstractDeclaredStatement<Integer> implements FractionDigitsStatement {
21
22     private static final Range<Integer> FRAC_DIGITS_ALLOWED = Range.closed(1, 18);
23
24     protected FractionDigitsStatementImpl(StmtContext<Integer, FractionDigitsStatement, ?> context) {
25         super(context);
26     }
27
28     public static class Definition
29             extends
30             AbstractStatementSupport<Integer, FractionDigitsStatement, EffectiveStatement<Integer, FractionDigitsStatement>> {
31
32         public Definition() {
33             super(Rfc6020Mapping.FRACTION_DIGITS);
34         }
35
36         @Override
37         public Integer parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
38
39             int fractionDigits;
40
41             try {
42                 fractionDigits = Integer.parseInt(value);
43             } catch (NumberFormatException e) {
44                 throw new IllegalArgumentException(String.format("%s is not valid fraction-digits integer argument",
45                         value), e);
46             }
47
48             if (!FRAC_DIGITS_ALLOWED.contains(fractionDigits)) {
49                 throw new IllegalArgumentException(String.format("fraction-digits argument should be integer within %s",
50                         FRAC_DIGITS_ALLOWED));
51             }
52
53             return fractionDigits;
54         }
55
56         @Override
57         public FractionDigitsStatement createDeclared(StmtContext<Integer, FractionDigitsStatement, ?> ctx) {
58             return new FractionDigitsStatementImpl(ctx);
59         }
60
61         @Override
62         public EffectiveStatement<Integer, FractionDigitsStatement> createEffective(
63                 StmtContext<Integer, FractionDigitsStatement, EffectiveStatement<Integer, FractionDigitsStatement>> ctx) {
64             return new FractionDigitsEffectiveStatementImpl(ctx);
65         }
66
67     }
68
69     @Override
70     public Integer getValue() {
71         return argument();
72     }
73 }