95fa54cec56feeb20e68af94bf1659184acde821
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / NamespaceStmtCtx.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 com.google.common.annotations.Beta;
11 import java.util.Map;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14
15 /**
16  * Support work with namespace content.
17  */
18 @Beta
19 public interface NamespaceStmtCtx extends CommonStmtCtx {
20     /**
21      * Return the selected namespace.
22      *
23      * @param <K> namespace key type
24      * @param <V> namespace value type
25      * @param <N> namespace type
26      * @param nsType namespace type class
27      * @return Namespace contents, if available
28      */
29     <K, V, N extends ParserNamespace<K, V>> @Nullable Map<K, V> namespace(Class<@NonNull N> nsType);
30
31     /**
32      * Return a value associated with specified key within a namespace.
33      *
34      * @param nsType Namespace type
35      * @param key Key
36      * @param <K> namespace key type
37      * @param <V> namespace value type
38      * @param <N> namespace type
39      * @param <T> key type
40      * @return Value, or null if there is no element
41      * @throws NamespaceNotAvailableException when the namespace is not available.
42      */
43     <K, V, T extends K, N extends ParserNamespace<K, V>> @Nullable V namespaceItem(Class<@NonNull N> nsType, T key);
44
45     /**
46      * Return the portion of specified namespace stored in this node. Depending on namespace behaviour this may or may
47      * not represent the complete contents of the namespace as available via {@link #namespace(Class)}.
48      *
49      * <p>
50      * This partial view is useful when the need is not to perform a proper namespace lookup, but rather act on current
51      * statement's contribution to the namespace.
52      *
53      * @param <K> namespace key type
54      * @param <V> namespace value type
55      * @param <N> namespace type
56      * @param nsType namespace type class
57      * @return Namespace portion stored in this node, if available
58      */
59     <K, V, N extends ParserNamespace<K, V>> @Nullable Map<K, V> localNamespacePortion(Class<@NonNull N> nsType);
60
61     /**
62      * Return the selected namespace.
63      *
64      * @param <K> namespace key type
65      * @param <V> namespace value type
66      * @param <N> namespace type
67      * @param nsType namespace type class
68      * @return Namespace contents, if available
69      */
70     // TODO: migrate users away
71     default <K, V, N extends ParserNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> nsType) {
72         return namespace(nsType);
73     }
74
75     /**
76      * Return a value associated with specified key within a namespace.
77      *
78      * @param type Namespace type
79      * @param key Key
80      * @param <K> namespace key type
81      * @param <V> namespace value type
82      * @param <N> namespace type
83      * @param <T> key type
84      * @return Value, or null if there is no element
85      * @throws NamespaceNotAvailableException when the namespace is not available.
86      */
87     // TODO: migrate users away
88     default <K, V, T extends K, N extends ParserNamespace<K, V>>
89             @Nullable V getFromNamespace(final Class<@NonNull N> type, final T key) {
90         return namespaceItem(type, key);
91     }
92 }