Fix StmtContext.produces{Declared,Effective} signatures
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextDefaults.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.spi.meta;
9
10 import java.util.Optional;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
13
14 /**
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.
17  */
18 public final class StmtContextDefaults {
19     private StmtContextDefaults() {
20         // Hidden on purpose
21     }
22
23     /**
24      * Default implementation of {@link StmtContext#findSubstatementArgument(Class)}. See that method for API contract.
25      *
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
31      */
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))
37                 .findAny()
38                 .map(ctx -> (A) ctx.coerceStatementArgument());
39     }
40
41     /**
42      * Default implementation of {@link StmtContext#hasSubstatement(Class)}. See that method for API contract.
43      *
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
47      */
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));
52     }
53 }