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