*/
package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.fraction_digits;
-import com.google.common.collect.Range;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsStatement;
+import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
+import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-public final class FractionDigitsStatementSupport extends AbstractStatementSupport<Integer, FractionDigitsStatement,
- EffectiveStatement<Integer, FractionDigitsStatement>> {
- private static final Range<Integer> FRAC_DIGITS_ALLOWED = Range.closed(1, 18);
- private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
- YangStmtMapping.FRACTION_DIGITS)
- .build();
+public final class FractionDigitsStatementSupport
+ extends AbstractStatementSupport<Integer, FractionDigitsStatement, FractionDigitsEffectiveStatement> {
+ private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
+ SubstatementValidator.builder(YangStmtMapping.FRACTION_DIGITS).build();
private static final FractionDigitsStatementSupport INSTANCE = new FractionDigitsStatementSupport();
+ // FIXME: move this to yang-model-spi
+ private static final ImmutableMap<FractionDigitsStatement, FractionDigitsEffectiveStatement> EMPTY_EFF;
+
+ static {
+ final Builder<FractionDigitsStatement, FractionDigitsEffectiveStatement> effBuilder = ImmutableMap.builder();
+ for (int i = 1; i <= 18; ++i) {
+ final FractionDigitsStatement decl = DeclaredStatements.createFractionDigits(i);
+ effBuilder.put(decl, EffectiveStatements.createFractionDigits(decl));
+ }
+ EMPTY_EFF = effBuilder.build();
+ }
+
private FractionDigitsStatementSupport() {
- super(YangStmtMapping.FRACTION_DIGITS);
+ super(YangStmtMapping.FRACTION_DIGITS, StatementPolicy.contextIndependent());
}
public static FractionDigitsStatementSupport getInstance() {
@Override
public Integer parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
- final Integer fractionDigits;
+ final int fractionDigits;
try {
- fractionDigits = Integer.valueOf(value);
+ fractionDigits = Integer.parseInt(value);
} catch (NumberFormatException e) {
- throw new SourceException(ctx.getStatementSourceReference(), e,
- "%s is not valid fraction-digits integer argument", value);
+ throw new SourceException(ctx, e, "%s is not valid fraction-digits integer argument", value);
+ }
+ if (fractionDigits < 1 || fractionDigits > 18) {
+ throw new SourceException("fraction-digits argument should be integer within [1..18]", ctx);
}
-
- SourceException.throwIf(!FRAC_DIGITS_ALLOWED.contains(fractionDigits), ctx.getStatementSourceReference(),
- "fraction-digits argument should be integer within %s", FRAC_DIGITS_ALLOWED);
return fractionDigits;
}
@Override
- public FractionDigitsStatement createDeclared(final StmtContext<Integer, FractionDigitsStatement, ?> ctx) {
- return new FractionDigitsStatementImpl(ctx);
+ protected SubstatementValidator getSubstatementValidator() {
+ return SUBSTATEMENT_VALIDATOR;
}
@Override
- public EffectiveStatement<Integer, FractionDigitsStatement> createEffective(
- final StmtContext<Integer, FractionDigitsStatement,
- EffectiveStatement<Integer, FractionDigitsStatement>> ctx) {
- return new FractionDigitsEffectiveStatementImpl(ctx);
+ protected FractionDigitsStatement createDeclared(final StmtContext<Integer, FractionDigitsStatement, ?> ctx,
+ final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+ return DeclaredStatements.createFractionDigits(ctx.getArgument(), substatements);
}
@Override
- protected SubstatementValidator getSubstatementValidator() {
- return SUBSTATEMENT_VALIDATOR;
+ protected FractionDigitsStatement createEmptyDeclared(final StmtContext<Integer, FractionDigitsStatement, ?> ctx) {
+ return DeclaredStatements.createFractionDigits(ctx.getArgument());
+ }
+
+ @Override
+ protected FractionDigitsEffectiveStatement createEffective(final Current<Integer, FractionDigitsStatement> stmt,
+ final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+ return substatements.isEmpty() ? createEmptyEffective(stmt.declared())
+ : EffectiveStatements.createFractionDigits(stmt.declared(), substatements);
+ }
+
+ private static @NonNull FractionDigitsEffectiveStatement createEmptyEffective(
+ final FractionDigitsStatement declared) {
+ final FractionDigitsEffectiveStatement shared = EMPTY_EFF.get(declared);
+ return shared != null ? shared : EffectiveStatements.createFractionDigits(declared);
}
-}
\ No newline at end of file
+}