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.data.util;
23 import java.util.Objects;
25 import org.oran.smo.yangtools.parser.PrefixResolver;
26 import org.oran.smo.yangtools.parser.util.NamespaceModuleIdentifier;
27 import org.oran.smo.yangtools.parser.util.QNameHelper;
30 * Holds the value of a data node instance of type "identityref"
32 * @author Mark Hollmann
34 public class IdentityRefValue {
36 private final NamespaceModuleIdentifier value;
38 public IdentityRefValue(final String namespace, final String moduleName, final String identityName) {
39 this.value = new NamespaceModuleIdentifier(namespace, moduleName, Objects.requireNonNull(identityName));
43 * Constructor for data encoded in XML, where prefixes are used and a prefix resolver is available for the namespace
46 public IdentityRefValue(final String val, final PrefixResolver prefixResolver, final String defaultNamespace) {
48 final boolean hasPrefix = QNameHelper.hasPrefix(val);
49 final String namespace = hasPrefix ?
50 prefixResolver.resolveNamespaceUri(QNameHelper.extractPrefix(val)) :
52 final String name = hasPrefix ? QNameHelper.extractName(val) : val;
54 value = new NamespaceModuleIdentifier(namespace, null, name);
58 * Constructor for data encoded in JSON, where module names are used.
60 public IdentityRefValue(final String val, final String defaultModuleName) {
62 final boolean hasPrefix = QNameHelper.hasPrefix(val);
63 final String moduleName = hasPrefix ? QNameHelper.extractPrefix(val) : defaultModuleName;
64 final String name = hasPrefix ? QNameHelper.extractName(val) : val;
66 value = new NamespaceModuleIdentifier(null, moduleName, name);
70 * The name of the identity
72 public String getIdentityName() {
73 return value.getIdentifier();
77 * The name of the module in which the identity has been declared. May return null if the value was encoded in XML.
79 public String getIdentityModuleName() {
80 return value.getModuleName();
84 * The namespace of the module in which the identity has been declared. May return null if the value was encoded in
87 public String getIdentityNamespace() {
88 return value.getNamespace();
92 public String toString() {
93 return "IdentityRef value " + getIdentityNamespace() + "/" + getIdentityModuleName() + "/" + getIdentityName();
97 public boolean equals(Object obj) {
99 if (obj == null || !obj.getClass().getName().equals(IdentityRefValue.class.getName())) {
103 final IdentityRefValue other = (IdentityRefValue) obj;
105 if (!this.getIdentityName().equals(other.getIdentityName())) {
109 if (this.getIdentityModuleName() != null && other.getIdentityModuleName() != null && this
110 .getIdentityNamespace() != null && other.getIdentityNamespace() != null) {
111 return this.getIdentityModuleName().equals(other.getIdentityModuleName()) && this.getIdentityNamespace().equals(
112 other.getIdentityNamespace());
116 * The comparison is a little different to how this would usually be done. Depending on the encoding
117 * of the input, either the namespace or the module name may be null. However, a client having constructed
118 * such an object, will typically have knowledge of both (as they will know the model). So we will try both.
120 if (this.getIdentityModuleName() != null && other.getIdentityModuleName() != null) {
121 return this.getIdentityModuleName().equals(other.getIdentityModuleName());
124 if (this.getIdentityNamespace() != null && other.getIdentityNamespace() != null) {
125 return this.getIdentityNamespace().equals(other.getIdentityNamespace());