Use case arrows in mergeIntoModifiedNode()
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / ChoiceSchemaNode.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
20
21 /**
22  * A ChoiceSchemaNode defines a set of alternatives. It consists of a number of branches defined as
23  * {@link CaseSchemaNode} objects.
24  */
25 public interface ChoiceSchemaNode extends DataSchemaNode, AugmentationTarget, MandatoryAware,
26         EffectiveStatementEquivalent<ChoiceEffectiveStatement> {
27     /**
28      * Returns cases of choice, keyed by their {@link SchemaNode#getQName()}. Returned map does not contain null keys
29      * nor values.
30      *
31      * @return set of {@link CaseSchemaNode} objects defined in this node which represents set of arguments of the YANG
32      *         {@code case} substatement of the {@code choice} statement.
33      */
34     Collection<? extends @NonNull CaseSchemaNode> getCases();
35
36     /**
37      * Returns the concrete case according to specified Q name.
38      *
39      * @param qname
40      *            QName of sought Choice Case Node
41      * @return child case node of this Choice if child with given name is present, empty otherwise.
42      * @throws NullPointerException {@code qname} is {@code null}
43      */
44     default Optional<? extends CaseSchemaNode> findCaseNode(final QName qname) {
45         requireNonNull(qname);
46         return getCases().stream().filter(node -> qname.equals(node.getQName())).findFirst();
47     }
48
49     /**
50      * Returns the concrete cases according to specified name, disregarding their namespace.
51      *
52      * @param localName local name of sought child as String
53      * @return child case nodes matching specified local name, empty list if no match is found.
54      * @throws NullPointerException if {@code localName} is {@code null}
55      */
56     @Beta
57     default List<? extends @NonNull CaseSchemaNode> findCaseNodes(final String localName) {
58         requireNonNull(localName);
59         return getCases().stream()
60             .filter(node -> localName.equals(node.getQName().getLocalName()))
61             .collect(ImmutableList.toImmutableList());
62     }
63
64     /**
65      * Find a specific data schema child, if present. This method searches among its {@link CaseSchemaNode}s,
66      * potentially recursing to nested choices.
67      *
68      * @param qname QName of sought data schema node
69      * @return Matching node, or empty if no match is found
70      * @throws NullPointerException if {@code qname} is {@code null}
71      */
72     @Beta
73     default Optional<DataSchemaNode> findDataSchemaChild(final QName qname) {
74         requireNonNull(qname);
75         for (var caseNode : getCases()) {
76             final var child = caseNode.dataChildByName(qname);
77             if (child != null) {
78                 return Optional.of(child);
79             }
80         }
81         return Optional.empty();
82     }
83
84     /**
85      * Returns name of case which is in the choice specified as default.
86      *
87      * @return string with the name of case which is specified in the argument of the YANG <code>default</code>
88      *         substatement of <code>choice</code> statement.
89      */
90     Optional<CaseSchemaNode> getDefaultCase();
91 }