Separate out StatementSupport.applyCopyPolicy()
[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 com.google.common.annotations.Beta;
14 import com.google.common.base.VerifyException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
22
23 /**
24  * Class providing necessary support for processing a YANG statement. This class is intended to be subclassed
25  * by developers who want to add semantic support for a statement to a parser reactor.
26  *
27  * @param <A> Argument type
28  * @param <D> Declared Statement representation
29  * @param <E> Effective Statement representation
30  */
31 public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
32         implements StatementDefinition, StatementFactory<A, D, E>, StatementSupport<A, D, E> {
33
34     private final @NonNull StatementDefinition type;
35     private final @NonNull CopyPolicy copyPolicy;
36
37     @Beta
38     protected AbstractStatementSupport(final StatementDefinition publicDefinition, final CopyPolicy copyPolicy) {
39         this.type = requireNonNull(publicDefinition);
40         this.copyPolicy = requireNonNull(copyPolicy);
41         checkArgument(publicDefinition != this);
42     }
43
44     @Override
45     public final StatementDefinition getPublicView() {
46         return type;
47     }
48
49     @Override
50     public final CopyPolicy copyPolicy() {
51         return copyPolicy;
52     }
53
54     @Override
55     public final StmtContext<?, ?, ?> effectiveCopyOf(final StmtContext<?, ?, ?> stmt, final Mutable<?, ?, ?> parent,
56             final CopyType copyType, final QNameModule targetModule) {
57         switch (copyPolicy) {
58             case CONTEXT_INDEPENDENT:
59                 return stmt;
60             case DECLARED_COPY:
61                 // FIXME: YANGTOOLS-1195: this is too harsh, we need to make a callout to subclass methods so they
62                 //                        actually examine the differences.
63                 return parent.childCopyOf(stmt, copyType, targetModule);
64             default:
65                 throw new VerifyException("Attempted to apply " + copyPolicy);
66         }
67     }
68
69     @Override
70     public void onStatementAdded(final StmtContext.Mutable<A, D, E> stmt) {
71         // NOOP for most implementations
72     }
73
74     /**
75      * {@inheritDoc}.
76      *
77      * <p>
78      * Subclasses of this class may override this method to perform actions on this event or register a modification
79      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
80      */
81     @Override
82     public void onPreLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
83         // NOOP for most implementations
84     }
85
86     /**
87      * {@inheritDoc}.
88      *
89      * <p>
90      * Subclasses of this class may override this method to perform actions on this event or register a modification
91      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
92      */
93     @Override
94     public void onLinkageDeclared(final StmtContext.Mutable<A, D, E> stmt) {
95         // NOOP for most implementations
96     }
97
98     /**
99      * {@inheritDoc}.
100      *
101      * <p>
102      * Subclasses of this class may override this method to perform actions on this event or register a modification
103      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
104      */
105     @Override
106     public void onStatementDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
107         // NOOP for most implementations
108     }
109
110     /**
111      * {@inheritDoc}.
112      *
113      * <p>
114      * Subclasses of this class may override this method to perform actions on this event or register a modification
115      * action using {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
116      */
117     @Override
118     public void onFullDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
119         final SubstatementValidator validator = getSubstatementValidator();
120         if (validator != null) {
121             validator.validate(stmt);
122         }
123     }
124
125     @Override
126     public boolean hasArgumentSpecificSupports() {
127         // Most of statement supports don't have any argument specific supports, so return 'false'.
128         return false;
129     }
130
131     @Override
132     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
133         // Most of statement supports don't have any argument specific supports, so return null.
134         return null;
135     }
136
137     /**
138      * Returns corresponding substatement validator of a statement support.
139      *
140      * @return substatement validator or null, if substatement validator is not defined
141      */
142     protected abstract @Nullable SubstatementValidator getSubstatementValidator();
143 }