e35aeb80bb151c31a5bc86aca0ca2dbe735762c4
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / stmt / SchemaTreeAwareEffectiveStatement.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.model.api.stmt;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19
20 /**
21  * Interface implemented by all {@link EffectiveStatement}s which can contain a {@code schema tree} child. This tree
22  * can be walked using {@link SchemaNodeIdentifier}, looking up each component of
23  * {@link SchemaNodeIdentifier#getPathFromRoot()} using {@link #findSchemaTreeNode(QName)}.
24  *
25  * @param <A> Argument type
26  * @param <D> Class representing declared version of this statement.
27  * @author Robert Varga
28  */
29 @Beta
30 public interface SchemaTreeAwareEffectiveStatement<A, D extends DeclaredStatement<A>> extends EffectiveStatement<A, D> {
31     /**
32      * Namespace of {@code schema node}s defined within this node.
33      *
34      * @param <T> Child statement type
35      * @author Robert Varga
36      */
37     @NonNullByDefault
38     abstract class Namespace<T extends SchemaTreeEffectiveStatement<?>> extends EffectiveStatementNamespace<T> {
39         private Namespace() {
40             // Should never be instantiated
41         }
42     }
43
44     /**
45      * Find a {@code schema tree} child {@link SchemaTreeEffectiveStatement}, as identified by its QName argument.
46      *
47      * @param qname Child identifier
48      * @return Schema tree child, or empty
49      * @throws NullPointerException if {@code qname} is null
50      */
51     default <E extends SchemaTreeEffectiveStatement<?>> @NonNull Optional<E> findSchemaTreeNode(
52             final @NonNull QName qname) {
53         return Optional.ofNullable((E)get(Namespace.class, requireNonNull(qname)));
54     }
55 }