2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
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.Status;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 * Specialization of {@link BaseStatementSupport} for QName statement arguments.
27 * @param <D> Declared Statement representation
28 * @param <E> Effective Statement representation
31 public abstract class BaseQNameStatementSupport<D extends DeclaredStatement<QName>,
32 E extends EffectiveStatement<QName, D>> extends AbstractQNameStatementSupport<D, E> {
33 protected BaseQNameStatementSupport(final StatementDefinition publicDefinition) {
34 super(publicDefinition);
38 public final D createDeclared(final StmtContext<QName, D, ?> ctx) {
39 final ImmutableList<? extends DeclaredStatement<?>> substatements = ctx.declaredSubstatements().stream()
40 .map(StmtContext::buildDeclared)
41 .collect(ImmutableList.toImmutableList());
42 return substatements.isEmpty() ? createEmptyDeclared(ctx) : createDeclared(ctx, substatements);
45 protected abstract @NonNull D createDeclared(@NonNull StmtContext<QName, D, ?> ctx,
46 @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
48 protected abstract @NonNull D createEmptyDeclared(@NonNull StmtContext<QName, D, ?> ctx);
51 public final E createEffective(final StmtContext<QName, D, E> ctx) {
52 final D declared = ctx.buildDeclared();
53 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements =
54 BaseStatementSupport.buildEffectiveSubstatements(ctx);
55 return substatements.isEmpty() ? createEmptyEffective(ctx, declared)
56 : createEffective(ctx, declared, substatements);
59 protected abstract @NonNull E createEffective(@NonNull StmtContext<QName, D, E> ctx, @NonNull D declared,
60 @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
62 protected abstract @NonNull E createEmptyEffective(@NonNull StmtContext<QName, D, E> ctx, @NonNull D declared);
64 protected static final <E extends EffectiveStatement<?, ?>> @Nullable E findFirstStatement(
65 final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type) {
66 return BaseStatementSupport.findFirstStatement(statements, type);
69 protected static final <A, E extends EffectiveStatement<A, ?>> A findFirstArgument(
70 final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type, final A defValue) {
71 return BaseStatementSupport.findFirstArgument(statements, type, defValue);
74 protected static final int historyAndStatusFlags(final StmtContext<?, ?, ?> ctx,
75 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
76 return new FlagsBuilder()
77 .setHistory(ctx.getCopyHistory())
78 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))