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