YANGTOOLS-706: Split up base utility classes into rfc6020.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / IntegralTypeEffectiveStatementImpl.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
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.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;
33
34 public final class IntegralTypeEffectiveStatementImpl<T extends RangeRestrictedTypeDefinition<T, N>,
35         N extends Number & Comparable<N>> extends DeclaredEffectiveStatementBase<String, TypeStatement>
36         implements TypeEffectiveStatement<TypeStatement> {
37
38     private final T typeDefinition;
39
40     private IntegralTypeEffectiveStatementImpl(
41             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
42             final RangeRestrictedTypeBuilder<T, N> builder) {
43         super(ctx);
44
45         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
46             if (stmt instanceof RangeEffectiveStatementImpl) {
47                 final RangeEffectiveStatementImpl rangeStmt = (RangeEffectiveStatementImpl)stmt;
48                 builder.setRangeConstraint(rangeStmt, rangeStmt.argument());
49             }
50             if (stmt instanceof UnknownSchemaNode) {
51                 builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
52             }
53         }
54
55         try {
56             typeDefinition = builder.build();
57         } catch (InvalidRangeConstraintException e) {
58             throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid range constraint: %s",
59                 e.getOffendingRanges());
60         }
61     }
62
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)));
68     }
69
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)));
75     }
76
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)));
82     }
83
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)));
89     }
90
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)));
96     }
97
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)));
103     }
104
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)));
110     }
111
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)));
117     }
118
119     @Nonnull
120     @Override
121     public T getTypeDefinition() {
122         return typeDefinition;
123     }
124 }