15d66c9bab0298c184154fad20a534dfcd147ee9
[yangtools.git] / data / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / DistinctNodeContainer.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.data.api.schema;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.base.VerifyException;
13 import java.util.Collection;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18
19 /**
20  * A {@link NormalizedNodeContainer} which contains directly-addressable children. It
21  *
22  * <p>
23  * NormalizedNodeContainer does not have a value, but it has a child nodes. Definition of possible and valid child nodes
24  * is introduced in subclasses of this interface.
25  *
26  * <p>
27  * This interface should not be used directly, but rather use of of derived subclasses such as
28  * {@link DataContainerNode}, {@link MapNode}, {@link LeafSetNode}.
29  *
30  * @param <K> Child path argument type
31  * @param <V> Child Node type
32  */
33 public interface DistinctNodeContainer<K extends PathArgument, V extends NormalizedNode>
34         extends NormalizedNodeContainer<V> {
35     /**
36      * {@inheritDoc}
37      *
38      * <p>
39      * All nodes returned in this iterable, MUST also be accessible via {@link #childByArg(PathArgument)} using their
40      * associated identifier.
41      *
42      * @return Iteration of all child nodes
43      */
44     @Override
45     Collection<@NonNull V> body();
46
47     /**
48      * Returns a child node identified by provided key.
49      *
50      * @param key Path argument identifying child node
51      * @return Matching child node, or null if no matching child exists
52      * @throws NullPointerException if {@code key} is null
53      */
54     @Nullable V childByArg(K key);
55
56     /**
57      * Attempts to find a child node identified by provided key.
58      *
59      * @param key Path argument identifying child node
60      * @return Optional with child node if child exists. {@link Optional#empty()} if child does not exist
61      * @throws NullPointerException if {@code key} is null
62      */
63     default Optional<V> findChildByArg(final K key) {
64         return Optional.ofNullable(childByArg(key));
65     }
66
67     /**
68      * Returns a child node identified by provided key, asserting its presence.
69      *
70      * @param key Path argument identifying child node
71      * @return Matching child node
72      * @throws NullPointerException if {@code key} is null
73      * @throws VerifyException if the child does not exist
74      */
75     default @NonNull V getChildByArg(final K key) {
76         return verifyNotNull(childByArg(key));
77     }
78 }