2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.util.ArrayList;
25 import java.util.List;
27 import lombok.NonNull;
28 import lombok.ToString;
29 import org.onap.policy.common.utils.validation.Assertions;
32 * A key, upon which other key subclasses can be built, providing implementations of the methods.
36 public abstract class PfKeyImpl extends PfKey {
37 private static final long serialVersionUID = 8932717618579392561L;
39 public static final String NAME_TOKEN = "name";
40 public static final String VERSION_TOKEN = "version";
43 * The default constructor creates a null concept key.
45 protected PfKeyImpl() {
46 this(NULL_KEY_NAME, NULL_KEY_VERSION);
52 * @param copyConcept the concept to copy from
54 protected PfKeyImpl(final PfKeyImpl copyConcept) {
56 setName(copyConcept.getName());
57 setVersion(copyConcept.getVersion());
61 * Constructor to create a key with the specified name and version.
63 * @param name the key name
64 * @param version the key version
66 protected PfKeyImpl(@NonNull final String name, @NonNull final String version) {
73 * Constructor to create a key using the key and version from the specified key ID.
75 * @param id the key ID in a format that respects the KEY_ID_REGEXP
77 protected PfKeyImpl(@NonNull final String id) {
78 // Check the incoming ID is valid
79 Assertions.validateStringParameter("id", id, getKeyIdRegEx());
81 // Split on colon, if the id passes the regular expression test above
82 // it'll have just one colon separating the name and version
83 // No need for range checks or size checks on the array
84 final String[] nameVersionArray = id.split(":");
87 setName(nameVersionArray[0]);
88 setVersion(nameVersionArray[1]);
91 public abstract void setName(@NonNull final String name);
93 public abstract void setVersion(@NonNull final String version);
96 public PfKeyImpl getKey() {
101 public List<PfKey> getKeys() {
102 final List<PfKey> keyList = new ArrayList<>();
103 keyList.add(getKey());
108 public String getId() {
109 return getName() + ':' + getVersion();
113 public boolean isNullKey() {
114 return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion()));
118 * Determines if the name is "null".
120 * @return {@code true} if the name is null, {@code false} otherwise
122 public boolean isNullName() {
123 return PfKey.NULL_KEY_NAME.equals(getName());
127 * Determines if the version is "null".
129 * @return {@code true} if the version is null, {@code false} otherwise
131 public boolean isNullVersion() {
132 return PfKey.NULL_KEY_VERSION.equals(getVersion());
136 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
137 if (!(otherKey instanceof PfKeyImpl)) {
138 return Compatibility.DIFFERENT;
140 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
142 if (this.equals(otherConceptKey)) {
143 return Compatibility.IDENTICAL;
145 if (!this.getName().equals(otherConceptKey.getName())) {
146 return Compatibility.DIFFERENT;
149 final String[] thisVersionArray = getVersion().split("\\.");
150 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
152 // There must always be at least one element in each version
153 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
154 return Compatibility.MAJOR;
157 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
158 && !thisVersionArray[1].equals(otherVersionArray[1])) {
159 return Compatibility.MINOR;
162 return Compatibility.PATCH;
166 public boolean isCompatible(@NonNull final PfKey otherKey) {
167 if (!(otherKey instanceof PfKeyImpl)) {
170 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
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.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]);
199 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
200 && !thisVersionArray[1].equals(otherVersionArray[1])) {
201 return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
204 if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
205 && !thisVersionArray[2].equals(otherVersionArray[2])) {
206 return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(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;