Cleanup use of Guava library
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Optional;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20
21 /**
22  * Class providing necessary support for processing a YANG statement. This class is intended to be subclassed
23  * by developers who want to add semantic support for a statement to a parser reactor.
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         this.type = requireNonNull(publicDefinition);
39         checkArgument(publicDefinition != this);
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      * <p>
85      * Subclasses of this class may override this method to perform actions on this event or register a modification
86      * action using {@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      * <p>
97      * Subclasses of this class may override this method to perform actions on this event or register a modification
98      * action using {@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      * <p>
109      * Subclasses of this class may override this method to perform actions on this event or register a modification
110      * action using {@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      * <p>
121      * Subclasses of this class may override this method to perform actions on this event or register a modification
122      * action using {@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 }