2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
11 import java.math.BigInteger;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
17 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.util.type.InvalidRangeConstraintException;
27 import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
28 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
29 import org.opendaylight.yangtools.yang.parser.rfc6020.util.DeclaredEffectiveStatementBase;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
34 public final class IntegralTypeEffectiveStatementImpl<T extends RangeRestrictedTypeDefinition<T, N>,
35 N extends Number & Comparable<N>> extends DeclaredEffectiveStatementBase<String, TypeStatement>
36 implements TypeEffectiveStatement<TypeStatement> {
38 private final T typeDefinition;
40 private IntegralTypeEffectiveStatementImpl(
41 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
42 final RangeRestrictedTypeBuilder<T, N> builder) {
45 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
46 if (stmt instanceof RangeEffectiveStatementImpl) {
47 final RangeEffectiveStatementImpl rangeStmt = (RangeEffectiveStatementImpl)stmt;
48 builder.setRangeConstraint(rangeStmt, rangeStmt.argument());
50 if (stmt instanceof UnknownSchemaNode) {
51 builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
56 typeDefinition = builder.build();
57 } catch (InvalidRangeConstraintException e) {
58 throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid range constraint: %s",
59 e.getOffendingRanges());
63 public static IntegralTypeEffectiveStatementImpl<Int8TypeDefinition, Byte> create(
64 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
65 final Int8TypeDefinition baseType) {
66 return new IntegralTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newInt8Builder(baseType,
67 TypeUtils.typeEffectiveSchemaPath(ctx)));
70 public static IntegralTypeEffectiveStatementImpl<Int16TypeDefinition, Short> create(
71 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
72 final Int16TypeDefinition baseType) {
73 return new IntegralTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newInt16Builder(baseType,
74 TypeUtils.typeEffectiveSchemaPath(ctx)));
77 public static IntegralTypeEffectiveStatementImpl<Int32TypeDefinition, Integer> create(
78 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
79 final Int32TypeDefinition baseType) {
80 return new IntegralTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newInt32Builder(baseType,
81 TypeUtils.typeEffectiveSchemaPath(ctx)));
84 public static IntegralTypeEffectiveStatementImpl<Int64TypeDefinition, Long> create(
85 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
86 final Int64TypeDefinition baseType) {
87 return new IntegralTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newInt64Builder(baseType,
88 TypeUtils.typeEffectiveSchemaPath(ctx)));
91 public static IntegralTypeEffectiveStatementImpl<Uint8TypeDefinition, Short> create(
92 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
93 final Uint8TypeDefinition baseType) {
94 return new IntegralTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newUint8Builder(baseType,
95 TypeUtils.typeEffectiveSchemaPath(ctx)));
98 public static IntegralTypeEffectiveStatementImpl<Uint16TypeDefinition, Integer> create(
99 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
100 final Uint16TypeDefinition baseType) {
101 return new IntegralTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newUint16Builder(baseType,
102 TypeUtils.typeEffectiveSchemaPath(ctx)));
105 public static IntegralTypeEffectiveStatementImpl<Uint32TypeDefinition, Long> create(
106 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
107 final Uint32TypeDefinition baseType) {
108 return new IntegralTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newUint32Builder(baseType,
109 TypeUtils.typeEffectiveSchemaPath(ctx)));
112 public static IntegralTypeEffectiveStatementImpl<Uint64TypeDefinition, BigInteger> create(
113 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
114 final Uint64TypeDefinition baseType) {
115 return new IntegralTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newUint64Builder(baseType,
116 TypeUtils.typeEffectiveSchemaPath(ctx)));
121 public T getTypeDefinition() {
122 return typeDefinition;