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.spi.meta;
10 import java.util.Optional;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 * Default implementations for various {@link StmtContext} methods. Code hosted here is meant for use by actual
16 * {@link StmtContext} implementations, not for general use by others.
18 public final class StmtContextDefaults {
19 private StmtContextDefaults() {
24 * Default implementation of {@link StmtContext#findSubstatementArgument(Class)}. See that method for API contract.
26 * @param <A> Substatement argument type
27 * @param <E> Substatement effective statement representation
28 * @param stmt Statement context to search
29 * @param type Effective statement representation being look up
30 * @return Effective statement argument, if found
32 @SuppressWarnings({"unchecked" })
33 public static <A, E extends EffectiveStatement<A, ?>> @NonNull Optional<A> findSubstatementArgument(
34 final @NonNull StmtContext<?, ?, ?> stmt, final @NonNull Class<E> type) {
35 return stmt.allSubstatementsStream()
36 .filter(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type))
38 .map(ctx -> (A) ctx.getArgument());
42 * Default implementation of {@link StmtContext#hasSubstatement(Class)}. See that method for API contract.
44 * @param stmt Statement context to search
45 * @param type Effective statement representation being look up
46 * @return True if a match is found, false otherwise
48 public static boolean hasSubstatement(final @NonNull StmtContext<?, ?, ?> stmt,
49 final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
50 return stmt.allSubstatementsStream()
51 .anyMatch(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type));