2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020, 2023 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
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=========================================================
22 package org.onap.policy.models.base;
24 import java.io.Serial;
25 import java.util.ArrayList;
26 import java.util.List;
28 import lombok.NonNull;
29 import lombok.ToString;
30 import org.onap.policy.common.utils.validation.Assertions;
33 * A key, upon which other key subclasses can be built, providing implementations of the methods.
37 public abstract class PfKeyImpl extends PfKey {
39 private static final long serialVersionUID = 8932717618579392561L;
41 public static final String NAME_TOKEN = "name";
42 public static final String VERSION_TOKEN = "version";
45 * The default constructor creates a null concept key.
47 protected PfKeyImpl() {
48 this(NULL_KEY_NAME, NULL_KEY_VERSION);
54 * @param copyConcept the concept to copy from
56 protected PfKeyImpl(final PfKeyImpl copyConcept) {
58 setName(copyConcept.getName());
59 setVersion(copyConcept.getVersion());
63 * Constructor to create a key with the specified name and version.
65 * @param name the key name
66 * @param version the key version
68 protected PfKeyImpl(@NonNull final String name, @NonNull final String version) {
75 * Constructor to create a key using the key and version from the specified key ID.
77 * @param id the key ID in a format that respects the KEY_ID_REGEXP
79 protected PfKeyImpl(@NonNull final String id) {
80 // Check the incoming ID is valid
81 Assertions.validateStringParameter("id", id, getKeyIdRegEx());
83 // Split on colon, if the id passes the regular expression test above
84 // it'll have just one colon separating the name and version
85 // No need for range checks or size checks on the array
86 final String[] nameVersionArray = id.split(":");
89 setName(nameVersionArray[0]);
90 setVersion(nameVersionArray[1]);
93 public abstract void setName(@NonNull final String name);
95 public abstract void setVersion(@NonNull final String version);
98 public PfKeyImpl getKey() {
103 public List<PfKey> getKeys() {
104 final List<PfKey> keyList = new ArrayList<>();
105 keyList.add(getKey());
110 public String getId() {
111 return getName() + ':' + getVersion();
115 public boolean isNullKey() {
116 return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion()));
120 * Determines if the name is "null".
122 * @return {@code true} if the name is null, {@code false} otherwise
124 public boolean isNullName() {
125 return PfKey.NULL_KEY_NAME.equals(getName());
129 * Determines if the version is "null".
131 * @return {@code true} if the version is null, {@code false} otherwise
133 public boolean isNullVersion() {
134 return PfKey.NULL_KEY_VERSION.equals(getVersion());
138 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
139 if (!(otherKey instanceof PfKeyImpl otherConceptKey)) {
140 return Compatibility.DIFFERENT;
143 if (this.equals(otherConceptKey)) {
144 return Compatibility.IDENTICAL;
146 if (!this.getName().equals(otherConceptKey.getName())) {
147 return Compatibility.DIFFERENT;
150 final String[] thisVersionArray = getVersion().split("\\.");
151 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
153 // There must always be at least one element in each version
154 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
155 return Compatibility.MAJOR;
158 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
159 && !thisVersionArray[1].equals(otherVersionArray[1])) {
160 return Compatibility.MINOR;
163 return Compatibility.PATCH;
167 public boolean isCompatible(@NonNull final PfKey otherKey) {
168 if (!(otherKey instanceof PfKeyImpl otherConceptKey)) {
172 final var compatibility = this.getCompatibility(otherConceptKey);
174 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
178 public boolean isNewerThan(@NonNull final PfKey otherKey) {
179 Assertions.instanceOf(otherKey, PfKeyImpl.class);
181 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
183 if (this.equals(otherConceptKey)) {
187 if (!this.getName().equals(otherConceptKey.getName())) {
188 return this.getName().compareTo(otherConceptKey.getName()) > 0;
191 final String[] thisVersionArray = getVersion().split("\\.");
192 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
194 // There must always be at least one element in each version
195 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
196 return Integer.parseInt(thisVersionArray[0]) > Integer.parseInt(otherVersionArray[0]);
199 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
200 && !thisVersionArray[1].equals(otherVersionArray[1])) {
201 return Integer.parseInt(thisVersionArray[1]) > Integer.parseInt(otherVersionArray[1]);
204 if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
205 && !thisVersionArray[2].equals(otherVersionArray[2])) {
206 return Integer.parseInt(thisVersionArray[2]) > Integer.parseInt(otherVersionArray[2]);
213 public int getMajorVersion() {
214 final String[] versionArray = getVersion().split("\\.");
216 // There must always be at least one element in each version
217 return Integer.parseInt(versionArray[0]);
221 public int getMinorVersion() {
222 final String[] versionArray = getVersion().split("\\.");
224 if (versionArray.length >= 2) {
225 return Integer.parseInt(versionArray[1]);
232 public int getPatchVersion() {
233 final String[] versionArray = getVersion().split("\\.");
235 if (versionArray.length >= 3) {
236 return Integer.parseInt(versionArray[2]);
243 public void clean() {
245 setVersion(getVersion());
249 public int compareTo(@NonNull final PfConcept otherObj) {
250 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
252 if (this == otherObj) {
255 if (getClass() != otherObj.getClass()) {
256 return getClass().getName().compareTo(otherObj.getClass().getName());
259 final PfKeyImpl other = (PfKeyImpl) otherObj;
261 if (!getName().equals(other.getName())) {
262 return getName().compareTo(other.getName());
264 return getVersion().compareTo(other.getVersion());
268 * Gets the regular expression used to validate a name.
270 * @return the regular expression used to validate a name
272 protected String getNameRegEx() {
277 * Gets the regular expression used to validate a version.
279 * @return the regular expression used to validate a version
281 protected String getVersionRegEx() {
282 return VERSION_REGEXP;
286 * Gets the regular expression used to validate a key id.
288 * @return the regular expression used to validate a key id
290 protected String getKeyIdRegEx() {
291 return KEY_ID_REGEXP;