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