Bug 6897: Adding getSubstatementValidator() method to AbstractStatementSupport
[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 javax.annotation.Nonnull;
12 import javax.annotation.Nullable;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19
20 /**
21  *
22  * Class providing necessary support for processing YANG statement.
23  *
24  * This class is intended to be subclassed by developers, which want to
25  * introduce support of statement to parser.
26  *
27  * @param <A>
28  *            Argument type
29  * @param <D>
30  *            Declared Statement representation
31  * @param <E>
32  *            Effective Statement representation
33  */
34 public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
35         implements StatementDefinition, StatementFactory<A, D, E>, StatementSupport<A, D, E> {
36
37     private final StatementDefinition type;
38
39     protected AbstractStatementSupport(final StatementDefinition publicDefinition) {
40         Preconditions.checkArgument(publicDefinition != this);
41         this.type = Preconditions.checkNotNull(publicDefinition);
42     }
43
44     @Nonnull
45     @Override
46     public final QName getStatementName() {
47         return type.getStatementName();
48     }
49
50     @Override
51     public final QName getArgumentName() {
52         return type.getArgumentName();
53     }
54
55     @Nonnull
56     @Override
57     public final Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
58         return type.getDeclaredRepresentationClass();
59     }
60
61     @Nonnull
62     @Override
63     public final Class<? extends EffectiveStatement<?,?>> getEffectiveRepresentationClass() {
64         return type.getEffectiveRepresentationClass();
65     }
66
67     @Override
68     public final StatementDefinition getPublicView() {
69         return type;
70     }
71
72     @Override
73     public abstract A parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) throws SourceException;
74
75     @Override
76     public void onStatementAdded(final StmtContext.Mutable<A, D, E> stmt) {
77         // NOOP for most implementations
78     }
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      */
89     @Override
90     public void onPreLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
91         // NOOP for most implementations
92     }
93
94     /**
95      *
96      * {@inheritDoc}
97      *
98      * Subclasses of this class may override this method to perform actions on
99      * this event or register modification action using
100      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
101      *
102      */
103     @Override
104     public void onLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) throws SourceException {
105         // NOOP for most implementations
106     }
107
108     /**
109      *
110      * {@inheritDoc}
111      *
112      * Subclasses of this class may override this method to perform actions on
113      * this event or register modification action using
114      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
115      *
116      */
117     @Override
118     public void onStatementDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) throws SourceException {
119         // NOOP for most implementations
120     }
121
122     /**
123      *
124      * {@inheritDoc}
125      *
126      * Subclasses of this class may override this method to perform actions on
127      * this event or register modification action using
128      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
129      *
130      */
131     @Override
132     public void onFullDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) throws SourceException {
133         // NOOP for most implementations
134     }
135
136     @Override
137     public boolean isArgumentYinElement() {
138         return getPublicView().isArgumentYinElement();
139     }
140
141     /**
142      * Returns corresponding substatement validator of a statement support
143      *
144      * @return substatement validator or null, if substatement validator is not
145      *         defined
146      */
147     @Nullable
148     protected abstract SubstatementValidator getSubstatementValidator();
149 }