Optimize {Position,Value}StatementSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / position / PositionStatementSupport.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.position;
9
10 import com.google.common.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
12 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.PositionEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.PositionStatement;
16 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseInternedStatementSupport;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20
21 public final class PositionStatementSupport
22         extends BaseInternedStatementSupport<Long, PositionStatement, PositionEffectiveStatement> {
23     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
24         YangStmtMapping.POSITION).build();
25     private static final PositionStatementSupport INSTANCE = new PositionStatementSupport();
26
27     private PositionStatementSupport() {
28         super(YangStmtMapping.POSITION);
29     }
30
31     public static PositionStatementSupport getInstance() {
32         return INSTANCE;
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     protected SubstatementValidator getSubstatementValidator() {
47         return SUBSTATEMENT_VALIDATOR;
48     }
49
50     @Override
51     protected PositionStatement createDeclared(final Long argument,
52             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
53         return new RegularPositionStatement(argument, substatements);
54     }
55
56     @Override
57     protected PositionStatement createEmptyDeclared(final Long argument) {
58         return new EmptyPositionStatement(argument);
59     }
60
61     @Override
62     protected PositionEffectiveStatement createEmptyEffective(final PositionStatement declared) {
63         return new EmptyPositionEffectiveStatement(declared);
64     }
65
66     @Override
67     protected PositionEffectiveStatement createEffective(
68             final StmtContext<Long, PositionStatement, PositionEffectiveStatement> ctx,
69             final PositionStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
70         return new RegularPositionEffectiveStatement(declared, substatements);
71     }
72 }