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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.yangtools.parser.util;
23 import java.util.Objects;
25 import org.oran.smo.yangtools.parser.model.schema.ModuleAndNamespaceResolver;
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.
33 * @author Mark Hollmann
35 public class NamespaceModuleIdentifier {
37 private String namespace;
38 private String moduleName;
39 private final String identifier;
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.
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);
52 * Returns the namespace, or null in case the namespace was not originally supplied and no resolution has been done yet.
54 public String getNamespace() {
59 * Returns the module name, or null in case the module name was not originally supplied, and no resolution has been done
62 public String getModuleName() {
66 public String getIdentifier() {
71 * Resolves the namespace or module name, as required.
73 public void resolveModuleOrNamespace(final ModuleAndNamespaceResolver resolver) {
75 if (moduleName == null && namespace != null) {
76 moduleName = resolver.getModuleForNamespace(namespace);
77 } else if (namespace == null && moduleName != null) {
78 namespace = resolver.getNamespaceForModule(moduleName);
83 public int hashCode() {
84 return identifier.hashCode();
88 public String toString() {
89 return getNamespace() + "/" + getModuleName() + "/" + getIdentifier();
93 public boolean equals(final Object obj) {
95 if (!(obj instanceof NamespaceModuleIdentifier)) {
99 final NamespaceModuleIdentifier other = (NamespaceModuleIdentifier) obj;
101 if (!this.identifier.equals(other.identifier)) {
105 if (this.moduleName != null && other.moduleName != null) {
106 return this.moduleName.equals(other.moduleName);
109 if (this.namespace != null && other.namespace != null) {
110 return this.namespace.equals(other.namespace);