3fb3758f32fd40d56bce45c4f03bbafc6d3a9119
[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 org.opendaylight.yangtools.yang.common.QName;
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.meta.StatementDefinition;
16 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
17
18 /**
19  *
20  * Class providing necessary support for processing YANG statement.
21  *
22  * This class is intended to be subclassed by developers, which 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(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 abstract A parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) throws SourceException;
72
73     @Override
74     public void onStatementAdded(StmtContext.Mutable<A, D, E> stmt) {
75         // NOOP for most implementations
76     }
77
78     /**
79      *
80      * {@inheritDoc}
81      *
82      * Subclasses of this class may override this method to perform actions on
83      * this event or register modification action using
84      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
85      *
86      */
87     @Override
88     public void onPreLinkageDeclared(StmtContext.Mutable<A, D, E> stmt) {
89         // NOOP for most implementations
90     }
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      */
101     @Override
102     public void onLinkageDeclared(StmtContext.Mutable<A, D, E> stmt) throws SourceException {
103         // NOOP for most implementations
104     }
105
106     /**
107      *
108      * {@inheritDoc}
109      *
110      * Subclasses of this class may override this method to perform actions on
111      * this event or register modification action using
112      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
113      *
114      */
115     @Override
116     public void onStatementDefinitionDeclared(StmtContext.Mutable<A, D, E> stmt) throws SourceException {
117         // NOOP for most implementations
118     }
119
120     /**
121      *
122      * {@inheritDoc}
123      *
124      * Subclasses of this class may override this method to perform actions on
125      * this event or register modification action using
126      * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
127      *
128      */
129     @Override
130     public void onFullDefinitionDeclared(StmtContext.Mutable<A, D, E> stmt) throws SourceException {
131         // NOOP for most implementations
132     }
133
134     @Override
135     public boolean isArgumentYinElement() {
136         return getPublicView().isArgumentYinElement();
137     }
138 }