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.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;
22 * Specialization of {@link BaseStatementSupport} for QName statement arguments.
24 * @param <D> Declared Statement representation
25 * @param <E> Effective Statement representation
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);
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);
42 protected abstract @NonNull D createDeclared(@NonNull StmtContext<QName, D, ?> ctx,
43 @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
45 protected abstract @NonNull D createEmptyDeclared(@NonNull StmtContext<QName, D, ?> ctx);
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);
56 protected abstract @NonNull E createEffective(@NonNull StmtContext<QName, D, E> ctx, @NonNull D declared,
57 @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
59 protected abstract @NonNull E createEmptyEffective(@NonNull StmtContext<QName, D, E> ctx, @NonNull D declared);
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);
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);