Merge "Changes for Checkstyle 8.32"
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfKeyImpl.java
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import lombok.Getter;
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;
31
32 /**
33  * A key, upon which other key subclasses can be built, providing implementations of the methods.
34  */
35 @Getter
36 @ToString
37 public abstract class PfKeyImpl extends PfKey {
38     private static final long serialVersionUID = 8932717618579392561L;
39
40     public static final String NAME_TOKEN = "name";
41     public static final String VERSION_TOKEN = "version";
42
43     /**
44      * The default constructor creates a null concept key.
45      */
46     public PfKeyImpl() {
47         this(NULL_KEY_NAME, NULL_KEY_VERSION);
48     }
49
50     /**
51      * Copy constructor.
52      *
53      * @param copyConcept the concept to copy from
54      */
55     public PfKeyImpl(final PfKeyImpl copyConcept) {
56         super(copyConcept);
57         setName(copyConcept.getName());
58         setVersion(copyConcept.getVersion());
59     }
60
61     /**
62      * Constructor to create a key with the specified name and version.
63      *
64      * @param name the key name
65      * @param version the key version
66      */
67     public PfKeyImpl(@NonNull final String name, @NonNull final String version) {
68         super();
69         setName(name);
70         setVersion(version);
71     }
72
73     /**
74      * Constructor to create a key using the key and version from the specified key ID.
75      *
76      * @param id the key ID in a format that respects the KEY_ID_REGEXP
77      */
78     public PfKeyImpl(@NonNull final String id) {
79         // Check the incoming ID is valid
80         Assertions.validateStringParameter("id", id, getKeyIdRegEx());
81
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(":");
86
87         // Return the new key
88         setName(nameVersionArray[0]);
89         setVersion(nameVersionArray[1]);
90     }
91
92     public abstract void setName(@NonNull final String name);
93
94     public abstract void setVersion(@NonNull final String version);
95
96     @Override
97     public PfKeyImpl getKey() {
98         return this;
99     }
100
101     @Override
102     public List<PfKey> getKeys() {
103         final List<PfKey> keyList = new ArrayList<>();
104         keyList.add(getKey());
105         return keyList;
106     }
107
108     @Override
109     public String getId() {
110         return getName() + ':' + getVersion();
111     }
112
113     @Override
114     public boolean isNullKey() {
115         return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion()));
116     }
117
118     /**
119      * Determines if the name is "null".
120      *
121      * @return {@code true} if the name is null, {@code false} otherwise
122      */
123     public boolean isNullName() {
124         return PfKey.NULL_KEY_NAME.equals(getName());
125     }
126
127     /**
128      * Determines if the version is "null".
129      *
130      * @return {@code true} if the version is null, {@code false} otherwise
131      */
132     public boolean isNullVersion() {
133         return PfKey.NULL_KEY_VERSION.equals(getVersion());
134     }
135
136     @Override
137     public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
138         if (!(otherKey instanceof PfKeyImpl)) {
139             return Compatibility.DIFFERENT;
140         }
141         final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
142
143         if (this.equals(otherConceptKey)) {
144             return Compatibility.IDENTICAL;
145         }
146         if (!this.getName().equals(otherConceptKey.getName())) {
147             return Compatibility.DIFFERENT;
148         }
149
150         final String[] thisVersionArray = getVersion().split("\\.");
151         final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
152
153         // There must always be at least one element in each version
154         if (!thisVersionArray[0].equals(otherVersionArray[0])) {
155             return Compatibility.MAJOR;
156         }
157
158         if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
159             && !thisVersionArray[1].equals(otherVersionArray[1])) {
160             return Compatibility.MINOR;
161         }
162
163         return Compatibility.PATCH;
164     }
165
166     @Override
167     public boolean isCompatible(@NonNull final PfKey otherKey) {
168         if (!(otherKey instanceof PfKeyImpl)) {
169             return false;
170         }
171         final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
172
173         final Compatibility compatibility = this.getCompatibility(otherConceptKey);
174
175         return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
176     }
177
178     @Override
179     public boolean isNewerThan(@NonNull final PfKey otherKey) {
180         Assertions.instanceOf(otherKey, PfKeyImpl.class);
181
182         final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
183
184         if (this.equals(otherConceptKey)) {
185             return false;
186         }
187
188         if (!this.getName().equals(otherConceptKey.getName())) {
189             return this.getName().compareTo(otherConceptKey.getName()) > 0;
190         }
191
192         final String[] thisVersionArray = getVersion().split("\\.");
193         final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
194
195         // There must always be at least one element in each version
196         if (!thisVersionArray[0].equals(otherVersionArray[0])) {
197             return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]);
198         }
199
200         if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
201             && !thisVersionArray[1].equals(otherVersionArray[1])) {
202             return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
203         }
204
205         if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
206             && !thisVersionArray[2].equals(otherVersionArray[2])) {
207             return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]);
208         }
209
210         return false;
211     }
212
213     @Override
214     public int getMajorVersion() {
215         final String[] versionArray = getVersion().split("\\.");
216
217         // There must always be at least one element in each version
218         return Integer.parseInt(versionArray[0]);
219     }
220
221     @Override
222     public int getMinorVersion() {
223         final String[] versionArray = getVersion().split("\\.");
224
225         if (versionArray.length >= 2) {
226             return Integer.parseInt(versionArray[1]);
227         } else {
228             return 0;
229         }
230     }
231
232     @Override
233     public int getPatchVersion() {
234         final String[] versionArray = getVersion().split("\\.");
235
236         if (versionArray.length >= 3) {
237             return Integer.parseInt(versionArray[2]);
238         } else {
239             return 0;
240         }
241     }
242
243     @Override
244     public PfValidationResult validate(final PfValidationResult result) {
245         final String nameValidationErrorMessage =
246             Assertions.getStringParameterValidationMessage(NAME_TOKEN, getName(), getNameRegEx());
247         if (nameValidationErrorMessage != null) {
248             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
249                 "name invalid-" + nameValidationErrorMessage));
250         }
251
252         final String versionValidationErrorMessage =
253             Assertions.getStringParameterValidationMessage(VERSION_TOKEN, getVersion(), getVersionRegEx());
254         if (versionValidationErrorMessage != null) {
255             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
256                 "version invalid-" + versionValidationErrorMessage));
257         }
258
259         return result;
260     }
261
262     @Override
263     public void clean() {
264         setName(getName());
265         setVersion(getVersion());
266     }
267
268     @Override
269     public int compareTo(@NonNull final PfConcept otherObj) {
270         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
271
272         if (this == otherObj) {
273             return 0;
274         }
275         if (getClass() != otherObj.getClass()) {
276             return getClass().getName().compareTo(otherObj.getClass().getName());
277         }
278
279         final PfKeyImpl other = (PfKeyImpl) otherObj;
280
281         if (!getName().equals(other.getName())) {
282             return getName().compareTo(other.getName());
283         }
284         return getVersion().compareTo(other.getVersion());
285     }
286
287     /**
288      * Gets the regular expression used to validate a name.
289      *
290      * @return the regular expression used to validate a name
291      */
292     protected String getNameRegEx() {
293         return NAME_REGEXP;
294     }
295
296     /**
297      * Gets the regular expression used to validate a version.
298      *
299      * @return the regular expression used to validate a version
300      */
301     protected String getVersionRegEx() {
302         return VERSION_REGEXP;
303     }
304
305     /**
306      * Gets the regular expression used to validate a key id.
307      *
308      * @return the regular expression used to validate a key id
309      */
310     protected String getKeyIdRegEx() {
311         return KEY_ID_REGEXP;
312     }
313 }