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