Allow derived context to be reused
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / BaseImplicitStatementSupport.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.rfc7950.stmt;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableList;
12 import org.eclipse.jdt.annotation.NonNull;
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.model.api.meta.StatementSource;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
20
21 /**
22  * A massively-misnamed superclass for statements which are both schema tree participants and can be created as implicit
23  * nodes. This covers {@code case}, {@code input} and {@code output} statements.
24  *
25  * @param <D> Declared Statement representation
26  * @param <E> Effective Statement representation
27  */
28 @Beta
29 public abstract class BaseImplicitStatementSupport<D extends DeclaredStatement<QName>,
30         E extends SchemaTreeEffectiveStatement<D>> extends BaseSchemaTreeStatementSupport<D, E> {
31     protected BaseImplicitStatementSupport(final StatementDefinition publicDefinition,
32             final StatementPolicy<QName, D> policy) {
33         super(publicDefinition, policy);
34     }
35
36     @Override
37     public final E copyEffective(final Current<QName, D> stmt, final E original) {
38         final StatementSource source = stmt.source();
39         switch (source) {
40             case CONTEXT:
41                 return copyUndeclaredEffective(stmt, original);
42             case DECLARATION:
43                 return copyDeclaredEffective(stmt, original);
44             default:
45                 throw new IllegalStateException("Unhandled statement source " + source);
46         }
47     }
48
49     @Override
50     protected E createEffective(final Current<QName, D> stmt,
51             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
52         final StatementSource source = stmt.source();
53         switch (source) {
54             case CONTEXT:
55                 return createUndeclaredEffective(stmt, substatements);
56             case DECLARATION:
57                 return createDeclaredEffective(stmt, substatements);
58             default:
59                 throw new IllegalStateException("Unhandled statement source " + source);
60         }
61     }
62
63     protected abstract @NonNull E copyDeclaredEffective(@NonNull Current<QName, D> stmt, @NonNull E original);
64
65     protected abstract @NonNull E copyUndeclaredEffective(@NonNull Current<QName, D> stmt, @NonNull E original);
66
67     protected abstract @NonNull E createDeclaredEffective(@NonNull Current<QName, D> stmt,
68             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
69
70     protected abstract @NonNull E createUndeclaredEffective(@NonNull Current<QName, D> stmt,
71             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
72 }