Update ChoiceSchemaNode design
[yangtools.git] / yang / 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.List;
15 import java.util.Optional;
16 import java.util.SortedMap;
17 import org.opendaylight.yangtools.yang.common.QName;
18
19 /**
20  * A ChoiceSchemaNode defines a set of alternatives. It consists of a number of branches defined as
21  * ChoiceCaseSchemaNode objects.
22  */
23 public interface ChoiceSchemaNode extends DataSchemaNode, AugmentationTarget {
24     /**
25      * Returns cases of choice, keyed by their {@link SchemaNode#getQName()}. Returned map does not contain null keys
26      * nor values.
27      *
28      * @return set of ChoiceCaseNode objects defined in this node which represents set of arguments of the YANG
29      *         <code>case</code> substatement of the <code>choice</code> statement.
30      */
31     SortedMap<QName, ChoiceCaseNode> getCases();
32
33     /**
34      * Returns the concrete case according to specified Q name.
35      *
36      * @param qname
37      *            QName of sought Choice Case Node
38      * @return child case node of this Choice if child with given name is present, empty otherwise.
39      * @throws NullPointerException if qname is null
40      */
41     default Optional<ChoiceCaseNode> findCase(final QName qname) {
42         return Optional.ofNullable(getCases().get(requireNonNull(qname)));
43     }
44
45     /**
46      * Returns the concrete cases according to specified name, disregarding their namespace.
47      *
48      * @param localname
49      *            local name of sought child as String
50      * @return child case nodes matching specified local name, empty list if no match is found.
51      * @throws NullPointerException if localname is null
52      */
53     @Beta
54     default List<ChoiceCaseNode> findCaseNodes(final String localname) {
55         return getCases().values().stream().filter(node -> localname.equals(node.getQName().getLocalName()))
56                 .collect(ImmutableList.toImmutableList());
57     }
58
59     /**
60      * Find a specific data schema child, if present. This method searches among its {@link ChoiceCaseNode}s,
61      * potentially recursing to nested choices.
62      *
63      * @param qname
64      *            QName of sought data schema node
65      * @return Matching node, or empty if no match is found
66      * @throws NullPointerException if qname is null
67      */
68     @Beta
69     default Optional<DataSchemaNode> findDataSchemaChild(final QName qname) {
70         requireNonNull(qname);
71         for (ChoiceCaseNode caseNode : getCases().values()) {
72             final Optional<DataSchemaNode> child = caseNode.findDataChildByName(qname);
73             if (child.isPresent()) {
74                 return child;
75             }
76         }
77
78         return Optional.empty();
79     }
80
81     /**
82      * Returns the concrete case according to specified QName.
83      *
84      * @param qname
85      *            QName of sought Choice Case Node
86      * @return child case node of this Choice if child with given name is present, <code>null</code> otherwise.
87      *
88      * @deprecated Use either {@code getCases().get(name)} or #findCase(QName)
89      */
90     @Deprecated
91     default ChoiceCaseNode getCaseNodeByName(final QName qname) {
92         return getCases().get(qname);
93     }
94
95     /**
96      * Returns name of case which is in the choice specified as default.
97      *
98      * @return string with the name of case which is specified in the argument of the YANG <code>default</code>
99      *         substatement of <code>choice</code> statement.
100      */
101     Optional<ChoiceCaseNode> getDefaultCase();
102 }