Make RangeRestrictedTypeDefinition type-aware
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / UnsignedIntegerTypeEffectiveStatementImpl.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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
10
11 import java.math.BigInteger;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
16 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
22 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
27
28 public final class UnsignedIntegerTypeEffectiveStatementImpl<T extends UnsignedIntegerTypeDefinition<N, T>,
29         N extends Number & Comparable<N>> extends DeclaredEffectiveStatementBase<String, TypeStatement>
30         implements TypeEffectiveStatement<TypeStatement> {
31
32     private final T typeDefinition;
33
34     private UnsignedIntegerTypeEffectiveStatementImpl(
35             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
36             final RangeRestrictedTypeBuilder<T, N> builder) {
37         super(ctx);
38
39         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
40             if (stmt instanceof RangeEffectiveStatementImpl) {
41                 final RangeEffectiveStatementImpl rangeStmt = (RangeEffectiveStatementImpl)stmt;
42                 builder.setRangeConstraint(rangeStmt, rangeStmt.argument());
43             }
44             if (stmt instanceof UnknownEffectiveStatementImpl) {
45                 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt);
46             }
47         }
48
49         typeDefinition = builder.build();
50     }
51
52     public static UnsignedIntegerTypeEffectiveStatementImpl<Uint8TypeDefinition, Short> create(
53             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
54             final Uint8TypeDefinition baseType) {
55         return new UnsignedIntegerTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newUint8Builder(baseType,
56             TypeUtils.typeEffectiveSchemaPath(ctx)));
57     }
58
59     public static UnsignedIntegerTypeEffectiveStatementImpl<Uint16TypeDefinition, Integer> create(
60             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
61             final Uint16TypeDefinition baseType) {
62         return new UnsignedIntegerTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newUint16Builder(baseType,
63             TypeUtils.typeEffectiveSchemaPath(ctx)));
64     }
65
66     public static UnsignedIntegerTypeEffectiveStatementImpl<Uint32TypeDefinition, Long> create(
67             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
68             final Uint32TypeDefinition baseType) {
69         return new UnsignedIntegerTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newUint32Builder(baseType,
70             TypeUtils.typeEffectiveSchemaPath(ctx)));
71     }
72
73     public static UnsignedIntegerTypeEffectiveStatementImpl<Uint64TypeDefinition, BigInteger> create(
74             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
75             final Uint64TypeDefinition baseType) {
76         return new UnsignedIntegerTypeEffectiveStatementImpl<>(ctx, RestrictedTypes.newUint64Builder(baseType,
77             TypeUtils.typeEffectiveSchemaPath(ctx)));
78     }
79
80     @Nonnull
81     @Override
82     public T getTypeDefinition() {
83         return typeDefinition;
84     }
85 }