2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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;
27 import lombok.NonNull;
28 import lombok.ToString;
29 import org.onap.policy.common.utils.validation.Assertions;
30 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
33 * A key, upon which other key subclasses can be built, providing implementations of the methods.
37 public abstract class PfKeyImpl extends PfKey {
38 private static final long serialVersionUID = 8932717618579392561L;
40 public static final String NAME_TOKEN = "name";
41 public static final String VERSION_TOKEN = "version";
44 * The default constructor creates a null concept key.
47 this(NULL_KEY_NAME, NULL_KEY_VERSION);
53 * @param copyConcept the concept to copy from
55 public PfKeyImpl(final PfKeyImpl copyConcept) {
57 setName(copyConcept.getName());
58 setVersion(copyConcept.getVersion());
62 * Constructor to create a key with the specified name and version.
64 * @param name the key name
65 * @param version the key version
67 public PfKeyImpl(@NonNull final String name, @NonNull final String version) {
74 * Constructor to create a key using the key and version from the specified key ID.
76 * @param id the key ID in a format that respects the KEY_ID_REGEXP
78 public PfKeyImpl(@NonNull final String id) {
79 // Check the incoming ID is valid
80 Assertions.validateStringParameter("id", id, getKeyIdRegEx());
82 // Split on colon, if the id passes the regular expression test above
83 // it'll have just one colon separating the name and version
84 // No need for range checks or size checks on the array
85 final String[] nameVersionArray = id.split(":");
88 setName(nameVersionArray[0]);
89 setVersion(nameVersionArray[1]);
92 public abstract void setName(@NonNull final String name);
94 public abstract void setVersion(@NonNull final String version);
97 public PfKeyImpl getKey() {
102 public List<PfKey> getKeys() {
103 final List<PfKey> keyList = new ArrayList<>();
104 keyList.add(getKey());
109 public String getId() {
110 return getName() + ':' + getVersion();
114 public boolean isNullKey() {
115 return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion()));
119 * Determines if the version is "null".
121 * @return {@code true} if the version is null, {@code false} otherwise
123 public boolean isNullVersion() {
124 return PfKey.NULL_KEY_VERSION.equals(getVersion());
128 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
129 if (!(otherKey instanceof PfKeyImpl)) {
130 return Compatibility.DIFFERENT;
132 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
134 if (this.equals(otherConceptKey)) {
135 return Compatibility.IDENTICAL;
137 if (!this.getName().equals(otherConceptKey.getName())) {
138 return Compatibility.DIFFERENT;
141 final String[] thisVersionArray = getVersion().split("\\.");
142 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
144 // There must always be at least one element in each version
145 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
146 return Compatibility.MAJOR;
149 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
150 && !thisVersionArray[1].equals(otherVersionArray[1])) {
151 return Compatibility.MINOR;
154 return Compatibility.PATCH;
158 public boolean isCompatible(@NonNull final PfKey otherKey) {
159 if (!(otherKey instanceof PfKeyImpl)) {
162 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
164 final Compatibility compatibility = this.getCompatibility(otherConceptKey);
166 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
170 public boolean isNewerThan(@NonNull final PfKey otherKey) {
171 Assertions.instanceOf(otherKey, PfKeyImpl.class);
173 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
175 if (this.equals(otherConceptKey)) {
179 if (!this.getName().equals(otherConceptKey.getName())) {
180 return this.getName().compareTo(otherConceptKey.getName()) > 0;
183 final String[] thisVersionArray = getVersion().split("\\.");
184 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
186 // There must always be at least one element in each version
187 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
188 return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]);
191 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
192 && !thisVersionArray[1].equals(otherVersionArray[1])) {
193 return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
196 if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
197 && !thisVersionArray[2].equals(otherVersionArray[2])) {
198 return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]);
205 public int getMajorVersion() {
206 final String[] versionArray = getVersion().split("\\.");
208 // There must always be at least one element in each version
209 return Integer.parseInt(versionArray[0]);
213 public int getMinorVersion() {
214 final String[] versionArray = getVersion().split("\\.");
216 if (versionArray.length >= 2) {
217 return Integer.parseInt(versionArray[1]);
225 public int getPatchVersion() {
226 final String[] versionArray = getVersion().split("\\.");
228 if (versionArray.length >= 3) {
229 return Integer.parseInt(versionArray[2]);
237 public PfValidationResult validate(final PfValidationResult result) {
238 final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, getName(),
240 if (nameValidationErrorMessage != null) {
241 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
242 "name invalid-" + nameValidationErrorMessage));
245 final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
246 getVersion(), getVersionRegEx());
247 if (versionValidationErrorMessage != null) {
248 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
249 "version invalid-" + versionValidationErrorMessage));
256 public void clean() {
258 setVersion(getVersion());
262 public int compareTo(@NonNull final PfConcept otherObj) {
263 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
265 if (this == otherObj) {
268 if (getClass() != otherObj.getClass()) {
269 return getClass().getName().compareTo(otherObj.getClass().getName());
272 final PfKeyImpl other = (PfKeyImpl) otherObj;
274 if (!getName().equals(other.getName())) {
275 return getName().compareTo(other.getName());
277 return getVersion().compareTo(other.getVersion());
281 * Gets the regular expression used to validate a name.
283 * @return the regular expression used to validate a name
285 protected String getNameRegEx() {
290 * Gets the regular expression used to validate a version.
292 * @return the regular expression used to validate a version
294 protected String getVersionRegEx() {
295 return VERSION_REGEXP;
299 * Gets the regular expression used to validate a key id.
301 * @return the regular expression used to validate a key id
303 protected String getKeyIdRegEx() {
304 return KEY_ID_REGEXP;