Refactor argument adaptation
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / AbstractStatementSupport.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 package org.opendaylight.yangtools.yang.parser.spi.meta;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Optional;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
18 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
19 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementDefinitionContext;
21
22 /**
23  * Class providing necessary support for processing a YANG statement.
24  *
25  * This class is intended to be subclassed by developers, who want to
26  * introduce support of statement to parser.
27  *
28  * @param <A>
29  *            Argument type
30  * @param <D>
31  *            Declared Statement representation
32  * @param <E>
33  *            Effective Statement representation
34  */
35 public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
36         implements StatementDefinition, StatementFactory<A, D, E>, StatementSupport<A, D, E> {
37
38     private final StatementDefinition type;
39
40     protected AbstractStatementSupport(final StatementDefinition publicDefinition) {
41         Preconditions.checkArgument(publicDefinition != this);
42         this.type = Preconditions.checkNotNull(publicDefinition);
43     }
44
45     @Nonnull
46     @Override
47     public final QName getStatementName() {
48         return type.getStatementName();
49     }
50
51     @Override
52     public final QName getArgumentName() {
53         return type.getArgumentName();
54     }
55
56     @Nonnull
57     @Override
58     public final Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
59         return type.getDeclaredRepresentationClass();
60     }
61
62     @Nonnull
63     @Override
64     public final Class<? extends EffectiveStatement<?,?>> getEffectiveRepresentationClass() {
65         return type.getEffectiveRepresentationClass();
66     }
67
68     @Override
69     public final StatementDefinition getPublicView() {
70         return type;
71     }
72
73     @Override
74     public Optional<StatementContextBase<?, ?, ?>> beforeSubStatementCreated(final StmtContext.Mutable<?, ?, ?> stmt,
75             final int offset, final StatementDefinitionContext<?, ?, ?> def, final StatementSourceReference ref,
76             final String argument) {
77         // NOOP for most implementations and also no implicit statements
78         return Optional.empty();
79     }
80
81     @Override
82     public void onStatementAdded(final StmtContext.Mutable<A, D, E> stmt) {
83         // NOOP for most implementations
84     }
85
86     /**
87      * {@inheritDoc}
88      *
89      * Subclasses of this class may override this method to perform actions on
90      * this event or register modification action using
91      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
92      */
93     @Override
94     public void onPreLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
95         // NOOP for most implementations
96     }
97
98     /**
99      * {@inheritDoc}
100      *
101      * Subclasses of this class may override this method to perform actions on
102      * this event or register modification action using
103      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
104      */
105     @Override
106     public void onLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
107         // NOOP for most implementations
108     }
109
110     /**
111      * {@inheritDoc}
112      *
113      * Subclasses of this class may override this method to perform actions on
114      * this event or register modification action using
115      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
116      */
117     @Override
118     public void onStatementDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
119         // NOOP for most implementations
120     }
121
122     /**
123      * {@inheritDoc}
124      *
125      * Subclasses of this class may override this method to perform actions on
126      * this event or register modification action using
127      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
128      */
129     @Override
130     public void onFullDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
131         final SubstatementValidator validator = getSubstatementValidator();
132         if (validator != null) {
133             validator.validate(stmt);
134         }
135     }
136
137     @Override
138     public boolean isArgumentYinElement() {
139         return getPublicView().isArgumentYinElement();
140     }
141
142     @Override
143     public boolean hasArgumentSpecificSupports() {
144         // Most of statement supports don't have any argument specific
145         // supports, so return 'false'.
146         return false;
147     }
148
149     @Override
150     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
151         // Most of statement supports don't have any argument specific
152         // supports, so return null.
153         return null;
154     }
155
156     /**
157      * Returns corresponding substatement validator of a statement support
158      *
159      * @return substatement validator or null, if substatement validator is not
160      *         defined
161      */
162     @Nullable
163     protected abstract SubstatementValidator getSubstatementValidator();
164 }