2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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;
28 import lombok.NonNull;
29 import lombok.ToString;
31 import org.onap.policy.common.utils.validation.Assertions;
32 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
35 * A key, upon which other key subclasses can be built, providing implementations of the methods.
39 public abstract class PfKeyImpl extends PfKey {
40 private static final long serialVersionUID = 8932717618579392561L;
42 public static final String NAME_TOKEN = "name";
43 public static final String VERSION_TOKEN = "version";
46 * The default constructor creates a null concept key.
49 this(NULL_KEY_NAME, NULL_KEY_VERSION);
55 * @param copyConcept the concept to copy from
57 public PfKeyImpl(final PfKeyImpl copyConcept) {
59 setName(copyConcept.getName());
60 setVersion(copyConcept.getVersion());
64 * Constructor to create a key with the specified name and version.
66 * @param name the key name
67 * @param version the key version
69 public PfKeyImpl(@NonNull final String name, @NonNull final String version) {
76 * Constructor to create a key using the key and version from the specified key ID.
78 * @param id the key ID in a format that respects the KEY_ID_REGEXP
80 public PfKeyImpl(@NonNull final String id) {
81 // Check the incoming ID is valid
82 Assertions.validateStringParameter("id", id, getKeyIdRegEx());
84 // Split on colon, if the id passes the regular expression test above
85 // it'll have just one colon separating the name and version
86 // No need for range checks or size checks on the array
87 final String[] nameVersionArray = id.split(":");
90 setName(nameVersionArray[0]);
91 setVersion(nameVersionArray[1]);
94 public abstract void setName(@NonNull final String name);
96 public abstract void setVersion(@NonNull final String version);
99 public PfKeyImpl getKey() {
104 public List<PfKey> getKeys() {
105 final List<PfKey> keyList = new ArrayList<>();
106 keyList.add(getKey());
111 public String getId() {
112 return getName() + ':' + getVersion();
116 public boolean isNullKey() {
117 return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion()));
121 * Determines if the name is "null".
123 * @return {@code true} if the name is null, {@code false} otherwise
125 public boolean isNullName() {
126 return PfKey.NULL_KEY_NAME.equals(getName());
130 * Determines if the version is "null".
132 * @return {@code true} if the version is null, {@code false} otherwise
134 public boolean isNullVersion() {
135 return PfKey.NULL_KEY_VERSION.equals(getVersion());
139 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
140 if (!(otherKey instanceof PfKeyImpl)) {
141 return Compatibility.DIFFERENT;
143 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
145 if (this.equals(otherConceptKey)) {
146 return Compatibility.IDENTICAL;
148 if (!this.getName().equals(otherConceptKey.getName())) {
149 return Compatibility.DIFFERENT;
152 final String[] thisVersionArray = getVersion().split("\\.");
153 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
155 // There must always be at least one element in each version
156 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
157 return Compatibility.MAJOR;
160 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
161 && !thisVersionArray[1].equals(otherVersionArray[1])) {
162 return Compatibility.MINOR;
165 return Compatibility.PATCH;
169 public boolean isCompatible(@NonNull final PfKey otherKey) {
170 if (!(otherKey instanceof PfKeyImpl)) {
173 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
175 final Compatibility compatibility = this.getCompatibility(otherConceptKey);
177 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
181 public boolean isNewerThan(@NonNull final PfKey otherKey) {
182 Assertions.instanceOf(otherKey, PfKeyImpl.class);
184 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
186 if (this.equals(otherConceptKey)) {
190 if (!this.getName().equals(otherConceptKey.getName())) {
191 return this.getName().compareTo(otherConceptKey.getName()) > 0;
194 final String[] thisVersionArray = getVersion().split("\\.");
195 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
197 // There must always be at least one element in each version
198 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
199 return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]);
202 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
203 && !thisVersionArray[1].equals(otherVersionArray[1])) {
204 return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
207 if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
208 && !thisVersionArray[2].equals(otherVersionArray[2])) {
209 return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]);
216 public int getMajorVersion() {
217 final String[] versionArray = getVersion().split("\\.");
219 // There must always be at least one element in each version
220 return Integer.parseInt(versionArray[0]);
224 public int getMinorVersion() {
225 final String[] versionArray = getVersion().split("\\.");
227 if (versionArray.length >= 2) {
228 return Integer.parseInt(versionArray[1]);
235 public int getPatchVersion() {
236 final String[] versionArray = getVersion().split("\\.");
238 if (versionArray.length >= 3) {
239 return Integer.parseInt(versionArray[2]);
246 public PfValidationResult validate(final PfValidationResult result) {
247 final String nameValidationErrorMessage =
248 Assertions.getStringParameterValidationMessage(NAME_TOKEN, getName(), getNameRegEx());
249 if (nameValidationErrorMessage != null) {
250 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
251 "name invalid-" + nameValidationErrorMessage));
254 final String versionValidationErrorMessage =
255 Assertions.getStringParameterValidationMessage(VERSION_TOKEN, getVersion(), getVersionRegEx());
256 if (versionValidationErrorMessage != null) {
257 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
258 "version invalid-" + versionValidationErrorMessage));
265 public void clean() {
267 setVersion(getVersion());
271 public int compareTo(@NonNull final PfConcept otherObj) {
272 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
274 if (this == otherObj) {
277 if (getClass() != otherObj.getClass()) {
278 return getClass().getName().compareTo(otherObj.getClass().getName());
281 final PfKeyImpl other = (PfKeyImpl) otherObj;
283 if (!getName().equals(other.getName())) {
284 return getName().compareTo(other.getName());
286 return getVersion().compareTo(other.getVersion());
290 * Gets the regular expression used to validate a name.
292 * @return the regular expression used to validate a name
294 protected String getNameRegEx() {
299 * Gets the regular expression used to validate a version.
301 * @return the regular expression used to validate a version
303 protected String getVersionRegEx() {
304 return VERSION_REGEXP;
308 * Gets the regular expression used to validate a key id.
310 * @return the regular expression used to validate a key id
312 protected String getKeyIdRegEx() {
313 return KEY_ID_REGEXP;