Migrate case statement
[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.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
21
22 /**
23  * A massively-misnamed superclass for statements which are both schema tree participants and can be created as implicit
24  * nodes. This covers {@code case}, {@code input} and {@code output} statements.
25  *
26  * @param <D> Declared Statement representation
27  * @param <E> Effective Statement representation
28  */
29 @Beta
30 public abstract class BaseImplicitStatementSupport<D extends DeclaredStatement<QName>,
31         E extends EffectiveStatement<QName, D>> extends BaseQNameStatementSupport<D, E> {
32     protected BaseImplicitStatementSupport(final StatementDefinition publicDefinition) {
33         super(publicDefinition);
34     }
35
36     @Override
37     public final void onStatementAdded(final Mutable<QName, D, E> stmt) {
38         stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
39     }
40
41     @Override
42     protected final E createEffective(
43             final StmtContext<QName, D, E> ctx,
44             final D declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
45         final StatementSource source = ctx.getStatementSource();
46         switch (ctx.getStatementSource()) {
47             case CONTEXT:
48                 return createUndeclaredEffective(ctx, substatements);
49             case DECLARATION:
50                 return createDeclaredEffective(ctx, substatements, declared);
51             default:
52                 throw new IllegalStateException("Unhandled statement source " + source);
53         }
54     }
55
56     @Override
57     protected final E createEmptyEffective(final StmtContext<QName, D, E> ctx, final D declared) {
58         return createEffective(ctx, declared, ImmutableList.of());
59     }
60
61     protected abstract @NonNull E createDeclaredEffective(@NonNull StmtContext<QName, D, E> ctx,
62             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements, @NonNull D declared);
63
64     protected abstract @NonNull E createUndeclaredEffective(@NonNull StmtContext<QName, D, E> ctx,
65             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
66
67 }