2 * Copyright (c) 2016 Cisco Systems, Inc. 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.stmt.rfc6020.effective.type;
10 import com.google.common.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
16 import org.opendaylight.yangtools.yang.model.api.type.ModifierKind;
17 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
18 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DescriptionEffectiveStatementImpl;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ErrorAppTagEffectiveStatementImpl;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ErrorMessageEffectiveStatementImpl;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ReferenceEffectiveStatementImpl;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc7950.effective.ModifierEffectiveStatementImpl;
27 abstract class AbstractConstraintEffectiveStatement<A, D extends DeclaredStatement<A>> extends
28 DeclaredEffectiveStatementBase<A, D> {
29 private final String description;
30 private final String reference;
31 private final String errorAppTag;
32 private final String errorMessage;
33 private final ModifierKind modifier;
34 private final A constraints;
36 public AbstractConstraintEffectiveStatement(final StmtContext<A, D, ?> ctx,
37 final ConstraintFactory<A> constraintFactory) {
39 String descriptionInit = null;
40 String referenceInit = null;
41 String errorAppTagInit = null;
42 String errorMessageInit = null;
43 ModifierKind modifierInit = null;
45 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
46 if (stmt instanceof DescriptionEffectiveStatementImpl) {
47 descriptionInit = ((DescriptionEffectiveStatementImpl) stmt).argument();
49 if (stmt instanceof ReferenceEffectiveStatementImpl) {
50 referenceInit = ((ReferenceEffectiveStatementImpl) stmt).argument();
52 if (stmt instanceof ErrorAppTagEffectiveStatementImpl) {
53 errorAppTagInit = ((ErrorAppTagEffectiveStatementImpl) stmt).argument();
55 if (stmt instanceof ErrorMessageEffectiveStatementImpl) {
56 errorMessageInit = ((ErrorMessageEffectiveStatementImpl) stmt).argument();
58 if (stmt instanceof ModifierEffectiveStatementImpl) {
59 modifierInit = ((ModifierEffectiveStatementImpl) stmt).argument();
63 this.description = descriptionInit;
64 this.reference = referenceInit;
65 this.errorAppTag = errorAppTagInit;
66 this.errorMessage = errorMessageInit;
67 this.modifier = modifierInit;
68 this.constraints = constraintFactory.createConstraints(this, super.argument());
72 public final A argument() {
76 public final boolean isCustomizedStatement() {
77 return this.description != null || this.reference != null || this.errorAppTag != null
78 || this.errorMessage != null || this.modifier != null;
81 public final String getDescription() {
85 public final ModifierKind getModifier() {
89 public final String getReference() {
93 public final String getErrorAppTag() {
97 public final String getErrorMessage() {
102 abstract class ConstraintFactory<A> {
103 abstract protected A createConstraints(AbstractConstraintEffectiveStatement<A, ?> stmt, A argument);
106 abstract class ListConstraintFactory<A> extends ConstraintFactory<List<A>> {
108 protected List<A> createConstraints(final AbstractConstraintEffectiveStatement<List<A>, ?> stmt,
109 final List<A> argument) {
110 if (!stmt.isCustomizedStatement()) {
111 return ImmutableList.copyOf(argument);
114 final List<A> customizedConstraints = new ArrayList<>(argument.size());
115 for (final A constraint : argument) {
116 customizedConstraints.add(createCustomizedConstraint(constraint, stmt));
118 return ImmutableList.copyOf(customizedConstraints);
121 abstract protected A createCustomizedConstraint(A constraint, AbstractConstraintEffectiveStatement<List<A>, ?> stmt);
124 final class LengthConstraintFactory extends ListConstraintFactory<LengthConstraint> {
126 protected LengthConstraint createCustomizedConstraint(final LengthConstraint lengthConstraint,
127 final AbstractConstraintEffectiveStatement<List<LengthConstraint>, ?> stmt) {
128 return new LengthConstraintEffectiveImpl(lengthConstraint.getMin(), lengthConstraint.getMax(),
129 stmt.getDescription(), stmt.getReference(), stmt.getErrorAppTag(), stmt.getErrorMessage());
133 final class RangeConstraintFactory extends ListConstraintFactory<RangeConstraint> {
135 protected RangeConstraint createCustomizedConstraint(final RangeConstraint rangeConstraint,
136 final AbstractConstraintEffectiveStatement<List<RangeConstraint>, ?> stmt) {
137 return new RangeConstraintEffectiveImpl(rangeConstraint.getMin(), rangeConstraint.getMax(),
138 stmt.getDescription(), stmt.getReference(), stmt.getErrorAppTag(), stmt.getErrorMessage());
142 final class PatternConstraintFactory extends ConstraintFactory<PatternConstraint> {
144 protected PatternConstraint createConstraints(
145 final AbstractConstraintEffectiveStatement<PatternConstraint, ?> stmt, final PatternConstraint argument) {
146 if (!stmt.isCustomizedStatement()) {
150 return createCustomizedConstraint(argument, stmt);
153 private static PatternConstraint createCustomizedConstraint(final PatternConstraint patternConstraint,
154 final AbstractConstraintEffectiveStatement<?, ?> stmt) {
155 return new PatternConstraintEffectiveImpl(patternConstraint.getRegularExpression(),
156 patternConstraint.getRawRegularExpression(), stmt.getDescription(), stmt.getReference(),
157 stmt.getErrorAppTag(), stmt.getErrorMessage(), stmt.getModifier());