Fix Status encoding
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / BaseQNameStatementSupport.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.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20
21 /**
22  * Specialization of {@link BaseStatementSupport} for QName statement arguments.
23  *
24  * @param <D> Declared Statement representation
25  * @param <E> Effective Statement representation
26  */
27 @Beta
28 public abstract class BaseQNameStatementSupport<D extends DeclaredStatement<QName>,
29         E extends EffectiveStatement<QName, D>> extends AbstractQNameStatementSupport<D, E> {
30     protected BaseQNameStatementSupport(final StatementDefinition publicDefinition) {
31         super(publicDefinition);
32     }
33
34     @Override
35     public final D createDeclared(final StmtContext<QName, D, ?> ctx) {
36         final ImmutableList<? extends DeclaredStatement<?>> substatements = ctx.declaredSubstatements().stream()
37                 .map(StmtContext::buildDeclared)
38                 .collect(ImmutableList.toImmutableList());
39         return substatements.isEmpty() ? createEmptyDeclared(ctx) : createDeclared(ctx, substatements);
40     }
41
42     protected abstract @NonNull D createDeclared(@NonNull StmtContext<QName, D, ?> ctx,
43             @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
44
45     protected abstract @NonNull D createEmptyDeclared(@NonNull StmtContext<QName, D, ?> ctx);
46
47     @Override
48     public final E createEffective(final StmtContext<QName, D, E> ctx) {
49         final D declared = ctx.buildDeclared();
50         final ImmutableList<? extends EffectiveStatement<?, ?>> substatements =
51                 BaseStatementSupport.buildEffectiveSubstatements(ctx);
52         return substatements.isEmpty() ? createEmptyEffective(ctx, declared)
53                 : createEffective(ctx, declared, substatements);
54     }
55
56     protected abstract @NonNull E createEffective(@NonNull StmtContext<QName, D, E> ctx, @NonNull D declared,
57             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
58
59     protected abstract @NonNull E createEmptyEffective(@NonNull StmtContext<QName, D, E> ctx, @NonNull D declared);
60
61     protected static final <E extends EffectiveStatement<?, ?>> @Nullable E findFirstStatement(
62             final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type) {
63         return BaseStatementSupport.findFirstStatement(statements, type);
64     }
65
66     protected static final <A, E extends EffectiveStatement<A, ?>> A findFirstArgument(
67             final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type, final A defValue) {
68         return BaseStatementSupport.findFirstArgument(statements, type, defValue);
69     }
70 }