f735fbc81f14dc59a2dae3bb009d722f674ac0c9
[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.util;
22
23 import java.util.Objects;
24
25 import org.oran.smo.yangtools.parser.model.schema.ModuleAndNamespaceResolver;
26
27 /**
28  * A simple base class to encapsulate a namespace, module name and identifier. The class allows
29  * for either namespace or module-name to be supplied, as sometimes the respective other is not
30  * immediately available during processing. Method {@link resolveModuleOrNamespace} should be
31  * used to populate the respective other.
32  *
33  * @author Mark Hollmann
34  */
35 public class NamespaceModuleIdentifier {
36
37     private String namespace;
38     private String moduleName;
39     private final String identifier;
40
41     /**
42      * Either namespace or module name should be supplied, and preferably both. There are valid usages
43      * where both can remain empty, so not enforcing this.
44      */
45     public NamespaceModuleIdentifier(final String namespace, final String moduleName, final String identifier) {
46         this.namespace = namespace;
47         this.moduleName = moduleName;
48         this.identifier = Objects.requireNonNull(identifier);
49     }
50
51     /**
52      * Returns the namespace, or null in case the namespace was not originally supplied and no resolution has been done yet.
53      */
54     public String getNamespace() {
55         return namespace;
56     }
57
58     /**
59      * Returns the module name, or null in case the module name was not originally supplied, and no resolution has been done
60      * yet.
61      */
62     public String getModuleName() {
63         return moduleName;
64     }
65
66     public String getIdentifier() {
67         return identifier;
68     }
69
70     /**
71      * Resolves the namespace or module name, as required.
72      */
73     public void resolveModuleOrNamespace(final ModuleAndNamespaceResolver resolver) {
74
75         if (moduleName == null && namespace != null) {
76             moduleName = resolver.getModuleForNamespace(namespace);
77         } else if (namespace == null && moduleName != null) {
78             namespace = resolver.getNamespaceForModule(moduleName);
79         }
80     }
81
82     @Override
83     public int hashCode() {
84         return identifier.hashCode();
85     }
86
87     @Override
88     public String toString() {
89         return getNamespace() + "/" + getModuleName() + "/" + getIdentifier();
90     }
91
92     @Override
93     public boolean equals(final Object obj) {
94
95         if (!(obj instanceof NamespaceModuleIdentifier)) {
96             return false;
97         }
98
99         final NamespaceModuleIdentifier other = (NamespaceModuleIdentifier) obj;
100
101         if (!this.identifier.equals(other.identifier)) {
102             return false;
103         }
104
105         if (this.moduleName != null && other.moduleName != null) {
106             return this.moduleName.equals(other.moduleName);
107         }
108
109         if (this.namespace != null && other.namespace != null) {
110             return this.namespace.equals(other.namespace);
111         }
112
113         return false;
114     }
115 }