d8b62a292da32b6996d9ccac4ba23a768186e655
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.model.schema;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Objects;
26
27 /**
28  * Holds mappings between module names and namespaces.
29  *
30  * @author Mark Hollmann
31  */
32 public class ModuleAndNamespaceResolver {
33
34     private final Map<String, String> moduleNameToNamespace = new HashMap<>();
35     private final Map<String, String> namespaceToModuleName = new HashMap<>();
36
37     /**
38      * Records a mapping between a module or submodule name, and its namespace (in the case of
39      * submodule, the namespace of the owning module).
40      */
41     public void recordModuleMapping(final String moduleName, final String namespace) {
42         moduleNameToNamespace.put(Objects.requireNonNull(moduleName), Objects.requireNonNull(namespace));
43     }
44
45     /**
46      * Records a mapping between a namespace and the module defining it.
47      */
48     public void recordNamespaceMapping(final String namespace, final String moduleName) {
49         namespaceToModuleName.put(Objects.requireNonNull(namespace), Objects.requireNonNull(moduleName));
50     }
51
52     /**
53      * Returns the namespace for the supplied module name. If the module name refers to a submodule, will
54      * return the namespace of the module owning the submodule. Returns null if the module is unknown.
55      */
56     public String getNamespaceForModule(final String moduleName) {
57         return moduleNameToNamespace.get(moduleName);
58     }
59
60     /**
61      * Returns the module name for the supplied namespace. Returns null if the namespace is unknown.
62      */
63     public String getModuleForNamespace(final String namespace) {
64         return namespaceToModuleName.get(namespace);
65     }
66
67     @Override
68     public String toString() {
69         return "Mappings: " + moduleNameToNamespace.toString();
70     }
71
72 }