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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import java.math.BigDecimal;
15 import java.util.Collection;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.Empty;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
37 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
42 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
50 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
51 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
52 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
53 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
54 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
55 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
56 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
57 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
58 import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder;
59 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
60 import org.opendaylight.yangtools.yang.model.util.type.InvalidRangeConstraintException;
61 import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder;
62 import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
63 import org.opendaylight.yangtools.yang.model.util.type.RequireInstanceRestrictedTypeBuilder;
64 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
65 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
66 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
67 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
68 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx;
69 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
70 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
71 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
72 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
73 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
74 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
75 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
76 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
77 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
78 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
79 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
80 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
81 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
82 import org.slf4j.Logger;
83 import org.slf4j.LoggerFactory;
85 abstract class AbstractTypeStatementSupport
86 extends AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
87 private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeStatementSupport.class);
89 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
91 .addOptional(YangStmtMapping.BASE)
92 .addAny(YangStmtMapping.BIT)
93 .addAny(YangStmtMapping.ENUM)
94 .addOptional(YangStmtMapping.FRACTION_DIGITS)
95 .addOptional(YangStmtMapping.LENGTH)
96 .addOptional(YangStmtMapping.PATH)
97 .addAny(YangStmtMapping.PATTERN)
98 .addOptional(YangStmtMapping.RANGE)
99 .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
100 .addAny(YangStmtMapping.TYPE)
103 static final String BINARY = "binary";
104 static final String BITS = "bits";
105 static final String BOOLEAN = "boolean";
106 static final String DECIMAL64 = "decimal64";
107 static final String EMPTY = "empty";
108 static final String ENUMERATION = "enumeration";
109 static final String IDENTITY_REF = "identityref";
110 static final String INSTANCE_IDENTIFIER = "instance-identifier";
111 static final String INT8 = "int8";
112 static final String INT16 = "int16";
113 static final String INT32 = "int32";
114 static final String INT64 = "int64";
115 static final String LEAF_REF = "leafref";
116 static final String STRING = "string";
117 static final String UINT8 = "uint8";
118 static final String UINT16 = "uint16";
119 static final String UINT32 = "uint32";
120 static final String UINT64 = "uint64";
121 static final String UNION = "union";
123 private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
126 .put(BOOLEAN, BOOLEAN)
127 .put(DECIMAL64, DECIMAL64)
129 .put(ENUMERATION, ENUMERATION)
130 .put(IDENTITY_REF,IDENTITY_REF)
131 .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
136 .put(LEAF_REF, LEAF_REF)
145 private static final ImmutableMap<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
146 ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
147 .put(BITS, new BitsSpecificationSupport())
148 .put(DECIMAL64, new Decimal64SpecificationSupport())
149 .put(ENUMERATION, new EnumSpecificationSupport())
150 .put(IDENTITY_REF, new IdentityRefSpecificationRFC6020Support())
151 .put(INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationSupport())
152 .put(LEAF_REF, new LeafrefSpecificationRFC6020Support())
153 .put(UNION, new UnionSpecificationSupport())
156 AbstractTypeStatementSupport() {
157 // FIXME: can a type statement be copied?
158 super(YangStmtMapping.TYPE, CopyPolicy.DECLARED_COPY);
162 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
167 public final void onFullDefinitionDeclared(
168 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
169 super.onFullDefinitionDeclared(stmt);
171 // if it is yang built-in type, no prerequisite is needed, so simply return
172 if (BUILT_IN_TYPES.containsKey(stmt.argument())) {
173 // FIXME: consider populating BaseTypeNamespace here, which could be done quite efficiently, moving the
174 // logic from resolveType()
178 final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.argument());
179 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
180 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
181 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
182 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
185 * If the type does not exist, throw an InferenceException.
186 * If the type exists, store a reference to it in BaseTypeNamespace.
188 typeAction.apply(new InferenceAction() {
190 public void apply(final InferenceContext ctx) {
191 stmt.addToNs(BaseTypeNamespace.class, Empty.getInstance(), typePrereq.resolve(ctx));
195 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
196 InferenceException.throwIf(failed.contains(typePrereq), stmt, "Type [%s] was not found.", typeQName);
202 public final String internArgument(final String rawArgument) {
204 return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
208 public boolean hasArgumentSpecificSupports() {
209 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
213 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
214 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
218 protected final SubstatementValidator getSubstatementValidator() {
219 return SUBSTATEMENT_VALIDATOR;
223 protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
224 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
225 return new RegularTypeStatement(ctx.getRawArgument(), substatements);
229 protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
230 final TypeStatement builtin;
231 return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin
232 : new EmptyTypeStatement(ctx.getRawArgument());
236 protected EffectiveStatement<String, TypeStatement> createEffective(final Current<String, TypeStatement> stmt,
237 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
238 // First look up the proper base type
239 final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(stmt);
240 if (substatements.isEmpty()) {
244 // Now instantiate the proper effective statement for that type
245 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
246 final TypeStatement declared = stmt.declared();
247 if (baseType instanceof BinaryTypeDefinition) {
248 return createBinary(stmt, (BinaryTypeDefinition) baseType, declared, substatements);
249 } else if (baseType instanceof BitsTypeDefinition) {
250 return createBits(stmt, (BitsTypeDefinition) baseType, declared, substatements);
251 } else if (baseType instanceof BooleanTypeDefinition) {
252 return createBoolean(stmt, (BooleanTypeDefinition) baseType, declared, substatements);
253 } else if (baseType instanceof DecimalTypeDefinition) {
254 return createDecimal(stmt, (DecimalTypeDefinition) baseType, declared, substatements);
255 } else if (baseType instanceof EmptyTypeDefinition) {
256 return createEmpty(stmt, (EmptyTypeDefinition) baseType, declared, substatements);
257 } else if (baseType instanceof EnumTypeDefinition) {
258 return createEnum(stmt, (EnumTypeDefinition) baseType, declared, substatements);
259 } else if (baseType instanceof IdentityrefTypeDefinition) {
260 return createIdentityref(stmt, (IdentityrefTypeDefinition) baseType, declared, substatements);
261 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
262 return createInstanceIdentifier(stmt, (InstanceIdentifierTypeDefinition) baseType, declared, substatements);
263 } else if (baseType instanceof Int8TypeDefinition) {
264 return createIntegral(stmt, declared, substatements,
265 RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
266 } else if (baseType instanceof Int16TypeDefinition) {
267 return createIntegral(stmt, declared, substatements,
268 RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
269 } else if (baseType instanceof Int32TypeDefinition) {
270 return createIntegral(stmt, declared, substatements,
271 RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
272 } else if (baseType instanceof Int64TypeDefinition) {
273 return createIntegral(stmt, declared, substatements,
274 RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
275 } else if (baseType instanceof LeafrefTypeDefinition) {
276 return createLeafref(stmt, (LeafrefTypeDefinition) baseType, declared, substatements);
277 } else if (baseType instanceof StringTypeDefinition) {
278 return createString(stmt, (StringTypeDefinition) baseType, declared, substatements);
279 } else if (baseType instanceof Uint8TypeDefinition) {
280 return createIntegral(stmt, declared, substatements,
281 RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
282 } else if (baseType instanceof Uint16TypeDefinition) {
283 return createIntegral(stmt, declared, substatements,
284 RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
285 } else if (baseType instanceof Uint32TypeDefinition) {
286 return createIntegral(stmt, declared, substatements,
287 RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
288 } else if (baseType instanceof Uint64TypeDefinition) {
289 return createIntegral(stmt, declared, substatements,
290 RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
291 } else if (baseType instanceof UnionTypeDefinition) {
292 return createUnion(stmt, (UnionTypeDefinition) baseType, declared, substatements);
294 throw new IllegalStateException("Unhandled base type " + baseType);
298 static final SchemaPath typeEffectiveSchemaPath(final Current<?, ?> stmt) {
299 final SchemaPath path = stmt.wrapSchemaPath();
301 // SchemaPath is forbidden with a system property
304 final SchemaPath parent = path.getParent();
305 final QName parentQName = parent.getLastComponent();
306 checkArgument(parentQName != null, "Path %s has an empty parent", path);
308 final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
309 return parent.createChild(qname);
313 * Resolve type reference, as pointed to by the context's argument.
315 * @param ctx Statement context
316 * @return Resolved type
317 * @throws SourceException if the target type cannot be found
319 private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final Current<String, ?> ctx) {
320 final StmtContext<?, ?, ?> baseType = ctx.namespaceItem(BaseTypeNamespace.class, Empty.getInstance());
321 if (baseType != null) {
322 return ((TypedefEffectiveStatement) baseType.buildEffective()).asTypeEffectiveStatement();
325 final String argument = ctx.getArgument();
328 return BuiltinEffectiveStatement.BINARY;
330 return BuiltinEffectiveStatement.BOOLEAN;
332 return BuiltinEffectiveStatement.EMPTY;
333 case INSTANCE_IDENTIFIER:
334 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
336 return BuiltinEffectiveStatement.INT8;
338 return BuiltinEffectiveStatement.INT16;
340 return BuiltinEffectiveStatement.INT32;
342 return BuiltinEffectiveStatement.INT64;
344 return BuiltinEffectiveStatement.STRING;
346 return BuiltinEffectiveStatement.UINT8;
348 return BuiltinEffectiveStatement.UINT16;
350 return BuiltinEffectiveStatement.UINT32;
352 return BuiltinEffectiveStatement.UINT64;
354 throw new IllegalStateException("Unhandled type argument " + argument);
358 private static @NonNull TypeEffectiveStatement<TypeStatement> createBinary(final Current<?, ?> ctx,
359 final BinaryTypeDefinition baseType, final TypeStatement declared,
360 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
361 final LengthRestrictedTypeBuilder<BinaryTypeDefinition> builder =
362 RestrictedTypes.newBinaryBuilder(baseType, typeEffectiveSchemaPath(ctx));
364 for (EffectiveStatement<?, ?> stmt : substatements) {
365 if (stmt instanceof LengthEffectiveStatement) {
366 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
369 builder.setLengthConstraint(length, length.argument());
370 } catch (IllegalStateException e) {
371 throw new SourceException(ctx, e, "Multiple length constraints encountered");
372 } catch (InvalidLengthConstraintException e) {
373 throw new SourceException(ctx, e, "Invalid length constraint %s", length.argument());
378 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
381 private @NonNull TypeEffectiveStatement<TypeStatement> createBits(final Current<?, ?> ctx,
382 final BitsTypeDefinition baseType, final TypeStatement declared,
383 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
384 final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.wrapSchemaPath());
386 for (final EffectiveStatement<?, ?> stmt : substatements) {
387 if (stmt instanceof BitEffectiveStatement) {
388 builder.addBit(addRestrictedBit(ctx, baseType, (BitEffectiveStatement) stmt));
392 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
395 abstract @NonNull Bit addRestrictedBit(@NonNull EffectiveStmtCtx stmt, @NonNull BitsTypeDefinition base,
396 @NonNull BitEffectiveStatement bit);
398 private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final Current<?, ?> ctx,
399 final BooleanTypeDefinition baseType, final TypeStatement declared,
400 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
401 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newBooleanBuilder(baseType,
402 typeEffectiveSchemaPath(ctx)));
405 private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final Current<?, ?> ctx,
406 final DecimalTypeDefinition baseType, final TypeStatement declared,
407 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
408 final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
409 RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
411 for (EffectiveStatement<?, ?> stmt : substatements) {
412 if (stmt instanceof RangeEffectiveStatement) {
413 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
414 builder.setRangeConstraint(range, range.argument());
416 if (stmt instanceof FractionDigitsEffectiveStatement) {
417 final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
418 SourceException.throwIf(baseType.getFractionDigits() != digits, ctx,
419 "Cannot override fraction-digits from base type %s to %s", baseType, digits);
423 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
426 private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final Current<?, ?> ctx,
427 final EmptyTypeDefinition baseType, final TypeStatement declared,
428 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
429 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newEmptyBuilder(baseType,
430 typeEffectiveSchemaPath(ctx)));
433 private @NonNull TypeEffectiveStatement<TypeStatement> createEnum(final Current<?, ?> ctx,
434 final EnumTypeDefinition baseType, final TypeStatement declared,
435 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
436 final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType, ctx.wrapSchemaPath());
438 for (final EffectiveStatement<?, ?> stmt : substatements) {
439 if (stmt instanceof EnumEffectiveStatement) {
440 builder.addEnum(addRestrictedEnum(ctx, baseType, (EnumEffectiveStatement) stmt));
444 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
447 abstract @NonNull EnumPair addRestrictedEnum(@NonNull EffectiveStmtCtx stmt, @NonNull EnumTypeDefinition base,
448 @NonNull EnumEffectiveStatement enumStmt);
450 private static @NonNull TypeEffectiveStatement<TypeStatement> createIdentityref(final Current<?, ?> ctx,
451 final IdentityrefTypeDefinition baseType, final TypeStatement declared,
452 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
453 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newIdentityrefBuilder(baseType,
454 typeEffectiveSchemaPath(ctx)));
457 private static @NonNull TypeEffectiveStatement<TypeStatement> createInstanceIdentifier(
458 final Current<?, ?> ctx, final InstanceIdentifierTypeDefinition baseType, final TypeStatement declared,
459 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
460 final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(baseType,
461 typeEffectiveSchemaPath(ctx));
463 for (EffectiveStatement<?, ?> stmt : substatements) {
464 if (stmt instanceof RequireInstanceEffectiveStatement) {
465 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
469 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
472 private static <T extends RangeRestrictedTypeDefinition<T, N>, N extends Number & Comparable<N>>
473 @NonNull TypeEffectiveStatement<TypeStatement> createIntegral(final Current<?, ?> ctx,
474 final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
475 final RangeRestrictedTypeBuilder<T, N> builder) {
476 for (EffectiveStatement<?, ?> stmt : substatements) {
477 if (stmt instanceof RangeEffectiveStatement) {
478 final RangeEffectiveStatement rangeStmt = (RangeEffectiveStatement)stmt;
479 builder.setRangeConstraint(rangeStmt, rangeStmt.argument());
484 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
485 } catch (InvalidRangeConstraintException e) {
486 throw new SourceException(ctx, e, "Invalid range constraint: %s", e.getOffendingRanges());
490 private static @NonNull TypeEffectiveStatement<TypeStatement> createLeafref(final Current<?, ?> ctx,
491 final LeafrefTypeDefinition baseType, final TypeStatement declared,
492 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
493 final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> builder =
494 RestrictedTypes.newLeafrefBuilder(baseType, AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
496 for (final EffectiveStatement<?, ?> stmt : substatements) {
497 if (stmt instanceof RequireInstanceEffectiveStatement) {
498 builder.setRequireInstance(((RequireInstanceEffectiveStatement) stmt).argument());
501 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
504 private static @NonNull TypeEffectiveStatement<TypeStatement> createString(final Current<?, ?> ctx,
505 final StringTypeDefinition baseType, final TypeStatement declared,
506 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
507 final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
508 AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
510 for (EffectiveStatement<?, ?> stmt : substatements) {
511 if (stmt instanceof LengthEffectiveStatement) {
512 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
515 builder.setLengthConstraint(length, length.argument());
516 } catch (IllegalStateException e) {
517 throw new SourceException(ctx, e, "Multiple length constraints encountered");
518 } catch (InvalidLengthConstraintException e) {
519 throw new SourceException(ctx, e, "Invalid length constraint %s", length.argument());
522 if (stmt instanceof PatternEffectiveStatement) {
523 builder.addPatternConstraint((PatternEffectiveStatement) stmt);
527 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
530 private static @NonNull TypeEffectiveStatement<TypeStatement> createUnion(final Current<?, ?> ctx,
531 final UnionTypeDefinition baseType, final TypeStatement declared,
532 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
533 return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newUnionBuilder(baseType,
534 typeEffectiveSchemaPath(ctx)));